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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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