File indexing completed on 2024-04-21 03:57:46

0001 /*
0002     SPDX-FileCopyrightText: 2001-2010 Christoph Cullmann <cullmann@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef _KATE_CMD_H
0008 #define _KATE_CMD_H
0009 
0010 #include <KCompletion>
0011 
0012 #include <QHash>
0013 #include <QStringList>
0014 
0015 namespace KTextEditor
0016 {
0017 class Command;
0018 }
0019 
0020 class KateCmd
0021 {
0022 public:
0023     KateCmd();
0024     ~KateCmd();
0025 
0026     static KateCmd *self();
0027 
0028     bool registerCommand(KTextEditor::Command *cmd);
0029     bool unregisterCommand(KTextEditor::Command *cmd);
0030     KTextEditor::Command *queryCommand(const QString &cmd) const;
0031     QList<KTextEditor::Command *> commands() const;
0032     QStringList commandList() const;
0033 
0034     QStringList cmds();
0035     void appendHistory(const QString &cmd);
0036     const QString fromHistory(int i) const;
0037     uint historyLength() const
0038     {
0039         return m_history.count();
0040     }
0041 
0042     KCompletion *commandCompletionObject();
0043 
0044 private:
0045     QHash<QString, KTextEditor::Command *> m_dict;
0046     QStringList m_cmds;
0047     QStringList m_history;
0048     KCompletion m_cmdCompletion; // shared completion object for all KateCmdLineEdits in each KTE::View
0049 };
0050 
0051 /**
0052  * A KCompletion object that completes last ?unquoted? word in the string
0053  * passed. Do not mistake "shell" for anything related to quoting, this
0054  * simply mimics shell tab completion by completing the last word in the
0055  * provided text.
0056  */
0057 class KateCmdShellCompletion : public KCompletion
0058 {
0059 public:
0060     KateCmdShellCompletion();
0061 
0062     /**
0063      * Finds completions to the given text.
0064      * The first match is returned and emitted in the signal match().
0065      * @param text the text to complete
0066      * @return the first match, or QString() if not found
0067      */
0068     QString makeCompletion(const QString &text) override;
0069 
0070 protected:
0071     // Called by KCompletion
0072     void postProcessMatch(QString *match) const override;
0073     void postProcessMatches(QStringList *matches) const override;
0074     void postProcessMatches(KCompletionMatches *matches) const override;
0075 
0076 private:
0077     /**
0078      * Split text at the last unquoted space
0079      *
0080      * @param text_start will be set to the text at the left, including the space
0081      * @param text_compl Will be set to the text at the right. This is the text to complete.
0082      */
0083     void splitText(const QString &text, QString &text_start, QString &text_compl) const;
0084 
0085     QChar m_word_break_char;
0086     QChar m_quote_char1;
0087     QChar m_quote_char2;
0088     QChar m_escape_char;
0089 
0090     QString m_text_start;
0091     QString m_text_compl;
0092 };
0093 
0094 #endif