| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 "GlobalCompilationDatabase.h" |
| 14 |
#include "LSPBinder.h" |
| 15 |
#include "Protocol.h" |
| 16 |
#include "Transport.h" |
| 17 |
#include "support/Context.h" |
| 18 |
#include "support/MemoryTree.h" |
| 19 |
#include "support/Path.h" |
| 20 |
#include "support/Threading.h" |
| 21 |
#include "llvm/Support/JSON.h" |
| 22 |
#include <chrono> |
| 23 |
#include <cstddef> |
| 24 |
#include <memory> |
| 25 |
#include <optional> |
| 26 |
#include <vector> |
| 27 |
|
| 28 |
namespace clang { |
| 29 |
namespace clangd { |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class ClangdLSPServer : private ClangdServer::Callbacks, |
| 37 |
private LSPBinder::RawOutgoing { |
| 38 |
public: |
| 39 |
struct Options : ClangdServer::Options { |
| 40 |
|
| 41 |
config::Provider *ConfigProvider = nullptr; |
| 42 |
|
| 43 |
|
| 44 |
bool UseDirBasedCDB = true; |
| 45 |
|
| 46 |
std::optional<OffsetEncoding> Encoding; |
| 47 |
|
| 48 |
|
| 49 |
std::function<void()> MemoryCleanup = nullptr; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
clangd::CodeCompleteOptions CodeComplete; |
| 54 |
MarkupKind SignatureHelpDocumentationFormat = MarkupKind::PlainText; |
| 55 |
clangd::RenameOptions Rename; |
| 56 |
|
| 57 |
std::function<bool(const Tweak &)> TweakFilter = [](const Tweak &T) { |
| 58 |
return !T.hidden(); |
| 59 |
}; |
| 60 |
|
| 61 |
|
| 62 |
size_t ReferencesLimit = 0; |
| 63 |
}; |
| 64 |
|
| 65 |
ClangdLSPServer(Transport &Transp, const ThreadsafeFS &TFS, |
| 66 |
const ClangdLSPServer::Options &Opts); |
| 67 |
|
| 68 |
~ClangdLSPServer(); |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
bool run(); |
| 75 |
|
| 76 |
|
| 77 |
void profile(MemoryTree &MT) const; |
| 78 |
|
| 79 |
private: |
| 80 |
|
| 81 |
void onDiagnosticsReady(PathRef File, llvm::StringRef Version, |
| 82 |
std::vector<Diag> Diagnostics) override; |
| 83 |
void onFileUpdated(PathRef File, const TUStatus &Status) override; |
| 84 |
void onBackgroundIndexProgress(const BackgroundQueue::Stats &Stats) override; |
| 85 |
void onSemanticsMaybeChanged(PathRef File) override; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
void onInitialize(const InitializeParams &, Callback<llvm::json::Value>); |
| 90 |
void onInitialized(const InitializedParams &); |
| 91 |
void onShutdown(const NoParams &, Callback<std::nullptr_t>); |
| 92 |
void onSync(const NoParams &, Callback<std::nullptr_t>); |
| 93 |
void onDocumentDidOpen(const DidOpenTextDocumentParams &); |
| 94 |
void onDocumentDidChange(const DidChangeTextDocumentParams &); |
| 95 |
void onDocumentDidClose(const DidCloseTextDocumentParams &); |
| 96 |
void onDocumentDidSave(const DidSaveTextDocumentParams &); |
| 97 |
void onAST(const ASTParams &, Callback<std::optional<ASTNode>>); |
| 98 |
void onDocumentOnTypeFormatting(const DocumentOnTypeFormattingParams &, |
| 99 |
Callback<std::vector<TextEdit>>); |
| 100 |
void onDocumentRangeFormatting(const DocumentRangeFormattingParams &, |
| 101 |
Callback<std::vector<TextEdit>>); |
| 102 |
void onDocumentFormatting(const DocumentFormattingParams &, |
| 103 |
Callback<std::vector<TextEdit>>); |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
void onDocumentSymbol(const DocumentSymbolParams &, |
| 108 |
Callback<llvm::json::Value>); |
| 109 |
void onFoldingRange(const FoldingRangeParams &, |
| 110 |
Callback<std::vector<FoldingRange>>); |
| 111 |
void onCodeAction(const CodeActionParams &, Callback<llvm::json::Value>); |
| 112 |
void onCompletion(const CompletionParams &, Callback<CompletionList>); |
| 113 |
void onSignatureHelp(const TextDocumentPositionParams &, |
| 114 |
Callback<SignatureHelp>); |
| 115 |
void onGoToDeclaration(const TextDocumentPositionParams &, |
| 116 |
Callback<std::vector<Location>>); |
| 117 |
void onGoToDefinition(const TextDocumentPositionParams &, |
| 118 |
Callback<std::vector<Location>>); |
| 119 |
void onGoToType(const TextDocumentPositionParams &, |
| 120 |
Callback<std::vector<Location>>); |
| 121 |
void onGoToImplementation(const TextDocumentPositionParams &, |
| 122 |
Callback<std::vector<Location>>); |
| 123 |
void onReference(const ReferenceParams &, Callback<std::vector<ReferenceLocation>>); |
| 124 |
void onSwitchSourceHeader(const TextDocumentIdentifier &, |
| 125 |
Callback<std::optional<URIForFile>>); |
| 126 |
void onDocumentHighlight(const TextDocumentPositionParams &, |
| 127 |
Callback<std::vector<DocumentHighlight>>); |
| 128 |
void onFileEvent(const DidChangeWatchedFilesParams &); |
| 129 |
void onWorkspaceSymbol(const WorkspaceSymbolParams &, |
| 130 |
Callback<std::vector<SymbolInformation>>); |
| 131 |
void onPrepareRename(const TextDocumentPositionParams &, |
| 132 |
Callback<std::optional<Range>>); |
| 133 |
void onRename(const RenameParams &, Callback<WorkspaceEdit>); |
| 134 |
void onHover(const TextDocumentPositionParams &, |
| 135 |
Callback<std::optional<Hover>>); |
| 136 |
void onPrepareTypeHierarchy(const TypeHierarchyPrepareParams &, |
| 137 |
Callback<std::vector<TypeHierarchyItem>>); |
| 138 |
void onSuperTypes(const ResolveTypeHierarchyItemParams &, |
| 139 |
Callback<std::optional<std::vector<TypeHierarchyItem>>>); |
| 140 |
void onSubTypes(const ResolveTypeHierarchyItemParams &, |
| 141 |
Callback<std::vector<TypeHierarchyItem>>); |
| 142 |
void onTypeHierarchy(const TypeHierarchyPrepareParams &, |
| 143 |
Callback<llvm::json::Value>); |
| 144 |
void onResolveTypeHierarchy(const ResolveTypeHierarchyItemParams &, |
| 145 |
Callback<llvm::json::Value>); |
| 146 |
void onPrepareCallHierarchy(const CallHierarchyPrepareParams &, |
| 147 |
Callback<std::vector<CallHierarchyItem>>); |
| 148 |
void onCallHierarchyIncomingCalls( |
| 149 |
const CallHierarchyIncomingCallsParams &, |
| 150 |
Callback<std::vector<CallHierarchyIncomingCall>>); |
| 151 |
void onCallHierarchyOutgoingCalls( |
| 152 |
const CallHierarchyOutgoingCallsParams &, |
| 153 |
Callback<std::vector<CallHierarchyOutgoingCall>>); |
| 154 |
void onClangdInlayHints(const InlayHintsParams &, |
| 155 |
Callback<llvm::json::Value>); |
| 156 |
void onInlayHint(const InlayHintsParams &, Callback<std::vector<InlayHint>>); |
| 157 |
void onChangeConfiguration(const DidChangeConfigurationParams &); |
| 158 |
void onSymbolInfo(const TextDocumentPositionParams &, |
| 159 |
Callback<std::vector<SymbolDetails>>); |
| 160 |
void onSelectionRange(const SelectionRangeParams &, |
| 161 |
Callback<std::vector<SelectionRange>>); |
| 162 |
void onDocumentLink(const DocumentLinkParams &, |
| 163 |
Callback<std::vector<DocumentLink>>); |
| 164 |
void onSemanticTokens(const SemanticTokensParams &, Callback<SemanticTokens>); |
| 165 |
void onSemanticTokensDelta(const SemanticTokensDeltaParams &, |
| 166 |
Callback<SemanticTokensOrDelta>); |
| 167 |
|
| 168 |
|
| 169 |
void onMemoryUsage(const NoParams &, Callback<MemoryTree>); |
| 170 |
void onCommand(const ExecuteCommandParams &, Callback<llvm::json::Value>); |
| 171 |
|
| 172 |
|
| 173 |
void onCommandApplyEdit(const WorkspaceEdit &, Callback<llvm::json::Value>); |
| 174 |
void onCommandApplyTweak(const TweakArgs &, Callback<llvm::json::Value>); |
| 175 |
|
| 176 |
|
| 177 |
LSPBinder::OutgoingMethod<ApplyWorkspaceEditParams, |
| 178 |
ApplyWorkspaceEditResponse> |
| 179 |
ApplyWorkspaceEdit; |
| 180 |
LSPBinder::OutgoingNotification<ShowMessageParams> ShowMessage; |
| 181 |
LSPBinder::OutgoingNotification<PublishDiagnosticsParams> PublishDiagnostics; |
| 182 |
LSPBinder::OutgoingNotification<FileStatus> NotifyFileStatus; |
| 183 |
LSPBinder::OutgoingMethod<WorkDoneProgressCreateParams, std::nullptr_t> |
| 184 |
CreateWorkDoneProgress; |
| 185 |
LSPBinder::OutgoingNotification<ProgressParams<WorkDoneProgressBegin>> |
| 186 |
BeginWorkDoneProgress; |
| 187 |
LSPBinder::OutgoingNotification<ProgressParams<WorkDoneProgressReport>> |
| 188 |
ReportWorkDoneProgress; |
| 189 |
LSPBinder::OutgoingNotification<ProgressParams<WorkDoneProgressEnd>> |
| 190 |
EndWorkDoneProgress; |
| 191 |
LSPBinder::OutgoingMethod<NoParams, std::nullptr_t> SemanticTokensRefresh; |
| 192 |
|
| 193 |
void applyEdit(WorkspaceEdit WE, llvm::json::Value Success, |
| 194 |
Callback<llvm::json::Value> Reply); |
| 195 |
|
| 196 |
void bindMethods(LSPBinder &, const ClientCapabilities &Caps); |
| 197 |
std::vector<Fix> getFixes(StringRef File, const clangd::Diagnostic &D); |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
bool shouldRunCompletion(const CompletionParams &Params) const; |
| 204 |
|
| 205 |
void applyConfiguration(const ConfigurationSettings &Settings); |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
void maybeExportMemoryProfile(); |
| 210 |
PeriodicThrottler ShouldProfile; |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
void maybeCleanupMemory(); |
| 215 |
PeriodicThrottler ShouldCleanupMemory; |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
Context BackgroundContext; |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
bool ShutdownRequestReceived = false; |
| 226 |
|
| 227 |
|
| 228 |
std::atomic<bool> IsBeingDestroyed = {false}; |
| 229 |
|
| 230 |
std::mutex FixItsMutex; |
| 231 |
typedef std::map<clangd::Diagnostic, std::vector<Fix>, LSPDiagnosticCompare> |
| 232 |
DiagnosticToReplacementMap; |
| 233 |
|
| 234 |
llvm::StringMap<DiagnosticToReplacementMap> FixItsMap; |
| 235 |
|
| 236 |
std::mutex SemanticTokensMutex; |
| 237 |
llvm::StringMap<SemanticTokens> LastSemanticTokens; |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
clangd::Transport &Transp; |
| 242 |
class MessageHandler; |
| 243 |
std::unique_ptr<MessageHandler> MsgHandler; |
| 244 |
std::mutex TranspWriter; |
| 245 |
|
| 246 |
void callMethod(StringRef Method, llvm::json::Value Params, |
| 247 |
Callback<llvm::json::Value> CB) override; |
| 248 |
void notify(StringRef Method, llvm::json::Value Params) override; |
| 249 |
|
| 250 |
LSPBinder::RawHandlers Handlers; |
| 251 |
|
| 252 |
const ThreadsafeFS &TFS; |
| 253 |
|
| 254 |
ClangdDiagnosticOptions DiagOpts; |
| 255 |
|
| 256 |
SymbolKindBitset SupportedSymbolKinds; |
| 257 |
|
| 258 |
CompletionItemKindBitset SupportedCompletionItemKinds; |
| 259 |
|
| 260 |
bool SupportsCodeAction = false; |
| 261 |
|
| 262 |
bool SupportsHierarchicalDocumentSymbol = false; |
| 263 |
|
| 264 |
bool SupportFileStatus = false; |
| 265 |
|
| 266 |
bool SupportsReferenceContainer = false; |
| 267 |
|
| 268 |
MarkupKind HoverContentFormat = MarkupKind::PlainText; |
| 269 |
|
| 270 |
bool SupportsOffsetsInSignatureHelp = false; |
| 271 |
std::mutex BackgroundIndexProgressMutex; |
| 272 |
enum class BackgroundIndexProgress { |
| 273 |
|
| 274 |
Unsupported, |
| 275 |
|
| 276 |
|
| 277 |
Empty, |
| 278 |
|
| 279 |
|
| 280 |
Creating, |
| 281 |
|
| 282 |
Live, |
| 283 |
} BackgroundIndexProgressState = BackgroundIndexProgress::Unsupported; |
| 284 |
|
| 285 |
|
| 286 |
BackgroundQueue::Stats PendingBackgroundIndexProgress; |
| 287 |
|
| 288 |
bool BackgroundIndexSkipCreate = false; |
| 289 |
|
| 290 |
Options Opts; |
| 291 |
|
| 292 |
std::unique_ptr<GlobalCompilationDatabase> BaseCDB; |
| 293 |
|
| 294 |
std::optional<OverlayCDB> CDB; |
| 295 |
|
| 296 |
std::optional<ClangdServer> Server; |
| 297 |
}; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
#endif |