2017-05-16 17:38:59 +08:00
|
|
|
//===--- 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"
|
2018-03-16 22:30:42 +08:00
|
|
|
#include "DraftStore.h"
|
[clangd] Implementation of workspace/symbol request
Summary:
This is a basic implementation of the "workspace/symbol" request which is
used to find symbols by a string query. Since this is similar to code completion
in terms of result, this implementation reuses the "fuzzyFind" in order to get
matches. For now, the scoring algorithm is the same as code completion and
improvements could be done in the future.
The index model doesn't contain quite enough symbols for this to cover
common symbols like methods, enum class enumerators, functions in unamed
namespaces, etc. The index model will be augmented separately to achieve this.
Reviewers: sammccall, ilya-biryukov
Reviewed By: sammccall
Subscribers: jkorous, hokein, simark, sammccall, klimek, mgorny, ilya-biryukov, mgrang, jkorous-apple, ioeric, MaskRay, cfe-commits
Differential Revision: https://reviews.llvm.org/D44882
llvm-svn: 330637
2018-04-24 04:00:52 +08:00
|
|
|
#include "FindSymbols.h"
|
2017-06-13 23:59:43 +08:00
|
|
|
#include "GlobalCompilationDatabase.h"
|
2017-05-16 17:38:59 +08:00
|
|
|
#include "Path.h"
|
|
|
|
#include "Protocol.h"
|
2017-09-30 18:08:52 +08:00
|
|
|
#include "ProtocolHandlers.h"
|
2017-05-16 17:38:59 +08:00
|
|
|
#include "clang/Tooling/Core/Replacement.h"
|
2017-07-19 23:43:35 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2017-05-16 17:38:59 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
|
|
|
|
class JSONOutput;
|
2018-01-10 22:44:34 +08:00
|
|
|
class SymbolIndex;
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
/// This class provides implementation of an LSP server, glueing the JSON
|
|
|
|
/// dispatch and ClangdServer together.
|
2017-09-30 18:08:52 +08:00
|
|
|
class ClangdLSPServer : private DiagnosticsConsumer, private ProtocolCallbacks {
|
2017-05-16 17:38:59 +08:00
|
|
|
public:
|
2017-10-02 23:13:20 +08:00
|
|
|
/// 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.
|
2018-03-06 01:28:54 +08:00
|
|
|
ClangdLSPServer(JSONOutput &Out, const clangd::CodeCompleteOptions &CCOpts,
|
2017-12-20 02:00:37 +08:00
|
|
|
llvm::Optional<Path> CompileCommandsDir,
|
2018-08-02 01:39:29 +08:00
|
|
|
bool ShouldUseInMemoryCDB, const ClangdServer::Options &Opts);
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
/// 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.
|
2017-10-25 16:45:41 +08:00
|
|
|
///
|
2018-06-05 17:34:46 +08:00
|
|
|
/// \return Whether we received a 'shutdown' request before an 'exit' request.
|
|
|
|
bool run(std::FILE *In,
|
2018-02-06 18:47:30 +08:00
|
|
|
JSONStreamStyle InputStyle = JSONStreamStyle::Standard);
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
private:
|
2017-09-30 18:08:52 +08:00
|
|
|
// Implement DiagnosticsConsumer.
|
2018-03-13 07:22:35 +08:00
|
|
|
void onDiagnosticsReady(PathRef File, std::vector<Diag> Diagnostics) override;
|
2017-09-30 18:08:52 +08:00
|
|
|
|
|
|
|
// Implement ProtocolCallbacks.
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
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;
|
2017-10-12 21:29:58 +08:00
|
|
|
void
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
onDocumentOnTypeFormatting(DocumentOnTypeFormattingParams &Params) override;
|
2017-10-12 21:29:58 +08:00
|
|
|
void
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
onDocumentRangeFormatting(DocumentRangeFormattingParams &Params) override;
|
|
|
|
void onDocumentFormatting(DocumentFormattingParams &Params) override;
|
2018-07-06 03:35:01 +08:00
|
|
|
void onDocumentSymbol(DocumentSymbolParams &Params) override;
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
void onCodeAction(CodeActionParams &Params) override;
|
|
|
|
void onCompletion(TextDocumentPositionParams &Params) override;
|
|
|
|
void onSignatureHelp(TextDocumentPositionParams &Params) override;
|
|
|
|
void onGoToDefinition(TextDocumentPositionParams &Params) override;
|
|
|
|
void onSwitchSourceHeader(TextDocumentIdentifier &Params) override;
|
|
|
|
void onDocumentHighlight(TextDocumentPositionParams &Params) override;
|
|
|
|
void onFileEvent(DidChangeWatchedFilesParams &Params) override;
|
|
|
|
void onCommand(ExecuteCommandParams &Params) override;
|
[clangd] Implementation of workspace/symbol request
Summary:
This is a basic implementation of the "workspace/symbol" request which is
used to find symbols by a string query. Since this is similar to code completion
in terms of result, this implementation reuses the "fuzzyFind" in order to get
matches. For now, the scoring algorithm is the same as code completion and
improvements could be done in the future.
The index model doesn't contain quite enough symbols for this to cover
common symbols like methods, enum class enumerators, functions in unamed
namespaces, etc. The index model will be augmented separately to achieve this.
Reviewers: sammccall, ilya-biryukov
Reviewed By: sammccall
Subscribers: jkorous, hokein, simark, sammccall, klimek, mgorny, ilya-biryukov, mgrang, jkorous-apple, ioeric, MaskRay, cfe-commits
Differential Revision: https://reviews.llvm.org/D44882
llvm-svn: 330637
2018-04-24 04:00:52 +08:00
|
|
|
void onWorkspaceSymbol(WorkspaceSymbolParams &Params) override;
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
void onRename(RenameParams &Parames) override;
|
[clangd] Implement textDocument/hover
Summary: Implemention of textDocument/hover as described in LSP definition.
This patch adds a basic Hover implementation. When hovering a variable,
function, method or namespace, clangd will return a text containing the
declaration's scope, as well as the declaration of the hovered entity.
For example, for a variable:
Declared in class Foo::Bar
int hello = 2
For macros, the macro definition is returned.
This patch doesn't include:
- markdown support (the client I use doesn't support it yet)
- range support (optional in the Hover response)
- comments associated to variables/functions/classes
They are kept as future work to keep this patch simpler.
I added tests in XRefsTests.cpp. hover.test contains one simple
smoketest to make sure the feature works from a black box perspective.
Reviewers: malaperle, krasimir, bkramer, ilya-biryukov
Subscribers: sammccall, mgrang, klimek, rwols, ilya-biryukov, arphaman, cfe-commits
Differential Revision: https://reviews.llvm.org/D35894
Signed-off-by: Simon Marchi <simon.marchi@ericsson.com>
Signed-off-by: William Enright <william.enright@polymtl.ca>
llvm-svn: 325395
2018-02-17 05:38:15 +08:00
|
|
|
void onHover(TextDocumentPositionParams &Params) override;
|
[clangd] DidChangeConfiguration Notification
Summary:
Implementation of DidChangeConfiguration notification handling in
clangd. This currently only supports changing one setting: the path of
the compilation database to be used for the current project. In other
words, it is no longer necessary to restart clangd with a different
command line argument in order to change the compilation database.
Reviewers: malaperle, krasimir, bkramer, ilya-biryukov
Subscribers: jkorous-apple, ioeric, simark, klimek, ilya-biryukov, arphaman, rwols, cfe-commits
Differential Revision: https://reviews.llvm.org/D39571
Signed-off-by: Simon Marchi <simon.marchi@ericsson.com>
Signed-off-by: William Enright <william.enright@polymtl.ca>
llvm-svn: 325784
2018-02-22 22:00:39 +08:00
|
|
|
void onChangeConfiguration(DidChangeConfigurationParams &Params) override;
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2018-03-12 23:28:22 +08:00
|
|
|
std::vector<Fix> getFixes(StringRef File, const clangd::Diagnostic &D);
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2018-03-16 22:30:42 +08:00
|
|
|
/// 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();
|
2018-08-01 19:28:49 +08:00
|
|
|
void applyConfiguration(const ClangdConfigurationParamsChange &Settings);
|
2018-03-16 22:30:42 +08:00
|
|
|
|
2017-05-16 17:38:59 +08:00
|
|
|
JSONOutput &Out;
|
2017-05-16 22:40:30 +08:00
|
|
|
/// Used to indicate that the 'shutdown' request was received from the
|
|
|
|
/// Language Server client.
|
2017-10-25 16:45:41 +08:00
|
|
|
bool ShutdownRequestReceived = false;
|
|
|
|
|
|
|
|
/// Used to indicate that the 'exit' notification was received from the
|
|
|
|
/// Language Server client.
|
2017-05-16 22:40:30 +08:00
|
|
|
/// It's used to break out of the LSP parsing loop.
|
|
|
|
bool IsDone = false;
|
2017-05-16 17:38:59 +08:00
|
|
|
|
|
|
|
std::mutex FixItsMutex;
|
2018-03-12 23:28:22 +08:00
|
|
|
typedef std::map<clangd::Diagnostic, std::vector<Fix>, LSPDiagnosticCompare>
|
2017-05-16 17:38:59 +08:00
|
|
|
DiagnosticToReplacementMap;
|
|
|
|
/// Caches FixIts per file and diagnostics
|
|
|
|
llvm::StringMap<DiagnosticToReplacementMap> FixItsMap;
|
2017-06-13 23:59:43 +08:00
|
|
|
|
2018-08-02 01:39:29 +08:00
|
|
|
/// 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;
|
|
|
|
};
|
|
|
|
|
2017-06-13 23:59:43 +08:00
|
|
|
// Various ClangdServer parameters go here. It's important they're created
|
|
|
|
// before ClangdServer.
|
2018-08-02 01:39:29 +08:00
|
|
|
CompilationDB CDB;
|
2018-06-13 17:20:41 +08:00
|
|
|
|
2017-06-13 23:59:43 +08:00
|
|
|
RealFileSystemProvider FSProvider;
|
2017-12-05 18:42:57 +08:00
|
|
|
/// Options used for code completion
|
|
|
|
clangd::CodeCompleteOptions CCOpts;
|
[clangd] Implementation of workspace/symbol request
Summary:
This is a basic implementation of the "workspace/symbol" request which is
used to find symbols by a string query. Since this is similar to code completion
in terms of result, this implementation reuses the "fuzzyFind" in order to get
matches. For now, the scoring algorithm is the same as code completion and
improvements could be done in the future.
The index model doesn't contain quite enough symbols for this to cover
common symbols like methods, enum class enumerators, functions in unamed
namespaces, etc. The index model will be augmented separately to achieve this.
Reviewers: sammccall, ilya-biryukov
Reviewed By: sammccall
Subscribers: jkorous, hokein, simark, sammccall, klimek, mgorny, ilya-biryukov, mgrang, jkorous-apple, ioeric, MaskRay, cfe-commits
Differential Revision: https://reviews.llvm.org/D44882
llvm-svn: 330637
2018-04-24 04:00:52 +08:00
|
|
|
/// The supported kinds of the client.
|
|
|
|
SymbolKindBitset SupportedSymbolKinds;
|
2018-03-16 22:30:42 +08:00
|
|
|
|
|
|
|
// Store of the current versions of the open documents.
|
|
|
|
DraftStore DraftMgr;
|
|
|
|
|
2017-05-16 17:38:59 +08:00
|
|
|
// 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;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|
|
|
|
|
|
|
|
#endif
|