1/*
2 SPDX-FileCopyrightText: 2008 Paul Giannaros <paul@giannaros.org>
3 SPDX-FileCopyrightText: 2009-2018 Dominik Haumann <dhaumann@kde.org>
4 SPDX-FileCopyrightText: 2010 Joseph Wenninger <jowenn@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#ifndef KATE_SCRIPT_MANAGER_H
10#define KATE_SCRIPT_MANAGER_H
11
12#include <KTextEditor/Command>
13#include <ktexteditor/cursor.h>
14
15#include <QHash>
16#include <QList>
17
18class QString;
19class KateIndentScript;
20class KateCommandLineScript;
21
22/**
23 * Manage the scripts on disks -- find them and query them.
24 * Provides access to loaded scripts too.
25 */
26class KateScriptManager : public KTextEditor::Command
27{
28 Q_OBJECT
29
30 KateScriptManager();
31 static KateScriptManager *m_instance;
32
33public:
34 ~KateScriptManager() override;
35
36 /** Get all scripts available in the command line */
37 const QList<KateCommandLineScript *> &commandLineScripts()
38 {
39 return m_commandLineScripts;
40 }
41
42 /**
43 * Get an indentation script for the given language -- if there is more than
44 * one result, this will return the script with the highest priority. If
45 * both have the same priority, an arbitrary script will be returned.
46 * If no scripts are found, 0 is returned.
47 */
48 KateIndentScript *indenter(const QString &language);
49
50 //
51 // KTextEditor::Command
52 //
53public:
54 /**
55 * execute command
56 * @param view view to use for execution
57 * @param cmd cmd string
58 * @param errorMsg error to return if no success
59 * @return success
60 */
61 bool exec(KTextEditor::View *view, const QString &cmd, QString &errorMsg, const KTextEditor::Range &) override;
62
63 /**
64 * get help for a command
65 * @param view view to use
66 * @param cmd cmd name
67 * @param msg help message
68 * @return help available or not
69 */
70 bool help(KTextEditor::View *view, const QString &cmd, QString &msg) override;
71
72 static KateScriptManager *self()
73 {
74 if (m_instance == nullptr) {
75 m_instance = new KateScriptManager();
76 }
77 return m_instance;
78 }
79
80 //
81 // Helper methods
82 //
83public:
84 /**
85 * Collect all scripts.
86 */
87 void collect();
88
89public:
90 KateIndentScript *indentationScript(const QString &scriptname)
91 {
92 return m_indentationScriptMap.value(key: scriptname);
93 }
94
95 int indentationScriptCount()
96 {
97 return m_indentationScripts.size();
98 }
99 KateIndentScript *indentationScriptByIndex(int index)
100 {
101 return m_indentationScripts[index];
102 }
103
104public:
105 /** explicitly reload all scripts */
106 void reload();
107
108Q_SIGNALS:
109 /** this signal is emitted when all scripts are _deleted_ and reloaded again. */
110 void reloaded();
111
112private:
113 /** List of all command line scripts */
114 QList<KateCommandLineScript *> m_commandLineScripts;
115
116 /** list of all indentation scripts */
117 QList<KateIndentScript *> m_indentationScripts;
118
119 /** hash of all existing indenter scripts, hashes basename -> script */
120 QHash<QString, KateIndentScript *> m_indentationScriptMap;
121
122 /** Map of language to indent scripts */
123 QHash<QString, QList<KateIndentScript *>> m_languageToIndenters;
124};
125
126#endif
127

source code of ktexteditor/src/script/katescriptmanager.h