File indexing completed on 2024-04-28 11:45:20

0001 /*
0002     SPDX-FileCopyrightText: 2008 Paul Giannaros <paul@giannaros.org>
0003     SPDX-FileCopyrightText: 2009-2018 Dominik Haumann <dhaumann@kde.org>
0004     SPDX-FileCopyrightText: 2010 Joseph Wenninger <jowenn@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #ifndef KATE_SCRIPT_MANAGER_H
0010 #define KATE_SCRIPT_MANAGER_H
0011 
0012 #include <KTextEditor/Command>
0013 #include <ktexteditor/cursor.h>
0014 
0015 #include <QVector>
0016 
0017 class QString;
0018 class KateIndentScript;
0019 class KateCommandLineScript;
0020 
0021 /**
0022  * Manage the scripts on disks -- find them and query them.
0023  * Provides access to loaded scripts too.
0024  */
0025 class KateScriptManager : public KTextEditor::Command
0026 {
0027     Q_OBJECT
0028 
0029     KateScriptManager();
0030     static KateScriptManager *m_instance;
0031 
0032 public:
0033     ~KateScriptManager() override;
0034 
0035     /** Get all scripts available in the command line */
0036     const QVector<KateCommandLineScript *> &commandLineScripts()
0037     {
0038         return m_commandLineScripts;
0039     }
0040 
0041     /**
0042      * Get an indentation script for the given language -- if there is more than
0043      * one result, this will return the script with the highest priority. If
0044      * both have the same priority, an arbitrary script will be returned.
0045      * If no scripts are found, 0 is returned.
0046      */
0047     KateIndentScript *indenter(const QString &language);
0048 
0049     //
0050     // KTextEditor::Command
0051     //
0052 public:
0053     /**
0054      * execute command
0055      * @param view view to use for execution
0056      * @param cmd cmd string
0057      * @param errorMsg error to return if no success
0058      * @return success
0059      */
0060     bool exec(KTextEditor::View *view, const QString &cmd, QString &errorMsg, const KTextEditor::Range &) override;
0061 
0062     /**
0063      * get help for a command
0064      * @param view view to use
0065      * @param cmd cmd name
0066      * @param msg help message
0067      * @return help available or not
0068      */
0069     bool help(KTextEditor::View *view, const QString &cmd, QString &msg) override;
0070 
0071     static KateScriptManager *self()
0072     {
0073         if (m_instance == nullptr) {
0074             m_instance = new KateScriptManager();
0075         }
0076         return m_instance;
0077     }
0078 
0079     //
0080     // Helper methods
0081     //
0082 public:
0083     /**
0084      * Collect all scripts.
0085      */
0086     void collect();
0087 
0088 public:
0089     KateIndentScript *indentationScript(const QString &scriptname)
0090     {
0091         return m_indentationScriptMap.value(scriptname);
0092     }
0093 
0094     int indentationScriptCount()
0095     {
0096         return m_indentationScripts.size();
0097     }
0098     KateIndentScript *indentationScriptByIndex(int index)
0099     {
0100         return m_indentationScripts[index];
0101     }
0102 
0103 public:
0104     /** explicitly reload all scripts */
0105     void reload();
0106 
0107 Q_SIGNALS:
0108     /** this signal is emitted when all scripts are _deleted_ and reloaded again. */
0109     void reloaded();
0110 
0111 private:
0112     /** List of all command line scripts */
0113     QVector<KateCommandLineScript *> m_commandLineScripts;
0114 
0115     /** list of all indentation scripts */
0116     QList<KateIndentScript *> m_indentationScripts;
0117 
0118     /** hash of all existing indenter scripts, hashes basename -> script */
0119     QHash<QString, KateIndentScript *> m_indentationScriptMap;
0120 
0121     /** Map of language to indent scripts */
0122     QHash<QString, QVector<KateIndentScript *>> m_languageToIndenters;
0123 };
0124 
0125 #endif