clang-tools  7.0.0
ClangdLSPServer.h
Go to the documentation of this file.
1 //===--- ClangdLSPServer.h - 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 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDLSPSERVER_H
11 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDLSPSERVER_H
12 
13 #include "ClangdServer.h"
14 #include "DraftStore.h"
15 #include "FindSymbols.h"
17 #include "Path.h"
18 #include "Protocol.h"
19 #include "ProtocolHandlers.h"
20 #include "clang/Tooling/Core/Replacement.h"
21 #include "llvm/ADT/Optional.h"
22 
23 namespace clang {
24 namespace clangd {
25 
26 class JSONOutput;
27 class SymbolIndex;
28 
29 /// This class provides implementation of an LSP server, glueing the JSON
30 /// dispatch and ClangdServer together.
32 public:
33  /// If \p CompileCommandsDir has a value, compile_commands.json will be
34  /// loaded only from \p CompileCommandsDir. Otherwise, clangd will look
35  /// for compile_commands.json in all parent directories of each file.
37  llvm::Optional<Path> CompileCommandsDir,
38  const ClangdServer::Options &Opts);
39 
40  /// Run LSP server loop, receiving input for it from \p In. \p In must be
41  /// opened in binary mode. Output will be written using Out variable passed to
42  /// class constructor. This method must not be executed more than once for
43  /// each instance of ClangdLSPServer.
44  ///
45  /// \return Whether we received a 'shutdown' request before an 'exit' request.
46  bool run(std::FILE *In,
48 
49 private:
50  // Implement DiagnosticsConsumer.
51  void onDiagnosticsReady(PathRef File, std::vector<Diag> Diagnostics) override;
52 
53  // Implement ProtocolCallbacks.
54  void onInitialize(InitializeParams &Params) override;
55  void onShutdown(ShutdownParams &Params) override;
56  void onExit(ExitParams &Params) override;
57  void onDocumentDidOpen(DidOpenTextDocumentParams &Params) override;
58  void onDocumentDidChange(DidChangeTextDocumentParams &Params) override;
59  void onDocumentDidClose(DidCloseTextDocumentParams &Params) override;
60  void
61  onDocumentOnTypeFormatting(DocumentOnTypeFormattingParams &Params) override;
62  void
63  onDocumentRangeFormatting(DocumentRangeFormattingParams &Params) override;
64  void onDocumentFormatting(DocumentFormattingParams &Params) override;
65  void onDocumentSymbol(DocumentSymbolParams &Params) override;
66  void onCodeAction(CodeActionParams &Params) override;
67  void onCompletion(TextDocumentPositionParams &Params) override;
68  void onSignatureHelp(TextDocumentPositionParams &Params) override;
69  void onGoToDefinition(TextDocumentPositionParams &Params) override;
70  void onSwitchSourceHeader(TextDocumentIdentifier &Params) override;
71  void onDocumentHighlight(TextDocumentPositionParams &Params) override;
72  void onFileEvent(DidChangeWatchedFilesParams &Params) override;
73  void onCommand(ExecuteCommandParams &Params) override;
74  void onWorkspaceSymbol(WorkspaceSymbolParams &Params) override;
75  void onRename(RenameParams &Parames) override;
76  void onHover(TextDocumentPositionParams &Params) override;
77  void onChangeConfiguration(DidChangeConfigurationParams &Params) override;
78 
79  std::vector<Fix> getFixes(StringRef File, const clangd::Diagnostic &D);
80 
81  /// Forces a reparse of all currently opened files. As a result, this method
82  /// may be very expensive. This method is normally called when the
83  /// compilation database is changed.
84  void reparseOpenedFiles();
85  void applyConfiguration(const ClangdConfigurationParamsChange &Settings);
86 
87  JSONOutput &Out;
88  /// Used to indicate that the 'shutdown' request was received from the
89  /// Language Server client.
90  bool ShutdownRequestReceived = false;
91 
92  /// Used to indicate that the 'exit' notification was received from the
93  /// Language Server client.
94  /// It's used to break out of the LSP parsing loop.
95  bool IsDone = false;
96 
97  std::mutex FixItsMutex;
98  typedef std::map<clangd::Diagnostic, std::vector<Fix>, LSPDiagnosticCompare>
99  DiagnosticToReplacementMap;
100  /// Caches FixIts per file and diagnostics
101  llvm::StringMap<DiagnosticToReplacementMap> FixItsMap;
102 
103  // Various ClangdServer parameters go here. It's important they're created
104  // before ClangdServer.
107 
108  RealFileSystemProvider FSProvider;
109  /// Options used for code completion
111  /// The supported kinds of the client.
112  SymbolKindBitset SupportedSymbolKinds;
113 
114  // Store of the current versions of the open documents.
115  DraftStore DraftMgr;
116 
117  // Server must be the last member of the class to allow its destructor to exit
118  // the worker thread that may otherwise run an async callback on partially
119  // destructed instance of ClangdLSPServer.
120  ClangdServer Server;
121 };
122 
123 } // namespace clangd
124 } // namespace clang
125 
126 #endif
JSONStreamStyle
Controls the way JSON-RPC messages are encoded (both input and output).
Encapsulates output and logs streams and provides thread-safe access to them.
Exact commands are not specified in the protocol so we define the ones supported by Clangd here...
Definition: Protocol.h:565
Clangd extension to set clangd-specific "initializationOptions" in the "initialize" request and for t...
Definition: Protocol.h:328
ClangdLSPServer(JSONOutput &Out, const clangd::CodeCompleteOptions &CCOpts, llvm::Optional< Path > CompileCommandsDir, const ClangdServer::Options &Opts)
If CompileCommandsDir has a value, compile_commands.json will be loaded only from CompileCommandsDir...
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:24
bool run(std::FILE *In, JSONStreamStyle InputStyle=JSONStreamStyle::Standard)
Run LSP server loop, receiving input for it from In.
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."))
A LSP-specific comparator used to find diagnostic in a container like std:map.
Definition: Protocol.h:521
Gets compile args from tooling::CompilationDatabases built for parent directories.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
std::bitset< SymbolKindMax+1 > SymbolKindBitset
Definition: Protocol.h:278
Encoding per the LSP specification, with mandatory Content-Length header.
A thread-safe container for files opened in a workspace, addressed by filenames.
Definition: DraftStore.h:27
The parameters of a Workspace Symbol Request.
Definition: Protocol.h:602
Provides API to manage ASTs for a collection of C++ files and request various language features...
Definition: ClangdServer.h:50
This class provides implementation of an LSP server, glueing the JSON dispatch and ClangdServer toget...
A wrapper around GlobalCompilationDatabase that caches the compile commands.
static llvm::cl::opt< JSONStreamStyle > InputStyle("input-style", llvm::cl::desc("Input JSON stream encoding"), llvm::cl::values(clEnumValN(JSONStreamStyle::Standard, "standard", "usual LSP protocol"), clEnumValN(JSONStreamStyle::Delimited, "delimited", "messages delimited by --- lines, with # comment support")), llvm::cl::init(JSONStreamStyle::Standard))