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