blob: 2a2e0bf971f0bfcf7edb552620ea127fa1e1f61b [file] [log] [blame]
Ilya Biryukov38d79772017-05-16 09:38:59 +00001//===--- 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 "JSONRPCDispatcher.h"
12
13using namespace clang::clangd;
14using namespace clang;
15
16class ClangdLSPServer::LSPDiagnosticsConsumer : public DiagnosticsConsumer {
17public:
18 LSPDiagnosticsConsumer(ClangdLSPServer &Server) : Server(Server) {}
19
20 virtual void onDiagnosticsReady(PathRef File,
21 std::vector<DiagWithFixIts> Diagnostics) {
22 Server.consumeDiagnostics(File, Diagnostics);
23 }
24
25private:
26 ClangdLSPServer &Server;
27};
28
29ClangdLSPServer::ClangdLSPServer(JSONOutput &Out, bool RunSynchronously)
30 : Out(Out),
31 Server(llvm::make_unique<DirectoryBasedGlobalCompilationDatabase>(),
32 llvm::make_unique<LSPDiagnosticsConsumer>(*this),
33 RunSynchronously) {}
34
35void ClangdLSPServer::openDocument(StringRef File, StringRef Contents) {
36 Server.addDocument(File, Contents);
37}
38
39void ClangdLSPServer::closeDocument(StringRef File) {
40 Server.removeDocument(File);
41}
42
43std::vector<CompletionItem> ClangdLSPServer::codeComplete(PathRef File,
44 Position Pos) {
45 return Server.codeComplete(File, Pos);
46}
47
48std::vector<clang::tooling::Replacement>
49ClangdLSPServer::getFixIts(StringRef File, const clangd::Diagnostic &D) {
50 std::lock_guard<std::mutex> Lock(FixItsMutex);
51 auto DiagToFixItsIter = FixItsMap.find(File);
52 if (DiagToFixItsIter == FixItsMap.end())
53 return {};
54
55 const auto &DiagToFixItsMap = DiagToFixItsIter->second;
56 auto FixItsIter = DiagToFixItsMap.find(D);
57 if (FixItsIter == DiagToFixItsMap.end())
58 return {};
59
60 return FixItsIter->second;
61}
62
63std::string ClangdLSPServer::getDocument(PathRef File) {
64 return Server.getDocument(File);
65}
66
67void ClangdLSPServer::consumeDiagnostics(
68 PathRef File, std::vector<DiagWithFixIts> Diagnostics) {
69 std::string DiagnosticsJSON;
70
71 DiagnosticToReplacementMap LocalFixIts; // Temporary storage
72 for (auto &DiagWithFixes : Diagnostics) {
73 auto Diag = DiagWithFixes.Diag;
74 DiagnosticsJSON +=
75 R"({"range":)" + Range::unparse(Diag.range) +
76 R"(,"severity":)" + std::to_string(Diag.severity) +
77 R"(,"message":")" + llvm::yaml::escape(Diag.message) +
78 R"("},)";
79
80 // We convert to Replacements to become independent of the SourceManager.
81 auto &FixItsForDiagnostic = LocalFixIts[Diag];
82 std::copy(DiagWithFixes.FixIts.begin(), DiagWithFixes.FixIts.end(),
83 std::back_inserter(FixItsForDiagnostic));
84 }
85
86 // Cache FixIts
87 {
88 // FIXME(ibiryukov): should be deleted when documents are removed
89 std::lock_guard<std::mutex> Lock(FixItsMutex);
90 FixItsMap[File] = LocalFixIts;
91 }
92
93 // Publish diagnostics.
94 if (!DiagnosticsJSON.empty())
95 DiagnosticsJSON.pop_back(); // Drop trailing comma.
96 Out.writeMessage(
97 R"({"jsonrpc":"2.0","method":"textDocument/publishDiagnostics","params":{"uri":")" +
98 URI::fromFile(File).uri + R"(","diagnostics":[)" + DiagnosticsJSON +
99 R"(]}})");
100}