File indexing completed on 2024-04-28 05:48:56

0001 /*
0002     SPDX-FileCopyrightText: 2019 Mark Nauwelaerts <mark.nauwelaerts@gmail.com>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QUrl>
0010 #include <QVariant>
0011 
0012 #include <KTextEditor/Message>
0013 #include <KTextEditor/Plugin>
0014 
0015 #include <map>
0016 #include <set>
0017 
0018 class LSPClientServerManager;
0019 
0020 class LSPClientPlugin : public KTextEditor::Plugin
0021 {
0022     Q_OBJECT
0023 
0024 public:
0025     explicit LSPClientPlugin(QObject *parent = nullptr, const QVariantList & = QVariantList());
0026     ~LSPClientPlugin() override;
0027 
0028     QObject *createView(KTextEditor::MainWindow *mainWindow) override;
0029 
0030     int configPages() const override;
0031     KTextEditor::ConfigPage *configPage(int number = 0, QWidget *parent = nullptr) override;
0032 
0033     void readConfig();
0034     void writeConfig() const;
0035 
0036     // path for local setting files, auto-created on load
0037     const QString m_settingsPath;
0038 
0039     // default config path
0040     const QUrl m_defaultConfigPath;
0041 
0042     // settings
0043     bool m_symbolDetails = false;
0044     bool m_symbolExpand = false;
0045     bool m_symbolTree = false;
0046     bool m_symbolSort = false;
0047     bool m_complDoc = false;
0048     bool m_refDeclaration = false;
0049     bool m_complParens = false;
0050     bool m_diagnostics = false;
0051     bool m_messages = false;
0052     bool m_autoHover = false;
0053     bool m_onTypeFormatting = false;
0054     bool m_incrementalSync = false;
0055     bool m_highlightGoto = true;
0056     QUrl m_configPath;
0057     bool m_semanticHighlighting = false;
0058     bool m_signatureHelp = true;
0059     bool m_autoImport = true;
0060     bool m_fmtOnSave = false;
0061     bool m_inlayHints = false;
0062 
0063     // debug mode?
0064     const bool m_debugMode;
0065 
0066     // hash of allowed and blacklisted server command lines
0067     std::map<QString, bool> m_serverCommandLineToAllowedState;
0068 
0069     // current active dialogs to ask for permission of some command line
0070     std::set<QString> m_currentActiveCommandLineDialogs;
0071 
0072     // get current config path
0073     QUrl configPath() const
0074     {
0075         return m_configPath.isEmpty() ? m_defaultConfigPath : m_configPath;
0076     }
0077 
0078     /**
0079      * Check if given command line is allowed to be executed.
0080      * Might ask the user for permission.
0081      * @param cmdline full command line including program to check
0082      * @return execution allowed?
0083      */
0084     bool isCommandLineAllowed(const QStringList &cmdline);
0085 
0086 Q_SIGNALS:
0087     // signal settings update
0088     void update() const;
0089 
0090     void showMessage(KTextEditor::Message::MessageType level, const QString &msg);
0091 
0092 private Q_SLOTS:
0093     /**
0094      * Ask the user via dialog if the given command line shall be allowed.
0095      * Will store the result internally and trigger LSP server restart after config change.
0096      * Will ensure we just ask once, even if multiple requests queue up.
0097      * @param fullCommandLineString full command line string to get permission for
0098      */
0099     void askForCommandLinePermission(const QString &fullCommandLineString);
0100 
0101 private:
0102     // server manager to pass along
0103     std::shared_ptr<LSPClientServerManager> m_serverManager;
0104 };