Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 1 | //===--- ClangdLSPServer.h - LSP server --------------------------*- C++-*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 6 | // |
Kirill Bobyrev | 8e35f1e | 2018-08-14 16:03:32 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 8 | |
| 9 | #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDLSPSERVER_H |
| 10 | #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDLSPSERVER_H |
| 11 | |
| 12 | #include "ClangdServer.h" |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 13 | #include "DraftStore.h" |
Saleem Abdulrasool | e1b9b9d | 2019-04-10 16:48:52 +0000 | [diff] [blame] | 14 | #include "Features.inc" |
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 "Protocol.h" |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 18 | #include "Transport.h" |
Sam McCall | ad97ccf | 2020-04-28 17:49:17 +0200 | [diff] [blame] | 19 | #include "support/Context.h" |
| 20 | #include "support/Path.h" |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 21 | #include "clang/Tooling/Core/Replacement.h" |
Krasimir Georgiev | 0dcb48e | 2017-07-19 15:43:35 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Optional.h" |
David Goldman | 60249c2 | 2020-01-13 17:01:10 -0500 | [diff] [blame] | 23 | #include "llvm/ADT/StringSet.h" |
Sam McCall | 2cd33e6 | 2020-03-04 00:33:29 +0100 | [diff] [blame] | 24 | #include "llvm/Support/JSON.h" |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 25 | #include <memory> |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 26 | |
| 27 | namespace clang { |
| 28 | namespace clangd { |
| 29 | |
Haojian Wu | ba28e9a | 2018-01-10 14:44:34 +0000 | [diff] [blame] | 30 | class SymbolIndex; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 31 | |
Sam McCall | 45be5cf | 2018-09-13 12:58:36 +0000 | [diff] [blame] | 32 | /// This class exposes ClangdServer's capabilities via Language Server Protocol. |
| 33 | /// |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 34 | /// MessageHandler binds the implemented LSP methods (e.g. onInitialize) to |
| 35 | /// corresponding JSON-RPC methods ("initialize"). |
| 36 | /// The server also supports $/cancelRequest (MessageHandler provides this). |
Sam McCall | 6ef1cce | 2020-01-24 14:08:56 +0100 | [diff] [blame] | 37 | class ClangdLSPServer : private ClangdServer::Callbacks { |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 38 | public: |
Ilya Biryukov | 0c1ca6b | 2017-10-02 15:13:20 +0000 | [diff] [blame] | 39 | /// If \p CompileCommandsDir has a value, compile_commands.json will be |
| 40 | /// loaded only from \p CompileCommandsDir. Otherwise, clangd will look |
| 41 | /// for compile_commands.json in all parent directories of each file. |
Sam McCall | c55d09a | 2018-11-02 13:09:36 +0000 | [diff] [blame] | 42 | /// If UseDirBasedCDB is false, compile commands are not read from disk. |
| 43 | // FIXME: Clean up signature around CDBs. |
Kadir Cetinkaya | 8d654df | 2020-06-17 18:09:54 +0200 | [diff] [blame] | 44 | ClangdLSPServer(Transport &Transp, const ThreadsafeFS &TFS, |
Haojian Wu | 1ca0c58 | 2019-01-22 09:39:05 +0000 | [diff] [blame] | 45 | const clangd::CodeCompleteOptions &CCOpts, |
Haojian Wu | 34d0e1b | 2020-02-19 15:37:36 +0100 | [diff] [blame] | 46 | const clangd::RenameOptions &RenameOpts, |
Sam McCall | c55d09a | 2018-11-02 13:09:36 +0000 | [diff] [blame] | 47 | llvm::Optional<Path> CompileCommandsDir, bool UseDirBasedCDB, |
Sam McCall | a69698f | 2019-03-27 17:47:49 +0000 | [diff] [blame] | 48 | llvm::Optional<OffsetEncoding> ForcedOffsetEncoding, |
Sam McCall | c55d09a | 2018-11-02 13:09:36 +0000 | [diff] [blame] | 49 | const ClangdServer::Options &Opts); |
Sam McCall | 8bda5f2 | 2019-10-23 11:11:18 +0200 | [diff] [blame] | 50 | /// The destructor blocks on any outstanding background tasks. |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 51 | ~ClangdLSPServer(); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 52 | |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 53 | /// Run LSP server loop, communicating with the Transport provided in the |
| 54 | /// constructor. This method must not be executed more than once. |
Ilya Biryukov | 0d9b8a3 | 2017-10-25 08:45:41 +0000 | [diff] [blame] | 55 | /// |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 56 | /// \return Whether we shut down cleanly with a 'shutdown' -> 'exit' sequence. |
| 57 | bool run(); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 58 | |
| 59 | private: |
Sam McCall | 6ef1cce | 2020-01-24 14:08:56 +0100 | [diff] [blame] | 60 | // Implement ClangdServer::Callbacks. |
Sam McCall | 2cd33e6 | 2020-03-04 00:33:29 +0100 | [diff] [blame] | 61 | void onDiagnosticsReady(PathRef File, llvm::StringRef Version, |
| 62 | std::vector<Diag> Diagnostics) override; |
Haojian Wu | b618849 | 2018-12-20 15:39:12 +0000 | [diff] [blame] | 63 | void onFileUpdated(PathRef File, const TUStatus &Status) override; |
Haojian Wu | 0a6000f | 2019-08-26 08:38:45 +0000 | [diff] [blame] | 64 | void |
Sam McCall | 2cd33e6 | 2020-03-04 00:33:29 +0100 | [diff] [blame] | 65 | onHighlightingsReady(PathRef File, llvm::StringRef Version, |
Haojian Wu | 0a6000f | 2019-08-26 08:38:45 +0000 | [diff] [blame] | 66 | std::vector<HighlightingToken> Highlightings) override; |
Sam McCall | 7d20e80 | 2020-01-22 19:41:45 +0100 | [diff] [blame] | 67 | void onBackgroundIndexProgress(const BackgroundQueue::Stats &Stats) override; |
Ilya Biryukov | 103c951 | 2017-06-13 15:59:43 +0000 | [diff] [blame] | 68 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 69 | // LSP methods. Notifications have signature void(const Params&). |
| 70 | // Calls have signature void(const Params&, Callback<Response>). |
| 71 | void onInitialize(const InitializeParams &, Callback<llvm::json::Value>); |
Sam McCall | 8a2d294 | 2020-03-03 12:12:14 +0100 | [diff] [blame] | 72 | void onInitialized(const InitializedParams &); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 73 | void onShutdown(const ShutdownParams &, Callback<std::nullptr_t>); |
Sam McCall | 422c828 | 2018-11-26 16:00:11 +0000 | [diff] [blame] | 74 | void onSync(const NoParams &, Callback<std::nullptr_t>); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 75 | void onDocumentDidOpen(const DidOpenTextDocumentParams &); |
| 76 | void onDocumentDidChange(const DidChangeTextDocumentParams &); |
| 77 | void onDocumentDidClose(const DidCloseTextDocumentParams &); |
Sam McCall | 596b63a | 2020-04-10 03:27:37 +0200 | [diff] [blame] | 78 | void onDocumentDidSave(const DidSaveTextDocumentParams &); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 79 | void onDocumentOnTypeFormatting(const DocumentOnTypeFormattingParams &, |
| 80 | Callback<std::vector<TextEdit>>); |
| 81 | void onDocumentRangeFormatting(const DocumentRangeFormattingParams &, |
| 82 | Callback<std::vector<TextEdit>>); |
| 83 | void onDocumentFormatting(const DocumentFormattingParams &, |
| 84 | Callback<std::vector<TextEdit>>); |
Ilya Biryukov | 19d7560 | 2018-11-23 15:21:19 +0000 | [diff] [blame] | 85 | // The results are serialized 'vector<DocumentSymbol>' if |
| 86 | // SupportsHierarchicalDocumentSymbol is true and 'vector<SymbolInformation>' |
| 87 | // otherwise. |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 88 | void onDocumentSymbol(const DocumentSymbolParams &, |
Ilya Biryukov | 19d7560 | 2018-11-23 15:21:19 +0000 | [diff] [blame] | 89 | Callback<llvm::json::Value>); |
Kirill Bobyrev | 7a514c9 | 2020-07-14 09:28:38 +0200 | [diff] [blame] | 90 | void onFoldingRange(const FoldingRangeParams &, |
| 91 | Callback<std::vector<FoldingRange>>); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 92 | void onCodeAction(const CodeActionParams &, Callback<llvm::json::Value>); |
Ilya Biryukov | b0826bd | 2019-01-03 13:37:12 +0000 | [diff] [blame] | 93 | void onCompletion(const CompletionParams &, Callback<CompletionList>); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 94 | void onSignatureHelp(const TextDocumentPositionParams &, |
| 95 | Callback<SignatureHelp>); |
Sam McCall | 866ba2c | 2019-02-01 11:26:13 +0000 | [diff] [blame] | 96 | void onGoToDeclaration(const TextDocumentPositionParams &, |
| 97 | Callback<std::vector<Location>>); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 98 | void onGoToDefinition(const TextDocumentPositionParams &, |
| 99 | Callback<std::vector<Location>>); |
| 100 | void onReference(const ReferenceParams &, Callback<std::vector<Location>>); |
| 101 | void onSwitchSourceHeader(const TextDocumentIdentifier &, |
Sam McCall | b9ec3e9 | 2019-05-07 08:30:32 +0000 | [diff] [blame] | 102 | Callback<llvm::Optional<URIForFile>>); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 103 | void onDocumentHighlight(const TextDocumentPositionParams &, |
| 104 | Callback<std::vector<DocumentHighlight>>); |
| 105 | void onFileEvent(const DidChangeWatchedFilesParams &); |
| 106 | void onCommand(const ExecuteCommandParams &, Callback<llvm::json::Value>); |
| 107 | void onWorkspaceSymbol(const WorkspaceSymbolParams &, |
| 108 | Callback<std::vector<SymbolInformation>>); |
Haojian Wu | f429ab6 | 2019-07-24 07:49:23 +0000 | [diff] [blame] | 109 | void onPrepareRename(const TextDocumentPositionParams &, |
| 110 | Callback<llvm::Optional<Range>>); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 111 | void onRename(const RenameParams &, Callback<WorkspaceEdit>); |
| 112 | void onHover(const TextDocumentPositionParams &, |
| 113 | Callback<llvm::Optional<Hover>>); |
Kadir Cetinkaya | 8665802 | 2019-03-19 09:27:04 +0000 | [diff] [blame] | 114 | void onTypeHierarchy(const TypeHierarchyParams &, |
| 115 | Callback<llvm::Optional<TypeHierarchyItem>>); |
Nathan Ridge | 087b044 | 2019-07-13 03:24:48 +0000 | [diff] [blame] | 116 | void onResolveTypeHierarchy(const ResolveTypeHierarchyItemParams &, |
| 117 | Callback<llvm::Optional<TypeHierarchyItem>>); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 118 | void onChangeConfiguration(const DidChangeConfigurationParams &); |
Jan Korous | b406701 | 2018-11-27 16:40:46 +0000 | [diff] [blame] | 119 | void onSymbolInfo(const TextDocumentPositionParams &, |
| 120 | Callback<std::vector<SymbolDetails>>); |
Utkarsh Saxena | 55925da | 2019-09-24 13:38:33 +0000 | [diff] [blame] | 121 | void onSelectionRange(const SelectionRangeParams &, |
| 122 | Callback<std::vector<SelectionRange>>); |
Sam McCall | 8d7ecc1 | 2019-12-16 19:08:51 +0100 | [diff] [blame] | 123 | void onDocumentLink(const DocumentLinkParams &, |
| 124 | Callback<std::vector<DocumentLink>>); |
Sam McCall | 71177ac | 2020-03-24 02:24:47 +0100 | [diff] [blame] | 125 | void onSemanticTokens(const SemanticTokensParams &, Callback<SemanticTokens>); |
Sam McCall | 5fea54b | 2020-07-10 16:08:14 +0200 | [diff] [blame] | 126 | void onSemanticTokensDelta(const SemanticTokensDeltaParams &, |
| 127 | Callback<SemanticTokensOrDelta>); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 128 | |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 129 | std::vector<Fix> getFixes(StringRef File, const clangd::Diagnostic &D); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 130 | |
Ilya Biryukov | b0826bd | 2019-01-03 13:37:12 +0000 | [diff] [blame] | 131 | /// Checks if completion request should be ignored. We need this due to the |
| 132 | /// limitation of the LSP. Per LSP, a client sends requests for all "trigger |
| 133 | /// character" we specify, but for '>' and ':' we need to check they actually |
| 134 | /// produce '->' and '::', respectively. |
| 135 | bool shouldRunCompletion(const CompletionParams &Params) const; |
| 136 | |
Sam McCall | 596b63a | 2020-04-10 03:27:37 +0200 | [diff] [blame] | 137 | /// Requests a reparse of currently opened files using their latest source. |
| 138 | /// This will typically only rebuild if something other than the source has |
| 139 | /// changed (e.g. the CDB yields different flags, or files included in the |
| 140 | /// preamble have been modified). |
| 141 | void reparseOpenFilesIfNeeded( |
| 142 | llvm::function_ref<bool(llvm::StringRef File)> Filter); |
Sam McCall | bc90461 | 2018-10-25 04:22:52 +0000 | [diff] [blame] | 143 | void applyConfiguration(const ConfigurationSettings &Settings); |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 144 | |
Johan Vikstrom | a848dab | 2019-07-04 07:53:12 +0000 | [diff] [blame] | 145 | /// Sends a "publishSemanticHighlighting" notification to the LSP client. |
Sam McCall | edf6a19 | 2020-03-24 00:31:14 +0100 | [diff] [blame] | 146 | void |
| 147 | publishTheiaSemanticHighlighting(const TheiaSemanticHighlightingParams &); |
Johan Vikstrom | a848dab | 2019-07-04 07:53:12 +0000 | [diff] [blame] | 148 | |
Ilya Biryukov | 49c1071 | 2019-03-25 10:15:11 +0000 | [diff] [blame] | 149 | /// Sends a "publishDiagnostics" notification to the LSP client. |
Sam McCall | 6525a6b | 2020-03-03 12:44:40 +0100 | [diff] [blame] | 150 | void publishDiagnostics(const PublishDiagnosticsParams &); |
Ilya Biryukov | 49c1071 | 2019-03-25 10:15:11 +0000 | [diff] [blame] | 151 | |
Kadir Cetinkaya | b181706 | 2019-10-15 14:59:08 +0000 | [diff] [blame] | 152 | /// Since initialization of CDBs and ClangdServer is done lazily, the |
| 153 | /// following context captures the one used while creating ClangdLSPServer and |
| 154 | /// passes it to above mentioned object instances to make sure they share the |
| 155 | /// same state. |
Kadir Cetinkaya | 9d66247 | 2019-10-15 14:20:52 +0000 | [diff] [blame] | 156 | Context BackgroundContext; |
| 157 | |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 158 | /// Used to indicate that the 'shutdown' request was received from the |
| 159 | /// Language Server client. |
Ilya Biryukov | 0d9b8a3 | 2017-10-25 08:45:41 +0000 | [diff] [blame] | 160 | bool ShutdownRequestReceived = false; |
| 161 | |
Haojian Wu | f251634 | 2019-08-05 12:48:09 +0000 | [diff] [blame] | 162 | /// Used to indicate the ClangdLSPServer is being destroyed. |
| 163 | std::atomic<bool> IsBeingDestroyed = {false}; |
| 164 | |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 165 | std::mutex FixItsMutex; |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 166 | typedef std::map<clangd::Diagnostic, std::vector<Fix>, LSPDiagnosticCompare> |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 167 | DiagnosticToReplacementMap; |
| 168 | /// Caches FixIts per file and diagnostics |
| 169 | llvm::StringMap<DiagnosticToReplacementMap> FixItsMap; |
Johan Vikstrom | c2653ef2 | 2019-08-01 08:08:44 +0000 | [diff] [blame] | 170 | std::mutex HighlightingsMutex; |
| 171 | llvm::StringMap<std::vector<HighlightingToken>> FileToHighlightings; |
Sam McCall | 9e3063e | 2020-04-01 16:21:44 +0200 | [diff] [blame] | 172 | // Last semantic-tokens response, for incremental requests. |
| 173 | std::mutex SemanticTokensMutex; |
| 174 | llvm::StringMap<SemanticTokens> LastSemanticTokens; |
Ilya Biryukov | 103c951 | 2017-06-13 15:59:43 +0000 | [diff] [blame] | 175 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 176 | // Most code should not deal with Transport directly. |
| 177 | // MessageHandler deals with incoming messages, use call() etc for outgoing. |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 178 | clangd::Transport &Transp; |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 179 | class MessageHandler; |
| 180 | std::unique_ptr<MessageHandler> MsgHandler; |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 181 | std::mutex TranspWriter; |
Haojian Wu | f251634 | 2019-08-05 12:48:09 +0000 | [diff] [blame] | 182 | |
Sam McCall | fa69b60 | 2020-09-24 01:14:12 +0200 | [diff] [blame] | 183 | template <typename T> |
| 184 | static Expected<T> parse(const llvm::json::Value &Raw, |
| 185 | llvm::StringRef PayloadName, |
| 186 | llvm::StringRef PayloadKind) { |
| 187 | T Result; |
| 188 | llvm::json::Path::Root Root; |
| 189 | if (!fromJSON(Raw, Result, Root)) { |
Sam McCall | 2bd5e3f | 2020-09-24 01:51:29 +0200 | [diff] [blame^] | 190 | elog("Failed to decode {0} {1}: {2}", PayloadName, PayloadKind, |
| 191 | Root.getError()); |
Sam McCall | fa69b60 | 2020-09-24 01:14:12 +0200 | [diff] [blame] | 192 | // Dump the relevant parts of the broken message. |
| 193 | std::string Context; |
| 194 | llvm::raw_string_ostream OS(Context); |
| 195 | Root.printErrorContext(Raw, OS); |
| 196 | vlog("{0}", OS.str()); |
| 197 | // Report the error (e.g. to the client). |
| 198 | return llvm::make_error<LSPError>( |
Sam McCall | 2bd5e3f | 2020-09-24 01:51:29 +0200 | [diff] [blame^] | 199 | llvm::formatv("failed to decode {0} {1}: {2}", PayloadName, |
| 200 | PayloadKind, fmt_consume(Root.getError())), |
Sam McCall | fa69b60 | 2020-09-24 01:14:12 +0200 | [diff] [blame] | 201 | ErrorCode::InvalidParams); |
| 202 | } |
| 203 | return std::move(Result); |
| 204 | } |
| 205 | |
Haojian Wu | f251634 | 2019-08-05 12:48:09 +0000 | [diff] [blame] | 206 | template <typename Response> |
| 207 | void call(StringRef Method, llvm::json::Value Params, Callback<Response> CB) { |
| 208 | // Wrap the callback with LSP conversion and error-handling. |
Benjamin Kramer | 9880b5d | 2019-08-15 14:16:06 +0000 | [diff] [blame] | 209 | auto HandleReply = |
Sam McCall | fa69b60 | 2020-09-24 01:14:12 +0200 | [diff] [blame] | 210 | [CB = std::move(CB), Ctx = Context::current().clone(), |
| 211 | Method = Method.str()]( |
Benjamin Kramer | 9880b5d | 2019-08-15 14:16:06 +0000 | [diff] [blame] | 212 | llvm::Expected<llvm::json::Value> RawResponse) mutable { |
Sam McCall | fa69b60 | 2020-09-24 01:14:12 +0200 | [diff] [blame] | 213 | if (!RawResponse) |
| 214 | return CB(RawResponse.takeError()); |
| 215 | CB(parse<Response>(*RawResponse, Method, "response")); |
Benjamin Kramer | 9880b5d | 2019-08-15 14:16:06 +0000 | [diff] [blame] | 216 | }; |
| 217 | callRaw(Method, std::move(Params), std::move(HandleReply)); |
Haojian Wu | f251634 | 2019-08-05 12:48:09 +0000 | [diff] [blame] | 218 | } |
| 219 | void callRaw(StringRef Method, llvm::json::Value Params, |
| 220 | Callback<llvm::json::Value> CB); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 221 | void notify(StringRef Method, llvm::json::Value Params); |
Sam McCall | 7d20e80 | 2020-01-22 19:41:45 +0100 | [diff] [blame] | 222 | template <typename T> void progress(const llvm::json::Value &Token, T Value) { |
| 223 | ProgressParams<T> Params; |
| 224 | Params.token = Token; |
| 225 | Params.value = std::move(Value); |
| 226 | notify("$/progress", Params); |
| 227 | } |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 228 | |
Kadir Cetinkaya | 8d654df | 2020-06-17 18:09:54 +0200 | [diff] [blame] | 229 | const ThreadsafeFS &TFS; |
Ilya Biryukov | d3b04e3 | 2017-12-05 10:42:57 +0000 | [diff] [blame] | 230 | /// Options used for code completion |
| 231 | clangd::CodeCompleteOptions CCOpts; |
Haojian Wu | 34d0e1b | 2020-02-19 15:37:36 +0100 | [diff] [blame] | 232 | /// Options used for rename. |
| 233 | clangd::RenameOptions RenameOpts; |
Alex Lorenz | 8626d36 | 2018-08-10 17:25:07 +0000 | [diff] [blame] | 234 | /// Options used for diagnostics. |
| 235 | ClangdDiagnosticOptions DiagOpts; |
Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 236 | /// The supported kinds of the client. |
| 237 | SymbolKindBitset SupportedSymbolKinds; |
Kadir Cetinkaya | 133d46f | 2018-09-27 17:13:07 +0000 | [diff] [blame] | 238 | /// The supported completion item kinds of the client. |
| 239 | CompletionItemKindBitset SupportedCompletionItemKinds; |
Haojian Wu | b618849 | 2018-12-20 15:39:12 +0000 | [diff] [blame] | 240 | /// Whether the client supports CodeAction response objects. |
Sam McCall | 20841d4 | 2018-10-16 16:29:41 +0000 | [diff] [blame] | 241 | bool SupportsCodeAction = false; |
Ilya Biryukov | 19d7560 | 2018-11-23 15:21:19 +0000 | [diff] [blame] | 242 | /// From capabilities of textDocument/documentSymbol. |
| 243 | bool SupportsHierarchicalDocumentSymbol = false; |
Haojian Wu | b618849 | 2018-12-20 15:39:12 +0000 | [diff] [blame] | 244 | /// Whether the client supports showing file status. |
| 245 | bool SupportFileStatus = false; |
Ilya Biryukov | f9169d0 | 2019-05-29 10:01:00 +0000 | [diff] [blame] | 246 | /// Which kind of markup should we use in textDocument/hover responses. |
| 247 | MarkupKind HoverContentFormat = MarkupKind::PlainText; |
Ilya Biryukov | 4ef0f82 | 2019-06-04 09:36:59 +0000 | [diff] [blame] | 248 | /// Whether the client supports offsets for parameter info labels. |
| 249 | bool SupportsOffsetsInSignatureHelp = false; |
Sam McCall | 7d20e80 | 2020-01-22 19:41:45 +0100 | [diff] [blame] | 250 | std::mutex BackgroundIndexProgressMutex; |
| 251 | enum class BackgroundIndexProgress { |
| 252 | // Client doesn't support reporting progress. No transitions possible. |
| 253 | Unsupported, |
| 254 | // The queue is idle, and the client has no progress bar. |
| 255 | // Can transition to Creating when we have some activity. |
| 256 | Empty, |
| 257 | // We've requested the client to create a progress bar. |
| 258 | // Meanwhile, the state is buffered in PendingBackgroundIndexProgress. |
| 259 | Creating, |
| 260 | // The client has a progress bar, and we can send it updates immediately. |
| 261 | Live, |
| 262 | } BackgroundIndexProgressState = BackgroundIndexProgress::Unsupported; |
| 263 | // The progress to send when the progress bar is created. |
| 264 | // Only valid in state Creating. |
| 265 | BackgroundQueue::Stats PendingBackgroundIndexProgress; |
| 266 | /// LSP extension: skip WorkDoneProgressCreate, just send progress streams. |
| 267 | bool BackgroundIndexSkipCreate = false; |
Ilya Biryukov | 4ef0f82 | 2019-06-04 09:36:59 +0000 | [diff] [blame] | 268 | // Store of the current versions of the open documents. |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 269 | DraftStore DraftMgr; |
| 270 | |
Sam McCall | d1c9d11 | 2018-10-23 14:19:54 +0000 | [diff] [blame] | 271 | // The CDB is created by the "initialize" LSP method. |
Sam McCall | c55d09a | 2018-11-02 13:09:36 +0000 | [diff] [blame] | 272 | bool UseDirBasedCDB; // FIXME: make this a capability. |
Sam McCall | 4b86bb0 | 2018-10-25 02:22:53 +0000 | [diff] [blame] | 273 | llvm::Optional<Path> CompileCommandsDir; // FIXME: merge with capability? |
Sam McCall | c55d09a | 2018-11-02 13:09:36 +0000 | [diff] [blame] | 274 | std::unique_ptr<GlobalCompilationDatabase> BaseCDB; |
Kazuaki Ishizaki | b7ecf1c | 2020-01-04 10:28:41 -0500 | [diff] [blame] | 275 | // CDB is BaseCDB plus any commands overridden via LSP extensions. |
Sam McCall | c55d09a | 2018-11-02 13:09:36 +0000 | [diff] [blame] | 276 | llvm::Optional<OverlayCDB> CDB; |
Sam McCall | 3d0adbe | 2018-10-18 14:41:50 +0000 | [diff] [blame] | 277 | ClangdServer::Options ClangdServerOpts; |
Sam McCall | a69698f | 2019-03-27 17:47:49 +0000 | [diff] [blame] | 278 | llvm::Optional<OffsetEncoding> NegotiatedOffsetEncoding; |
Sam McCall | 8bda5f2 | 2019-10-23 11:11:18 +0200 | [diff] [blame] | 279 | // The ClangdServer is created by the "initialize" LSP method. |
| 280 | llvm::Optional<ClangdServer> Server; |
Kadir Cetinkaya | 689bf93 | 2018-08-24 13:09:41 +0000 | [diff] [blame] | 281 | }; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 282 | } // namespace clangd |
| 283 | } // namespace clang |
| 284 | |
Kirill Bobyrev | 8e35f1e | 2018-08-14 16:03:32 +0000 | [diff] [blame] | 285 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDLSPSERVER_H |