Utils: Change signature of MacroExpander::registerFileVariables

... to take a function returning a FilePath as base.

Implementation is (not) yet changed.

Change-Id: I624efab35cf38631c816b630be5296bdf696899e
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
hjk
2021-06-01 18:18:02 +02:00
parent 018dfc533c
commit f4a529ea95
11 changed files with 37 additions and 36 deletions

View File

@@ -395,22 +395,22 @@ void MacroExpander::registerIntVariable(const QByteArray &variable,
* \sa registerVariable(), registerIntVariable(), registerPrefix()
*/
void MacroExpander::registerFileVariables(const QByteArray &prefix,
const QString &heading, const StringFunction &base, bool visibleInChooser)
const QString &heading, const FileFunction &base, bool visibleInChooser)
{
registerVariable(prefix + kFilePathPostfix,
tr("%1: Full path including file name.").arg(heading),
[base]() -> QString { QString tmp = base(); return tmp.isEmpty() ? QString() : QFileInfo(tmp).filePath(); },
[base]() -> QString { QString tmp = base().toString(); return tmp.isEmpty() ? QString() : QFileInfo(tmp).filePath(); },
visibleInChooser);
registerVariable(prefix + kPathPostfix,
tr("%1: Full path excluding file name.").arg(heading),
[base]() -> QString { QString tmp = base(); return tmp.isEmpty() ? QString() : QFileInfo(tmp).path(); },
[base]() -> QString { QString tmp = base().toString(); return tmp.isEmpty() ? QString() : QFileInfo(tmp).path(); },
visibleInChooser);
registerVariable(prefix + kNativeFilePathPostfix,
tr("%1: Full path including file name, with native path separator (backslash on Windows).").arg(heading),
[base]() -> QString {
QString tmp = base();
QString tmp = base().toString();
return tmp.isEmpty() ? QString() : QDir::toNativeSeparators(QFileInfo(tmp).filePath());
},
visibleInChooser);
@@ -418,19 +418,19 @@ void MacroExpander::registerFileVariables(const QByteArray &prefix,
registerVariable(prefix + kNativePathPostfix,
tr("%1: Full path excluding file name, with native path separator (backslash on Windows).").arg(heading),
[base]() -> QString {
QString tmp = base();
QString tmp = base().toString();
return tmp.isEmpty() ? QString() : QDir::toNativeSeparators(QFileInfo(tmp).path());
},
visibleInChooser);
registerVariable(prefix + kFileNamePostfix,
tr("%1: File name without path.").arg(heading),
[base]() -> QString { QString tmp = base(); return tmp.isEmpty() ? QString() : FilePath::fromString(tmp).fileName(); },
[base]() -> QString { QString tmp = base().toString(); return tmp.isEmpty() ? QString() : FilePath::fromString(tmp).fileName(); },
visibleInChooser);
registerVariable(prefix + kFileBaseNamePostfix,
tr("%1: File base name without path and suffix.").arg(heading),
[base]() -> QString { QString tmp = base(); return tmp.isEmpty() ? QString() : QFileInfo(tmp).baseName(); },
[base]() -> QString { QString tmp = base().toString(); return tmp.isEmpty() ? QString() : QFileInfo(tmp).baseName(); },
visibleInChooser);
}

View File

@@ -65,6 +65,7 @@ public:
using PrefixFunction = std::function<QString(QString)>;
using ResolverFunction = std::function<bool(QString, QString *)>;
using StringFunction = std::function<QString()>;
using FileFunction = std::function<FilePath()>;
using IntFunction = std::function<int()>;
void registerPrefix(const QByteArray &prefix,
@@ -78,7 +79,7 @@ public:
const QString &description, const IntFunction &value);
void registerFileVariables(const QByteArray &prefix,
const QString &heading, const StringFunction &value,
const QString &heading, const FileFunction &value,
bool visibleInChooser = true);
void registerExtraResolver(const ResolverFunction &value);

View File

@@ -297,10 +297,10 @@ void CMakeKitAspect::addToMacroExpander(Kit *k, Utils::MacroExpander *expander)
{
QTC_ASSERT(k, return);
expander->registerFileVariables("CMake:Executable", tr("Path to the cmake executable"),
[k]() -> QString {
CMakeTool *tool = CMakeKitAspect::cmakeTool(k);
return tool ? tool->cmakeExecutable().toString() : QString();
});
[k] {
CMakeTool *tool = CMakeKitAspect::cmakeTool(k);
return tool ? tool->cmakeExecutable() : Utils::FilePath();
});
}
QSet<Utils::Id> CMakeKitAspect::availableFeatures(const Kit *k) const

View File

