CppTools: Some code cosmetics

Namespaces, foreach.

Change-Id: I0129ee1ed1f5d1625869e8b5cb6173e3f71166e1
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
hjk
2021-08-20 14:40:17 +02:00
parent a9f5a9c6a4
commit 652aa041b3
2 changed files with 30 additions and 31 deletions

View File

@@ -95,9 +95,8 @@ void CppCodeModelSettingsWidget::setupClangCodeModelWidgets()
m_ui->clangDiagnosticConfigsSelectionWidget m_ui->clangDiagnosticConfigsSelectionWidget
->refresh(diagnosticConfigsModel(), ->refresh(diagnosticConfigsModel(),
m_settings->clangDiagnosticConfigId(), m_settings->clangDiagnosticConfigId(),
[](const CppTools::ClangDiagnosticConfigs &configs, [](const ClangDiagnosticConfigs &configs, const Utils::Id &configToSelect) {
const Utils::Id &configToSelect) { return new ClangDiagnosticConfigsWidget(configs, configToSelect);
return new CppTools::ClangDiagnosticConfigsWidget(configs, configToSelect);
}); });
const bool isClangActive = CppModelManager::instance()->isClangCodeModelActive(); const bool isClangActive = CppModelManager::instance()->isClangCodeModelActive();

View File

