-
Notifications
You must be signed in to change notification settings - Fork 10.8k
/
ClangdLSPServer.h
191 lines (163 loc) · 7.82 KB
/
ClangdLSPServer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//===--- ClangdLSPServer.h - LSP server --------------------------*- C++-*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDLSPSERVER_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDLSPSERVER_H
#include "ClangdServer.h"
#include "DraftStore.h"
#include "FindSymbols.h"
#include "GlobalCompilationDatabase.h"
#include "Path.h"
#include "Protocol.h"
#include "ProtocolHandlers.h"
#include "clang/Tooling/Core/Replacement.h"
#include "llvm/ADT/Optional.h"
namespace clang {
namespace clangd {
class JSONOutput;
class SymbolIndex;
/// This class provides implementation of an LSP server, glueing the JSON
/// dispatch and ClangdServer together.
class ClangdLSPServer : private DiagnosticsConsumer, private ProtocolCallbacks {
public:
/// If \p CompileCommandsDir has a value, compile_commands.json will be
/// loaded only from \p CompileCommandsDir. Otherwise, clangd will look
/// for compile_commands.json in all parent directories of each file.
ClangdLSPServer(JSONOutput &Out, const clangd::CodeCompleteOptions &CCOpts,
llvm::Optional<Path> CompileCommandsDir,
bool ShouldUseInMemoryCDB, const ClangdServer::Options &Opts);
/// Run LSP server loop, receiving input for it from \p In. \p In must be
/// opened in binary mode. Output will be written using Out variable passed to
/// class constructor. This method must not be executed more than once for
/// each instance of ClangdLSPServer.
///
/// \return Whether we received a 'shutdown' request before an 'exit' request.
bool run(std::FILE *In,
JSONStreamStyle InputStyle = JSONStreamStyle::Standard);
private:
// Implement DiagnosticsConsumer.
void onDiagnosticsReady(PathRef File, std::vector<Diag> Diagnostics) override;
// Implement ProtocolCallbacks.
void onInitialize(InitializeParams &Params) override;
void onShutdown(ShutdownParams &Params) override;
void onExit(ExitParams &Params) override;
void onDocumentDidOpen(DidOpenTextDocumentParams &Params) override;
void onDocumentDidChange(DidChangeTextDocumentParams &Params) override;
void onDocumentDidClose(DidCloseTextDocumentParams &Params) override;
void
onDocumentOnTypeFormatting(DocumentOnTypeFormattingParams &Params) override;
void
onDocumentRangeFormatting(DocumentRangeFormattingParams &Params) override;
void onDocumentFormatting(DocumentFormattingParams &Params) override;
void onDocumentSymbol(DocumentSymbolParams &Params) override;
void onCodeAction(CodeActionParams &Params) override;
void onCompletion(TextDocumentPositionParams &Params) override;
void onSignatureHelp(TextDocumentPositionParams &Params) override;
void onGoToDefinition(TextDocumentPositionParams &Params) override;
void onReference(ReferenceParams &Params) override;
void onSwitchSourceHeader(TextDocumentIdentifier &Params) override;
void onDocumentHighlight(TextDocumentPositionParams &Params) override;
void onFileEvent(DidChangeWatchedFilesParams &Params) override;
void onCommand(ExecuteCommandParams &Params) override;
void onWorkspaceSymbol(WorkspaceSymbolParams &Params) override;
void onRename(RenameParams &Parames) override;
void onHover(TextDocumentPositionParams &Params) override;
void onChangeConfiguration(DidChangeConfigurationParams &Params) override;
void onCancelRequest(CancelParams &Params) override;
std::vector<Fix> getFixes(StringRef File, const clangd::Diagnostic &D);
/// Forces a reparse of all currently opened files. As a result, this method
/// may be very expensive. This method is normally called when the
/// compilation database is changed.
void reparseOpenedFiles();
void applyConfiguration(const ClangdConfigurationParamsChange &Settings);
JSONOutput &Out;
/// Used to indicate that the 'shutdown' request was received from the
/// Language Server client.
bool ShutdownRequestReceived = false;
/// Used to indicate that the 'exit' notification was received from the
/// Language Server client.
/// It's used to break out of the LSP parsing loop.
bool IsDone = false;
std::mutex FixItsMutex;
typedef std::map<clangd::Diagnostic, std::vector<Fix>, LSPDiagnosticCompare>
DiagnosticToReplacementMap;
/// Caches FixIts per file and diagnostics
llvm::StringMap<DiagnosticToReplacementMap> FixItsMap;
/// Encapsulates the directory-based or the in-memory compilation database
/// that's used by the LSP server.
class CompilationDB {
public:
static CompilationDB makeInMemory();
static CompilationDB
makeDirectoryBased(llvm::Optional<Path> CompileCommandsDir);
void invalidate(PathRef File);
/// Sets the compilation command for a particular file.
/// Only valid for in-memory CDB, no-op and error log on DirectoryBasedCDB.
///
/// \returns True if the File had no compilation command before.
bool
setCompilationCommandForFile(PathRef File,
tooling::CompileCommand CompilationCommand);
/// Adds extra compilation flags to the compilation command for a particular
/// file. Only valid for directory-based CDB, no-op and error log on
/// InMemoryCDB;
void setExtraFlagsForFile(PathRef File,
std::vector<std::string> ExtraFlags);
/// Set the compile commands directory to \p P.
/// Only valid for directory-based CDB, no-op and error log on InMemoryCDB;
void setCompileCommandsDir(Path P);
/// Returns a CDB that should be used to get compile commands for the
/// current instance of ClangdLSPServer.
GlobalCompilationDatabase &getCDB();
private:
CompilationDB(std::unique_ptr<GlobalCompilationDatabase> CDB,
std::unique_ptr<CachingCompilationDb> CachingCDB,
bool IsDirectoryBased)
: CDB(std::move(CDB)), CachingCDB(std::move(CachingCDB)),
IsDirectoryBased(IsDirectoryBased) {}
// if IsDirectoryBased is true, an instance of InMemoryCDB.
// If IsDirectoryBased is false, an instance of DirectoryBasedCDB.
// unique_ptr<GlobalCompilationDatabase> CDB;
std::unique_ptr<GlobalCompilationDatabase> CDB;
// Non-null only for directory-based CDB
std::unique_ptr<CachingCompilationDb> CachingCDB;
bool IsDirectoryBased;
};
// Various ClangdServer parameters go here. It's important they're created
// before ClangdServer.
CompilationDB CDB;
RealFileSystemProvider FSProvider;
/// Options used for code completion
clangd::CodeCompleteOptions CCOpts;
/// Options used for diagnostics.
ClangdDiagnosticOptions DiagOpts;
/// The supported kinds of the client.
SymbolKindBitset SupportedSymbolKinds;
// Store of the current versions of the open documents.
DraftStore DraftMgr;
// Server must be the last member of the class to allow its destructor to exit
// the worker thread that may otherwise run an async callback on partially
// destructed instance of ClangdLSPServer.
ClangdServer Server;
// Holds task handles for running requets. Key of the map is a serialized
// request id.
llvm::StringMap<TaskHandle> TaskHandles;
std::mutex TaskHandlesMutex;
// Following three functions are for managing TaskHandles map. They store or
// remove a task handle for the request-id stored in current Context.
// FIXME(kadircet): Wrap the following three functions in a RAII object to
// make sure these do not get misused. The object might be stored in the
// Context of the thread or moved around until a reply is generated for the
// request.
void CleanupTaskHandle();
void CreateSpaceForTaskHandle();
void StoreTaskHandle(TaskHandle TH);
};
} // namespace clangd
} // namespace clang
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDLSPSERVER_H