@@ -704,9 +704,9 @@ void EditorManagerPrivate::init()
d->m_openEditorsFactory = new OpenEditorsViewFactory();
globalMacroExpander()->registerFileVariables(kCurrentDocumentPrefix, tr("Current document"),
[]() -> QString {
[] {
IDocument *document = EditorManager::currentDocument();
return document ? document->filePath().toString() : QString();
return document ? document->filePath() : FilePath();
});
globalMacroExpander()->registerIntVariable(kCurrentDocumentXPos,

View File

@@ -201,7 +201,7 @@ bool CppToolsPlugin::initialize(const QStringList &arguments, QString *error)
[]() { return CppToolsPlugin::licenseTemplate(); });
expander->registerFileVariables("Cpp:LicenseTemplatePath",
tr("The configured path to the license template"),
[]() { return CppToolsPlugin::licenseTemplatePath().toString(); });
[]() { return CppToolsPlugin::licenseTemplatePath(); });
expander->registerVariable(
"Cpp:PragmaOnce",

View File

@@ -598,7 +598,7 @@ void DebuggerRunTool::start()
Utils::globalMacroExpander()->registerFileVariables(
"DebuggedExecutable", tr("Debugged executable"),
[this] { return m_runParameters.inferior.executable.toString(); }
[this] { return m_runParameters.inferior.executable; }
);
runControl()->setDisplayName(m_runParameters.displayName);

View File

@@ -167,8 +167,8 @@ void SysRootKitAspect::addToMacroExpander(Kit *kit, Utils::MacroExpander *expand
{
QTC_ASSERT(kit, return);
expander->registerFileVariables("SysRoot", tr("Sys Root"), [kit]() -> QString {
return SysRootKitAspect::sysRoot(kit).toString();
expander->registerFileVariables("SysRoot", tr("Sys Root"), [kit] {
return SysRootKitAspect::sysRoot(kit);
});
}

View File

@@ -1779,19 +1779,19 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er
Utils::MacroExpander *expander = Utils::globalMacroExpander();
expander->registerFileVariables(Constants::VAR_CURRENTPROJECT_PREFIX,
tr("Current project's main file."),
[]() -> QString {
Utils::FilePath projectFilePath;
[]() -> FilePath {
FilePath projectFilePath;
if (Project *project = ProjectTree::currentProject())
projectFilePath = project->projectFilePath();
return projectFilePath.toString();
return projectFilePath;
}, false);
expander->registerFileVariables("CurrentDocument:Project",
tr("Main file of the project the current document belongs to."),
[]() -> QString {
Utils::FilePath projectFilePath;
[]() -> FilePath {
FilePath projectFilePath;
if (Project *project = ProjectTree::currentProject())
projectFilePath = project->projectFilePath();
return projectFilePath.toString();
return projectFilePath;
}, false);
expander->registerVariable(Constants::VAR_CURRENTPROJECT_NAME,
@@ -1850,9 +1850,9 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er
return {};
});
expander->registerFileVariables("ActiveProject", tr("Active project's main file."),
[]() -> QString {
[]() -> FilePath {
if (const Project * const project = SessionManager::startupProject())
return project->projectFilePath().toString();
return project->projectFilePath();
return {};
});
expander->registerVariable("ActiveProject:Kit:Name",
@@ -1909,10 +1909,10 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er
});
expander->registerFileVariables("ActiveProject:RunConfig:Executable",
tr("The executable of the active project's active run configuration."),
[]() -> QString {
[]() -> FilePath {
if (const RunConfiguration * const rc = activeRunConfiguration())
return rc->commandLine().executable().toString();
return QString();
return rc->commandLine().executable();
return {};
});
const char activeRunEnvVar[] = "ActiveProject:RunConfig:Env";
Utils::EnvironmentProvider::addProvider(
@@ -1944,7 +1944,7 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er
});
const auto fileHandler = [] {
return SessionManager::sessionNameToFileName(SessionManager::activeSession()).toString();
return SessionManager::sessionNameToFileName(SessionManager::activeSession());
};
expander->registerFileVariables("Session", tr("File where current session is saved."),
fileHandler);

View File

@@ -36,10 +36,10 @@ ProjectMacroExpander::ProjectMacroExpander(const Utils::FilePath &mainFilePath,
// TODO: Remove "Current" variants in ~4.16
registerFileVariables(Constants::VAR_CURRENTPROJECT_PREFIX,
QCoreApplication::translate("ProjectExplorer", "Main file of current project"),
[mainFilePath] { return mainFilePath.toString(); }, false);
[mainFilePath] { return mainFilePath; }, false);
registerFileVariables("Project",
QCoreApplication::translate("ProjectExplorer", "Main file of the project"),
[mainFilePath] { return mainFilePath.toString(); });
[mainFilePath] { return mainFilePath; });
registerVariable(Constants::VAR_CURRENTPROJECT_NAME,
QCoreApplication::translate("ProjectExplorer", "Name of current project"),
[projectName] { return projectName; }, false);

View File

@@ -203,7 +203,7 @@ RunConfiguration::RunConfiguration(Target *target, Utils::Id id)
[this] { return displayName(); });
m_expander.registerFileVariables("RunConfig:Executable",
tr("The run configuration's executable."),
[this] { return commandLine().executable().toString(); });
[this] { return commandLine().executable(); });
m_commandLineGetter = [this] {

View File

@@ -180,10 +180,10 @@ Target::Target(Project *project, Kit *k, _constructor_tag) :
}, false);
d->m_macroExpander.registerFileVariables("CurrentRun:Executable",
tr("The currently active run configuration's executable (if applicable)."),
[this]() -> QString {
[this]() -> FilePath {
if (RunConfiguration * const rc = activeRunConfiguration())
return rc->commandLine().executable().toString();
return QString();
return rc->commandLine().executable();
return FilePath();
}, false);
d->m_macroExpander.registerPrefix("CurrentRun:Env", tr("Variables in the current run environment."),
[this](const QString &var) {