@@ -67,6 +67,7 @@
using namespace Core; using namespace Core;
using namespace CPlusPlus; using namespace CPlusPlus;
using namespace ProjectExplorer;
using namespace Utils; using namespace Utils;
namespace CppTools { namespace CppTools {
@@ -125,9 +126,9 @@ void CppToolsPlugin::clearHeaderSourceCache()
m_headerSourceMapping.clear(); m_headerSourceMapping.clear();
} }
Utils::FilePath CppToolsPlugin::licenseTemplatePath() FilePath CppToolsPlugin::licenseTemplatePath()
{ {
return Utils::FilePath::fromString(m_instance->d->m_fileSettings.licenseTemplatePath); return FilePath::fromString(m_instance->d->m_fileSettings.licenseTemplatePath);
} }
QString CppToolsPlugin::licenseTemplate() QString CppToolsPlugin::licenseTemplate()
@@ -191,14 +192,14 @@ bool CppToolsPlugin::initialize(const QStringList &arguments, QString *error)
QAction *openInNextSplitAction = new QAction(tr("Open Corresponding Header/Source in Next Split"), this); QAction *openInNextSplitAction = new QAction(tr("Open Corresponding Header/Source in Next Split"), this);
command = ActionManager::registerAction(openInNextSplitAction, Constants::OPEN_HEADER_SOURCE_IN_NEXT_SPLIT, context, true); command = ActionManager::registerAction(openInNextSplitAction, Constants::OPEN_HEADER_SOURCE_IN_NEXT_SPLIT, context, true);
command->setDefaultKeySequence(QKeySequence(Utils::HostOsInfo::isMacHost() command->setDefaultKeySequence(QKeySequence(HostOsInfo::isMacHost()
? tr("Meta+E, F4") ? tr("Meta+E, F4")
: tr("Ctrl+E, F4"))); : tr("Ctrl+E, F4")));
mcpptools->addAction(command); mcpptools->addAction(command);
connect(openInNextSplitAction, &QAction::triggered, connect(openInNextSplitAction, &QAction::triggered,
this, &CppToolsPlugin::switchHeaderSourceInNextSplit); this, &CppToolsPlugin::switchHeaderSourceInNextSplit);
Utils::MacroExpander *expander = Utils::globalMacroExpander(); MacroExpander *expander = globalMacroExpander();
expander->registerVariable("Cpp:LicenseTemplate", expander->registerVariable("Cpp:LicenseTemplate",
tr("The license template."), tr("The license template."),
[]() { return CppToolsPlugin::licenseTemplate(); }); []() { return CppToolsPlugin::licenseTemplate(); });
@@ -211,13 +212,13 @@ bool CppToolsPlugin::initialize(const QStringList &arguments, QString *error)
tr("Insert \"#pragma once\" instead of \"#ifndef\" include guards into header file"), tr("Insert \"#pragma once\" instead of \"#ifndef\" include guards into header file"),
[] { return usePragmaOnce() ? QString("true") : QString(); }); [] { return usePragmaOnce() ? QString("true") : QString(); });
const auto panelFactory = new ProjectExplorer::ProjectPanelFactory; const auto panelFactory = new ProjectPanelFactory;
panelFactory->setPriority(100); panelFactory->setPriority(100);
panelFactory->setDisplayName(tr("Clangd")); panelFactory->setDisplayName(tr("Clangd"));
panelFactory->setCreateWidgetFunction([](ProjectExplorer::Project *project) { panelFactory->setCreateWidgetFunction([](Project *project) {
return new ClangdProjectSettingsWidget(project); return new ClangdProjectSettingsWidget(project);
}); });
ProjectExplorer::ProjectPanelFactory::registerFactory(panelFactory); ProjectPanelFactory::registerFactory(panelFactory);
return true; return true;
} }
@@ -256,8 +257,7 @@ void CppToolsPlugin::switchHeaderSourceInNextSplit()
EditorManager::openEditor(otherFile, Id(), EditorManager::OpenInOtherSplit); EditorManager::openEditor(otherFile, Id(), EditorManager::OpenInOtherSplit);
} }
static QStringList findFilesInProject(const QString &name, static QStringList findFilesInProject(const QString &name, const Project *project)
const ProjectExplorer::Project *project)
{ {
if (debug) if (debug)
qDebug() << Q_FUNC_INFO << name << project; qDebug() << Q_FUNC_INFO << name << project;
@@ -268,11 +268,11 @@ static QStringList findFilesInProject(const QString &name,
QString pattern = QString(1, QLatin1Char('/')); QString pattern = QString(1, QLatin1Char('/'));
pattern += name; pattern += name;
const QStringList projectFiles const QStringList projectFiles
= Utils::transform(project->files(ProjectExplorer::Project::AllFiles), &Utils::FilePath::toString); = transform(project->files(Project::AllFiles), &FilePath::toString);
const QStringList::const_iterator pcend = projectFiles.constEnd(); const QStringList::const_iterator pcend = projectFiles.constEnd();
QStringList candidateList; QStringList candidateList;
for (QStringList::const_iterator it = projectFiles.constBegin(); it != pcend; ++it) { for (QStringList::const_iterator it = projectFiles.constBegin(); it != pcend; ++it) {
if (it->endsWith(pattern, Utils::HostOsInfo::fileNameCaseSensitivity())) if (it->endsWith(pattern, HostOsInfo::fileNameCaseSensitivity()))
candidateList.append(*it); candidateList.append(*it);
} }
return candidateList; return candidateList;
@@ -310,7 +310,7 @@ static QStringList baseNameWithAllSuffixes(const QString &baseName, const QStrin
{ {
QStringList result; QStringList result;
const QChar dot = QLatin1Char('.'); const QChar dot = QLatin1Char('.');
foreach (const QString &suffix, suffixes) { for (const QString &suffix : suffixes) {
QString fileName = baseName; QString fileName = baseName;
fileName += dot; fileName += dot;
fileName += suffix; fileName += suffix;
@@ -325,16 +325,16 @@ static QStringList baseNamesWithAllPrefixes(const QStringList &baseNames, bool i
const QStringList &sourcePrefixes = m_instance->sourcePrefixes(); const QStringList &sourcePrefixes = m_instance->sourcePrefixes();
const QStringList &headerPrefixes = m_instance->headerPrefixes(); const QStringList &headerPrefixes = m_instance->headerPrefixes();
foreach (const QString &name, baseNames) { for (const QString &name : baseNames) {
foreach (const QString &prefix, isHeader ? headerPrefixes : sourcePrefixes) { for (const QString &prefix : isHeader ? headerPrefixes : sourcePrefixes) {
if (name.startsWith(prefix)) { if (name.startsWith(prefix)) {
QString nameWithoutPrefix = name.mid(prefix.size()); QString nameWithoutPrefix = name.mid(prefix.size());
result += nameWithoutPrefix; result += nameWithoutPrefix;
foreach (const QString &prefix, isHeader ? sourcePrefixes : headerPrefixes) for (const QString &prefix : isHeader ? sourcePrefixes : headerPrefixes)
result += prefix + nameWithoutPrefix; result += prefix + nameWithoutPrefix;
} }
} }
foreach (const QString &prefix, isHeader ? sourcePrefixes : headerPrefixes) for (const QString &prefix : isHeader ? sourcePrefixes : headerPrefixes)
result += prefix + name; result += prefix + name;
} }
@@ -344,7 +344,7 @@ static QStringList baseNamesWithAllPrefixes(const QStringList &baseNames, bool i
static QStringList baseDirWithAllDirectories(const QDir &baseDir, const QStringList &directories) static QStringList baseDirWithAllDirectories(const QDir &baseDir, const QStringList &directories)
{ {
QStringList result; QStringList result;
foreach (const QString &dir, directories) for (const QString &dir : directories)
result << QDir::cleanPath(baseDir.absoluteFilePath(dir)); result << QDir::cleanPath(baseDir.absoluteFilePath(dir));
return result; return result;
} }
@@ -353,7 +353,7 @@ static int commonFilePathLength(const QString &s1, const QString &s2)
{ {
int length = qMin(s1.length(), s2.length()); int length = qMin(s1.length(), s2.length());
for (int i = 0; i < length; ++i) for (int i = 0; i < length; ++i)
if (Utils::HostOsInfo::fileNameCaseSensitivity() == Qt::CaseSensitive) { if (HostOsInfo::fileNameCaseSensitivity() == Qt::CaseSensitive) {
if (s1[i] != s2[i]) if (s1[i] != s2[i])
return i; return i;
} else { } else {
@@ -365,16 +365,16 @@ static int commonFilePathLength(const QString &s1, const QString &s2)
static QString correspondingHeaderOrSourceInProject(const QFileInfo &fileInfo, static QString correspondingHeaderOrSourceInProject(const QFileInfo &fileInfo,
const QStringList &candidateFileNames, const QStringList &candidateFileNames,
const ProjectExplorer::Project *project, const Project *project,
CacheUsage cacheUsage) CacheUsage cacheUsage)
{ {
QString bestFileName; QString bestFileName;
int compareValue = 0; int compareValue = 0;
const QString filePath = fileInfo.filePath(); const QString filePath = fileInfo.filePath();
foreach (const QString &candidateFileName, candidateFileNames) { for (const QString &candidateFileName : candidateFileNames) {
const QStringList projectFiles = findFilesInProject(candidateFileName, project); const QStringList projectFiles = findFilesInProject(candidateFileName, project);
// Find the file having the most common path with fileName // Find the file having the most common path with fileName
foreach (const QString &projectFile, projectFiles) { for (const QString &projectFile : projectFiles) {
int value = commonFilePathLength(filePath, projectFile); int value = commonFilePathLength(filePath, projectFile);
if (value > compareValue) { if (value > compareValue) {
compareValue = value; compareValue = value;
@@ -442,10 +442,10 @@ QString correspondingHeaderOrSource(const QString &fileName, bool *wasHeader, Ca
candidateFileNames += baseNamesWithAllPrefixes(candidateFileNames, isHeader); candidateFileNames += baseNamesWithAllPrefixes(candidateFileNames, isHeader);
// Try to find a file in the same or sibling directories first // Try to find a file in the same or sibling directories first
foreach (const QString &candidateDir, candidateDirs) { for (const QString &candidateDir : qAsConst(candidateDirs)) {
foreach (const QString &candidateFileName, candidateFileNames) { for (const QString &candidateFileName : qAsConst(candidateFileNames)) {
const QString candidateFilePath = candidateDir + QLatin1Char('/') + candidateFileName; const QString candidateFilePath = candidateDir + QLatin1Char('/') + candidateFileName;
const QString normalized = Utils::FileUtils::normalizePathName(candidateFilePath); const QString normalized = FileUtils::normalizePathName(candidateFilePath);
const QFileInfo candidateFi(normalized); const QFileInfo candidateFi(normalized);
if (candidateFi.isFile()) { if (candidateFi.isFile()) {
if (cacheUsage == CacheUsage::ReadWrite) { if (cacheUsage == CacheUsage::ReadWrite) {
@@ -459,7 +459,7 @@ QString correspondingHeaderOrSource(const QString &fileName, bool *wasHeader, Ca
} }
// Find files in the current project // Find files in the current project
ProjectExplorer::Project *currentProject = ProjectExplorer::ProjectTree::currentProject(); Project *currentProject = ProjectTree::currentProject();
if (currentProject) { if (currentProject) {
const QString path = correspondingHeaderOrSourceInProject(fi, candidateFileNames, const QString path = correspondingHeaderOrSourceInProject(fi, candidateFileNames,
currentProject, cacheUsage); currentProject, cacheUsage);
@@ -469,9 +469,9 @@ QString correspondingHeaderOrSource(const QString &fileName, bool *wasHeader, Ca
// Find files in other projects // Find files in other projects
} else { } else {
CppModelManager *modelManager = CppModelManager::instance(); CppModelManager *modelManager = CppModelManager::instance();
QList<ProjectInfo::Ptr> projectInfos = modelManager->projectInfos(); const QList<ProjectInfo::Ptr> projectInfos = modelManager->projectInfos();
foreach (const ProjectInfo::Ptr &projectInfo, projectInfos) { for (const ProjectInfo::Ptr &projectInfo : projectInfos) {
const ProjectExplorer::Project *project = projectForProjectInfo(*projectInfo); const Project *project = projectForProjectInfo(*projectInfo);
if (project == currentProject) if (project == currentProject)
continue; // We have already checked the current project. continue; // We have already checked the current project.