File indexing completed on 2024-04-28 05:49:00

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 "lspclientplugin.h"
0010 #include "lspclientserver.h"
0011 
0012 #include <KTextEditor/Message>
0013 
0014 #include <memory>
0015 
0016 namespace KTextEditor
0017 {
0018 class MainWindow;
0019 class Document;
0020 class View;
0021 class MovingInterface;
0022 }
0023 
0024 class LSPClientRevisionSnapshot;
0025 
0026 /*
0027  * A helper class that manages LSP servers in relation to a KTextDocument.
0028  * That is, spin up a server for a document when so requested, and then
0029  * monitor when the server is up (or goes down) and the document (for changes).
0030  * Those changes may then be synced to the server when so requested (e.g. prior
0031  * to another component performing an LSP request for a document).
0032  * So, other than managing servers, it also manages the document-server
0033  * relationship (and document), what's in a name ...
0034  */
0035 class LSPClientServerManager : public QObject
0036 {
0037     Q_OBJECT
0038 
0039 public:
0040     // factory method; private implementation by interface
0041     static std::shared_ptr<LSPClientServerManager> new_(LSPClientPlugin *plugin);
0042 
0043     virtual std::shared_ptr<LSPClientServer> findServer(KTextEditor::View *view, bool updatedoc = true) = 0;
0044 
0045     virtual QJsonValue findServerConfig(KTextEditor::Document *document) = 0;
0046 
0047     virtual void update(KTextEditor::Document *doc, bool force) = 0;
0048 
0049     virtual void restart(LSPClientServer *server) = 0;
0050 
0051     virtual void setIncrementalSync(bool inc) = 0;
0052 
0053     virtual LSPClientCapabilities &clientCapabilities() = 0;
0054 
0055     // latest sync'ed revision of doc (-1 if N/A)
0056     virtual qint64 revision(KTextEditor::Document *doc) = 0;
0057 
0058     // lock all relevant documents' current revision and sync that to server
0059     // locks are released when returned snapshot is delete'd
0060     virtual LSPClientRevisionSnapshot *snapshot(LSPClientServer *server) = 0;
0061 
0062     // helper method providing descriptive label for a server
0063     static QString serverDescription(LSPClientServer *server)
0064     {
0065         if (server) {
0066             auto root = server->root().toLocalFile();
0067             return QStringLiteral("%1@%2").arg(server->langId(), root);
0068         } else {
0069             return {};
0070         }
0071     }
0072 
0073 public:
0074 Q_SIGNALS:
0075     void serverChanged();
0076     // proxy server signals in case those are emitted very early
0077     void serverShowMessage(LSPClientServer *server, const LSPShowMessageParams &);
0078     void serverLogMessage(LSPClientServer *server, const LSPShowMessageParams &);
0079     void serverWorkDoneProgress(LSPClientServer *server, const LSPWorkDoneProgressParams &);
0080     void showMessageRequest(const LSPShowMessageParams &message,
0081                             const QList<LSPMessageRequestAction> &actions,
0082                             const std::function<void()> chooseNothing,
0083                             bool &handled);
0084 };
0085 
0086 class LSPClientRevisionSnapshot : public QObject
0087 {
0088     Q_OBJECT
0089 
0090 public:
0091     // find a locked revision for url in snapshot
0092     virtual void find(const QUrl &url, KTextEditor::Document *&doc, qint64 &revision) const = 0;
0093 };