clang-tools 8.0.0

ClangdLSPServer.cpp
Go to the documentation of this file.
1 //===--- ClangdLSPServer.cpp - 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 //
8 //===----------------------------------------------------------------------===//
9
10 #include "ClangdLSPServer.h"
11 #include "Diagnostics.h"
12 #include "SourceCode.h"
13 #include "Trace.h"
14 #include "URI.h"
15 #include "llvm/ADT/ScopeExit.h"
16 #include "llvm/Support/Errc.h"
17 #include "llvm/Support/FormatVariadic.h"
18 #include "llvm/Support/Path.h"
19 #include "llvm/Support/ScopedPrinter.h"
20
21 namespace clang {
22 namespace clangd {
23 namespace {
24 class IgnoreCompletionError : public llvm::ErrorInfo<CancelledError> {
25 public:
26 void log(llvm::raw_ostream &OS) const override {
27 OS << "ignored auto-triggered completion, preceding char did not match";
28 }
29 std::error_code convertToErrorCode() const override {
30 return std::make_error_code(std::errc::operation_canceled);
31 }
32 };
33
34 void adjustSymbolKinds(llvm::MutableArrayRef<DocumentSymbol> Syms,
35 SymbolKindBitset Kinds) {
36 for (auto &S : Syms) {
37 S.kind = adjustKindToCapability(S.kind, Kinds);
38 adjustSymbolKinds(S.children, Kinds);
39 }
40 }
41
42 SymbolKindBitset defaultSymbolKinds() {
43 SymbolKindBitset Defaults;
44 for (size_t I = SymbolKindMin; I <= static_cast<size_t>(SymbolKind::Array);
45 ++I)
46 Defaults.set(I);
47 return Defaults;
48 }
49
50 CompletionItemKindBitset defaultCompletionItemKinds() {
51 CompletionItemKindBitset Defaults;
52 for (size_t I = CompletionItemKindMin;
53 I <= static_cast<size_t>(CompletionItemKind::Reference); ++I)
54 Defaults.set(I);
55 return Defaults;
56 }
57
58 } // namespace
59
60 // MessageHandler dispatches incoming LSP messages.
61 // It handles cross-cutting concerns:
62 // - serializes/deserializes protocol objects to JSON
63 // - logging of inbound messages
64 // - cancellation handling
65 // - basic call tracing
66 // MessageHandler ensures that initialize() is called before any other handler.
67 class ClangdLSPServer::MessageHandler : public Transport::MessageHandler {
68 public:
69 MessageHandler(ClangdLSPServer &Server) : Server(Server) {}
70
71 bool onNotify(llvm::StringRef Method, llvm::json::Value Params) override {
72 log("<-- {0}", Method);
73 if (Method == "exit")
74 return false;
75 if (!Server.Server)
76 elog("Notification {0} before initialization", Method);
77 else if (Method == "$/cancelRequest")
78 onCancel(std::move(Params));
79 else if (auto Handler = Notifications.lookup(Method))
80 Handler(std::move(Params));
81 else
82 log("unhandled notification {0}", Method);
83 return true;
84 }
85
86 bool onCall(llvm::StringRef Method, llvm::json::Value Params,
87 llvm::json::Value ID) override {
88 // Calls can be canceled by the client. Add cancellation context.
89 WithContext WithCancel(cancelableRequestContext(ID));
90 trace::Span Tracer(Method);
91 SPAN_ATTACH(Tracer, "Params", Params);
92 ReplyOnce Reply(ID, Method, &Server, Tracer.Args);
93 log("<-- {0}({1})", Method, ID);
94 if (!Server.Server && Method != "initialize") {
95 elog("Call {0} before initialization.", Method);
96 Reply(llvm::make_error<LSPError>("server not initialized",
97 ErrorCode::ServerNotInitialized));
98 } else if (auto Handler = Calls.lookup(Method))
99 Handler(std::move(Params), std::move(Reply));
100 else
101 Reply(llvm::make_error<LSPError>("method not found",
102 ErrorCode::MethodNotFound));
103 return true;
104 }
105
106 bool onReply(llvm::json::Value ID,
107 llvm::Expected<llvm::json::Value> Result) override {
108 // We ignore replies, just log them.
109 if (Result)
110 log("<-- reply({0})", ID);
111 else
112 log("<-- reply({0}) error: {1}", ID, llvm::toString(Result.takeError()));
113 return true;
114 }
115
116 // Bind an LSP method name to a call.
117 template <typename Param, typename Result>
118 void bind(const char *Method,
119 void (ClangdLSPServer::*Handler)(const Param &, Callback<Result>)) {
120 Calls[Method] = [Method, Handler, this](llvm::json::Value RawParams,
121 ReplyOnce Reply) {
122 Param P;
123 if (fromJSON(RawParams, P)) {
124 (Server.*Handler)(P, std::move(Reply));
125 } else {
126 elog("Failed to decode {0} request.", Method);
127 Reply(llvm::make_error<LSPError>("failed to decode request",
128 ErrorCode::InvalidRequest));
129 }
130 };
131 }
132
133 // Bind an LSP method name to a notification.
134 template <typename Param>
135 void bind(const char *Method,
136 void (ClangdLSPServer::*Handler)(const Param &)) {
137 Notifications[Method] = [Method, Handler,
138 this](llvm::json::Value RawParams) {
139 Param P;
140 if (!fromJSON(RawParams, P)) {
141 elog("Failed to decode {0} request.", Method);
142 return;
143 }
144 trace::Span Tracer(Method);
145 SPAN_ATTACH(Tracer, "Params", RawParams);
146 (Server.*Handler)(P);
147 };
148 }
149
150 private:
151 // Function object to reply to an LSP call.
152 // Each instance must be called exactly once, otherwise:
153 // - the bug is logged, and (in debug mode) an assert will fire
154 // - if there was no reply, an error reply is sent
155 // - if there were multiple replies, only the first is sent
156 class ReplyOnce {
157 std::atomic<bool> Replied = {false};
158 std::chrono::steady_clock::time_point Start;
159 llvm::json::Value ID;
160 std::string Method;
161 ClangdLSPServer *Server; // Null when moved-from.
162 llvm::json::Object *TraceArgs;
163
164 public:
165 ReplyOnce(const llvm::json::Value &ID, llvm::StringRef Method,
166 ClangdLSPServer *Server, llvm::json::Object *TraceArgs)
167 : Start(std::chrono::steady_clock::now()), ID(ID), Method(Method),
168 Server(Server), TraceArgs(TraceArgs) {
169 assert(Server);
170 }
171 ReplyOnce(ReplyOnce &&Other)
172 : Replied(Other.Replied.load()), Start(Other.Start),
173 ID(std::move(Other.ID)), Method(std::move(Other.Method)),
174 Server(Other.Server), TraceArgs(Other.TraceArgs) {
175 Other.Server = nullptr;
176 }
177 ReplyOnce &operator=(ReplyOnce &&) = delete;
178 ReplyOnce(const ReplyOnce &) = delete;
179 ReplyOnce &operator=(const ReplyOnce &) = delete;
180
181 ~ReplyOnce() {
182 if (Server && !Replied) {
183 elog("No reply to message {0}({1})", Method, ID);
184 assert(false && "must reply to all calls!");
185 (*this)(llvm::make_error<LSPError>("server failed to reply",
186 ErrorCode::InternalError));
187 }
188 }
189
190 void operator()(llvm::Expected<llvm::json::Value> Reply) {
191 assert(Server && "moved-from!");
192 if (Replied.exchange(true)) {
193 elog("Replied twice to message {0}({1})", Method, ID);
194 assert(false && "must reply to each call only once!");
195 return;
196 }
197 auto Duration = std::chrono::steady_clock::now() - Start;
198 if (Reply) {
199 log("--> reply:{0}({1}) {2:ms}", Method, ID, Duration);
200 if (TraceArgs)
201 (*TraceArgs)["Reply"] = *Reply;
202 std::lock_guard<std::mutex> Lock(Server->TranspWriter);
203 Server->Transp.reply(std::move(ID), std::move(Reply));
204 } else {
205 llvm::Error Err = Reply.takeError();
206 log("--> reply:{0}({1}) {2:ms}, error: {3}", Method, ID, Duration, Err);
207 if (TraceArgs)
208 (*TraceArgs)["Error"] = llvm::to_string(Err);
209 std::lock_guard<std::mutex> Lock(Server->TranspWriter);
210 Server->Transp.reply(std::move(ID), std::move(Err));
211 }
212 }
213 };
214
215 llvm::StringMap<std::function<void(llvm::json::Value)>> Notifications;
216 llvm::StringMap<std::function<void(llvm::json::Value, ReplyOnce)>> Calls;
217
218 // Method calls may be cancelled by ID, so keep track of their state.
219 // This needs a mutex: handlers may finish on a different thread, and that's
220 // when we clean up entries in the map.
221 mutable std::mutex RequestCancelersMutex;
222 llvm::StringMap<std::pair<Canceler, /*Cookie*/ unsigned>> RequestCancelers;
223 unsigned NextRequestCookie = 0; // To disambiguate reused IDs, see below.
224 void onCancel(const llvm::json::Value &Params) {
225 const llvm::json::Value *ID = nullptr;
226 if (auto *O = Params.getAsObject())
227 ID = O->get("id");
228 if (!ID) {
229 elog("Bad cancellation request: {0}", Params);
230 return;
231 }
232 auto StrID = llvm::to_string(*ID);
233 std::lock_guard<std::mutex> Lock(RequestCancelersMutex);
234 auto It = RequestCancelers.find(StrID);
235 if (It != RequestCancelers.end())
236 It->second.first(); // Invoke the canceler.
237 }
238 // We run cancelable requests in a context that does two things:
239 // - allows cancellation using RequestCancelers[ID]
240 // - cleans up the entry in RequestCancelers when it's no longer needed
241 // If a client reuses an ID, the last wins and the first cannot be canceled.
242 Context cancelableRequestContext(const llvm::json::Value &ID) {
243 auto Task = cancelableTask();
244 auto StrID = llvm::to_string(ID); // JSON-serialize ID for map key.
245 auto Cookie = NextRequestCookie++; // No lock, only called on main thread.
246 {
247 std::lock_guard<std::mutex> Lock(RequestCancelersMutex);
248 RequestCancelers[StrID] = {std::move(Task.second), Cookie};
249 }
250 // When the request ends, we can clean up the entry we just added.
251 // The cookie lets us check that it hasn't been overwritten due to ID
252 // reuse.
253 return Task.first.derive(llvm::make_scope_exit([this, StrID, Cookie] {
254 std::lock_guard<std::mutex> Lock(RequestCancelersMutex);
255 auto It = RequestCancelers.find(StrID);
256 if (It != RequestCancelers.end() && It->second.second == Cookie)
257 RequestCancelers.erase(It);
258 }));
259 }
260
261 ClangdLSPServer &Server;
262 };
263
264 // call(), notify(), and reply() wrap the Transport, adding logging and locking.
265 void ClangdLSPServer::call(llvm::StringRef Method, llvm::json::Value Params) {
266 auto ID = NextCallID++;
267 log("--> {0}({1})", Method, ID);
268 // We currently don't handle responses, so no need to store ID anywhere.
269 std::lock_guard<std::mutex> Lock(TranspWriter);
270 Transp.call(Method, std::move(Params), ID);
271 }
272
273 void ClangdLSPServer::notify(llvm::StringRef Method, llvm::json::Value Params) {
274 log("--> {0}", Method);
275 std::lock_guard<std::mutex> Lock(TranspWriter);
276 Transp.notify(Method, std::move(Params));
277 }
278
279 void ClangdLSPServer::onInitialize(const InitializeParams &Params,
280 Callback<llvm::json::Value> Reply) {
281 if (Params.rootUri && *Params.rootUri)
282 ClangdServerOpts.WorkspaceRoot = Params.rootUri->file();
283 else if (Params.rootPath && !Params.rootPath->empty())
284 ClangdServerOpts.WorkspaceRoot = *Params.rootPath;
285 if (Server)
286 return Reply(llvm::make_error<LSPError>("server already initialized",
287 ErrorCode::InvalidRequest));
288 if (const auto &Dir = Params.initializationOptions.compilationDatabasePath)
289 CompileCommandsDir = Dir;
290 if (UseDirBasedCDB)
291 BaseCDB = llvm::make_unique<DirectoryBasedGlobalCompilationDatabase>(
292 CompileCommandsDir);
293 CDB.emplace(BaseCDB.get(), Params.initializationOptions.fallbackFlags,
294 ClangdServerOpts.ResourceDir);
295 Server.emplace(*CDB, FSProvider, static_cast<DiagnosticsConsumer &>(*this),
296 ClangdServerOpts);
297 applyConfiguration(Params.initializationOptions.ConfigSettings);
298
299 CCOpts.EnableSnippets = Params.capabilities.CompletionSnippets;
300 DiagOpts.EmbedFixesInDiagnostics = Params.capabilities.DiagnosticFixes;
301 DiagOpts.SendDiagnosticCategory = Params.capabilities.DiagnosticCategory;
302 if (Params.capabilities.WorkspaceSymbolKinds)
303 SupportedSymbolKinds |= *Params.capabilities.WorkspaceSymbolKinds;
304 if (Params.capabilities.CompletionItemKinds)
305 SupportedCompletionItemKinds |= *Params.capabilities.CompletionItemKinds;
306 SupportsCodeAction = Params.capabilities.CodeActionStructure;
307 SupportsHierarchicalDocumentSymbol =
308 Params.capabilities.HierarchicalDocumentSymbol;
309 SupportFileStatus = Params.initializationOptions.FileStatus;
310 Reply(llvm::json::Object{
311 {{"capabilities",
312 llvm::json::Object{
313 {"textDocumentSync", (int)TextDocumentSyncKind::Incremental},
314 {"documentFormattingProvider", true},
315 {"documentRangeFormattingProvider", true},
316 {"documentOnTypeFormattingProvider",
317 llvm::json::Object{
318 {"firstTriggerCharacter", "}"},
319 {"moreTriggerCharacter", {}},
320 }},
321 {"codeActionProvider", true},
322 {"completionProvider",
323 llvm::json::Object{
324 {"resolveProvider", false},
325 // We do extra checks for '>' and ':' in completion to only
326 // trigger on '->' and '::'.
327 {"triggerCharacters", {".", ">", ":"}},
328 }},
329 {"signatureHelpProvider",
330 llvm::json::Object{
331 {"triggerCharacters", {"(", ","}},
332 }},
333 {"definitionProvider", true},
334 {"documentHighlightProvider", true},
335 {"hoverProvider", true},
336 {"renameProvider", true},
337 {"documentSymbolProvider", true},
338 {"workspaceSymbolProvider", true},
339 {"referencesProvider", true},
340 {"executeCommandProvider",
341 llvm::json::Object{
342 {"commands", {ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND}},
343 }},
344 }}}});
345 }
346
347 void ClangdLSPServer::onShutdown(const ShutdownParams &Params,
348 Callback<std::nullptr_t> Reply) {
349 // Do essentially nothing, just say we're ready to exit.
350 ShutdownRequestReceived = true;
351 Reply(nullptr);
352 }
353
354 // sync is a clangd extension: it blocks until all background work completes.
355 // It blocks the calling thread, so no messages are processed until it returns!
356 void ClangdLSPServer::onSync(const NoParams &Params,
357 Callback<std::nullptr_t> Reply) {
358 if (Server->blockUntilIdleForTest(/*TimeoutSeconds=*/60))
359 Reply(nullptr);
360 else
361 Reply(llvm::createStringError(llvm::inconvertibleErrorCode(),
362 "Not idle after a minute"));
363 }
364
365 void ClangdLSPServer::onDocumentDidOpen(
366 const DidOpenTextDocumentParams &Params) {
367 PathRef File = Params.textDocument.uri.file();
368
369 const std::string &Contents = Params.textDocument.text;
370
371 DraftMgr.addDraft(File, Contents);
372 Server->addDocument(File, Contents, WantDiagnostics::Yes);
373 }
374
375 void ClangdLSPServer::onDocumentDidChange(
376 const DidChangeTextDocumentParams &Params) {
377 auto WantDiags = WantDiagnostics::Auto;
378 if (Params.wantDiagnostics.hasValue())
379 WantDiags = Params.wantDiagnostics.getValue() ? WantDiagnostics::Yes
380 : WantDiagnostics::No;
381
382 PathRef File = Params.textDocument.uri.file();
383 llvm::Expected<std::string> Contents =
384 DraftMgr.updateDraft(File, Params.contentChanges);
385 if (!Contents) {
386 // If this fails, we are most likely going to be not in sync anymore with
387 // the client. It is better to remove the draft and let further operations
388 // fail rather than giving wrong results.
389 DraftMgr.removeDraft(File);
390 Server->removeDocument(File);
391 elog("Failed to update {0}: {1}", File, Contents.takeError());
392 return;
393 }
394
395 Server->addDocument(File, *Contents, WantDiags);
396 }
397
398 void ClangdLSPServer::onFileEvent(const DidChangeWatchedFilesParams &Params) {
399 Server->onFileEvent(Params);
400 }
401
402 void ClangdLSPServer::onCommand(const ExecuteCommandParams &Params,
403 Callback<llvm::json::Value> Reply) {
404 auto ApplyEdit = [&](WorkspaceEdit WE) {
405 ApplyWorkspaceEditParams Edit;
406 Edit.edit = std::move(WE);
407 // Ideally, we would wait for the response and if there is no error, we
408 // would reply success/failure to the original RPC.
409 call("workspace/applyEdit", Edit);
410 };
411 if (Params.command == ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND &&
412 Params.workspaceEdit) {
413 // The flow for "apply-fix" :
414 // 1. We publish a diagnostic, including fixits
415 // 2. The user clicks on the diagnostic, the editor asks us for code actions
416 // 3. We send code actions, with the fixit embedded as context
417 // 4. The user selects the fixit, the editor asks us to apply it
418 // 5. We unwrap the changes and send them back to the editor
419 // 6. The editor applies the changes (applyEdit), and sends us a reply (but
420 // we ignore it)
421
422 Reply("Fix applied.");
423 ApplyEdit(*Params.workspaceEdit);
424 } else {
425 // We should not get here because ExecuteCommandParams would not have
426 // parsed in the first place and this handler should not be called. But if
427 // more commands are added, this will be here has a safe guard.
428 Reply(llvm::make_error<LSPError>(
429 llvm::formatv("Unsupported command \"{0}\".", Params.command).str(),
430 ErrorCode::InvalidParams));
431 }
432 }
433
434 void ClangdLSPServer::onWorkspaceSymbol(
435 const WorkspaceSymbolParams &Params,
436 Callback<std::vector<SymbolInformation>> Reply) {
437 Server->workspaceSymbols(
438 Params.query, CCOpts.Limit,
439 Bind(
440 [this](decltype(Reply) Reply,
441 llvm::Expected<std::vector<SymbolInformation>> Items) {
442 if (!Items)
443 return Reply(Items.takeError());
444 for (auto &Sym : *Items)
445 Sym.kind = adjustKindToCapability(Sym.kind, SupportedSymbolKinds);
446
447 Reply(std::move(*Items));
448 },
449 std::move(Reply)));
450 }
451
452 void ClangdLSPServer::onRename(const RenameParams &Params,
453 Callback<WorkspaceEdit> Reply) {
454 Path File = Params.textDocument.uri.file();
455 llvm::Optional<std::string> Code = DraftMgr.getDraft(File);
456 if (!Code)
457 return Reply(llvm::make_error<LSPError>(
458 "onRename called for non-added file", ErrorCode::InvalidParams));
459
460 Server->rename(
461 File, Params.position, Params.newName,
462 Bind(
463 [File, Code, Params](
464 decltype(Reply) Reply,
465 llvm::Expected<std::vector<tooling::Replacement>> Replacements) {
466 if (!Replacements)
467 return Reply(Replacements.takeError());
468
469 // Turn the replacements into the format specified by the Language
470 // Server Protocol. Fuse them into one big JSON array.
471 std::vector<TextEdit> Edits;
472 for (const auto &R : *Replacements)
473 Edits.push_back(replacementToEdit(*Code, R));
474 WorkspaceEdit WE;
475 WE.changes = {{Params.textDocument.uri.uri(), Edits}};
476 Reply(WE);
477 },
478 std::move(Reply)));
479 }
480
481 void ClangdLSPServer::onDocumentDidClose(
482 const DidCloseTextDocumentParams &Params) {
483 PathRef File = Params.textDocument.uri.file();
484 DraftMgr.removeDraft(File);
485 Server->removeDocument(File);
486 }
487
488 void ClangdLSPServer::onDocumentOnTypeFormatting(
489 const DocumentOnTypeFormattingParams &Params,
490 Callback<std::vector<TextEdit>> Reply) {
491 auto File = Params.textDocument.uri.file();
492 auto Code = DraftMgr.getDraft(File);
493 if (!Code)
494 return Reply(llvm::make_error<LSPError>(
495 "onDocumentOnTypeFormatting called for non-added file",
496 ErrorCode::InvalidParams));
497
498 auto ReplacementsOrError = Server->formatOnType(*Code, File, Params.position);
499 if (ReplacementsOrError)
500 Reply(replacementsToEdits(*Code, ReplacementsOrError.get()));
501 else
502 Reply(ReplacementsOrError.takeError());
503 }
504
505 void ClangdLSPServer::onDocumentRangeFormatting(
506 const DocumentRangeFormattingParams &Params,
507 Callback<std::vector<TextEdit>> Reply) {
508 auto File = Params.textDocument.uri.file();
509 auto Code = DraftMgr.getDraft(File);
510 if (!Code)
511 return Reply(llvm::make_error<LSPError>(
512 "onDocumentRangeFormatting called for non-added file",
513 ErrorCode::InvalidParams));
514
515 auto ReplacementsOrError = Server->formatRange(*Code, File, Params.range);
516 if (ReplacementsOrError)
517 Reply(replacementsToEdits(*Code, ReplacementsOrError.get()));
518 else
519 Reply(ReplacementsOrError.takeError());
520 }
521
522 void ClangdLSPServer::onDocumentFormatting(
523 const DocumentFormattingParams &Params,
524 Callback<std::vector<TextEdit>> Reply) {
525 auto File = Params.textDocument.uri.file();
526 auto Code = DraftMgr.getDraft(File);
527 if (!Code)
528 return Reply(llvm::make_error<LSPError>(
529 "onDocumentFormatting called for non-added file",
530 ErrorCode::InvalidParams));
531
532 auto ReplacementsOrError = Server->formatFile(*Code, File);
533 if (ReplacementsOrError)
534 Reply(replacementsToEdits(*Code, ReplacementsOrError.get()));
535 else
536 Reply(ReplacementsOrError.takeError());
537 }
538
539 /// The functions constructs a flattened view of the DocumentSymbol hierarchy.
540 /// Used by the clients that do not support the hierarchical view.
541 static std::vector<SymbolInformation>
542 flattenSymbolHierarchy(llvm::ArrayRef<DocumentSymbol> Symbols,
543 const URIForFile &FileURI) {
544
545 std::vector<SymbolInformation> Results;
546 std::function<void(const DocumentSymbol &, llvm::StringRef)> Process =
547 [&](const DocumentSymbol &S, llvm::Optional<llvm::StringRef> ParentName) {
548 SymbolInformation SI;
549 SI.containerName = ParentName ? "" : *ParentName;
550 SI.name = S.name;
551 SI.kind = S.kind;
552 SI.location.range = S.range;
553 SI.location.uri = FileURI;
554
555 Results.push_back(std::move(SI));
556 std::string FullName =
557 !ParentName ? S.name : (ParentName->str() + "::" + S.name);
558 for (auto &C : S.children)
559 Process(C, /*ParentName=*/FullName);
560 };
561 for (auto &S : Symbols)
562 Process(S, /*ParentName=*/"");
563 return Results;
564 }
565
566 void ClangdLSPServer::onDocumentSymbol(const DocumentSymbolParams &Params,
567 Callback<llvm::json::Value> Reply) {
568 URIForFile FileURI = Params.textDocument.uri;
569 Server->documentSymbols(
570 Params.textDocument.uri.file(),
571 Bind(
572 [this, FileURI](decltype(Reply) Reply,
573 llvm::Expected<std::vector<DocumentSymbol>> Items) {
574 if (!Items)
575 return Reply(Items.takeError());
576 adjustSymbolKinds(*Items, SupportedSymbolKinds);
577 if (SupportsHierarchicalDocumentSymbol)
578 return Reply(std::move(*Items));
579 else
580 return Reply(flattenSymbolHierarchy(*Items, FileURI));
581 },
582 std::move(Reply)));
583 }
584
585 static llvm::Optional<Command> asCommand(const CodeAction &Action) {
586 Command Cmd;
587 if (Action.command && Action.edit)
588 return None; // Not representable. (We never emit these anyway).
589 if (Action.command) {
590 Cmd = *Action.command;
591 } else if (Action.edit) {
592 Cmd.command = Command::CLANGD_APPLY_FIX_COMMAND;
593 Cmd.workspaceEdit = *Action.edit;
594 } else {
595 return None;
596 }
597 Cmd.title = Action.title;
598 if (Action.kind && *Action.kind == CodeAction::QUICKFIX_KIND)
599 Cmd.title = "Apply fix: " + Cmd.title;
600 return Cmd;
601 }
602
603 void ClangdLSPServer::onCodeAction(const CodeActionParams &Params,
604 Callback<llvm::json::Value> Reply) {
605 auto Code = DraftMgr.getDraft(Params.textDocument.uri.file());
606 if (!Code)
607 return Reply(llvm::make_error<LSPError>(
608 "onCodeAction called for non-added file", ErrorCode::InvalidParams));
609 // We provide a code action for Fixes on the specified diagnostics.
610 std::vector<CodeAction> Actions;
611 for (const Diagnostic &D : Params.context.diagnostics) {
612 for (auto &F : getFixes(Params.textDocument.uri.file(), D)) {
613 Actions.push_back(toCodeAction(F, Params.textDocument.uri));
614 Actions.back().diagnostics = {D};
615 }
616 }
617
618 if (SupportsCodeAction)
619 Reply(llvm::json::Array(Actions));
620 else {
621 std::vector<Command> Commands;
622 for (const auto &Action : Actions)
623 if (auto Command = asCommand(Action))
624 Commands.push_back(std::move(*Command));
625 Reply(llvm::json::Array(Commands));
626 }
627 }
628
629 void ClangdLSPServer::onCompletion(const CompletionParams &Params,
630 Callback<CompletionList> Reply) {
631 if (!shouldRunCompletion(Params))
632 return Reply(llvm::make_error<IgnoreCompletionError>());
633 Server->codeComplete(Params.textDocument.uri.file(), Params.position, CCOpts,
634 Bind(
635 [this](decltype(Reply) Reply,
636 llvm::Expected<CodeCompleteResult> List) {
637 if (!List)
638 return Reply(List.takeError());
639 CompletionList LSPList;
640 LSPList.isIncomplete = List->HasMore;
641 for (const auto &R : List->Completions) {
642 CompletionItem C = R.render(CCOpts);
643 C.kind = adjustKindToCapability(
644 C.kind, SupportedCompletionItemKinds);
645 LSPList.items.push_back(std::move(C));
646 }
647 return Reply(std::move(LSPList));
648 },
649 std::move(Reply)));
650 }
651
652 void ClangdLSPServer::onSignatureHelp(const TextDocumentPositionParams &Params,
653 Callback<SignatureHelp> Reply) {
654 Server->signatureHelp(Params.textDocument.uri.file(), Params.position,
655 std::move(Reply));
656 }
657
658 void ClangdLSPServer::onGoToDefinition(const TextDocumentPositionParams &Params,
659 Callback<std::vector<Location>> Reply) {
660 Server->findDefinitions(Params.textDocument.uri.file(), Params.position,
661 std::move(Reply));
662 }
663
664 void ClangdLSPServer::onSwitchSourceHeader(const TextDocumentIdentifier &Params,
665 Callback<std::string> Reply) {
666 llvm::Optional<Path> Result = Server->switchSourceHeader(Params.uri.file());
667 Reply(Result ? URI::createFile(*Result).toString() : "");
668 }
669
670 void ClangdLSPServer::onDocumentHighlight(
671 const TextDocumentPositionParams &Params,
672 Callback<std::vector<DocumentHighlight>> Reply) {
673 Server->findDocumentHighlights(Params.textDocument.uri.file(),
674 Params.position, std::move(Reply));
675 }
676
677 void ClangdLSPServer::onHover(const TextDocumentPositionParams &Params,
678 Callback<llvm::Optional<Hover>> Reply) {
679 Server->findHover(Params.textDocument.uri.file(), Params.position,
680 std::move(Reply));
681 }
682
683 void ClangdLSPServer::applyConfiguration(
684 const ConfigurationSettings &Settings) {
685 // Per-file update to the compilation database.
686 bool ShouldReparseOpenFiles = false;
687 for (auto &Entry : Settings.compilationDatabaseChanges) {
688 /// The opened files need to be reparsed only when some existing
689 /// entries are changed.
690 PathRef File = Entry.first;
691 auto Old = CDB->getCompileCommand(File);
692 auto New =
693 tooling::CompileCommand(std::move(Entry.second.workingDirectory), File,
694 std::move(Entry.second.compilationCommand),
695 /*Output=*/"");
696 if (Old != New) {
697 CDB->setCompileCommand(File, std::move(New));
698 ShouldReparseOpenFiles = true;
699 }
700 }
701 if (ShouldReparseOpenFiles)
702 reparseOpenedFiles();
703 }
704
705 // FIXME: This function needs to be properly tested.
706 void ClangdLSPServer::onChangeConfiguration(
707 const DidChangeConfigurationParams &Params) {
708 applyConfiguration(Params.settings);
709 }
710
711 void ClangdLSPServer::onReference(const ReferenceParams &Params,
712 Callback<std::vector<Location>> Reply) {
713 Server->findReferences(Params.textDocument.uri.file(), Params.position,
714 CCOpts.Limit, std::move(Reply));
715 }
716
717 void ClangdLSPServer::onSymbolInfo(const TextDocumentPositionParams &Params,
718 Callback<std::vector<SymbolDetails>> Reply) {
719 Server->symbolInfo(Params.textDocument.uri.file(), Params.position,
720 std::move(Reply));
721 }
722
723 ClangdLSPServer::ClangdLSPServer(class Transport &Transp,
724 const clangd::CodeCompleteOptions &CCOpts,
725 llvm::Optional<Path> CompileCommandsDir,
726 bool UseDirBasedCDB,
727 const ClangdServer::Options &Opts)
728 : Transp(Transp), MsgHandler(new MessageHandler(*this)), CCOpts(CCOpts),
729 SupportedSymbolKinds(defaultSymbolKinds()),
730 SupportedCompletionItemKinds(defaultCompletionItemKinds()),
731 UseDirBasedCDB(UseDirBasedCDB),
732 CompileCommandsDir(std::move(CompileCommandsDir)),
733 ClangdServerOpts(Opts) {
734 // clang-format off
735 MsgHandler->bind("initialize", &ClangdLSPServer::onInitialize);
736 MsgHandler->bind("shutdown", &ClangdLSPServer::onShutdown);
737 MsgHandler->bind("sync", &ClangdLSPServer::onSync);
738 MsgHandler->bind("textDocument/rangeFormatting", &ClangdLSPServer::onDocumentRangeFormatting);
739 MsgHandler->bind("textDocument/onTypeFormatting", &ClangdLSPServer::onDocumentOnTypeFormatting);
740 MsgHandler->bind("textDocument/formatting", &ClangdLSPServer::onDocumentFormatting);
741 MsgHandler->bind("textDocument/codeAction", &ClangdLSPServer::onCodeAction);
742 MsgHandler->bind("textDocument/completion", &ClangdLSPServer::onCompletion);
743 MsgHandler->bind("textDocument/signatureHelp", &ClangdLSPServer::onSignatureHelp);
744 MsgHandler->bind("textDocument/definition", &ClangdLSPServer::onGoToDefinition);
745 MsgHandler->bind("textDocument/references", &ClangdLSPServer::onReference);
746 MsgHandler->bind("textDocument/switchSourceHeader", &ClangdLSPServer::onSwitchSourceHeader);
747 MsgHandler->bind("textDocument/rename", &ClangdLSPServer::onRename);
748 MsgHandler->bind("textDocument/hover", &ClangdLSPServer::onHover);
749 MsgHandler->bind("textDocument/documentSymbol", &ClangdLSPServer::onDocumentSymbol);
750 MsgHandler->bind("workspace/executeCommand", &ClangdLSPServer::onCommand);
751 MsgHandler->bind("textDocument/documentHighlight", &ClangdLSPServer::onDocumentHighlight);
752 MsgHandler->bind("workspace/symbol", &ClangdLSPServer::onWorkspaceSymbol);
753 MsgHandler->bind("textDocument/didOpen", &ClangdLSPServer::onDocumentDidOpen);
754 MsgHandler->bind("textDocument/didClose", &ClangdLSPServer::onDocumentDidClose);
755 MsgHandler->bind("textDocument/didChange", &ClangdLSPServer::onDocumentDidChange);
756 MsgHandler->bind("workspace/didChangeWatchedFiles", &ClangdLSPServer::onFileEvent);
757 MsgHandler->bind("workspace/didChangeConfiguration", &ClangdLSPServer::onChangeConfiguration);
758 MsgHandler->bind("textDocument/symbolInfo", &ClangdLSPServer::onSymbolInfo);
759 // clang-format on
760 }
761
762 ClangdLSPServer::~ClangdLSPServer() = default;
763
764 bool ClangdLSPServer::run() {
765 // Run the Language Server loop.
766 bool CleanExit = true;
767 if (auto Err = Transp.loop(*MsgHandler)) {
768 elog("Transport error: {0}", std::move(Err));
769 CleanExit = false;
770 }
771
772 // Destroy ClangdServer to ensure all worker threads finish.
773 Server.reset();
774 return CleanExit && ShutdownRequestReceived;
775 }
776
777 std::vector<Fix> ClangdLSPServer::getFixes(llvm::StringRef File,
778 const clangd::Diagnostic &D) {
779 std::lock_guard<std::mutex> Lock(FixItsMutex);
780 auto DiagToFixItsIter = FixItsMap.find(File);
781 if (DiagToFixItsIter == FixItsMap.end())
782 return {};
783
784 const auto &DiagToFixItsMap = DiagToFixItsIter->second;
785 auto FixItsIter = DiagToFixItsMap.find(D);
786 if (FixItsIter == DiagToFixItsMap.end())
787 return {};
788
789 return FixItsIter->second;
790 }
791
792 bool ClangdLSPServer::shouldRunCompletion(
793 const CompletionParams &Params) const {
794 llvm::StringRef Trigger = Params.context.triggerCharacter;
795 if (Params.context.triggerKind != CompletionTriggerKind::TriggerCharacter ||
796 (Trigger != ">" && Trigger != ":"))
797 return true;
798
799 auto Code = DraftMgr.getDraft(Params.textDocument.uri.file());
800 if (!Code)
801 return true; // completion code will log the error for untracked doc.
802
803 // A completion request is sent when the user types '>' or ':', but we only
804 // want to trigger on '->' and '::'. We check the preceeding character to make
805 // sure it matches what we expected.
806 // Running the lexer here would be more robust (e.g. we can detect comments
807 // and avoid triggering completion there), but we choose to err on the side
808 // of simplicity here.
809 auto Offset = positionToOffset(*Code, Params.position,
810 /*AllowColumnsBeyondLineLength=*/false);
811 if (!Offset) {
812 vlog("could not convert position '{0}' to offset for file '{1}'",
813 Params.position, Params.textDocument.uri.file());
814 return true;
815 }
816 if (*Offset < 2)
817 return false;
818
819 if (Trigger == ">")
820 return (*Code)[*Offset - 2] == '-'; // trigger only on '->'.
821 if (Trigger == ":")
822 return (*Code)[*Offset - 2] == ':'; // trigger only on '::'.
823 assert(false && "unhandled trigger character");
824 return true;
825 }
826
827 void ClangdLSPServer::onDiagnosticsReady(PathRef File,
828 std::vector<Diag> Diagnostics) {
829 auto URI = URIForFile::canonicalize(File, /*TUPath=*/File);
830 std::vector<Diagnostic> LSPDiagnostics;
831 DiagnosticToReplacementMap LocalFixIts; // Temporary storage
832 for (auto &Diag : Diagnostics) {
833 toLSPDiags(Diag, URI, DiagOpts,
834 [&](clangd::Diagnostic Diag, llvm::ArrayRef<Fix> Fixes) {
835 auto &FixItsForDiagnostic = LocalFixIts[Diag];
836 llvm::copy(Fixes, std::back_inserter(FixItsForDiagnostic));
837 LSPDiagnostics.push_back(std::move(Diag));
838 });
839 }
840
841 // Cache FixIts
842 {
843 // FIXME(ibiryukov): should be deleted when documents are removed
844 std::lock_guard<std::mutex> Lock(FixItsMutex);
845 FixItsMap[File] = LocalFixIts;
846 }
847
848 // Publish diagnostics.
849 notify("textDocument/publishDiagnostics",
850 llvm::json::Object{
851 {"uri", URI},
852 {"diagnostics", std::move(LSPDiagnostics)},
853 });
854 }
855
856 void ClangdLSPServer::onFileUpdated(PathRef File, const TUStatus &Status) {
857 if (!SupportFileStatus)
858 return;
859 // FIXME: we don't emit "BuildingFile" and `RunningAction`, as these
860 // two statuses are running faster in practice, which leads the UI constantly
861 // changing, and doesn't provide much value. We may want to emit status at a
862 // reasonable time interval (e.g. 0.5s).
863 if (Status.Action.S == TUAction::BuildingFile ||
864 Status.Action.S == TUAction::RunningAction)
865 return;
866 notify("textDocument/clangd.fileStatus", Status.render(File));
867 }
868
869 void ClangdLSPServer::reparseOpenedFiles() {
870 for (const Path &FilePath : DraftMgr.getActiveFiles())
871 Server->addDocument(FilePath, *DraftMgr.getDraft(FilePath),
872 WantDiagnostics::Auto);
873 }
874
875 } // namespace clangd
876 } // namespace clang
clang::clangd::DocumentRangeFormattingParams::range
Range range
The range to format.
Definition: Protocol.h:523
Command
const tooling::CompileCommand & Command
Definition: CodeComplete.cpp:1005
clang::clangd::DocumentFormattingParams::textDocument
TextDocumentIdentifier textDocument
The document to format.
Definition: Protocol.h:547
clang::clangd::SymbolInformation::location
Location location
The location of this symbol.
Definition: Protocol.h:729
clang::clangd::DocumentRangeFormattingParams
Definition: Protocol.h:518
Trace.h
Contents
llvm::StringRef Contents
Definition: CodeComplete.cpp:1007
clang::clangd::ExecuteCommandParams
Exact commands are not specified in the protocol so we define the ones supported by Clangd here...
Definition: Protocol.h:643
clang::clangd::DocumentOnTypeFormattingParams::textDocument
TextDocumentIdentifier textDocument
The document to format.
Definition: Protocol.h:532
clang::clangd::DocumentSymbolParams
Definition: Protocol.h:554
clang::clangd::ClientCapabilities::WorkspaceSymbolKinds
llvm::Optional< SymbolKindBitset > WorkspaceSymbolKinds
The supported set of SymbolKinds for workspace/symbol.
Definition: Protocol.h:341
clang::clangd::InitializeParams::rootUri
llvm::Optional< URIForFile > rootUri
The rootUri of the workspace.
Definition: Protocol.h:422
clang::clangd::ApplyWorkspaceEditParams::edit
WorkspaceEdit edit
Definition: Protocol.h:765
clang::clangd::DidChangeConfigurationParams::settings
ConfigurationSettings settings
Definition: Protocol.h:504
clang::clangd::ErrorCode::MethodNotFound
clang::clangd::Location::range
Range range
Definition: Protocol.h:183
clang::clangd::flattenSymbolHierarchy
static std::vector< SymbolInformation > flattenSymbolHierarchy(llvm::ArrayRef< DocumentSymbol > Symbols, const URIForFile &FileURI)
The functions constructs a flattened view of the DocumentSymbol hierarchy.
Definition: ClangdLSPServer.cpp:542
clang::clangd::WorkspaceEdit
Definition: Protocol.h:625
clang::clangd::CompletionList
Represents a collection of completion items to be presented in the editor.
Definition: Protocol.h:901
clang::clangd::WantDiagnostics::No
Diagnostics must be generated for this snapshot.
clang::clangd::DidChangeTextDocumentParams::wantDiagnostics
llvm::Optional< bool > wantDiagnostics
Forces diagnostics to be generated, or to not be generated, for this version of the file...
Definition: Protocol.h:475
clang::clangd::ApplyWorkspaceEditParams
Definition: Protocol.h:764
clang::clangd::Canceler
std::function< void()> Canceler
A canceller requests cancellation of a task, when called.
Definition: Cancellation.h:71
clang::clangd::TUStatus::render
FileStatus render(PathRef File) const
Serialize this to an LSP file status item.
Definition: TUScheduler.cpp:772
clang::clangd::CodeActionParams::context
CodeActionContext context
Context carrying additional information.
Definition: Protocol.h:621
clang::clangd::ClangdLSPServer::MessageHandler::onReply
bool onReply(llvm::json::Value ID, llvm::Expected< llvm::json::Value > Result) override
Definition: ClangdLSPServer.cpp:106
clang::clangd::ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND
static const llvm::StringLiteral CLANGD_APPLY_FIX_COMMAND
Definition: Protocol.h:645
clang::clangd::CompletionItem::kind
CompletionItemKind kind
The kind of this completion item.
Definition: Protocol.h:849
clang::clangd::ClientCapabilities::CompletionSnippets
bool CompletionSnippets
Client supports snippets as insert text.
Definition: Protocol.h:354
clang::clangd::CompletionList::items
std::vector< CompletionItem > items
The completion items.
Definition: Protocol.h:907
clang::clangd::ClangdLSPServer::MessageHandler::bind
void bind(const char *Method, void(ClangdLSPServer::*Handler)(const Param &))
Definition: ClangdLSPServer.cpp:135
clang::clangd::CompletionItem
Definition: Protocol.h:842
clang::clangd::toCodeAction
CodeAction toCodeAction(const Fix &F, const URIForFile &File)
Convert from Fix to LSP CodeAction.
Definition: Diagnostics.cpp:232
clang::clangd::TextDocumentSyncKind::Incremental
Documents are synced by sending the full content on open.
clang::clangd::DidChangeWatchedFilesParams
Definition: Protocol.h:497
clang::clangd::WorkspaceEdit::changes
llvm::Optional< std::map< std::string, std::vector< TextEdit > > > changes
Holds changes to existing resources.
Definition: Protocol.h:627
Commands
static cl::list< std::string > Commands("c", cl::desc("Specify command to run"), cl::value_desc("command"), cl::cat(ClangQueryCategory))
clang::clangd::Transport
Definition: Transport.h:35
clang::clangd::CodeAction::kind
llvm::Optional< std::string > kind
The kind of the code action.
Definition: Protocol.h:671
SourceCode.h
clang::clangd::CodeAction::title
std::string title
A short, human-readable, title for this code action.
Definition: Protocol.h:667
clang::clangd::DidChangeTextDocumentParams
Definition: Protocol.h:462
clang::clangd::DidCloseTextDocumentParams::textDocument
TextDocumentIdentifier textDocument
The document that was closed.
Definition: Protocol.h:446
clang::clangd::ClangdLSPServer::run
bool run()
Run LSP server loop, communicating with the Transport provided in the constructor.
Definition: ClangdLSPServer.cpp:764
clang::clangd::CodeAction
A code action represents a change that can be performed in code, e.g.
Definition: Protocol.h:665
clang::clangd::InitializeParams
Definition: Protocol.h:406
clang::clangd::Location::uri
URIForFile uri
The text document&#39;s URI.
Definition: Protocol.h:182
clang::clangd::PathRef
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:24
clang::clangd::CodeAction::edit
llvm::Optional< WorkspaceEdit > edit
The workspace edit this code action performs.
Definition: Protocol.h:678
clang::tidy::cppcoreguidelines::toString
static llvm::StringRef toString(SpecialMemberFunctionsCheck::SpecialMemberFunctionKind K)
Definition: SpecialMemberFunctionsCheck.cpp:59
clang::clangd::TUAction::RunningAction
Definition: TUScheduler.h:58
clang::clangd::ErrorCode::InternalError
Results
std::vector< CodeCompletionResult > Results
Definition: CodeComplete.cpp:702
clang::clangd::TUAction::S
State S
Definition: TUScheduler.h:66
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:29
clang::clangd::SymbolKindMin
constexpr auto SymbolKindMin
Definition: Protocol.h:328
clang::clangd::InitializationOptions::compilationDatabasePath
llvm::Optional< std::string > compilationDatabasePath
Definition: Protocol.h:395
clang::clangd::CompletionItemKindMin
constexpr auto CompletionItemKindMin
Definition: Protocol.h:288
clang::clangd::TUStatus::Action
TUAction Action
Definition: TUScheduler.h:83
clang::clangd::DocumentFormattingParams
Definition: Protocol.h:545
clang::clangd::CompletionItemKindBitset
std::bitset< CompletionItemKindMax+1 > CompletionItemKindBitset
Definition: Protocol.h:292
clang::clangd::TextDocumentSyncKind::None
Documents should not be synced at all.
clang::clangd::CompletionList::isIncomplete
bool isIncomplete
The list is not complete.
Definition: Protocol.h:904
clang::clangd::RenameParams
Definition: Protocol.h:959
clang::clangd::DocumentSymbol::range
Range range
The range enclosing this symbol not including leading/trailing whitespace but everything else like co...
Definition: Protocol.h:707
clang::clangd::SymbolKind::Array
clang::clangd::vlog
void vlog(const char *Fmt, Ts &&... Vals)
Definition: Logger.h:68
clang::clangd::TUAction::BuildingFile
Definition: TUScheduler.h:60
clang::clangd::elog
void elog(const char *Fmt, Ts &&... Vals)
Definition: Logger.h:57
clang::clangd::DocumentSymbol
Represents programming constructs like variables, classes, interfaces etc.
Definition: Protocol.h:690
clang::clangd::ClangdLSPServer::MessageHandler::onCall
bool onCall(llvm::StringRef Method, llvm::json::Value Params, llvm::json::Value ID) override
Definition: ClangdLSPServer.cpp:86
clang::clangd::InitializationOptions::ConfigSettings
ConfigurationSettings ConfigSettings
Definition: Protocol.h:393
clang::clangd::Diag
A top-level diagnostic that may have Notes and Fixes.
Definition: Diagnostics.h:67
clang::clangd::URIForFile::uri
std::string uri() const
Definition: Protocol.h:94
clang::clangd::NoParams
Definition: Protocol.h:239
clang::clangd::WantDiagnostics::Yes
clang::clangd::TextDocumentItem::uri
URIForFile uri
The text document&#39;s URI.
Definition: Protocol.h:219
clang::clangd::Transport::MessageHandler
Definition: Transport.h:47
clang::clangd::URI::createFile
static URI createFile(llvm::StringRef AbsolutePath)
This creates a file:// URI for AbsolutePath. The path must be absolute.
Definition: URI.cpp:217
clang::clangd::RenameParams::textDocument
TextDocumentIdentifier textDocument
The document that was opened.
Definition: Protocol.h:961
clang::clangd::toLSPDiags
void toLSPDiags(const Diag &D, const URIForFile &File, const ClangdDiagnosticOptions &Opts, llvm::function_ref< void(clangd::Diagnostic, llvm::ArrayRef< Fix >)> OutFn)
Conversion to LSP diagnostics.
Definition: Diagnostics.cpp:242
clang::clangd::ClientCapabilities::DiagnosticCategory
bool DiagnosticCategory
Whether the client accepts diagnostics with category attached to it using the "category" extension...
Definition: Protocol.h:350
clang::clangd::RenameParams::newName
std::string newName
The new name of the symbol.
Definition: Protocol.h:967
clang::clangd::ConfigurationSettings
Clangd extension: parameters configurable at any time, via the workspace/didChangeConfiguration notif...
Definition: Protocol.h:381
clang::clangd::ExecuteCommandParams::command
std::string command
The command identifier, e.g. CLANGD_APPLY_FIX_COMMAND.
Definition: Protocol.h:648
clang::clangd::InitializeParams::initializationOptions
InitializationOptions initializationOptions
User-provided initialization options.
Definition: Protocol.h:434
clang::clangd::DocumentSymbolParams::textDocument
TextDocumentIdentifier textDocument
Definition: Protocol.h:556
clang::clangd::ClangdLSPServer::MessageHandler::MessageHandler
MessageHandler(ClangdLSPServer &Server)
Definition: ClangdLSPServer.cpp:69
clang::clangd::positionToOffset
llvm::Expected< size_t > positionToOffset(llvm::StringRef Code, Position P, bool AllowColumnsBeyondLineLength)
Turn a [line, column] pair into an offset in Code.
Definition: SourceCode.cpp:81
clang::clangd::Bind
ForwardBinder< Func, Args... > Bind(Func F, Args &&... As)
Creates an object that stores a callable (F) and first arguments to the callable (As) and allows to c...
Definition: Function.h:82
clang::clangd::CodeActionParams::textDocument
TextDocumentIdentifier textDocument
The document in which the command was invoked.
Definition: Protocol.h:615
clang::clangd::ClangdLSPServer::ClangdLSPServer
ClangdLSPServer(Transport &Transp, const clangd::CodeCompleteOptions &CCOpts, llvm::Optional< Path > CompileCommandsDir, bool UseDirBasedCDB, const ClangdServer::Options &Opts)
If CompileCommandsDir has a value, compile_commands.json will be loaded only from CompileCommandsDir...
Definition: ClangdLSPServer.cpp:723
Action
llvm::unique_function< void()> Action
Definition: TUScheduler.cpp:221
Diagnostics.h
clang::clangd::ClangdLSPServer::MessageHandler
Definition: ClangdLSPServer.cpp:67
clang::clangd::InitializationOptions::fallbackFlags
std::vector< std::string > fallbackFlags
Definition: Protocol.h:399
clang::clangd::log
void log(const char *Fmt, Ts &&... Vals)
Definition: Logger.h:63
clang::clangd::Path
std::string Path
A typedef to represent a file path.
Definition: Path.h:21
clang::clangd::CompletionContext::triggerKind
CompletionTriggerKind triggerKind
How the completion was triggered.
Definition: Protocol.h:791
clang::clangd::ReferenceParams
Definition: Protocol.h:999
clang::clangd::URIForFile::canonicalize
static URIForFile canonicalize(llvm::StringRef AbsPath, llvm::StringRef TUPath)
Canonicalizes AbsPath via URI.
Definition: Protocol.cpp:32
clang::clangd::CodeCompleteOptions
Definition: CodeComplete.h:40
clang::clangd::TextDocumentPositionParams::position
Position position
The position inside the text document.
Definition: Protocol.h:774
clang::clangd::TextDocumentIdentifier
Definition: Protocol.h:118
clang::clangd::DocumentSymbol::children
std::vector< DocumentSymbol > children
Children of this symbol, e.g. properties of a class.
Definition: Protocol.h:714
D
const Decl * D
Definition: XRefs.cpp:79
Result
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
Definition: ClangdServer.cpp:60
clang::clangd::SymbolInformation::kind
SymbolKind kind
The kind of this symbol.
Definition: Protocol.h:726
clang::clangd::URIForFile
Definition: Protocol.h:71
clang::clangd::InitializationOptions::FileStatus
bool FileStatus
Clients supports show file status for textDocument/clangd.fileStatus.
Definition: Protocol.h:402
clang::clangd::DocumentSymbol::name
std::string name
The name of this symbol.
Definition: Protocol.h:692
clang::clangd::TUStatus
Definition: TUScheduler.h:73
clang::clangd::CompletionItemKind::Method
clang::clangd::cancelableTask
std::pair< Context, Canceler > cancelableTask()
Defines a new task whose cancellation may be requested.
Definition: Cancellation.cpp:19
clang::clangd::asCommand
static llvm::Optional< Command > asCommand(const CodeAction &Action)
Definition: ClangdLSPServer.cpp:585
clang::clangd::ClientCapabilities::DiagnosticFixes
bool DiagnosticFixes
Whether the client accepts diagnostics with codeActions attached inline.
Definition: Protocol.h:345
clang::clangd::InitializeParams::capabilities
ClientCapabilities capabilities
The capabilities provided by the client (editor or tool)
Definition: Protocol.h:428
clang::clangd::DidOpenTextDocumentParams::textDocument
TextDocumentItem textDocument
The document that was opened.
Definition: Protocol.h:440
clang::clangd::DocumentOnTypeFormattingParams
Definition: Protocol.h:530
clang::clangd::ErrorCode::InvalidParams
clang::clangd::TextDocumentPositionParams
Definition: Protocol.h:769
clang::clangd::Context
A context is an immutable container for per-request data that must be propagated through layers that ...
Definition: Context.h:70
clang::clangd::DidChangeTextDocumentParams::textDocument
TextDocumentIdentifier textDocument
The document that did change.
Definition: Protocol.h:466
clang::clangd::ConfigurationSettings::compilationDatabaseChanges
std::map< std::string, ClangdCompileCommand > compilationDatabaseChanges
Definition: Protocol.h:384
clang::clangd::CompletionTriggerKind::TriggerCharacter
Completion was triggered by a trigger character specified by the triggerCharacters properties of the ...
clang::clangd::CompletionParams
Definition: Protocol.h:798
clang::clangd::ClangdLSPServer::MessageHandler::onNotify
bool onNotify(llvm::StringRef Method, llvm::json::Value Params) override
Definition: ClangdLSPServer.cpp:71
clang::clangd::ClientCapabilities::CompletionItemKinds
llvm::Optional< CompletionItemKindBitset > CompletionItemKinds
The supported set of CompletionItemKinds for textDocument/completion.
Definition: Protocol.h:361
clang::clangd::ClangdLSPServer::~ClangdLSPServer
~ClangdLSPServer()
clang::clangd::InitializeParams::rootPath
llvm::Optional< std::string > rootPath
The rootPath of the workspace.
Definition: Protocol.h:417
clang::clangd::Transport::loop
virtual llvm::Error loop(MessageHandler &)=0
clang::clangd::DocumentOnTypeFormattingParams::position
Position position
The position at which this request was sent.
Definition: Protocol.h:535
clang::clangd::ClientCapabilities::CodeActionStructure
bool CodeActionStructure
Client supports CodeAction return value for textDocument/codeAction.
Definition: Protocol.h:365
clang::clangd::WithContext
WithContext replaces Context::current() with a provided scope.
Definition: Context.h:190
clang::clangd::fromJSON
bool fromJSON(const llvm::json::Value &Parameters, FuzzyFindRequest &Request)
Definition: Index.cpp:176
clang::clangd::ClangdLSPServer::MessageHandler::bind
void bind(const char *Method, void(ClangdLSPServer::*Handler)(const Param &, Callback< Result >))
Definition: ClangdLSPServer.cpp:118
clang::clangd::DidChangeTextDocumentParams::contentChanges
std::vector< TextDocumentContentChangeEvent > contentChanges
The actual content changes.
Definition: Protocol.h:469
clang::clangd::DidChangeConfigurationParams
Definition: Protocol.h:503
clang::clangd::CompletionParams::context
CompletionContext context
Definition: Protocol.h:799
clang::clangd::WorkspaceSymbolParams::query
std::string query
A non-empty query string.
Definition: Protocol.h:760
clang::clangd::DocumentSymbol::kind
SymbolKind kind
The kind of this symbol.
Definition: Protocol.h:698
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18
clang::clangd::SymbolKindBitset
std::bitset< SymbolKindMax+1 > SymbolKindBitset
Definition: Protocol.h:330
clang::clangd::CompletionContext::triggerCharacter
std::string triggerCharacter
The trigger character (a single character) that has trigger code complete.
Definition: Protocol.h:794
clang::clangd::TextDocumentPositionParams::textDocument
TextDocumentIdentifier textDocument
The text document.
Definition: Protocol.h:771
clang::clangd::replacementToEdit
TextEdit replacementToEdit(llvm::StringRef Code, const tooling::Replacement &R)
Definition: SourceCode.cpp:171
clang::clangd::Transport::reply
virtual void reply(llvm::json::Value ID, llvm::Expected< llvm::json::Value > Result)=0
clang::clangd::DidOpenTextDocumentParams
Definition: Protocol.h:438
clang::clangd::adjustKindToCapability
SymbolKind adjustKindToCapability(SymbolKind Kind, SymbolKindBitset &SupportedSymbolKinds)
Definition: Protocol.cpp:198
clang::clangd::CodeAction::QUICKFIX_KIND
static const llvm::StringLiteral QUICKFIX_KIND
Definition: Protocol.h:672
clang::clangd::URI
A URI describes the location of a source file.
Definition: URI.h:29
clang::clangd::CodeActionContext::diagnostics
std::vector< Diagnostic > diagnostics
An array of diagnostics.
Definition: Protocol.h:609
clang::clangd::CodeAction::command
llvm::Optional< Command > command
A command this code action executes.
Definition: Protocol.h:682
clang::clangd::replacementsToEdits
std::vector< TextEdit > replacementsToEdits(llvm::StringRef Code, const tooling::Replacements &Repls)
Definition: SourceCode.cpp:179
clang::clangd::WorkspaceSymbolParams
The parameters of a Workspace Symbol Request.
Definition: Protocol.h:758
URI.h
clang::clangd::TextDocumentItem::text
std::string text
The content of the opened text document.
Definition: Protocol.h:228
clang::clangd::DidCloseTextDocumentParams
Definition: Protocol.h:444
clang::clangd::SymbolInformation::containerName
std::string containerName
The name of the symbol containing this symbol.
Definition: Protocol.h:732
clang::clangd::RenameParams::position
Position position
The position at which this request was sent.
Definition: Protocol.h:964
clang::clangd::TextDocumentIdentifier::uri
URIForFile uri
The text document&#39;s URI.
Definition: Protocol.h:120
clang::clangd::Diagnostic
Definition: Protocol.h:561
clang::clangd::ErrorCode::ServerNotInitialized
clang::clangd::DraftStore::getActiveFiles
std::vector< Path > getActiveFiles() const
Definition: DraftStore.cpp:27
clang::clangd::DocumentRangeFormattingParams::textDocument
TextDocumentIdentifier textDocument
The document to format.
Definition: Protocol.h:520
clang::clangd::ExecuteCommandParams::workspaceEdit
llvm::Optional< WorkspaceEdit > workspaceEdit
Definition: Protocol.h:651
clang::clangd::ClangdServer::Options
Definition: ClangdServer.h:67
clang::clangd::ClangdLSPServer
This class exposes ClangdServer&#39;s capabilities via Language Server Protocol.
Definition: ClangdLSPServer.h:34
clang::clangd::trace::Span::Args
llvm::json::Object *const Args
Mutable metadata, if this span is interested.
Definition: Trace.h:91
clang::clangd::CompileCommandsDir
static llvm::cl::opt< Path > CompileCommandsDir("compile-commands-dir", llvm::cl::desc("Specify a path to look for compile_commands.json. If path " "is invalid, clangd will look in the current directory and " "parent paths of each source file."))
clang::clangd::SymbolInformation::name
std::string name
The name of this symbol.
Definition: Protocol.h:723
ClangdLSPServer.h
clang::clangd::trace::Span
Records an event whose duration is the lifetime of the Span object.
Definition: Trace.h:83
clang::clangd::CompletionItemKind::File
clang::clangd::CodeActionParams
Definition: Protocol.h:613
clang::clangd::URI::toString
std::string toString() const
Returns a string URI with all components percent-encoded.
Definition: URI.cpp:151
clang::clangd::ErrorCode::InvalidRequest
SPAN_ATTACH
#define SPAN_ATTACH(S, Name, Expr)
Attach a key-value pair to a Span event.
Definition: Trace.h:99
clang::clangd::WantDiagnostics::Auto
Diagnostics must not be generated for this snapshot.
Entry
Definition: Modularize.cpp:430
clang::clangd::CompletionItemKind::Reference
clang::clangd::DraftStore::getDraft
llvm::Optional< std::string > getDraft(PathRef File) const
Definition: DraftStore.cpp:17
clang::clangd::ClientCapabilities::HierarchicalDocumentSymbol
bool HierarchicalDocumentSymbol
Client supports hierarchical document symbols.
Definition: Protocol.h:357
clang::clangd::URIForFile::file
llvm::StringRef file() const
Retrieves absolute path to the file.
Definition: Protocol.h:91
clang::clangd::SymbolInformation
Represents information about programming constructs like variables, classes, interfaces etc...
Definition: Protocol.h:721