Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 1 | //===--- ClangdLSPServer.h - LSP server --------------------------*- C++-*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
Kirill Bobyrev | 8e35f1e | 2018-08-14 16:03:32 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 9 | |
| 10 | #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDLSPSERVER_H |
| 11 | #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDLSPSERVER_H |
| 12 | |
| 13 | #include "ClangdServer.h" |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 14 | #include "DraftStore.h" |
Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 15 | #include "FindSymbols.h" |
Ilya Biryukov | 103c951 | 2017-06-13 15:59:43 +0000 | [diff] [blame] | 16 | #include "GlobalCompilationDatabase.h" |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 17 | #include "Path.h" |
| 18 | #include "Protocol.h" |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 19 | #include "Transport.h" |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 20 | #include "clang/Tooling/Core/Replacement.h" |
Krasimir Georgiev | 0dcb48e | 2017-07-19 15:43:35 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Optional.h" |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 22 | #include <memory> |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 23 | |
| 24 | namespace clang { |
| 25 | namespace clangd { |
| 26 | |
Haojian Wu | ba28e9a | 2018-01-10 14:44:34 +0000 | [diff] [blame] | 27 | class SymbolIndex; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 28 | |
Sam McCall | 45be5cf | 2018-09-13 12:58:36 +0000 | [diff] [blame] | 29 | /// This class exposes ClangdServer's capabilities via Language Server Protocol. |
| 30 | /// |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 31 | /// MessageHandler binds the implemented LSP methods (e.g. onInitialize) to |
| 32 | /// corresponding JSON-RPC methods ("initialize"). |
| 33 | /// The server also supports $/cancelRequest (MessageHandler provides this). |
| 34 | class ClangdLSPServer : private DiagnosticsConsumer { |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 35 | public: |
Ilya Biryukov | 0c1ca6b | 2017-10-02 15:13:20 +0000 | [diff] [blame] | 36 | /// If \p CompileCommandsDir has a value, compile_commands.json will be |
| 37 | /// loaded only from \p CompileCommandsDir. Otherwise, clangd will look |
| 38 | /// for compile_commands.json in all parent directories of each file. |
Sam McCall | c55d09a | 2018-11-02 13:09:36 +0000 | [diff] [blame^] | 39 | /// If UseDirBasedCDB is false, compile commands are not read from disk. |
| 40 | // FIXME: Clean up signature around CDBs. |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 41 | ClangdLSPServer(Transport &Transp, const clangd::CodeCompleteOptions &CCOpts, |
Sam McCall | c55d09a | 2018-11-02 13:09:36 +0000 | [diff] [blame^] | 42 | llvm::Optional<Path> CompileCommandsDir, bool UseDirBasedCDB, |
| 43 | const ClangdServer::Options &Opts); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 44 | ~ClangdLSPServer(); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 45 | |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 46 | /// Run LSP server loop, communicating with the Transport provided in the |
| 47 | /// constructor. This method must not be executed more than once. |
Ilya Biryukov | 0d9b8a3 | 2017-10-25 08:45:41 +0000 | [diff] [blame] | 48 | /// |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 49 | /// \return Whether we shut down cleanly with a 'shutdown' -> 'exit' sequence. |
| 50 | bool run(); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 51 | |
| 52 | private: |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 53 | // Implement DiagnosticsConsumer. |
Sam McCall | a7bb0cc | 2018-03-12 23:22:35 +0000 | [diff] [blame] | 54 | void onDiagnosticsReady(PathRef File, std::vector<Diag> Diagnostics) override; |
Ilya Biryukov | 103c951 | 2017-06-13 15:59:43 +0000 | [diff] [blame] | 55 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 56 | // LSP methods. Notifications have signature void(const Params&). |
| 57 | // Calls have signature void(const Params&, Callback<Response>). |
| 58 | void onInitialize(const InitializeParams &, Callback<llvm::json::Value>); |
| 59 | void onShutdown(const ShutdownParams &, Callback<std::nullptr_t>); |
| 60 | void onDocumentDidOpen(const DidOpenTextDocumentParams &); |
| 61 | void onDocumentDidChange(const DidChangeTextDocumentParams &); |
| 62 | void onDocumentDidClose(const DidCloseTextDocumentParams &); |
| 63 | void onDocumentOnTypeFormatting(const DocumentOnTypeFormattingParams &, |
| 64 | Callback<std::vector<TextEdit>>); |
| 65 | void onDocumentRangeFormatting(const DocumentRangeFormattingParams &, |
| 66 | Callback<std::vector<TextEdit>>); |
| 67 | void onDocumentFormatting(const DocumentFormattingParams &, |
| 68 | Callback<std::vector<TextEdit>>); |
| 69 | void onDocumentSymbol(const DocumentSymbolParams &, |
| 70 | Callback<std::vector<SymbolInformation>>); |
| 71 | void onCodeAction(const CodeActionParams &, Callback<llvm::json::Value>); |
| 72 | void onCompletion(const TextDocumentPositionParams &, |
| 73 | Callback<CompletionList>); |
| 74 | void onSignatureHelp(const TextDocumentPositionParams &, |
| 75 | Callback<SignatureHelp>); |
| 76 | void onGoToDefinition(const TextDocumentPositionParams &, |
| 77 | Callback<std::vector<Location>>); |
| 78 | void onReference(const ReferenceParams &, Callback<std::vector<Location>>); |
| 79 | void onSwitchSourceHeader(const TextDocumentIdentifier &, |
| 80 | Callback<std::string>); |
| 81 | void onDocumentHighlight(const TextDocumentPositionParams &, |
| 82 | Callback<std::vector<DocumentHighlight>>); |
| 83 | void onFileEvent(const DidChangeWatchedFilesParams &); |
| 84 | void onCommand(const ExecuteCommandParams &, Callback<llvm::json::Value>); |
| 85 | void onWorkspaceSymbol(const WorkspaceSymbolParams &, |
| 86 | Callback<std::vector<SymbolInformation>>); |
| 87 | void onRename(const RenameParams &, Callback<WorkspaceEdit>); |
| 88 | void onHover(const TextDocumentPositionParams &, |
| 89 | Callback<llvm::Optional<Hover>>); |
| 90 | void onChangeConfiguration(const DidChangeConfigurationParams &); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 91 | |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 92 | std::vector<Fix> getFixes(StringRef File, const clangd::Diagnostic &D); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 93 | |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 94 | /// Forces a reparse of all currently opened files. As a result, this method |
| 95 | /// may be very expensive. This method is normally called when the |
| 96 | /// compilation database is changed. |
| 97 | void reparseOpenedFiles(); |
Sam McCall | bc90461 | 2018-10-25 04:22:52 +0000 | [diff] [blame] | 98 | void applyConfiguration(const ConfigurationSettings &Settings); |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 99 | |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 100 | /// Used to indicate that the 'shutdown' request was received from the |
| 101 | /// Language Server client. |
Ilya Biryukov | 0d9b8a3 | 2017-10-25 08:45:41 +0000 | [diff] [blame] | 102 | bool ShutdownRequestReceived = false; |
| 103 | |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 104 | std::mutex FixItsMutex; |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 105 | typedef std::map<clangd::Diagnostic, std::vector<Fix>, LSPDiagnosticCompare> |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 106 | DiagnosticToReplacementMap; |
| 107 | /// Caches FixIts per file and diagnostics |
| 108 | llvm::StringMap<DiagnosticToReplacementMap> FixItsMap; |
Ilya Biryukov | 103c951 | 2017-06-13 15:59:43 +0000 | [diff] [blame] | 109 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 110 | // Most code should not deal with Transport directly. |
| 111 | // MessageHandler deals with incoming messages, use call() etc for outgoing. |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 112 | clangd::Transport &Transp; |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 113 | class MessageHandler; |
| 114 | std::unique_ptr<MessageHandler> MsgHandler; |
| 115 | std::atomic<int> NextCallID = {0}; |
| 116 | std::mutex TranspWriter; |
| 117 | void call(StringRef Method, llvm::json::Value Params); |
| 118 | void notify(StringRef Method, llvm::json::Value Params); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 119 | |
Ilya Biryukov | 103c951 | 2017-06-13 15:59:43 +0000 | [diff] [blame] | 120 | RealFileSystemProvider FSProvider; |
Ilya Biryukov | d3b04e3 | 2017-12-05 10:42:57 +0000 | [diff] [blame] | 121 | /// Options used for code completion |
| 122 | clangd::CodeCompleteOptions CCOpts; |
Alex Lorenz | 8626d36 | 2018-08-10 17:25:07 +0000 | [diff] [blame] | 123 | /// Options used for diagnostics. |
| 124 | ClangdDiagnosticOptions DiagOpts; |
Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 125 | /// The supported kinds of the client. |
| 126 | SymbolKindBitset SupportedSymbolKinds; |
Kadir Cetinkaya | 133d46f | 2018-09-27 17:13:07 +0000 | [diff] [blame] | 127 | /// The supported completion item kinds of the client. |
| 128 | CompletionItemKindBitset SupportedCompletionItemKinds; |
Sam McCall | 20841d4 | 2018-10-16 16:29:41 +0000 | [diff] [blame] | 129 | // Whether the client supports CodeAction response objects. |
| 130 | bool SupportsCodeAction = false; |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 131 | |
| 132 | // Store of the current versions of the open documents. |
| 133 | DraftStore DraftMgr; |
| 134 | |
Sam McCall | d1c9d11 | 2018-10-23 14:19:54 +0000 | [diff] [blame] | 135 | // The CDB is created by the "initialize" LSP method. |
Sam McCall | c55d09a | 2018-11-02 13:09:36 +0000 | [diff] [blame^] | 136 | bool UseDirBasedCDB; // FIXME: make this a capability. |
Sam McCall | 4b86bb0 | 2018-10-25 02:22:53 +0000 | [diff] [blame] | 137 | llvm::Optional<Path> CompileCommandsDir; // FIXME: merge with capability? |
Sam McCall | c55d09a | 2018-11-02 13:09:36 +0000 | [diff] [blame^] | 138 | std::unique_ptr<GlobalCompilationDatabase> BaseCDB; |
| 139 | // CDB is BaseCDB plus any comands overridden via LSP extensions. |
| 140 | llvm::Optional<OverlayCDB> CDB; |
Sam McCall | 3d0adbe | 2018-10-18 14:41:50 +0000 | [diff] [blame] | 141 | // The ClangdServer is created by the "initialize" LSP method. |
| 142 | // It is destroyed before run() returns, to ensure worker threads exit. |
| 143 | ClangdServer::Options ClangdServerOpts; |
| 144 | llvm::Optional<ClangdServer> Server; |
Kadir Cetinkaya | 689bf93 | 2018-08-24 13:09:41 +0000 | [diff] [blame] | 145 | }; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 146 | } // namespace clangd |
| 147 | } // namespace clang |
| 148 | |
Kirill Bobyrev | 8e35f1e | 2018-08-14 16:03:32 +0000 | [diff] [blame] | 149 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDLSPSERVER_H |