Utils: add FilePath::completeBaseName

Removing some FilePath::toFileInfo() calls again.

Change-Id: I6610beebf2c30754fde525b71f4c4a34ceb5e30b
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
David Schulz
2021-06-04 07:59:00 +02:00
parent 5544fd35c4
commit 5ec611b68e
24 changed files with 48 additions and 32 deletions

View File

@@ -761,12 +761,28 @@ QString FilePath::fileNameWithPathComponents(int pathComponents) const
return m_data; return m_data;
} }
/// \returns the base name of the file without the path.
///
/// The base name consists of all characters in the file up to
/// (but not including) the first '.' character.
QString FilePath::baseName() const QString FilePath::baseName() const
{ {
const QString &name = fileName(); const QString &name = fileName();
return name.left(name.indexOf('.')); return name.left(name.indexOf('.'));
} }
/// \returns the complete base name of the file without the path.
///
/// The complete base name consists of all characters in the file up to
/// (but not including) the last '.' character
QString FilePath::completeBaseName() const
{
const QString &name = fileName();
return name.left(name.lastIndexOf('.'));
}
void FilePath::setScheme(const QString &scheme) void FilePath::setScheme(const QString &scheme)
{ {
QTC_CHECK(!scheme.contains('/')); QTC_CHECK(!scheme.contains('/'));

View File

@@ -103,7 +103,9 @@ public:
QString fileName() const; QString fileName() const;
QString fileNameWithPathComponents(int pathComponents) const; QString fileNameWithPathComponents(int pathComponents) const;
QString baseName() const; QString baseName() const;
QString completeBaseName() const;
QString scheme() const { return m_scheme; } QString scheme() const { return m_scheme; }
void setScheme(const QString &scheme); void setScheme(const QString &scheme);

View File

@@ -195,7 +195,7 @@ GenericProject::GenericProject(const Utils::FilePath &fileName)
{ {
setId(Constants::GENERICPROJECT_ID); setId(Constants::GENERICPROJECT_ID);
setProjectLanguages(Context(ProjectExplorer::Constants::CXX_LANGUAGE_ID)); setProjectLanguages(Context(ProjectExplorer::Constants::CXX_LANGUAGE_ID));
setDisplayName(fileName.toFileInfo().completeBaseName()); setDisplayName(fileName.completeBaseName());
setBuildSystemCreator([](Target *t) { return new GenericBuildSystem(t); }); setBuildSystemCreator([](Target *t) { return new GenericBuildSystem(t); });
} }
@@ -475,7 +475,7 @@ void GenericBuildSystem::refresh(RefreshOptions options)
if (options & Files) { if (options & Files) {
auto newRoot = std::make_unique<ProjectNode>(projectDirectory()); auto newRoot = std::make_unique<ProjectNode>(projectDirectory());
newRoot->setDisplayName(projectFilePath().toFileInfo().completeBaseName()); newRoot->setDisplayName(projectFilePath().completeBaseName());
// find the common base directory of all source files // find the common base directory of all source files
FilePath baseDir = findCommonSourceRoot(); FilePath baseDir = findCommonSourceRoot();

View File

@@ -38,7 +38,7 @@ NimbleProject::NimbleProject(const Utils::FilePath &fileName)
: ProjectExplorer::Project(Constants::C_NIMBLE_MIMETYPE, fileName) : ProjectExplorer::Project(Constants::C_NIMBLE_MIMETYPE, fileName)
{ {
setId(Constants::C_NIMBLEPROJECT_ID); setId(Constants::C_NIMBLEPROJECT_ID);
setDisplayName(fileName.toFileInfo().completeBaseName()); setDisplayName(fileName.completeBaseName());
// ensure debugging is enabled (Nim plugin translates nim code to C code) // ensure debugging is enabled (Nim plugin translates nim code to C code)
setProjectLanguages(Core::Context(ProjectExplorer::Constants::CXX_LANGUAGE_ID)); setProjectLanguages(Core::Context(ProjectExplorer::Constants::CXX_LANGUAGE_ID));
setBuildSystemCreator([] (Target *t) { return new NimbleBuildSystem(t); }); setBuildSystemCreator([] (Target *t) { return new NimbleBuildSystem(t); });

View File

@@ -41,7 +41,7 @@ namespace Nim {
NimProject::NimProject(const FilePath &fileName) : Project(Constants::C_NIM_MIMETYPE, fileName) NimProject::NimProject(const FilePath &fileName) : Project(Constants::C_NIM_MIMETYPE, fileName)
{ {
setId(Constants::C_NIMPROJECT_ID); setId(Constants::C_NIMPROJECT_ID);
setDisplayName(fileName.toFileInfo().completeBaseName()); setDisplayName(fileName.completeBaseName());
// ensure debugging is enabled (Nim plugin translates nim code to C code) // ensure debugging is enabled (Nim plugin translates nim code to C code)
setProjectLanguages(Core::Context(ProjectExplorer::Constants::CXX_LANGUAGE_ID)); setProjectLanguages(Core::Context(ProjectExplorer::Constants::CXX_LANGUAGE_ID));

View File

@@ -118,7 +118,7 @@ void DesktopRunConfiguration::updateTargetInformation()
if (profile.isEmpty()) if (profile.isEmpty())
setDefaultDisplayName(tr("Qt Run Configuration")); setDefaultDisplayName(tr("Qt Run Configuration"));
else else
setDefaultDisplayName(profile.toFileInfo().completeBaseName()); setDefaultDisplayName(profile.completeBaseName());
aspect<EnvironmentAspect>()->environmentChanged(); aspect<EnvironmentAspect>()->environmentChanged();

View File

@@ -1022,7 +1022,7 @@ QList<ToolChain *> GccToolChainFactory::autoDetect(const QList<ToolChain *> &alr
QList<ToolChain *> GccToolChainFactory::detectForImport(const ToolChainDescription &tcd) QList<ToolChain *> GccToolChainFactory::detectForImport(const ToolChainDescription &tcd)
{ {
const QString fileName = tcd.compilerPath.toFileInfo().completeBaseName(); const QString fileName = tcd.compilerPath.completeBaseName();
if ((tcd.language == Constants::C_LANGUAGE_ID && (fileName.startsWith("gcc") if ((tcd.language == Constants::C_LANGUAGE_ID && (fileName.startsWith("gcc")
|| fileName.endsWith("gcc") || fileName.endsWith("gcc")
|| fileName == "cc")) || fileName == "cc"))
@@ -1806,7 +1806,7 @@ QList<ToolChain *> MingwToolChainFactory::autoDetect(const QList<ToolChain *> &a
QList<ToolChain *> MingwToolChainFactory::detectForImport(const ToolChainDescription &tcd) QList<ToolChain *> MingwToolChainFactory::detectForImport(const ToolChainDescription &tcd)
{ {
const QString fileName = tcd.compilerPath.toFileInfo().completeBaseName(); const QString fileName = tcd.compilerPath.completeBaseName();
if ((tcd.language == Constants::C_LANGUAGE_ID && (fileName.startsWith("gcc") if ((tcd.language == Constants::C_LANGUAGE_ID && (fileName.startsWith("gcc")
|| fileName.endsWith("gcc"))) || fileName.endsWith("gcc")))
|| (tcd.language == Constants::CXX_LANGUAGE_ID && (fileName.startsWith("g++") || (tcd.language == Constants::CXX_LANGUAGE_ID && (fileName.startsWith("g++")

View File

@@ -1788,8 +1788,7 @@ QString MsvcToolChainFactory::vcVarsBatFor(const QString &basePath,
return result; return result;
} }
static QList<ToolChain *> findOrCreateToolChain(const QList<ToolChain *> &alreadyKnown, static QList<ToolChain *> findOrCreateToolChain(const QList<ToolChain *> &alreadyKnown, const QString &name,
const QString &name,
const Abi &abi, const Abi &abi,
const QString &varsBat, const QString &varsBat,
const QString &varsBatArg) const QString &varsBatArg)

View File

@@ -224,7 +224,7 @@ bool FlatModel::setData(const QModelIndex &index, const QVariant &value, int rol
if (reply == QMessageBox::Yes) { if (reply == QMessageBox::Yes) {
for (Node * const n : candidateNodes) { for (Node * const n : candidateNodes) {
QString targetFilePath = orgFileInfo.absolutePath() + '/' QString targetFilePath = orgFileInfo.absolutePath() + '/'
+ newFilePath.toFileInfo().completeBaseName(); + newFilePath.completeBaseName();
const QString suffix = n->filePath().toFileInfo().suffix(); const QString suffix = n->filePath().toFileInfo().suffix();
if (!suffix.isEmpty()) if (!suffix.isEmpty())
targetFilePath.append('.').append(suffix); targetFilePath.append('.').append(suffix);

View File

@@ -477,7 +477,7 @@ const QList<Node *> ProjectTree::siblingsWithSameBaseName(const Node *fileNode)
const auto filter = [&fi](const Node *n) { const auto filter = [&fi](const Node *n) {
return n->asFileNode() return n->asFileNode()
&& n->filePath().toFileInfo().dir() == fi.dir() && n->filePath().toFileInfo().dir() == fi.dir()
&& n->filePath().toFileInfo().completeBaseName() == fi.completeBaseName() && n->filePath().completeBaseName() == fi.completeBaseName()
&& n->filePath().toString() != fi.filePath(); && n->filePath().toString() != fi.filePath();
}; };
return productNode->findNodes(filter); return productNode->findNodes(filter);

View File

@@ -205,7 +205,7 @@ public:
PythonProjectNode(const Utils::FilePath &path) PythonProjectNode(const Utils::FilePath &path)
: ProjectNode(path) : ProjectNode(path)
{ {
setDisplayName(path.toFileInfo().completeBaseName()); setDisplayName(path.completeBaseName());
setAddFileFilter("*.py"); setAddFileFilter("*.py");
} }
}; };
@@ -215,7 +215,7 @@ PythonProject::PythonProject(const FilePath &fileName)
{ {
setId(PythonProjectId); setId(PythonProjectId);
setProjectLanguages(Context(ProjectExplorer::Constants::PYTHON_LANGUAGE_ID)); setProjectLanguages(Context(ProjectExplorer::Constants::PYTHON_LANGUAGE_ID));
setDisplayName(fileName.toFileInfo().completeBaseName()); setDisplayName(fileName.completeBaseName());
setNeedsBuildConfigurations(false); setNeedsBuildConfigurations(false);
setBuildSystemCreator([](Target *t) { return new PythonBuildSystem(t); }); setBuildSystemCreator([](Target *t) { return new PythonBuildSystem(t); });

View File

@@ -504,9 +504,8 @@ static QStringList replImportArgs(const FilePath &pythonFile, ReplType type)
if (type == ReplType::Unmodified || !isPython) if (type == ReplType::Unmodified || !isPython)
return {}; return {};
const auto import = type == ReplType::Import const auto import = type == ReplType::Import
? QString("import %1").arg(pythonFile.toFileInfo().completeBaseName()) ? QString("import %1").arg(pythonFile.completeBaseName())
: QString("from %1 import *") : QString("from %1 import *").arg(pythonFile.completeBaseName());
.arg(pythonFile.toFileInfo().completeBaseName());
return {"-c", QString("%1; print('Running \"%1\"')").arg(import)}; return {"-c", QString("%1; print('Running \"%1\"')").arg(import)};
} }

View File

@@ -64,7 +64,7 @@ static FilePath defaultBuildDirectory(const FilePath &projectFilePath, const Kit
const QString &bcName, const QString &bcName,
BuildConfiguration::BuildType buildType) BuildConfiguration::BuildType buildType)
{ {
const QString projectName = projectFilePath.toFileInfo().completeBaseName(); const QString projectName = projectFilePath.completeBaseName();
ProjectMacroExpander expander(projectFilePath, projectName, k, bcName, buildType); ProjectMacroExpander expander(projectFilePath, projectName, k, bcName, buildType);
FilePath projectDir = Project::projectDirectory(projectFilePath); FilePath projectDir = Project::projectDirectory(projectFilePath);
QString buildPath = expander.expand(ProjectExplorerPlugin::buildDirectoryTemplate()); QString buildPath = expander.expand(ProjectExplorerPlugin::buildDirectoryTemplate());

View File

@@ -213,7 +213,7 @@ QbsProjectNode *QbsNodeTreeBuilder::buildTree(const QString &projectName,
if (root->displayName().isEmpty()) if (root->displayName().isEmpty())
root->setDisplayName(projectName); root->setDisplayName(projectName);
if (root->displayName().isEmpty()) if (root->displayName().isEmpty())
root->setDisplayName(projectFile.toFileInfo().completeBaseName()); root->setDisplayName(projectFile.completeBaseName());
auto buildSystemFiles = std::make_unique<FolderNode>(projectDir); auto buildSystemFiles = std::make_unique<FolderNode>(projectDir);
buildSystemFiles->setDisplayName(QCoreApplication::translate("QbsProjectNode", "Qbs files")); buildSystemFiles->setDisplayName(QCoreApplication::translate("QbsProjectNode", "Qbs files"));

View File

@@ -123,7 +123,7 @@ QbsProject::QbsProject(const FilePath &fileName)
setId(Constants::PROJECT_ID); setId(Constants::PROJECT_ID);
setProjectLanguages(Context(ProjectExplorer::Constants::CXX_LANGUAGE_ID)); setProjectLanguages(Context(ProjectExplorer::Constants::CXX_LANGUAGE_ID));
setCanBuildProducts(); setCanBuildProducts();
setDisplayName(fileName.toFileInfo().completeBaseName()); setDisplayName(fileName.completeBaseName());
} }
QbsProject::~QbsProject() QbsProject::~QbsProject()

View File

@@ -88,7 +88,7 @@ QbsProjectImporter::QbsProjectImporter(const FilePath &path) : QtProjectImporter
static FilePath buildDir(const FilePath &projectFilePath, const Kit *k) static FilePath buildDir(const FilePath &projectFilePath, const Kit *k)
{ {
const QString projectName = projectFilePath.toFileInfo().completeBaseName(); const QString projectName = projectFilePath.completeBaseName();
ProjectMacroExpander expander(projectFilePath, projectName, k, QString(), ProjectMacroExpander expander(projectFilePath, projectName, k, QString(),
BuildConfiguration::Unknown); BuildConfiguration::Unknown);
const FilePath projectDir = Project::projectDirectory(projectFilePath); const FilePath projectDir = Project::projectDirectory(projectFilePath);
@@ -222,7 +222,7 @@ const QList<BuildInfo> QbsProjectImporter::buildInfoList(void *directoryData) co
{ {
const auto * const bgData = static_cast<BuildGraphData *>(directoryData); const auto * const bgData = static_cast<BuildGraphData *>(directoryData);
BuildInfo info; BuildInfo info;
info.displayName = bgData->bgFilePath.toFileInfo().completeBaseName(); info.displayName = bgData->bgFilePath.completeBaseName();
info.buildType = bgData->buildVariant == "debug" info.buildType = bgData->buildVariant == "debug"
? BuildConfiguration::Debug : BuildConfiguration::Release; ? BuildConfiguration::Debug : BuildConfiguration::Release;
info.buildDirectory = bgData->bgFilePath.parentDir().parentDir(); info.buildDirectory = bgData->bgFilePath.parentDir().parentDir();

View File

@@ -105,7 +105,7 @@ FilePath QmakeBuildConfiguration::shadowBuildDirectory(const FilePath &proFilePa
if (proFilePath.isEmpty()) if (proFilePath.isEmpty())
return {}; return {};
const QString projectName = proFilePath.toFileInfo().completeBaseName(); const QString projectName = proFilePath.completeBaseName();
ProjectMacroExpander expander(proFilePath, projectName, k, suffix, buildType); ProjectMacroExpander expander(proFilePath, projectName, k, suffix, buildType);
FilePath projectDir = Project::projectDirectory(proFilePath); FilePath projectDir = Project::projectDirectory(proFilePath);
QString buildPath = expander.expand(ProjectExplorerPlugin::buildDirectoryTemplate()); QString buildPath = expander.expand(ProjectExplorerPlugin::buildDirectoryTemplate());

View File

@@ -197,7 +197,7 @@ static void createTree(QmakeBuildSystem *buildSystem,
auto topLevel = std::make_unique<ResourceEditor::ResourceTopLevelNode> auto topLevel = std::make_unique<ResourceEditor::ResourceTopLevelNode>
(file.first, vfolder->filePath(), contents); (file.first, vfolder->filePath(), contents);
topLevel->setEnabled(file.second == FileOrigin::ExactParse); topLevel->setEnabled(file.second == FileOrigin::ExactParse);
const QString baseName = file.first.toFileInfo().completeBaseName(); const QString baseName = file.first.completeBaseName();
topLevel->setIsGenerated(baseName.startsWith("qmake_") topLevel->setIsGenerated(baseName.startsWith("qmake_")
|| baseName.endsWith("_qmlcache")); || baseName.endsWith("_qmlcache"));
vfolder->addNode(std::move(topLevel)); vfolder->addNode(std::move(topLevel));

View File

@@ -165,7 +165,7 @@ FilePath QmakePriFile::directoryPath() const
QString QmakePriFile::displayName() const QString QmakePriFile::displayName() const
{ {
return filePath().toFileInfo().completeBaseName(); return filePath().completeBaseName();
} }
QmakePriFile *QmakePriFile::parent() const QmakePriFile *QmakePriFile::parent() const
@@ -2068,13 +2068,13 @@ FilePaths QmakeProFile::generatedFiles(const FilePath &buildDir,
if (location.isEmpty()) if (location.isEmpty())
return { }; return { };
location = location.pathAppended("ui_" location = location.pathAppended("ui_"
+ sourceFile.toFileInfo().completeBaseName() + sourceFile.completeBaseName()
+ singleVariableValue(Variable::HeaderExtension)); + singleVariableValue(Variable::HeaderExtension));
return { Utils::FilePath::fromString(QDir::cleanPath(location.toString())) }; return { Utils::FilePath::fromString(QDir::cleanPath(location.toString())) };
} else if (sourceFileType == FileType::StateChart) { } else if (sourceFileType == FileType::StateChart) {
if (buildDir.isEmpty()) if (buildDir.isEmpty())
return { }; return { };
const FilePath location = buildDir.pathAppended(sourceFile.toFileInfo().completeBaseName()); const FilePath location = buildDir.pathAppended(sourceFile.completeBaseName());
return { return {
location.stringAppended(singleVariableValue(Variable::HeaderExtension)), location.stringAppended(singleVariableValue(Variable::HeaderExtension)),
location.stringAppended(singleVariableValue(Variable::CppExtension)) location.stringAppended(singleVariableValue(Variable::CppExtension))

View File

@@ -174,7 +174,7 @@ QmakeProject::QmakeProject(const FilePath &fileName) :
{ {
setId(Constants::QMAKEPROJECT_ID); setId(Constants::QMAKEPROJECT_ID);
setProjectLanguages(Core::Context(ProjectExplorer::Constants::CXX_LANGUAGE_ID)); setProjectLanguages(Core::Context(ProjectExplorer::Constants::CXX_LANGUAGE_ID));
setDisplayName(fileName.toFileInfo().completeBaseName()); setDisplayName(fileName.completeBaseName());
setCanBuildProducts(); setCanBuildProducts();
setHasMakeInstallEquivalent(true); setHasMakeInstallEquivalent(true);
} }
@@ -1162,7 +1162,7 @@ void QmakeBuildSystem::updateBuildSystemData()
bti.targetFilePath = FilePath::fromString(executableFor(node->proFile())); bti.targetFilePath = FilePath::fromString(executableFor(node->proFile()));
bti.projectFilePath = node->filePath(); bti.projectFilePath = node->filePath();
bti.workingDirectory = FilePath::fromString(workingDir); bti.workingDirectory = FilePath::fromString(workingDir);
bti.displayName = bti.projectFilePath.toFileInfo().completeBaseName(); bti.displayName = bti.projectFilePath.completeBaseName();
const FilePath relativePathInProject const FilePath relativePathInProject
= bti.projectFilePath.relativeChildPath(projectDirectory()); = bti.projectFilePath.relativeChildPath(projectDirectory());
if (!relativePathInProject.isEmpty()) { if (!relativePathInProject.isEmpty()) {

View File

@@ -96,7 +96,7 @@ QmlProject::QmlProject(const Utils::FilePath &fileName)
{ {
setId(QmlProjectManager::Constants::QML_PROJECT_ID); setId(QmlProjectManager::Constants::QML_PROJECT_ID);
setProjectLanguages(Context(ProjectExplorer::Constants::QMLJS_LANGUAGE_ID)); setProjectLanguages(Context(ProjectExplorer::Constants::QMLJS_LANGUAGE_ID));
setDisplayName(fileName.toFileInfo().completeBaseName()); setDisplayName(fileName.completeBaseName());
setNeedsBuildConfigurations(false); setNeedsBuildConfigurations(false);
setBuildSystemCreator([](Target *t) { return new QmlBuildSystem(t); }); setBuildSystemCreator([](Target *t) { return new QmlBuildSystem(t); });

View File

@@ -38,7 +38,7 @@ namespace Internal {
QmlProjectNode::QmlProjectNode(Project *project) QmlProjectNode::QmlProjectNode(Project *project)
: ProjectNode(project->projectDirectory()) : ProjectNode(project->projectDirectory())
{ {
setDisplayName(project->projectFilePath().toFileInfo().completeBaseName()); setDisplayName(project->projectFilePath().completeBaseName());
setIcon(DirectoryIcon(":/projectexplorer/images/fileoverlay_qml.png")); setIcon(DirectoryIcon(":/projectexplorer/images/fileoverlay_qml.png"));
} }

View File

@@ -235,7 +235,7 @@ ICodeStylePreferences *CodeStylePool::loadCodeStyle(const Utils::FilePath &fileN
reader.load(fileName); reader.load(fileName);
QVariantMap m = reader.restoreValues(); QVariantMap m = reader.restoreValues();
if (m.contains(QLatin1String(codeStyleDataKey))) { if (m.contains(QLatin1String(codeStyleDataKey))) {
const QByteArray id = fileName.toFileInfo().completeBaseName().toUtf8(); const QByteArray id = fileName.completeBaseName().toUtf8();
const QString displayName = reader.restoreValue(QLatin1String(displayNameKey)).toString(); const QString displayName = reader.restoreValue(QLatin1String(displayNameKey)).toString();
const QVariantMap map = reader.restoreValue(QLatin1String(codeStyleDataKey)).toMap(); const QVariantMap map = reader.restoreValue(QLatin1String(codeStyleDataKey)).toMap();
if (d->m_factory) { if (d->m_factory) {

View File

@@ -132,7 +132,7 @@ Highlighter::Definitions Highlighter::definitionsForDocument(const TextDocument
// cmake configure_file input filenames without the .in extension // cmake configure_file input filenames without the .in extension
if (filePath.endsWith(".in")) { if (filePath.endsWith(".in")) {
definitions = definitionsForFileName( definitions = definitionsForFileName(
Utils::FilePath::fromString(filePath.toFileInfo().completeBaseName())); Utils::FilePath::fromString(filePath.completeBaseName()));
} }
} }
if (definitions.isEmpty()) { if (definitions.isEmpty()) {