clang-tools 10.0.0

ClangdLSPServer.h
Go to the documentation of this file.
1 //===--- ClangdLSPServer.h - LSP server --------------------------*- C++-*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDLSPSERVER_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDLSPSERVER_H
11
12 #include "ClangdServer.h"
13 #include "Context.h"
14 #include "DraftStore.h"
15 #include "Features.inc"
16 #include "FindSymbols.h"
17 #include "GlobalCompilationDatabase.h"
18 #include "Path.h"
19 #include "Protocol.h"
20 #include "Transport.h"
21 #include "clang/Tooling/Core/Replacement.h"
22 #include "llvm/ADT/Optional.h"
23 #include <memory>
24
25 namespace clang {
26 namespace clangd {
27
28 class SymbolIndex;
29
30 /// This class exposes ClangdServer's capabilities via Language Server Protocol.
31 ///
32 /// MessageHandler binds the implemented LSP methods (e.g. onInitialize) to
33 /// corresponding JSON-RPC methods ("initialize").
34 /// The server also supports $/cancelRequest (MessageHandler provides this).
35 class ClangdLSPServer : private DiagnosticsConsumer {
36 public:
37 /// If \p CompileCommandsDir has a value, compile_commands.json will be
38 /// loaded only from \p CompileCommandsDir. Otherwise, clangd will look
39 /// for compile_commands.json in all parent directories of each file.
40 /// If UseDirBasedCDB is false, compile commands are not read from disk.
41 // FIXME: Clean up signature around CDBs.
42 ClangdLSPServer(Transport &Transp, const FileSystemProvider &FSProvider,
43 const clangd::CodeCompleteOptions &CCOpts,
44 llvm::Optional<Path> CompileCommandsDir, bool UseDirBasedCDB,
45 llvm::Optional<OffsetEncoding> ForcedOffsetEncoding,
46 const ClangdServer::Options &Opts);
47 /// The destructor blocks on any outstanding background tasks.
48 ~ClangdLSPServer();
49
50 /// Run LSP server loop, communicating with the Transport provided in the
51 /// constructor. This method must not be executed more than once.
52 ///
53 /// \return Whether we shut down cleanly with a 'shutdown' -> 'exit' sequence.
54 bool run();
55
56 private:
57 // Implement DiagnosticsConsumer.
58 void onDiagnosticsReady(PathRef File, std::vector<Diag> Diagnostics) override;
59 void onFileUpdated(PathRef File, const TUStatus &Status) override;
60 void
61 onHighlightingsReady(PathRef File,
62 std::vector<HighlightingToken> Highlightings) override;
63
64 // LSP methods. Notifications have signature void(const Params&).
65 // Calls have signature void(const Params&, Callback<Response>).
66 void onInitialize(const InitializeParams &, Callback<llvm::json::Value>);
67 void onShutdown(const ShutdownParams &, Callback<std::nullptr_t>);
68 void onSync(const NoParams &, Callback<std::nullptr_t>);
69 void onDocumentDidOpen(const DidOpenTextDocumentParams &);
70 void onDocumentDidChange(const DidChangeTextDocumentParams &);
71 void onDocumentDidClose(const DidCloseTextDocumentParams &);
72 void onDocumentOnTypeFormatting(const DocumentOnTypeFormattingParams &,
73 Callback<std::vector<TextEdit>>);
74 void onDocumentRangeFormatting(const DocumentRangeFormattingParams &,
75 Callback<std::vector<TextEdit>>);
76 void onDocumentFormatting(const DocumentFormattingParams &,
77 Callback<std::vector<TextEdit>>);
78 // The results are serialized 'vector<DocumentSymbol>' if
79 // SupportsHierarchicalDocumentSymbol is true and 'vector<SymbolInformation>'
80 // otherwise.
81 void onDocumentSymbol(const DocumentSymbolParams &,
82 Callback<llvm::json::Value>);
83 void onCodeAction(const CodeActionParams &, Callback<llvm::json::Value>);
84 void onCompletion(const CompletionParams &, Callback<CompletionList>);
85 void onSignatureHelp(const TextDocumentPositionParams &,
86 Callback<SignatureHelp>);
87 void onGoToDeclaration(const TextDocumentPositionParams &,
88 Callback<std::vector<Location>>);
89 void onGoToDefinition(const TextDocumentPositionParams &,
90 Callback<std::vector<Location>>);
91 void onReference(const ReferenceParams &, Callback<std::vector<Location>>);
92 void onSwitchSourceHeader(const TextDocumentIdentifier &,
93 Callback<llvm::Optional<URIForFile>>);
94 void onDocumentHighlight(const TextDocumentPositionParams &,
95 Callback<std::vector<DocumentHighlight>>);
96 void onFileEvent(const DidChangeWatchedFilesParams &);
97 void onCommand(const ExecuteCommandParams &, Callback<llvm::json::Value>);
98 void onWorkspaceSymbol(const WorkspaceSymbolParams &,
99 Callback<std::vector<SymbolInformation>>);
100 void onPrepareRename(const TextDocumentPositionParams &,
101 Callback<llvm::Optional<Range>>);
102 void onRename(const RenameParams &, Callback<WorkspaceEdit>);
103 void onHover(const TextDocumentPositionParams &,
104 Callback<llvm::Optional<Hover>>);
105 void onTypeHierarchy(const TypeHierarchyParams &,
106 Callback<llvm::Optional<TypeHierarchyItem>>);
107 void onResolveTypeHierarchy(const ResolveTypeHierarchyItemParams &,
108 Callback<llvm::Optional<TypeHierarchyItem>>);
109 void onChangeConfiguration(const DidChangeConfigurationParams &);
110 void onSymbolInfo(const TextDocumentPositionParams &,
111 Callback<std::vector<SymbolDetails>>);
112 void onSelectionRange(const SelectionRangeParams &,
113 Callback<std::vector<SelectionRange>>);
114 void onDocumentLink(const DocumentLinkParams &,
115 Callback<std::vector<DocumentLink>>);
116
117 std::vector<Fix> getFixes(StringRef File, const clangd::Diagnostic &D);
118
119 /// Checks if completion request should be ignored. We need this due to the
120 /// limitation of the LSP. Per LSP, a client sends requests for all "trigger
121 /// character" we specify, but for '>' and ':' we need to check they actually
122 /// produce '->' and '::', respectively.
123 bool shouldRunCompletion(const CompletionParams &Params) const;
124
125 /// Forces a reparse of all currently opened files. As a result, this method
126 /// may be very expensive. This method is normally called when the
127 /// compilation database is changed.
128 void reparseOpenedFiles();
129 void applyConfiguration(const ConfigurationSettings &Settings);
130
131 /// Sends a "publishSemanticHighlighting" notification to the LSP client.
132 void publishSemanticHighlighting(SemanticHighlightingParams Params);
133
134 /// Sends a "publishDiagnostics" notification to the LSP client.
135 void publishDiagnostics(const URIForFile &File,
136 std::vector<clangd::Diagnostic> Diagnostics);
137
138 /// Since initialization of CDBs and ClangdServer is done lazily, the
139 /// following context captures the one used while creating ClangdLSPServer and
140 /// passes it to above mentioned object instances to make sure they share the
141 /// same state.
142 Context BackgroundContext;
143
144 /// Used to indicate that the 'shutdown' request was received from the
145 /// Language Server client.
146 bool ShutdownRequestReceived = false;
147
148 /// Used to indicate the ClangdLSPServer is being destroyed.
149 std::atomic<bool> IsBeingDestroyed = {false};
150
151 std::mutex FixItsMutex;
152 typedef std::map<clangd::Diagnostic, std::vector<Fix>, LSPDiagnosticCompare>
153 DiagnosticToReplacementMap;
154 /// Caches FixIts per file and diagnostics
155 llvm::StringMap<DiagnosticToReplacementMap> FixItsMap;
156 std::mutex HighlightingsMutex;
157 llvm::StringMap<std::vector<HighlightingToken>> FileToHighlightings;
158
159 // Most code should not deal with Transport directly.
160 // MessageHandler deals with incoming messages, use call() etc for outgoing.
161 clangd::Transport &Transp;
162 class MessageHandler;
163 std::unique_ptr<MessageHandler> MsgHandler;
164 std::mutex TranspWriter;
165
166 template <typename Response>
167 void call(StringRef Method, llvm::json::Value Params, Callback<Response> CB) {
168 // Wrap the callback with LSP conversion and error-handling.
169 auto HandleReply =
170 [CB = std::move(CB), Ctx = Context::current().clone()](
171 llvm::Expected<llvm::json::Value> RawResponse) mutable {
172 Response Rsp;
173 if (!RawResponse) {
174 CB(RawResponse.takeError());
175 } else if (fromJSON(*RawResponse, Rsp)) {
176 CB(std::move(Rsp));
177 } else {
178 elog("Failed to decode {0} response", *RawResponse);
179 CB(llvm::make_error<LSPError>("failed to decode reponse",
180 ErrorCode::InvalidParams));
181 }
182 };
183 callRaw(Method, std::move(Params), std::move(HandleReply));
184 }
185 void callRaw(StringRef Method, llvm::json::Value Params,
186 Callback<llvm::json::Value> CB);
187 void notify(StringRef Method, llvm::json::Value Params);
188
189 const FileSystemProvider &FSProvider;
190 /// Options used for code completion
191 clangd::CodeCompleteOptions CCOpts;
192 /// Options used for diagnostics.
193 ClangdDiagnosticOptions DiagOpts;
194 /// The supported kinds of the client.
195 SymbolKindBitset SupportedSymbolKinds;
196 /// The supported completion item kinds of the client.
197 CompletionItemKindBitset SupportedCompletionItemKinds;
198 /// Whether the client supports CodeAction response objects.
199 bool SupportsCodeAction = false;
200 /// From capabilities of textDocument/documentSymbol.
201 bool SupportsHierarchicalDocumentSymbol = false;
202 /// Whether the client supports showing file status.
203 bool SupportFileStatus = false;
204 /// Which kind of markup should we use in textDocument/hover responses.
205 MarkupKind HoverContentFormat = MarkupKind::PlainText;
206 /// Whether the client supports offsets for parameter info labels.
207 bool SupportsOffsetsInSignatureHelp = false;
208 // Store of the current versions of the open documents.
209 DraftStore DraftMgr;
210
211 // The CDB is created by the "initialize" LSP method.
212 bool UseDirBasedCDB; // FIXME: make this a capability.
213 llvm::Optional<Path> CompileCommandsDir; // FIXME: merge with capability?
214 std::unique_ptr<GlobalCompilationDatabase> BaseCDB;
215 // CDB is BaseCDB plus any commands overridden via LSP extensions.
216 llvm::Optional<OverlayCDB> CDB;
217 ClangdServer::Options ClangdServerOpts;
218 llvm::Optional<OffsetEncoding> NegotiatedOffsetEncoding;
219 // The ClangdServer is created by the "initialize" LSP method.
220 llvm::Optional<ClangdServer> Server;
221 };
222 } // namespace clangd
223 } // namespace clang
224
225 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDLSPSERVER_H
clang::clangd::DocumentRangeFormattingParams
Definition: Protocol.h:602
clang::clangd::ExecuteCommandParams
Exact commands are not specified in the protocol so we define the ones supported by Clangd here...
Definition: Protocol.h:746
clang::clangd::DocumentSymbolParams
Definition: Protocol.h:629
clang::clangd::MarkupKind
MarkupKind
Definition: Protocol.h:359
FindSymbols.h
ClangdServer.h
clang::clangd::DidChangeWatchedFilesParams
Definition: Protocol.h:583
clang::clangd::Transport
Definition: Transport.h:34
clang::clangd::DidChangeTextDocumentParams
Definition: Protocol.h:548
Protocol.h
clang::clangd::ClangdLSPServer::run
bool run()
Run LSP server loop, communicating with the Transport provided in the constructor.
Definition: ClangdLSPServer.cpp:1279
clang::clangd::InitializeParams
Definition: Protocol.h:470
clang::clangd::PathRef
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:23
clang::clangd::Callback
llvm::unique_function< void(llvm::Expected< T >)> Callback
A Callback<T> is a void function that accepts Expected<T>.
Definition: Function.h:28
clang::clangd::DocumentFormattingParams
Definition: Protocol.h:623
clang::clangd::CompletionItemKindBitset
std::bitset< CompletionItemKindMax+1 > CompletionItemKindBitset
Definition: Protocol.h:293
Context.h
clang::clangd::MarkupKind::PlainText
clang::clangd::RenameParams
Definition: Protocol.h:1081
clang::clangd::FileSystemProvider
Definition: FSProvider.h:20
clang::clangd::elog
void elog(const char *Fmt, Ts &&... Vals)
Definition: Logger.h:56
GlobalCompilationDatabase.h
FSProvider
MockFSProvider FSProvider
Definition: FindSymbolsTests.cpp:67
clang::clangd::NoParams
Definition: Protocol.h:240
clang::clangd::SelectionRangeParams
Definition: Protocol.h:1238
Transport.h
Ctx
Context Ctx
Definition: TUScheduler.cpp:237
clang::clangd::SemanticHighlightingParams
Parameters for the semantic highlighting (server-side) push notification.
Definition: Protocol.h:1230
clang::clangd::ConfigurationSettings
Clangd extension: parameters configurable at any time, via the workspace/didChangeConfiguration notif...
Definition: Protocol.h:445
clang::clangd::Context::clone
Context clone() const
Clone this context object.
Definition: Context.cpp:20
clang::clangd::ClangdLSPServer::MessageHandler
Definition: ClangdLSPServer.cpp:159
clang::clangd::Context::current
static const Context & current()
Returns the context for the current thread, creating it if needed.
Definition: Context.cpp:27
clang::clangd::ClangdDiagnosticOptions
Definition: Diagnostics.h:31
clang::clangd::ReferenceParams
Definition: Protocol.h:1195
clang::clangd::CodeCompleteOptions
Definition: CodeComplete.h:39
clang::clangd::TextDocumentIdentifier
Definition: Protocol.h:120
clang::clangd::URIForFile
Definition: Protocol.h:73
clang::clangd::TUStatus
Definition: TUScheduler.h:81
clang::clangd::CompletionItemKind::Method
clang::clangd::DocumentOnTypeFormattingParams
Definition: Protocol.h:611
clang::clangd::ErrorCode::InvalidParams
clang::clangd::TextDocumentPositionParams
Definition: Protocol.h:883
clang::clangd::Context
A context is an immutable container for per-request data that must be propagated through layers that ...
Definition: Context.h:69
clang::clangd::CompletionParams
Definition: Protocol.h:912
clang::clangd::ClangdLSPServer::~ClangdLSPServer
~ClangdLSPServer()
The destructor blocks on any outstanding background tasks.
Definition: ClangdLSPServer.cpp:1273
clang::clangd::fromJSON
bool fromJSON(const llvm::json::Value &Parameters, FuzzyFindRequest &Request)
Definition: Index.cpp:34
DraftStore.h
clang::clangd::LSPDiagnosticCompare
A LSP-specific comparator used to find diagnostic in a container like std:map.
Definition: Protocol.h:687
clang::clangd::DiagnosticsConsumer
Definition: ClangdServer.h:45
clang::clangd::DidChangeConfigurationParams
Definition: Protocol.h:589
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::SymbolKindBitset
std::bitset< SymbolKindMax+1 > SymbolKindBitset
Definition: Protocol.h:331
Server
ClangdServer Server
Definition: FindSymbolsTests.cpp:70
clang::clangd::TypeHierarchyParams
The type hierarchy params is an extension of the TextDocumentPositionsParams with optional properties...
Definition: Protocol.h:1127
Path.h
clang::clangd::DidOpenTextDocumentParams
Definition: Protocol.h:524
clang::clangd::DraftStore
A thread-safe container for files opened in a workspace, addressed by filenames.
Definition: DraftStore.h:26
clang::clangd::WorkspaceSymbolParams
The parameters of a Workspace Symbol Request.
Definition: Protocol.h:866
clang::clangd::ResolveTypeHierarchyItemParams
Parameters for the typeHierarchy/resolve request.
Definition: Protocol.h:1183
clang::clangd::DidCloseTextDocumentParams
Definition: Protocol.h:530
clang::clangd::Diagnostic
Definition: Protocol.h:647
clang::clangd::DocumentLinkParams
Parameters for the document link request.
Definition: Protocol.h:1261
clang::clangd::ClangdServer::Options
Definition: ClangdServer.h:82
clang::clangd::ClangdLSPServer
This class exposes ClangdServer&#39;s capabilities via Language Server Protocol.
Definition: ClangdLSPServer.h:35
clang::clangd::CompletionItemKind::File
clang::clangd::CodeActionParams
Definition: Protocol.h:701
clang::clangd::ClangdLSPServer::ClangdLSPServer
ClangdLSPServer(Transport &Transp, const FileSystemProvider &FSProvider, const clangd::CodeCompleteOptions &CCOpts, llvm::Optional< Path > CompileCommandsDir, bool UseDirBasedCDB, llvm::Optional< OffsetEncoding > ForcedOffsetEncoding, const ClangdServer::Options &Opts)
If CompileCommandsDir has a value, compile_commands.json will be loaded only from CompileCommandsDir...
Definition: ClangdLSPServer.cpp:1226