2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2014-10-13 10:24:29 +02:00
|
|
|
|
|
|
|
|
#include "macroexpander.h"
|
|
|
|
|
|
2014-10-20 23:13:13 +02:00
|
|
|
#include "algorithm.h"
|
2021-05-11 14:34:56 +02:00
|
|
|
#include "commandline.h"
|
2022-09-01 11:23:26 +02:00
|
|
|
#include "environment.h"
|
2014-11-05 17:35:24 +01:00
|
|
|
#include "qtcassert.h"
|
|
|
|
|
#include "stringutils.h"
|
2023-01-24 16:49:41 +01:00
|
|
|
#include "utilstr.h"
|
2014-10-20 23:13:13 +02:00
|
|
|
|
2015-03-20 13:10:28 +01:00
|
|
|
#include <QDir>
|
2014-10-13 12:49:05 +02:00
|
|
|
#include <QFileInfo>
|
2021-07-06 09:12:35 +02:00
|
|
|
#include <QLoggingCategory>
|
2014-10-13 12:49:05 +02:00
|
|
|
#include <QMap>
|
|
|
|
|
|
2014-10-13 10:24:29 +02:00
|
|
|
namespace Utils {
|
2014-10-13 12:49:05 +02:00
|
|
|
namespace Internal {
|
|
|
|
|
|
2021-07-06 09:12:35 +02:00
|
|
|
static Q_LOGGING_CATEGORY(expanderLog, "qtc.utils.macroexpander", QtWarningMsg)
|
|
|
|
|
|
2014-10-13 12:49:05 +02:00
|
|
|
const char kFilePathPostfix[] = ":FilePath";
|
|
|
|
|
const char kPathPostfix[] = ":Path";
|
2015-03-20 13:10:28 +01:00
|
|
|
const char kNativeFilePathPostfix[] = ":NativeFilePath";
|
|
|
|
|
const char kNativePathPostfix[] = ":NativePath";
|
2014-10-13 12:49:05 +02:00
|
|
|
const char kFileNamePostfix[] = ":FileName";
|
|
|
|
|
const char kFileBaseNamePostfix[] = ":FileBaseName";
|
|
|
|
|
|
2014-10-21 13:19:38 +02:00
|
|
|
class MacroExpanderPrivate : public AbstractMacroExpander
|
2014-10-13 12:49:05 +02:00
|
|
|
{
|
|
|
|
|
public:
|
2018-07-19 16:39:41 +02:00
|
|
|
MacroExpanderPrivate() = default;
|
2014-10-20 23:13:13 +02:00
|
|
|
|
2018-07-19 16:39:41 +02:00
|
|
|
bool resolveMacro(const QString &name, QString *ret, QSet<AbstractMacroExpander *> &seen) override
|
2014-10-21 13:19:38 +02:00
|
|
|
{
|
2017-06-07 16:50:04 +02:00
|
|
|
// Prevent loops:
|
|
|
|
|
const int count = seen.count();
|
|
|
|
|
seen.insert(this);
|
|
|
|
|
if (seen.count() == count)
|
|
|
|
|
return false;
|
|
|
|
|
|
2014-10-21 13:19:38 +02:00
|
|
|
bool found;
|
|
|
|
|
*ret = value(name.toUtf8(), &found);
|
|
|
|
|
if (found)
|
|
|
|
|
return true;
|
|
|
|
|
|
2017-06-07 16:50:04 +02:00
|
|
|
found = Utils::anyOf(m_subProviders, [name, ret, &seen] (const MacroExpanderProvider &p) -> bool {
|
2014-10-21 13:19:38 +02:00
|
|
|
MacroExpander *expander = p ? p() : 0;
|
2017-06-07 16:50:04 +02:00
|
|
|
return expander && expander->d->resolveMacro(name, ret, seen);
|
2014-10-21 13:19:38 +02:00
|
|
|
});
|
|
|
|
|
|
2014-10-22 16:22:39 +02:00
|
|
|
if (found)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
found = Utils::anyOf(m_extraResolvers, [name, ret] (const MacroExpander::ResolverFunction &resolver) {
|
|
|
|
|
return resolver(name, ret);
|
|
|
|
|
});
|
|
|
|
|
|
2014-10-21 13:19:38 +02:00
|
|
|
if (found)
|
|
|
|
|
return true;
|
|
|
|
|
|
2017-06-07 16:50:04 +02:00
|
|
|
return this == globalMacroExpander()->d ? false : globalMacroExpander()->d->resolveMacro(name, ret, seen);
|
2014-10-21 13:19:38 +02:00
|
|
|
}
|
|
|
|
|
|
2020-11-21 01:04:56 +01:00
|
|
|
QString value(const QByteArray &variable, bool *found) const
|
2014-10-21 13:19:38 +02:00
|
|
|
{
|
|
|
|
|
MacroExpander::StringFunction sf = m_map.value(variable);
|
|
|
|
|
if (sf) {
|
|
|
|
|
if (found)
|
|
|
|
|
*found = true;
|
|
|
|
|
return sf();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto it = m_prefixMap.constBegin(); it != m_prefixMap.constEnd(); ++it) {
|
|
|
|
|
if (variable.startsWith(it.key())) {
|
|
|
|
|
MacroExpander::PrefixFunction pf = it.value();
|
|
|
|
|
if (found)
|
|
|
|
|
*found = true;
|
|
|
|
|
return pf(QString::fromUtf8(variable.mid(it.key().count())));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (found)
|
|
|
|
|
*found = false;
|
|
|
|
|
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-13 12:49:05 +02:00
|
|
|
QHash<QByteArray, MacroExpander::StringFunction> m_map;
|
|
|
|
|
QHash<QByteArray, MacroExpander::PrefixFunction> m_prefixMap;
|
2014-10-22 16:22:39 +02:00
|
|
|
QVector<MacroExpander::ResolverFunction> m_extraResolvers;
|
2014-10-13 12:49:05 +02:00
|
|
|
QMap<QByteArray, QString> m_descriptions;
|
2014-10-13 18:49:44 +02:00
|
|
|
QString m_displayName;
|
2014-10-20 23:13:13 +02:00
|
|
|
QVector<MacroExpanderProvider> m_subProviders;
|
2018-07-19 16:39:41 +02:00
|
|
|
bool m_accumulating = false;
|
2014-11-05 11:47:11 +01:00
|
|
|
|
2018-07-19 16:39:41 +02:00
|
|
|
bool m_aborted = false;
|
|
|
|
|
int m_lockDepth = 0;
|
2014-10-13 12:49:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // Internal
|
|
|
|
|
|
|
|
|
|
using namespace Internal;
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
\class Utils::MacroExpander
|
2023-05-22 14:38:47 +02:00
|
|
|
\inmodule QtCreator
|
2014-10-13 12:49:05 +02:00
|
|
|
\brief The MacroExpander class manages \QC wide variables, that a user
|
|
|
|
|
can enter into many string settings. The variables are replaced by an actual value when the string
|
|
|
|
|
is used, similar to how environment variables are expanded by a shell.
|
|
|
|
|
|
|
|
|
|
\section1 Variables
|
|
|
|
|
|
|
|
|
|
Variable names can be basically any string without dollar sign and braces,
|
|
|
|
|
though it is recommended to only use 7-bit ASCII without special characters and whitespace.
|
|
|
|
|
|
|
|
|
|
If there are several variables that contain different aspects of the same object,
|
|
|
|
|
it is convention to give them the same prefix, followed by a colon and a postfix
|
|
|
|
|
that describes the aspect.
|
|
|
|
|
Examples of this are \c{CurrentDocument:FilePath} and \c{CurrentDocument:Selection}.
|
|
|
|
|
|
|
|
|
|
When the variable manager is requested to replace variables in a string, it looks for
|
|
|
|
|
variable names enclosed in %{ and }, like %{CurrentDocument:FilePath}.
|
|
|
|
|
|
|
|
|
|
Environment variables are accessible using the %{Env:...} notation.
|
|
|
|
|
For example, to access the SHELL environment variable, use %{Env:SHELL}.
|
|
|
|
|
|
|
|
|
|
\note The names of the variables are stored as QByteArray. They are typically
|
|
|
|
|
7-bit-clean. In cases where this is not possible, UTF-8 encoding is
|
|
|
|
|
assumed.
|
|
|
|
|
|
|
|
|
|
\section1 Providing Variable Values
|
|
|
|
|
|
|
|
|
|
Plugins can register variables together with a description through registerVariable().
|
|
|
|
|
A typical setup is to register variables in the Plugin::initialize() function.
|
|
|
|
|
|
|
|
|
|
\code
|
|
|
|
|
bool MyPlugin::initialize(const QStringList &arguments, QString *errorString)
|
|
|
|
|
{
|
|
|
|
|
[...]
|
|
|
|
|
MacroExpander::registerVariable(
|
|
|
|
|
"MyVariable",
|
2023-01-24 16:49:41 +01:00
|
|
|
Tr::tr("The current value of whatever I want."));
|
2023-04-04 11:20:40 +02:00
|
|
|
[] {
|
2014-10-13 12:49:05 +02:00
|
|
|
QString value;
|
|
|
|
|
// do whatever is necessary to retrieve the value
|
|
|
|
|
[...]
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
[...]
|
|
|
|
|
}
|
|
|
|
|
\endcode
|
2014-10-13 10:24:29 +02:00
|
|
|
|
|
|
|
|
|
2014-10-13 12:49:05 +02:00
|
|
|
For variables that refer to a file, you should use the convenience function
|
|
|
|
|
MacroExpander::registerFileVariables().
|
|
|
|
|
The functions take a variable prefix, like \c MyFileVariable,
|
|
|
|
|
and automatically handle standardized postfixes like \c{:FilePath},
|
|
|
|
|
\c{:Path} and \c{:FileBaseName}, resulting in the combined variables, such as
|
|
|
|
|
\c{MyFileVariable:FilePath}.
|
|
|
|
|
|
|
|
|
|
\section1 Providing and Expanding Parametrized Strings
|
|
|
|
|
|
|
|
|
|
Though it is possible to just ask the variable manager for the value of some variable in your
|
|
|
|
|
code, the preferred use case is to give the user the possibility to parametrize strings, for
|
|
|
|
|
example for settings.
|
|
|
|
|
|
|
|
|
|
(If you ever think about doing the former, think twice. It is much more efficient
|
|
|
|
|
to just ask the plugin that provides the variable value directly, without going through
|
|
|
|
|
string conversions, and through the variable manager which will do a large scale poll. To be
|
|
|
|
|
more concrete, using the example from the Providing Variable Values section: instead of
|
|
|
|
|
calling \c{MacroExpander::value("MyVariable")}, it is much more efficient to just ask directly
|
|
|
|
|
with \c{MyPlugin::variableValue()}.)
|
|
|
|
|
|
|
|
|
|
\section2 User Interface
|
|
|
|
|
|
|
|
|
|
If the string that you want to parametrize is settable by the user, through a QLineEdit or
|
|
|
|
|
QTextEdit derived class, you should add a variable chooser to your UI, which allows adding
|
2020-09-18 12:11:40 +02:00
|
|
|
variables to the string by browsing through a list. See Utils::VariableChooser for more
|
2014-10-13 12:49:05 +02:00
|
|
|
details.
|
|
|
|
|
|
|
|
|
|
\section2 Expanding Strings
|
|
|
|
|
|
|
|
|
|
Expanding variable values in strings is done by "macro expanders".
|
|
|
|
|
Utils::AbstractMacroExpander is the base class for these, and the variable manager
|
|
|
|
|
provides an implementation that expands \QC variables through
|
|
|
|
|
MacroExpander::macroExpander().
|
|
|
|
|
|
|
|
|
|
There are several different ways to expand a string, covering the different use cases,
|
|
|
|
|
listed here sorted by relevance:
|
|
|
|
|
\list
|
|
|
|
|
\li Using MacroExpander::expandedString(). This is the most comfortable way to get a string
|
|
|
|
|
with variable values expanded, but also the least flexible one. If this is sufficient for
|
|
|
|
|
you, use it.
|
|
|
|
|
\li Using the Utils::expandMacros() functions. These take a string and a macro expander (for which
|
|
|
|
|
you would use the one provided by the variable manager). Mostly the same as
|
|
|
|
|
MacroExpander::expandedString(), but also has a variant that does the replacement inline
|
|
|
|
|
instead of returning a new string.
|
2023-05-09 22:49:14 +02:00
|
|
|
\li Using Utils::CommandLine::expandMacros(). This expands the string while conforming to the
|
2014-10-13 12:49:05 +02:00
|
|
|
quoting rules of the platform it is run on. Use this function with the variable manager's
|
|
|
|
|
macro expander if your string will be passed as a command line parameter string to an
|
|
|
|
|
external command.
|
|
|
|
|
\li Writing your own macro expander that nests the variable manager's macro expander. And then
|
|
|
|
|
doing one of the above. This allows you to expand additional "local" variables/macros,
|
|
|
|
|
that do not come from the variable manager.
|
|
|
|
|
\endlist
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \internal
|
|
|
|
|
*/
|
|
|
|
|
MacroExpander::MacroExpander()
|
|
|
|
|
{
|
|
|
|
|
d = new MacroExpanderPrivate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \internal
|
|
|
|
|
*/
|
|
|
|
|
MacroExpander::~MacroExpander()
|
|
|
|
|
{
|
|
|
|
|
delete d;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \internal
|
|
|
|
|
*/
|
2014-10-21 13:19:38 +02:00
|
|
|
bool MacroExpander::resolveMacro(const QString &name, QString *ret) const
|
2014-10-13 10:24:29 +02:00
|
|
|
{
|
2017-06-07 16:50:04 +02:00
|
|
|
QSet<AbstractMacroExpander*> seen;
|
|
|
|
|
return d->resolveMacro(name, ret, seen);
|
2014-10-13 12:49:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Returns the value of the given \a variable. If \a found is given, it is
|
|
|
|
|
* set to true if the variable has a value at all, false if not.
|
|
|
|
|
*/
|
2014-10-21 13:19:38 +02:00
|
|
|
QString MacroExpander::value(const QByteArray &variable, bool *found) const
|
2014-10-13 12:49:05 +02:00
|
|
|
{
|
2014-10-21 13:19:38 +02:00
|
|
|
return d->value(variable, found);
|
2014-10-13 12:49:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Returns \a stringWithVariables with all variables replaced by their values.
|
|
|
|
|
* See the MacroExpander overview documentation for other ways to expand variables.
|
|
|
|
|
*
|
|
|
|
|
* \sa MacroExpander
|
|
|
|
|
* \sa macroExpander()
|
|
|
|
|
*/
|
2014-10-21 13:19:38 +02:00
|
|
|
QString MacroExpander::expand(const QString &stringWithVariables) const
|
2014-10-13 12:49:05 +02:00
|
|
|
{
|
2014-11-05 12:31:03 +01:00
|
|
|
if (d->m_lockDepth == 0)
|
|
|
|
|
d->m_aborted = false;
|
|
|
|
|
|
2015-07-13 12:13:18 +02:00
|
|
|
if (d->m_lockDepth > 10) { // Limit recursion.
|
2014-11-05 12:31:03 +01:00
|
|
|
d->m_aborted = true;
|
2014-11-05 11:47:11 +01:00
|
|
|
return QString();
|
2014-11-05 12:31:03 +01:00
|
|
|
}
|
2014-11-05 11:47:11 +01:00
|
|
|
|
|
|
|
|
++d->m_lockDepth;
|
|
|
|
|
|
2014-10-20 23:13:13 +02:00
|
|
|
QString res = stringWithVariables;
|
2014-10-21 13:19:38 +02:00
|
|
|
Utils::expandMacros(&res, d);
|
2014-11-05 11:47:11 +01:00
|
|
|
|
|
|
|
|
--d->m_lockDepth;
|
|
|
|
|
|
2014-11-05 12:31:03 +01:00
|
|
|
if (d->m_lockDepth == 0 && d->m_aborted)
|
2023-01-24 16:49:41 +01:00
|
|
|
return Tr::tr("Infinite recursion error") + QLatin1String(": ") + stringWithVariables;
|
2014-11-05 12:31:03 +01:00
|
|
|
|
2014-10-15 14:45:31 +02:00
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
FilePath MacroExpander::expand(const FilePath &fileNameWithVariables) const
|
2019-05-15 13:59:43 +02:00
|
|
|
{
|
2022-11-28 16:48:16 +01:00
|
|
|
// We want single variables to expand to fully qualified strings.
|
2023-05-23 12:45:51 +02:00
|
|
|
return FilePath::fromUserInput(expand(fileNameWithVariables.toString()));
|
2019-05-15 13:59:43 +02:00
|
|
|
}
|
|
|
|
|
|
2014-10-21 13:19:38 +02:00
|
|
|
QByteArray MacroExpander::expand(const QByteArray &stringWithVariables) const
|
2014-10-15 14:45:31 +02:00
|
|
|
{
|
|
|
|
|
return expand(QString::fromLatin1(stringWithVariables)).toLatin1();
|
2014-10-13 12:49:05 +02:00
|
|
|
}
|
|
|
|
|
|
2019-04-16 16:46:36 +02:00
|
|
|
QVariant MacroExpander::expandVariant(const QVariant &v) const
|
|
|
|
|
{
|
|
|
|
|
const auto type = QMetaType::Type(v.type());
|
|
|
|
|
if (type == QMetaType::QString) {
|
|
|
|
|
return expand(v.toString());
|
|
|
|
|
} else if (type == QMetaType::QStringList) {
|
|
|
|
|
return Utils::transform(v.toStringList(),
|
|
|
|
|
[this](const QString &s) -> QVariant { return expand(s); });
|
|
|
|
|
} else if (type == QMetaType::QVariantList) {
|
|
|
|
|
return Utils::transform(v.toList(), [this](const QVariant &v) { return expandVariant(v); });
|
|
|
|
|
} else if (type == QMetaType::QVariantMap) {
|
|
|
|
|
const auto map = v.toMap();
|
|
|
|
|
QVariantMap result;
|
2019-07-24 13:43:54 +02:00
|
|
|
for (auto it = map.cbegin(), end = map.cend(); it != end; ++it)
|
2019-04-16 16:46:36 +02:00
|
|
|
result.insert(it.key(), expandVariant(it.value()));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-21 13:19:38 +02:00
|
|
|
QString MacroExpander::expandProcessArgs(const QString &argsWithVariables) const
|
|
|
|
|
{
|
2021-07-06 09:12:35 +02:00
|
|
|
QString result = argsWithVariables;
|
|
|
|
|
const bool ok = ProcessArgs::expandMacros(&result, d);
|
|
|
|
|
QTC_ASSERT(ok, qCDebug(expanderLog) << "Expanding failed: " << argsWithVariables);
|
|
|
|
|
return result;
|
2014-10-21 13:19:38 +02:00
|
|
|
}
|
|
|
|
|
|
2015-09-17 13:48:02 +02:00
|
|
|
static QByteArray fullPrefix(const QByteArray &prefix)
|
|
|
|
|
{
|
|
|
|
|
QByteArray result = prefix;
|
|
|
|
|
if (!result.endsWith(':'))
|
|
|
|
|
result.append(':');
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-13 12:49:05 +02:00
|
|
|
/*!
|
|
|
|
|
* Makes the given string-valued \a prefix known to the variable manager,
|
|
|
|
|
* together with a localized \a description.
|
|
|
|
|
*
|
|
|
|
|
* The \a value PrefixFunction will be called and gets the full variable name
|
|
|
|
|
* with the prefix stripped as input.
|
|
|
|
|
*
|
|
|
|
|
* \sa registerVariables(), registerIntVariable(), registerFileVariables()
|
|
|
|
|
*/
|
|
|
|
|
void MacroExpander::registerPrefix(const QByteArray &prefix, const QString &description,
|
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 <eike.ziller@qt.io>
2020-09-28 16:18:44 +02:00
|
|
|
const MacroExpander::PrefixFunction &value, bool visible)
|
2014-10-13 12:49:05 +02:00
|
|
|
{
|
2015-09-17 13:48:02 +02:00
|
|
|
QByteArray tmp = fullPrefix(prefix);
|
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 <eike.ziller@qt.io>
2020-09-28 16:18:44 +02:00
|
|
|
if (visible)
|
|
|
|
|
d->m_descriptions.insert(tmp + "<value>", description);
|
2014-10-13 12:49:05 +02:00
|
|
|
d->m_prefixMap.insert(tmp, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Makes the given string-valued \a variable known to the variable manager,
|
|
|
|
|
* together with a localized \a description.
|
|
|
|
|
*
|
|
|
|
|
* \sa registerFileVariables(), registerIntVariable(), registerPrefix()
|
|
|
|
|
*/
|
|
|
|
|
void MacroExpander::registerVariable(const QByteArray &variable,
|
2014-11-05 10:30:51 +01:00
|
|
|
const QString &description, const StringFunction &value, bool visibleInChooser)
|
2014-10-13 12:49:05 +02:00
|
|
|
{
|
2016-01-04 13:57:57 +01:00
|
|
|
if (visibleInChooser)
|
|
|
|
|
d->m_descriptions.insert(variable, description);
|
2014-10-13 12:49:05 +02:00
|
|
|
d->m_map.insert(variable, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Makes the given integral-valued \a variable known to the variable manager,
|
|
|
|
|
* together with a localized \a description.
|
|
|
|
|
*
|
|
|
|
|
* \sa registerVariable(), registerFileVariables(), registerPrefix()
|
|
|
|
|
*/
|
|
|
|
|
void MacroExpander::registerIntVariable(const QByteArray &variable,
|
|
|
|
|
const QString &description, const MacroExpander::IntFunction &value)
|
|
|
|
|
{
|
|
|
|
|
const MacroExpander::IntFunction valuecopy = value; // do not capture a reference in a lambda
|
|
|
|
|
registerVariable(variable, description,
|
2022-12-07 14:34:32 +01:00
|
|
|
[valuecopy] { return QString::number(valuecopy ? valuecopy() : 0); });
|
2014-10-13 12:49:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Convenience function to register several variables with the same \a prefix, that have a file
|
|
|
|
|
* as a value. Takes the prefix and registers variables like \c{prefix:FilePath} and
|
|
|
|
|
* \c{prefix:Path}, with descriptions that start with the given \a heading.
|
|
|
|
|
* For example \c{registerFileVariables("CurrentDocument", tr("Current Document"))} registers
|
|
|
|
|
* variables such as \c{CurrentDocument:FilePath} with description
|
|
|
|
|
* "Current Document: Full path including file name."
|
|
|
|
|
*
|
|
|
|
|
* \sa registerVariable(), registerIntVariable(), registerPrefix()
|
|
|
|
|
*/
|
|
|
|
|
void MacroExpander::registerFileVariables(const QByteArray &prefix,
|
2021-06-01 18:18:02 +02:00
|
|
|
const QString &heading, const FileFunction &base, bool visibleInChooser)
|
2014-10-13 12:49:05 +02:00
|
|
|
{
|
2023-04-04 09:49:37 +02:00
|
|
|
registerVariable(
|
|
|
|
|
prefix + kFilePathPostfix,
|
|
|
|
|
Tr::tr("%1: Full path including file name.").arg(heading),
|
2023-04-18 18:01:27 +03:00
|
|
|
[base] { return base().path(); },
|
2023-04-04 09:49:37 +02:00
|
|
|
visibleInChooser);
|
|
|
|
|
|
|
|
|
|
registerVariable(
|
|
|
|
|
prefix + kPathPostfix,
|
|
|
|
|
Tr::tr("%1: Full path excluding file name.").arg(heading),
|
2023-04-18 18:01:27 +03:00
|
|
|
[base] { return base().parentDir().path(); },
|
2023-04-04 09:49:37 +02:00
|
|
|
visibleInChooser);
|
|
|
|
|
|
|
|
|
|
registerVariable(
|
|
|
|
|
prefix + kNativeFilePathPostfix,
|
|
|
|
|
Tr::tr(
|
|
|
|
|
"%1: Full path including file name, with native path separator (backslash on Windows).")
|
|
|
|
|
.arg(heading),
|
2023-04-04 11:20:40 +02:00
|
|
|
[base] { return base().nativePath(); },
|
2023-04-04 09:49:37 +02:00
|
|
|
visibleInChooser);
|
|
|
|
|
|
|
|
|
|
registerVariable(
|
|
|
|
|
prefix + kNativePathPostfix,
|
|
|
|
|
Tr::tr(
|
|
|
|
|
"%1: Full path excluding file name, with native path separator (backslash on Windows).")
|
|
|
|
|
.arg(heading),
|
2023-04-04 11:20:40 +02:00
|
|
|
[base] { return base().parentDir().nativePath(); },
|
2023-04-04 09:49:37 +02:00
|
|
|
visibleInChooser);
|
|
|
|
|
|
|
|
|
|
registerVariable(
|
|
|
|
|
prefix + kFileNamePostfix,
|
|
|
|
|
Tr::tr("%1: File name without path.").arg(heading),
|
2023-04-04 11:20:40 +02:00
|
|
|
[base] { return base().fileName(); },
|
2023-04-04 09:49:37 +02:00
|
|
|
visibleInChooser);
|
|
|
|
|
|
|
|
|
|
registerVariable(
|
|
|
|
|
prefix + kFileBaseNamePostfix,
|
|
|
|
|
Tr::tr("%1: File base name without path and suffix.").arg(heading),
|
2023-04-04 11:20:40 +02:00
|
|
|
[base] { return base().baseName(); },
|
2023-04-04 09:49:37 +02:00
|
|
|
visibleInChooser);
|
2014-10-13 12:49:05 +02:00
|
|
|
}
|
|
|
|
|
|
2014-10-22 16:22:39 +02:00
|
|
|
void MacroExpander::registerExtraResolver(const MacroExpander::ResolverFunction &value)
|
|
|
|
|
{
|
|
|
|
|
d->m_extraResolvers.append(value);
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-13 12:49:05 +02:00
|
|
|
/*!
|
|
|
|
|
* Returns all registered variable names.
|
|
|
|
|
*
|
|
|
|
|
* \sa registerVariable()
|
|
|
|
|
* \sa registerFileVariables()
|
|
|
|
|
*/
|
2014-11-05 10:30:51 +01:00
|
|
|
QList<QByteArray> MacroExpander::visibleVariables() const
|
2014-10-13 12:49:05 +02:00
|
|
|
{
|
2016-01-04 13:57:57 +01:00
|
|
|
return d->m_descriptions.keys();
|
2014-10-13 12:49:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Returns the description that was registered for the \a variable.
|
|
|
|
|
*/
|
2014-10-21 13:19:38 +02:00
|
|
|
QString MacroExpander::variableDescription(const QByteArray &variable) const
|
2014-10-13 12:49:05 +02:00
|
|
|
{
|
|
|
|
|
return d->m_descriptions.value(variable);
|
2014-10-13 10:24:29 +02:00
|
|
|
}
|
|
|
|
|
|
2015-09-17 13:48:02 +02:00
|
|
|
bool MacroExpander::isPrefixVariable(const QByteArray &variable) const
|
|
|
|
|
{
|
|
|
|
|
return d->m_prefixMap.contains(fullPrefix(variable));
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-27 17:45:49 +01:00
|
|
|
MacroExpanderProviders MacroExpander::subProviders() const
|
2014-10-20 23:13:13 +02:00
|
|
|
{
|
2014-11-27 17:45:49 +01:00
|
|
|
return d->m_subProviders;
|
2014-10-20 23:13:13 +02:00
|
|
|
}
|
|
|
|
|
|
2014-10-13 18:49:44 +02:00
|
|
|
QString MacroExpander::displayName() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_displayName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MacroExpander::setDisplayName(const QString &displayName)
|
|
|
|
|
{
|
|
|
|
|
d->m_displayName = displayName;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-20 23:13:13 +02:00
|
|
|
void MacroExpander::registerSubProvider(const MacroExpanderProvider &provider)
|
|
|
|
|
{
|
|
|
|
|
d->m_subProviders.append(provider);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MacroExpander::isAccumulating() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_accumulating;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MacroExpander::setAccumulating(bool on)
|
|
|
|
|
{
|
|
|
|
|
d->m_accumulating = on;
|
|
|
|
|
}
|
2014-10-13 18:49:44 +02:00
|
|
|
|
|
|
|
|
class GlobalMacroExpander : public MacroExpander
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
GlobalMacroExpander()
|
|
|
|
|
{
|
2023-01-24 16:49:41 +01:00
|
|
|
setDisplayName(Tr::tr("Global variables"));
|
|
|
|
|
registerPrefix("Env", Tr::tr("Access environment variables."),
|
2022-09-01 11:23:26 +02:00
|
|
|
[](const QString &value) { return qtcEnvironmentVariable(value); });
|
2014-10-13 18:49:44 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* Returns the expander for globally registered variables.
|
|
|
|
|
*/
|
|
|
|
|
MacroExpander *globalMacroExpander()
|
|
|
|
|
{
|
|
|
|
|
static GlobalMacroExpander theGlobalExpander;
|
|
|
|
|
return &theGlobalExpander;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-13 10:24:29 +02:00
|
|
|
} // namespace Utils
|