From b55825a42000ee81e37c8e25bcd0784d26d6dc18 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Mon, 28 Sep 2020 16:18:44 +0200 Subject: [PATCH] ProjectExplorer: Clean up variables Global variables with names such as "CurrentProject*", "CurrentKit*" etc are harmful, because the term "current project" as used in Qt Creator does not refer to the "active project", but simply stands for the project that contains the node that is currently selected in the project tree, which in turn may or may not correspond to the current editor document, depending on the "sync with editor" setting. In other words, the "current project" is almost a random value with little meaning outside the project tree itself. Therefore, we remove "CurrentProject*" and friends, except the ones that are currently intentionally in use. The latter get renamed to "CurrentDocument:Project*", so their purpose becomes clear. Their old names are kept around for backward compatibility, but are not suggested by the variable chooser anymore, so new usages are unlikely and we can remove them at some point. We also add some ActiveProject* variants that have been requested in the past. Also remove the "CurrentSession" prefix that was deprecated six years ago. Fixes: QTCREATORBUG-12724 Fixes: QTCREATORBUG-24606 Change-Id: Ibba5d0e0ce3d2beb444a5eec01fbb9b745d90a1d Reviewed-by: Eike Ziller --- .../src/editors/creator-code-completion.qdoc | 5 +- share/qtcreator/snippets/cpp.xml | 14 +- src/libs/utils/macroexpander.cpp | 5 +- src/libs/utils/macroexpander.h | 2 +- .../coreplugin/locator/executefilter.cpp | 2 +- .../projectexplorer/buildconfiguration.cpp | 7 + src/plugins/projectexplorer/kit.cpp | 20 ++- .../kitmanagerconfigwidget.cpp | 2 +- .../projectexplorer/projectexplorer.cpp | 166 +++++------------- .../projectexplorerconstants.h | 6 - .../projectexplorer/projectmacroexpander.cpp | 21 ++- .../projectexplorer/runconfiguration.cpp | 17 -- src/plugins/projectexplorer/target.cpp | 39 +++- src/plugins/qtsupport/qtsupportplugin.cpp | 63 ++++--- src/plugins/vcsbase/vcsbaseconstants.h | 6 +- .../qtcreator/externaltools/lrelease.xml | 8 +- src/share/qtcreator/externaltools/lupdate.xml | 8 +- .../qtcreator/externaltools/qmlscene.xml | 2 +- .../qtcreator/externaltools/qmlviewer.xml | 2 +- 19 files changed, 190 insertions(+), 205 deletions(-) diff --git a/doc/qtcreator/src/editors/creator-code-completion.qdoc b/doc/qtcreator/src/editors/creator-code-completion.qdoc index c21ebef7b3e..7d7b627eb32 100644 --- a/doc/qtcreator/src/editors/creator-code-completion.qdoc +++ b/doc/qtcreator/src/editors/creator-code-completion.qdoc @@ -230,8 +230,9 @@ %{variable} \endcode - For example, the following variable expands to the name of the current - project: \c {%{CurrentProject:Name}}. + For example, the following variable expands to the name of the + project containing the file that is currently open in the editor: + \c {%{CurrentDocument:Project:Name}}. Use unique variable names within a snippet, because all instances of a variable are renamed when you specify a value for it. diff --git a/share/qtcreator/snippets/cpp.xml b/share/qtcreator/snippets/cpp.xml index 17cf2ebf1ca..82a62b36e6a 100644 --- a/share/qtcreator/snippets/cpp.xml +++ b/share/qtcreator/snippets/cpp.xml @@ -59,8 +59,8 @@ public: %{Cpp:LicenseTemplate} $$ /** -@if ('%{CurrentProject:Name}' !== '') - ** This file is part of the %{CurrentProject:Name} project. +@if ('%{CurrentDocument:Project:Name}' !== '') + ** This file is part of the %{CurrentDocument:Project:Name} project. @endif @if ('%{Env:QTC_COPYRIGHT_USER}' === '' || '%{Env:QTC_COPYRIGHT_EMAIL}' === '') ** Copyright %{CurrentDate:yyyy} $copyright_user$ <$copyright_email$>. @@ -95,8 +95,8 @@ $$ $$ /** -@if ('%{CurrentProject:Name}' !== '') - ** This file is part of the %{CurrentProject:Name} project. +@if ('%{CurrentDocument:Project:Name}' !== '') + ** This file is part of the %{CurrentDocument:Project:Name} project. @endif @if ('%{Env:QTC_COPYRIGHT_USER}' === '' || '%{Env:QTC_COPYRIGHT_EMAIL}' === '') ** Copyright %{CurrentDate:yyyy} $copyright_user$ <$copyright_email$>. @@ -120,8 +120,8 @@ $$ $$ /** -@if ('%{CurrentProject:Name}' !== '') - ** This file is part of the %{CurrentProject:Name} project. +@if ('%{CurrentDocument:Project:Name}' !== '') + ** This file is part of the %{CurrentDocument:Project:Name} project. @endif @if ('%{Env:QTC_COPYRIGHT_USER}' === '' || '%{Env:QTC_COPYRIGHT_EMAIL}' === '') ** Copyright %{CurrentDate:yyyy} $copyright_user$ <$copyright_email$>. @@ -153,7 +153,7 @@ $$ @endif ** Contact: https://www.qt.io/licensing/ ** -** This file is part of %{CurrentProject:Name} +** This file is part of %{CurrentDocument:Project:Name} ** ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in diff --git a/src/libs/utils/macroexpander.cpp b/src/libs/utils/macroexpander.cpp index d624d7cf2d0..f5aa71a16c1 100644 --- a/src/libs/utils/macroexpander.cpp +++ b/src/libs/utils/macroexpander.cpp @@ -343,10 +343,11 @@ static QByteArray fullPrefix(const QByteArray &prefix) * \sa registerVariables(), registerIntVariable(), registerFileVariables() */ void MacroExpander::registerPrefix(const QByteArray &prefix, const QString &description, - const MacroExpander::PrefixFunction &value) + const MacroExpander::PrefixFunction &value, bool visible) { QByteArray tmp = fullPrefix(prefix); - d->m_descriptions.insert(tmp + "", description); + if (visible) + d->m_descriptions.insert(tmp + "", description); d->m_prefixMap.insert(tmp, value); } diff --git a/src/libs/utils/macroexpander.h b/src/libs/utils/macroexpander.h index 70448069c51..e0a330956c9 100644 --- a/src/libs/utils/macroexpander.h +++ b/src/libs/utils/macroexpander.h @@ -68,7 +68,7 @@ public: using IntFunction = std::function; void registerPrefix(const QByteArray &prefix, - const QString &description, const PrefixFunction &value); + const QString &description, const PrefixFunction &value, bool visible = true); void registerVariable(const QByteArray &variable, const QString &description, const StringFunction &value, diff --git a/src/plugins/coreplugin/locator/executefilter.cpp b/src/plugins/coreplugin/locator/executefilter.cpp index b827b601910..c08a568ac3b 100644 --- a/src/plugins/coreplugin/locator/executefilter.cpp +++ b/src/plugins/coreplugin/locator/executefilter.cpp @@ -97,7 +97,7 @@ void ExecuteFilter::accept(LocatorFilterEntry selection, bool found; QString workingDirectory = Utils::globalMacroExpander()->value("CurrentDocument:Path", &found); if (!found || workingDirectory.isEmpty()) - workingDirectory = Utils::globalMacroExpander()->value("CurrentProject:Path", &found); + workingDirectory = Utils::globalMacroExpander()->value("CurrentDocument:Project:Path", &found); ExecuteData d; d.workingDirectory = workingDirectory; diff --git a/src/plugins/projectexplorer/buildconfiguration.cpp b/src/plugins/projectexplorer/buildconfiguration.cpp index df9ecce5e01..c54e6bc5f7b 100644 --- a/src/plugins/projectexplorer/buildconfiguration.cpp +++ b/src/plugins/projectexplorer/buildconfiguration.cpp @@ -172,11 +172,18 @@ BuildConfiguration::BuildConfiguration(Target *target, Utils::Id id) expander->registerVariable("buildDir", tr("Build directory"), [this] { return buildDirectory().toUserOutput(); }); + // TODO: Remove "Current" variants in ~4.16. expander->registerVariable(Constants::VAR_CURRENTBUILD_NAME, tr("Name of current build"), [this] { return displayName(); }, false); + expander->registerVariable("BuildConfig:Name", tr("Name of the build configuration"), + [this] { return displayName(); }); + expander->registerPrefix(Constants::VAR_CURRENTBUILD_ENV, tr("Variables in the current build environment"), + [this](const QString &var) { return environment().expandedValueForKey(var); }, false); + expander->registerPrefix("BuildConfig:Env", + tr("Variables in the build configuration's environment"), [this](const QString &var) { return environment().expandedValueForKey(var); }); updateCacheAndEmitEnvironmentChanged(); diff --git a/src/plugins/projectexplorer/kit.cpp b/src/plugins/projectexplorer/kit.cpp index 949b3c480ef..2fad1bf4032 100644 --- a/src/plugins/projectexplorer/kit.cpp +++ b/src/plugins/projectexplorer/kit.cpp @@ -98,20 +98,30 @@ public: for (KitAspect *aspect : KitManager::kitAspects()) aspect->addToMacroExpander(kit, &m_macroExpander); - // This provides the same global fall back as the global expander - // without relying on the currentKit() discovery process there. - m_macroExpander.registerVariable(Constants::VAR_CURRENTKIT_NAME, + // TODO: Remove the "Current" variants in ~4.16 + m_macroExpander.registerVariable("CurrentKit:Name", tr("The name of the currently active kit."), [kit] { return kit->displayName(); }, false); - m_macroExpander.registerVariable(Constants::VAR_CURRENTKIT_FILESYSTEMNAME, + m_macroExpander.registerVariable("Kit:Name", + tr("The name of the kit."), + [kit] { return kit->displayName(); }); + + m_macroExpander.registerVariable("CurrentKit:FileSystemName", tr("The name of the currently active kit in a filesystem-friendly version."), [kit] { return kit->fileSystemFriendlyName(); }, false); - m_macroExpander.registerVariable(Constants::VAR_CURRENTKIT_ID, + m_macroExpander.registerVariable("Kit:FileSystemName", + tr("The name of the kit in a filesystem-friendly version."), + [kit] { return kit->fileSystemFriendlyName(); }); + + m_macroExpander.registerVariable("CurrentKit:Id", tr("The id of the currently active kit."), [kit] { return kit->id().toString(); }, false); + m_macroExpander.registerVariable("Kit:Id", + tr("The id of the kit."), + [kit] { return kit->id().toString(); }); } DisplayName m_unexpandedDisplayName; diff --git a/src/plugins/projectexplorer/kitmanagerconfigwidget.cpp b/src/plugins/projectexplorer/kitmanagerconfigwidget.cpp index 24fb3857edb..caeed15cdf8 100644 --- a/src/plugins/projectexplorer/kitmanagerconfigwidget.cpp +++ b/src/plugins/projectexplorer/kitmanagerconfigwidget.cpp @@ -84,7 +84,7 @@ KitManagerConfigWidget::KitManagerConfigWidget(Kit *k) : tr("

The name of the kit suitable for generating " "directory names. This value is used for the variable %1, " "which for example determines the name of the shadow build directory." - "

").arg(QLatin1String(Constants::VAR_CURRENTKIT_FILESYSTEMNAME)); + "

").arg(QLatin1String("Kit:FileSystemName")); m_fileSystemFriendlyNameLineEdit->setToolTip(toolTip); QRegularExpression fileSystemFriendlyNameRegexp(QLatin1String("^[A-Za-z0-9_-]*$")); Q_ASSERT(fileSystemFriendlyNameRegexp.isValid()); diff --git a/src/plugins/projectexplorer/projectexplorer.cpp b/src/plugins/projectexplorer/projectexplorer.cpp index 1ed8c7c37d5..344f9c1766b 100644 --- a/src/plugins/projectexplorer/projectexplorer.cpp +++ b/src/plugins/projectexplorer/projectexplorer.cpp @@ -258,7 +258,7 @@ const char FOLDER_OPEN_LOCATIONS_CONTEXT_MENU[] = "Project.F.OpenLocation.CtxMe const char PROJECT_OPEN_LOCATIONS_CONTEXT_MENU[] = "Project.P.OpenLocation.CtxMenu"; // Default directories: -const char DEFAULT_BUILD_DIRECTORY_TEMPLATE[] = "../%{JS: Util.asciify(\"build-%{CurrentProject:Name}-%{CurrentKit:FileSystemName}-%{CurrentBuild:Name}\")}"; +const char DEFAULT_BUILD_DIRECTORY_TEMPLATE[] = "../%{JS: Util.asciify(\"build-%{Project:Name}-%{Kit:FileSystemName}-%{BuildConfig:Name}\")}"; const char DEFAULT_BUILD_DIRECTORY_TEMPLATE_KEY[] = "Directories/BuildDirectory.Template"; const char BUILD_BEFORE_DEPLOY_SETTINGS_KEY[] = "ProjectExplorer/Settings/BuildBeforeDeploy"; @@ -332,18 +332,6 @@ static BuildConfiguration *activeBuildConfiguration() return target ? target->activeBuildConfiguration() : nullptr; } -static RunConfiguration *activeRunConfiguration() -{ - Target *target = activeTarget(); - return target ? target->activeRunConfiguration() : nullptr; -} - -static Kit *currentKit() -{ - Target *target = activeTarget(); - return target ? target->kit() : nullptr; -} - static bool isTextFile(const QString &fileName) { return Utils::mimeTypeForFile(fileName).inherits( @@ -1543,10 +1531,18 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er = s->value(Constants::ABORT_BUILD_ALL_ON_ERROR_SETTINGS_KEY, true).toBool(); dd->m_projectExplorerSettings.lowBuildPriority = s->value(Constants::LOW_BUILD_PRIORITY_SETTINGS_KEY, false).toBool(); + dd->m_buildPropertiesSettings.buildDirectoryTemplate = s->value(Constants::DEFAULT_BUILD_DIRECTORY_TEMPLATE_KEY).toString(); if (dd->m_buildPropertiesSettings.buildDirectoryTemplate.isEmpty()) dd->m_buildPropertiesSettings.buildDirectoryTemplate = Constants::DEFAULT_BUILD_DIRECTORY_TEMPLATE; + // TODO: Remove in ~4.16 + dd->m_buildPropertiesSettings.buildDirectoryTemplate.replace("%{CurrentProject:Name}", + "%{Project:Name}"); + dd->m_buildPropertiesSettings.buildDirectoryTemplate.replace("%{CurrentKit:FileSystemName}", + "%{Kit:FileSystemName}"); + dd->m_buildPropertiesSettings.buildDirectoryTemplate.replace("%{CurrentBuild:Name}", + "%{BuildConfig:Name}"); const auto loadTriStateValue = [&s](const QString &key) { return TriState::fromVariant(s->value(key, TriState::Default.toVariant())); @@ -1745,6 +1741,7 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er // FIXME: These are mostly "legacy"/"convenience" entries, relying on // the global entry point ProjectExplorer::currentProject(). They should // not be used in the Run/Build configuration pages. + // TODO: Remove the CurrentProject versions in ~4.16 Utils::MacroExpander *expander = Utils::globalMacroExpander(); expander->registerFileVariables(Constants::VAR_CURRENTPROJECT_PREFIX, tr("Current project's main file."), @@ -1753,115 +1750,39 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er if (Project *project = ProjectTree::currentProject()) projectFilePath = project->projectFilePath(); return projectFilePath.toString(); - }); - - expander->registerVariable("CurrentProject:BuildPath", - tr("Full build path of the current project's active build configuration."), + }, false); + expander->registerFileVariables("CurrentDocument:Project", + tr("Main file of the project the current document belongs to."), []() -> QString { - BuildConfiguration *bc = activeBuildConfiguration(); - return bc ? bc->buildDirectory().toUserOutput() : QString(); - }); + Utils::FilePath projectFilePath; + if (Project *project = ProjectTree::currentProject()) + projectFilePath = project->projectFilePath(); + return projectFilePath.toString(); + }, false); expander->registerVariable(Constants::VAR_CURRENTPROJECT_NAME, tr("The name of the current project."), []() -> QString { Project *project = ProjectTree::currentProject(); return project ? project->displayName() : QString(); - }); - - expander->registerVariable(Constants::VAR_CURRENTKIT_NAME, - tr("The name of the currently active kit."), + }, false); + expander->registerVariable("CurrentDocument:Project:Name", + tr("The name of the project the current document belongs to."), []() -> QString { - Kit *kit = currentKit(); - return kit ? kit->displayName() : QString(); - }); - - expander->registerVariable(Constants::VAR_CURRENTKIT_FILESYSTEMNAME, - tr("The name of the currently active kit as a filesystem-friendly version."), - []() -> QString { - Kit *kit = currentKit(); - return kit ? kit->fileSystemFriendlyName() : QString(); - }); - - expander->registerVariable(Constants::VAR_CURRENTKIT_ID, - tr("The ID of the currently active kit."), - []() -> QString { - Kit *kit = currentKit(); - return kit ? kit->id().toString() : QString(); - }); - - expander->registerVariable("CurrentDevice:HostAddress", - tr("The host address of the device in the currently active kit."), - []() -> QString { - Kit *kit = currentKit(); - const IDevice::ConstPtr device = DeviceKitAspect::device(kit); - return device ? device->sshParameters().host() : QString(); - }); - - expander->registerVariable("CurrentDevice:SshPort", - tr("The SSH port of the device in the currently active kit."), - []() -> QString { - Kit *kit = currentKit(); - const IDevice::ConstPtr device = DeviceKitAspect::device(kit); - return device ? QString::number(device->sshParameters().port()) : QString(); - }); - - expander->registerVariable("CurrentDevice:UserName", - tr("The username with which to log into the device in the currently active kit."), - []() -> QString { - Kit *kit = currentKit(); - const IDevice::ConstPtr device = DeviceKitAspect::device(kit); - return device ? device->sshParameters().userName() : QString(); - }); - - - expander->registerVariable("CurrentDevice:PrivateKeyFile", - tr("The private key file with which to authenticate when logging into the device " - "in the currently active kit."), - []() -> QString { - Kit *kit = currentKit(); - const IDevice::ConstPtr device = DeviceKitAspect::device(kit); - return device ? device->sshParameters().privateKeyFile : QString(); - }); - - expander->registerVariable(Constants::VAR_CURRENTBUILD_NAME, - tr("The currently active build configuration's name."), - [&]() -> QString { - BuildConfiguration *bc = activeBuildConfiguration(); - return bc ? bc->displayName() : QString(); - }); - - expander->registerVariable(Constants::VAR_CURRENTRUN_NAME, - tr("The currently active run configuration's name."), - []() -> QString { - if (Target *target = activeTarget()) { - if (RunConfiguration *rc = target->activeRunConfiguration()) - return rc->displayName(); - } - return QString(); - }); - - expander->registerFileVariables("CurrentRun:Executable", - tr("The currently active run configuration's executable (if applicable)."), - []() -> QString { - if (Target *target = activeTarget()) { - if (RunConfiguration *rc = target->activeRunConfiguration()) - return rc->commandLine().executable().toString(); - } - return QString(); - }); - - expander->registerVariable(Constants::VAR_CURRENTBUILD_TYPE, - tr("The currently active build configuration's type."), - [&]() -> QString { - BuildConfiguration *bc = activeBuildConfiguration(); - const BuildConfiguration::BuildType type - = bc ? bc->buildType() : BuildConfiguration::Unknown; - return BuildConfiguration::buildTypeName(type); + Project *project = ProjectTree::currentProject(); + return project ? project->displayName() : QString(); }); expander->registerPrefix(Constants::VAR_CURRENTBUILD_ENV, BuildConfiguration::tr("Variables in the current build environment"), + [](const QString &var) { + if (BuildConfiguration *bc = activeBuildConfiguration()) + return bc->environment().expandedValueForKey(var); + return QString(); + }, false); + expander->registerPrefix("CurrentDocument:Project:BuildConfig:Env", + BuildConfiguration::tr("Variables in the active build environment " + "of the project containing the currently open document"), [](const QString &var) { if (BuildConfiguration *bc = activeBuildConfiguration()) return bc->environment().expandedValueForKey(var); @@ -1874,24 +1795,21 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er return bc->environment(); return Utils::Environment::systemEnvironment(); }}); - Utils::EnvironmentProvider::addProvider( - {"CurrentRun:Env", tr("Current Run Environment"), []() { - if (RunConfiguration *rc = activeRunConfiguration()) - if (auto envAspect = rc->aspect()) - return envAspect->environment(); + {"CurrentDocument:Project:BuildConfig:Env", tr("Current Build Environment"), []() { + if (BuildConfiguration *bc = activeBuildConfiguration()) + return bc->environment(); return Utils::Environment::systemEnvironment(); }}); - QString fileDescription = tr("File where current session is saved."); - auto fileHandler = [] { return SessionManager::sessionNameToFileName(SessionManager::activeSession()).toString(); }; - expander->registerFileVariables("Session", fileDescription, fileHandler); - expander->registerFileVariables("CurrentSession", fileDescription, fileHandler, false); - - QString nameDescription = tr("Name of current session."); - auto nameHandler = [] { return SessionManager::activeSession(); }; - expander->registerVariable("Session:Name", nameDescription, nameHandler); - expander->registerVariable("CurrentSession:Name", nameDescription, nameHandler, false); + const auto fileHandler = [] { + return SessionManager::sessionNameToFileName(SessionManager::activeSession()).toString(); + }; + expander->registerFileVariables("Session", tr("File where current session is saved."), + fileHandler); + expander->registerVariable("Session:Name", tr("Name of current session."), [] { + return SessionManager::activeSession(); + }); return true; } diff --git a/src/plugins/projectexplorer/projectexplorerconstants.h b/src/plugins/projectexplorer/projectexplorerconstants.h index 3351bc039f7..08671c7c6c1 100644 --- a/src/plugins/projectexplorer/projectexplorerconstants.h +++ b/src/plugins/projectexplorer/projectexplorerconstants.h @@ -189,14 +189,8 @@ const char ANDROID_ABI_X86_64[] = "x86_64"; // Variable Names: const char VAR_CURRENTPROJECT_PREFIX[] = "CurrentProject"; const char VAR_CURRENTPROJECT_NAME[] = "CurrentProject:Name"; -const char VAR_CURRENTKIT_NAME[] = "CurrentKit:Name"; -const char VAR_CURRENTKIT_FILESYSTEMNAME[] = "CurrentKit:FileSystemName"; -const char VAR_CURRENTKIT_ID[] = "CurrentKit:Id"; const char VAR_CURRENTBUILD_NAME[] = "CurrentBuild:Name"; -const char VAR_CURRENTBUILD_TYPE[] = "CurrentBuild:Type"; const char VAR_CURRENTBUILD_ENV[] = "CurrentBuild:Env"; -const char VAR_CURRENTRUN_NAME[] = "CurrentRun:Name"; -const char VAR_CURRENTRUN_WORKINGDIR[] = "CurrentRun:WorkingDir"; // JsonWizard: const char PAGE_ID_PREFIX[] = "PE.Wizard.Page."; diff --git a/src/plugins/projectexplorer/projectmacroexpander.cpp b/src/plugins/projectexplorer/projectmacroexpander.cpp index 6fcdb89f942..03f095caf2d 100644 --- a/src/plugins/projectexplorer/projectmacroexpander.cpp +++ b/src/plugins/projectexplorer/projectmacroexpander.cpp @@ -33,22 +33,33 @@ ProjectMacroExpander::ProjectMacroExpander(const Utils::FilePath &mainFilePath, const Kit *kit, const QString &bcName, BuildConfiguration::BuildType buildType) { + // 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); + registerFileVariables("Project", + QCoreApplication::translate("ProjectExplorer", "Main file of the project"), [mainFilePath] { return mainFilePath.toString(); }); - registerVariable(Constants::VAR_CURRENTPROJECT_NAME, QCoreApplication::translate("ProjectExplorer", "Name of current project"), + [projectName] { return projectName; }, false); + registerVariable("Project:Name", + QCoreApplication::translate("ProjectExplorer", "Name of the project"), [projectName] { return projectName; }); - registerVariable(Constants::VAR_CURRENTBUILD_NAME, QCoreApplication::translate("ProjectExplorer", "Name of current build"), + [bcName] { return bcName; }, false); + registerVariable("BuildConfig:Name", + QCoreApplication::translate( + "ProjectExplorer", "Name of the project's active build configuration"), [bcName] { return bcName; }); - - registerVariable(Constants::VAR_CURRENTBUILD_TYPE, + registerVariable("CurrentBuild:Type", QCoreApplication::translate("ProjectExplorer", "Type of current build"), + [buildType] { return BuildConfiguration::buildTypeName(buildType); }, false); + registerVariable("BuildConfig:Type", + QCoreApplication::translate( + "ProjectExplorer", "Type of the project's active build configuration"), [buildType] { return BuildConfiguration::buildTypeName(buildType); }); - registerSubProvider([kit] { return kit->macroExpander(); }); } diff --git a/src/plugins/projectexplorer/runconfiguration.cpp b/src/plugins/projectexplorer/runconfiguration.cpp index 01579ccfbcb..37b0e9fc92a 100644 --- a/src/plugins/projectexplorer/runconfiguration.cpp +++ b/src/plugins/projectexplorer/runconfiguration.cpp @@ -177,23 +177,6 @@ RunConfiguration::RunConfiguration(Target *target, Utils::Id id) BuildConfiguration *bc = target->activeBuildConfiguration(); return bc ? bc->macroExpander() : target->macroExpander(); }); - m_expander.registerPrefix("CurrentRun:Env", tr("Variables in the current run environment"), - [this](const QString &var) { - const auto envAspect = aspect(); - return envAspect ? envAspect->environment().expandedValueForKey(var) : QString(); - }); - - m_expander.registerVariable(Constants::VAR_CURRENTRUN_WORKINGDIR, - tr("The currently active run configuration's working directory"), - [this] { - const auto wdAspect = aspect(); - return wdAspect ? wdAspect->workingDirectory(&m_expander).toString() : QString(); - }); - - m_expander.registerVariable(Constants::VAR_CURRENTRUN_NAME, - QCoreApplication::translate("ProjectExplorer", "The currently active run configuration's name."), - [this] { return displayName(); }, false); - m_commandLineGetter = [this] { FilePath executable; if (const auto executableAspect = aspect()) diff --git a/src/plugins/projectexplorer/target.cpp b/src/plugins/projectexplorer/target.cpp index 6f898598d6d..5d253eb0977 100644 --- a/src/plugins/projectexplorer/target.cpp +++ b/src/plugins/projectexplorer/target.cpp @@ -34,6 +34,7 @@ #include "deployconfiguration.h" #include "deploymentdata.h" #include "devicesupport/devicemanager.h" +#include "environmentaspect.h" #include "kit.h" #include "kitinformation.h" #include "kitmanager.h" @@ -43,6 +44,7 @@ #include "projectexplorericons.h" #include "projectexplorersettings.h" #include "runconfiguration.h" +#include "runconfigurationaspects.h" #include "session.h" #include @@ -157,11 +159,46 @@ Target::Target(Project *project, Kit *k, _constructor_tag) : d->m_macroExpander.registerVariable("sourceDir", tr("Source directory"), [project] { return project->projectDirectory().toUserOutput(); }); - // Legacy support. + // TODO: Remove in ~4.16. d->m_macroExpander.registerVariable(Constants::VAR_CURRENTPROJECT_NAME, QCoreApplication::translate("ProjectExplorer", "Name of current project"), [project] { return project->displayName(); }, false); + d->m_macroExpander.registerVariable("Project:Name", + QCoreApplication::translate("ProjectExplorer", "Name of current project"), + [project] { return project->displayName(); }); + + d->m_macroExpander.registerVariable("CurrentRun:Name", + tr("The currently active run configuration's name."), + [this]() -> QString { + if (RunConfiguration * const rc = activeRunConfiguration()) + return rc->displayName(); + return QString(); + }); + d->m_macroExpander.registerFileVariables("CurrentRun:Executable", + tr("The currently active run configuration's executable (if applicable)."), + [this]() -> QString { + if (RunConfiguration * const rc = activeRunConfiguration()) + return rc->commandLine().executable().toString(); + return QString(); + }); + d->m_macroExpander.registerPrefix("CurrentRun:Env", tr("Variables in the current run environment"), + [this](const QString &var) { + if (RunConfiguration * const rc = activeRunConfiguration()) { + if (const auto envAspect = rc->aspect()) + return envAspect->environment().expandedValueForKey(var); + } + return QString(); + }); + d->m_macroExpander.registerVariable("CurrentRun:WorkingDir", + tr("The currently active run configuration's working directory"), + [this] { + if (RunConfiguration * const rc = activeRunConfiguration()) { + if (const auto wdAspect = rc->aspect()) + return wdAspect->workingDirectory(&d->m_macroExpander).toString(); + } + return QString(); + }); } Target::~Target() diff --git a/src/plugins/qtsupport/qtsupportplugin.cpp b/src/plugins/qtsupport/qtsupportplugin.cpp index 38c83d11e84..af27b738863 100644 --- a/src/plugins/qtsupport/qtsupportplugin.cpp +++ b/src/plugins/qtsupport/qtsupportplugin.cpp @@ -47,14 +47,12 @@ #include #include #include +#include #include #include #include -const char kHostBins[] = "CurrentProject:QT_HOST_BINS"; -const char kInstallBins[] = "CurrentProject:QT_INSTALL_BINS"; - using namespace Core; using namespace ProjectExplorer; @@ -107,15 +105,6 @@ bool QtSupportPlugin::initialize(const QStringList &arguments, QString *errorMes return true; } -static BaseQtVersion *qtVersion() -{ - ProjectExplorer::Project *project = ProjectExplorer::ProjectTree::currentProject(); - if (!project || !project->activeTarget()) - return nullptr; - - return QtKitAspect::qtVersion(project->activeTarget()->kit()); -} - const char kLinkWithQtInstallationSetting[] = "LinkWithQtInstallation"; static void askAboutQtInstallation() @@ -144,21 +133,55 @@ void QtSupportPlugin::extensionsInitialized() { Utils::MacroExpander *expander = Utils::globalMacroExpander(); + static const auto currentQtVersion = []() -> const BaseQtVersion * { + ProjectExplorer::Project *project = ProjectExplorer::ProjectTree::currentProject(); + if (!project || !project->activeTarget()) + return nullptr; + return QtKitAspect::qtVersion(project->activeTarget()->kit()); + }; + static const char kCurrentHostBins[] = "CurrentDocument:Project:QT_HOST_BINS"; expander->registerVariable( - kHostBins, - tr("Full path to the host bin directory of the current project's Qt version."), + kCurrentHostBins, + tr("Full path to the host bin directory of the Qt version in the active kit " + "of the project containing the current document."), []() { - BaseQtVersion *qt = qtVersion(); + const BaseQtVersion * const qt = currentQtVersion(); return qt ? qt->hostBinPath().toUserOutput() : QString(); }); expander->registerVariable( - kInstallBins, - tr("Full path to the target bin directory of the current project's Qt version.
" - "You probably want %1 instead.") - .arg(QString::fromLatin1(kInstallBins)), + "CurrentDocument:Project:QT_INSTALL_BINS", + tr("Full path to the target bin directory of the Qt version in the active kit " + "of the project containing the current document.
You probably want %1 instead.") + .arg(QString::fromLatin1(kCurrentHostBins)), []() { - BaseQtVersion *qt = qtVersion(); + const BaseQtVersion * const qt = currentQtVersion(); + return qt ? qt->binPath().toUserOutput() : QString(); + }); + + static const auto activeQtVersion = []() -> const BaseQtVersion * { + ProjectExplorer::Project *project = SessionManager::startupProject(); + if (!project || !project->activeTarget()) + return nullptr; + return QtKitAspect::qtVersion(project->activeTarget()->kit()); + }; + static const char kActiveHostBins[] = "ActiveProject:QT_HOST_BINS"; + expander->registerVariable( + kActiveHostBins, + tr("Full path to the host bin directory of the Qt version in the active kit " + "of the active project."), + []() { + const BaseQtVersion * const qt = activeQtVersion(); + return qt ? qt->hostBinPath().toUserOutput() : QString(); + }); + + expander->registerVariable( + "ActiveProject:QT_INSTALL_BINS", + tr("Full path to the target bin directory of the Qt version in the active kit " + "of the active project.
You probably want %1 instead.") + .arg(QString::fromLatin1(kActiveHostBins)), + []() { + const BaseQtVersion * const qt = activeQtVersion(); return qt ? qt->binPath().toUserOutput() : QString(); }); diff --git a/src/plugins/vcsbase/vcsbaseconstants.h b/src/plugins/vcsbase/vcsbaseconstants.h index 05b02a1ba71..bce79f81135 100644 --- a/src/plugins/vcsbase/vcsbaseconstants.h +++ b/src/plugins/vcsbase/vcsbaseconstants.h @@ -42,9 +42,9 @@ const char VCS_ID_SUBVERSION[] = "J.Subversion"; const char VCS_ID_PERFORCE[] = "P.Perforce"; const char VCS_ID_CVS[] = "Z.CVS"; -const char VAR_VCS_NAME[] = "CurrentProject:VcsName"; -const char VAR_VCS_TOPIC[] = "CurrentProject:VcsTopic"; -const char VAR_VCS_TOPLEVELPATH[] = "CurrentProject:VcsTopLevelPath"; +const char VAR_VCS_NAME[] = "CurrentDocument:Project:VcsName"; +const char VAR_VCS_TOPIC[] = "CurrentDocument:Project:VcsTopic"; +const char VAR_VCS_TOPLEVELPATH[] = "CurrentDocument:Project:VcsTopLevelPath"; } // namespace Constants } // namespace VcsBase diff --git a/src/share/qtcreator/externaltools/lrelease.xml b/src/share/qtcreator/externaltools/lrelease.xml index edd71f3b057..f35d6d81866 100644 --- a/src/share/qtcreator/externaltools/lrelease.xml +++ b/src/share/qtcreator/externaltools/lrelease.xml @@ -32,10 +32,10 @@ Linguist 2 - %{CurrentProject:QT_INSTALL_BINS}/lrelease + %{CurrentDocument:Project:QT_INSTALL_BINS}/lrelease lrelease - %{CurrentProject:FilePath} - %{CurrentProject:Path} - CurrentBuild:Env + %{CurrentDocument:Project:FilePath} + %{CurrentDocument:Project:Path} + CurrentDocument:Project:BuildConfig:Env diff --git a/src/share/qtcreator/externaltools/lupdate.xml b/src/share/qtcreator/externaltools/lupdate.xml index 769008712dd..693d2c06010 100644 --- a/src/share/qtcreator/externaltools/lupdate.xml +++ b/src/share/qtcreator/externaltools/lupdate.xml @@ -32,10 +32,10 @@ Linguist 1 - %{CurrentProject:QT_INSTALL_BINS}/lupdate + %{CurrentDocument:Project:QT_INSTALL_BINS}/lupdate lupdate - %{CurrentProject:FilePath} - %{CurrentProject:Path} - CurrentBuild:Env + %{CurrentDocument:Project:FilePath} + %{CurrentDocument:Project:Path} + CurrentDocument:Project:BuildConfig:Env diff --git a/src/share/qtcreator/externaltools/qmlscene.xml b/src/share/qtcreator/externaltools/qmlscene.xml index 33d8084bd45..a414a3f7cb0 100644 --- a/src/share/qtcreator/externaltools/qmlscene.xml +++ b/src/share/qtcreator/externaltools/qmlscene.xml @@ -32,7 +32,7 @@ Qt Quick 1 - %{CurrentProject:QT_INSTALL_BINS}/qmlscene + %{CurrentDocument:Project:QT_INSTALL_BINS}/qmlscene qmlscene %{CurrentDocument:FilePath} %{CurrentDocument:Path} diff --git a/src/share/qtcreator/externaltools/qmlviewer.xml b/src/share/qtcreator/externaltools/qmlviewer.xml index 7635167a744..32a362d6b84 100644 --- a/src/share/qtcreator/externaltools/qmlviewer.xml +++ b/src/share/qtcreator/externaltools/qmlviewer.xml @@ -32,7 +32,7 @@ Qt Quick 1 - %{CurrentProject:QT_INSTALL_BINS}/qmlviewer + %{CurrentDocument:Project:QT_INSTALL_BINS}/qmlviewer qmlviewer %{CurrentDocument:FilePath} %{CurrentDocument:Path}