File indexing completed on 2024-04-14 03:55:32

0001 /*
0002     SPDX-FileCopyrightText: 2019 Dominik Haumann <dhaumann@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KTEXTEDITOR_VARIABLE_H
0008 #define KTEXTEDITOR_VARIABLE_H
0009 
0010 #include <QStringList>
0011 #include <functional>
0012 
0013 namespace KTextEditor
0014 {
0015 class View;
0016 
0017 /**
0018  * @brief Variable for variable expansion.
0019  *
0020  * @section variable_intro Introduction
0021  *
0022  * A Variable is used by the KTextEditor::Editor to expand variables, also
0023  * know as expanding macros. A Variable itself is defined by the variable
0024  * name() as well as a description() and a function that replaces the variable
0025  * by its value.
0026  *
0027  * To register a Variable in the Editor use either Editor::registerVariableMatch()
0028  * or Editor::registerPrefixMatch().
0029  *
0030  * @see KTextEditor::Editor, KTextEditor::Editor::registerVariableMatch(),
0031  *      KTextEditor::Editor::registerPrefixMatch()
0032  * @author Dominik Haumann \<dhaumann@kde.org\>
0033  */
0034 class Variable
0035 {
0036 public:
0037     /**
0038      * Function that is called to expand a variable in @p text.
0039      * @param text
0040      */
0041     using ExpandFunction = std::function<QString(const QStringView &text, KTextEditor::View *view)>;
0042 
0043     /**
0044      * Constructs an invalid Variable, see isValid().
0045      */
0046     Variable() = default;
0047 
0048     /**
0049      * Constructor defining a Variable by its @p name, its @p description, and
0050      * its function @p expansionFunc to expand a variable to its corresponding
0051      * value. The parameter @p isPrefixMatch indicates whether this Variable
0052      * represents an exact match (false) or a prefix match (true).
0053      *
0054      * @note The @p name should @e not be translated.
0055      */
0056     Variable(const QString &name, const QString &description, ExpandFunction expansionFunc, bool isPrefixMatch);
0057 
0058     /**
0059      * Copy constructor.
0060      */
0061     Variable(const Variable &copy) = default;
0062 
0063     /**
0064      * Assignment operator.
0065      */
0066     Variable &operator=(const Variable &copy) = default;
0067 
0068     /**
0069      * Returns true, if the name is non-empty and the function provided in the
0070      * constructor is not a nullptr.
0071      */
0072     bool isValid() const;
0073 
0074     /**
0075      * Returns whether this Variable represents an exact match (false) or a
0076      * prefix match (true).
0077      */
0078     bool isPrefixMatch() const;
0079 
0080     /**
0081      * Returns the @p name that was provided in the constructor.
0082      * Depending on where the Variable is registered, this name is used to
0083      * identify an exact match or a prefix match.
0084      */
0085     QString name() const;
0086 
0087     /**
0088      * Returns the description that was provided in the constructor.
0089      */
0090     QString description() const;
0091 
0092     /**
0093      * Expands the Variable to its value.
0094      *
0095      * As example for an exact match, a variable "CurerntDocument:Cursor:Line"
0096      * uses the @p view to return the current line of the text cursor. In this
0097      * case @p prefix equals the text of the variable itself, i.e.
0098      * "CurerntDocument:Cursor:Line".
0099      *
0100      * As example of a prefix match, a variable "ENV:value" expands the
0101      * environment value @e value, e.g. "ENV:HOME". In this case, the @p prefix
0102      * equals the text "ENV:HOME" and @p view would be unused.
0103      *
0104      * @return the expanded variable.
0105      */
0106     QString evaluate(const QStringView &prefix, KTextEditor::View *view) const;
0107 
0108 private:
0109     QString m_name;
0110     QString m_description;
0111     ExpandFunction m_function;
0112     bool m_isPrefixMatch = false;
0113 };
0114 
0115 }
0116 
0117 #endif