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