Warning, file /frameworks/ktexteditor/src/include/ktexteditor/command.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2005 Christoph Cullmann <cullmann@kde.org>
0003     SPDX-FileCopyrightText: 2005-2006 Dominik Haumann <dhdev@gmx.de>
0004     SPDX-FileCopyrightText: 2008 Erlend Hamberg <ehamberg@gmail.com>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #ifndef KTEXTEDITOR_COMMAND_H
0010 #define KTEXTEDITOR_COMMAND_H
0011 
0012 #include <KCompletion>
0013 #include <ktexteditor/range.h>
0014 #include <ktexteditor_export.h>
0015 
0016 #include <QObject>
0017 #include <QStringList>
0018 
0019 class KCompletion;
0020 
0021 namespace KTextEditor
0022 {
0023 class View;
0024 
0025 /**
0026  * \class Command command.h <KTextEditor/Command>
0027  *
0028  * \brief An Editor command line command.
0029  *
0030  * \section cmd_intro Introduction
0031  *
0032  * The Command class represents a command for the editor command line. A
0033  * command simply consists of a string, for example \e find. The command
0034  * auto-registers itself at the Editor. The Editor itself queries
0035  * the command for a list of accepted strings/commands by calling cmds().
0036  * If the command gets invoked the function exec() is called, i.e. you have
0037  * to implement the \e reaction in exec(). Whenever the user needs help for
0038  * a command help() is called.
0039  *
0040  * \section cmd_information Command Information
0041  * To provide reasonable information about a specific command there are the
0042  * following accessor functions for a given command string:
0043  *  - name() returns a label
0044  *  - description() returns a descriptive text
0045  *  - category() returns a category into which the command fits.
0046  *
0047  * These getters allow KTextEditor implementations to plug commands into menus
0048  * and toolbars, so that a user can assign shortcuts.
0049  *
0050  * \section cmd_completion Command Completion
0051  *
0052  * The Command optionally can show a completion popup to help the user select
0053  * a valid entry as first parameter to the Command. To this end, return a
0054  * valid completion item by reiplementing completionObject().
0055  *
0056  * The returned completion object is deleted automatically once it is not needed
0057  * anymore. Therefore neither delete the completion object yourself nor return
0058  * the same completion object twice.
0059  *
0060  * \section cmd_interactive Interactive Commands
0061  *
0062  * In case the Command needs to interactively process the text of the parameters,
0063  * override wantsToProcessText() by returning @e true and reimplement processText().
0064  *
0065  * A typical example of an iterative command would be the incremental search.
0066  *
0067  * \see KTextEditor::CommandInterface
0068  * \author Christoph Cullmann \<cullmann@kde.org\>
0069  */
0070 class KTEXTEDITOR_EXPORT Command : public QObject
0071 {
0072     Q_OBJECT
0073 
0074 public:
0075     /**
0076      * Constructor with \p parent.
0077      * Will register this command for the commands names given in \p cmds at the global editor instance.
0078      */
0079     Command(const QStringList &cmds, QObject *parent = nullptr);
0080 
0081     /**
0082      * Virtual destructor.
0083      * Will unregister this command at the global editor instance.
0084      */
0085     ~Command() override;
0086 
0087 public:
0088     /**
0089      * Return a list of strings a command may begin with.
0090      * This is the same list the command was constructed with.
0091      * A string is the start part of a pure text which can be handled by this
0092      * command, i.e. for the command s/sdl/sdf/g the corresponding string is
0093      * simply \e s, and for char:1212 simply \e char.
0094      * \return list of supported commands
0095      */
0096     inline const QStringList &cmds() const
0097     {
0098         return m_cmds;
0099     }
0100 
0101     /**
0102      * Find out if a given command can act on a range. This is used for checking
0103      * if a command should be called when the user also gave a range or if an
0104      * error should be raised.
0105      *
0106      * \return \e true if command supports acting on a range of lines, \e false
0107      *          not. The default implementation returns \e false.
0108      */
0109     virtual bool supportsRange(const QString &cmd);
0110 
0111     /**
0112      * Execute the command for the given \p view and \p cmd string.
0113      * Return the success value and a \p msg for status. As example we
0114      * consider a replace command. The replace command would return the number
0115      * of replaced strings as \p msg, like "16 replacements made." If an error
0116      * occurred in the usage it would return \e false and set the \p msg to
0117      * something like "missing argument." or such.
0118      *
0119      * If a non-invalid range is given, the command shall be executed on that range.
0120      * supportsRange() tells if the command supports that.
0121      *
0122      * \return \e true on success, otherwise \e false
0123      */
0124     virtual bool exec(KTextEditor::View *view, const QString &cmd, QString &msg, const KTextEditor::Range &range = KTextEditor::Range::invalid()) = 0;
0125 
0126     /**
0127      * Shows help for the given \p view and \p cmd string.
0128      * If your command has a help text for \p cmd you have to return \e true
0129      * and set the \p msg to a meaningful text. The help text is embedded by
0130      * the Editor in a Qt::RichText enabled widget, e.g. a QToolTip.
0131      * \return \e true if your command has a help text, otherwise \e false
0132      */
0133     virtual bool help(KTextEditor::View *view, const QString &cmd, QString &msg) = 0;
0134 
0135     /**
0136      * Return a KCompletion object that will substitute the command line
0137      * default one while typing the first argument of the command \p cmdname.
0138      * The text will be added to the command separated by one space character.
0139      *
0140      * Override this method if your command can provide a completion object.
0141      * The returned completion object is deleted automatically once it is not needed
0142      * anymore. Therefore neither delete the completion object yourself nor return
0143      * the same completion object twice.
0144      *
0145      * The default implementation returns a null pointer (\e nullptr).
0146      *
0147      * \param view the view the command will work on
0148      * \param cmdname the command name associated with this request.
0149      * \return a valid completion object or \e nullptr, if a completion object is
0150      *         not supported
0151      */
0152     virtual KCompletion *completionObject(KTextEditor::View *view, const QString &cmdname);
0153 
0154     /**
0155      * Check, whether the command wants to process text interactively for the
0156      * given command with name \p cmdname.
0157      * If you return true, the command's processText() method is called
0158      * whenever the text in the command line changed.
0159      *
0160      * Reimplement this to return true, if your commands wants to process the
0161      * text while typing.
0162      *
0163      * \param cmdname the command name associated with this query.
0164      * \return \e true, if your command wants to process text interactively,
0165      *         otherwise \e false
0166      * \see processText()
0167      */
0168     virtual bool wantsToProcessText(const QString &cmdname);
0169 
0170     /**
0171      * This is called by the command line each time the argument text for the
0172      * command changed, if wantsToProcessText() returns \e true.
0173      * \param view the current view
0174      * \param text the current command text typed by the user
0175      * \see wantsToProcessText()
0176      */
0177     virtual void processText(KTextEditor::View *view, const QString &text);
0178 
0179 private:
0180     /**
0181      * the command list this command got constructed with
0182      */
0183     const QStringList m_cmds;
0184 
0185     /**
0186      * Private d-pointer
0187      */
0188     class CommandPrivate *const d;
0189 };
0190 
0191 }
0192 
0193 #endif