2017-05-16 17:38:59 +08:00
|
|
|
//===--- ClangdLSPServer.cpp - LSP server ------------------------*- C++-*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
2018-08-15 00:03:32 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2017-05-16 17:38:59 +08:00
|
|
|
|
|
|
|
#include "ClangdLSPServer.h"
|
2018-08-24 21:09:41 +08:00
|
|
|
#include "Cancellation.h"
|
2018-03-12 23:28:22 +08:00
|
|
|
#include "Diagnostics.h"
|
2017-05-16 17:38:59 +08:00
|
|
|
#include "JSONRPCDispatcher.h"
|
2017-12-19 20:23:48 +08:00
|
|
|
#include "SourceCode.h"
|
2018-01-29 23:37:46 +08:00
|
|
|
#include "URI.h"
|
2018-08-24 21:09:41 +08:00
|
|
|
#include "llvm/ADT/ScopeExit.h"
|
2018-03-16 22:30:42 +08:00
|
|
|
#include "llvm/Support/Errc.h"
|
[clangd] Handle clangd.applyFix server-side
Summary:
When the user selects a fix-it (or any code action with commands), it is
possible to let the client forward the selected command to the server.
When the clangd.applyFix command is handled on the server, it can send a
workspace/applyEdit request to the client. This has the advantage that
the client doesn't explicitly have to know how to handle
clangd.applyFix. Therefore, the code to handle clangd.applyFix in the VS
Code extension (and any other Clangd client) is not required anymore.
Reviewers: ilya-biryukov, sammccall, Nebiroth, hokein
Reviewed By: hokein
Subscribers: ioeric, hokein, rwols, puremourning, bkramer, ilya-biryukov
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D39276
llvm-svn: 317322
2017-11-03 21:39:15 +08:00
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
2018-02-01 00:26:27 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
[clangd] Handle clangd.applyFix server-side
Summary:
When the user selects a fix-it (or any code action with commands), it is
possible to let the client forward the selected command to the server.
When the clangd.applyFix command is handled on the server, it can send a
workspace/applyEdit request to the client. This has the advantage that
the client doesn't explicitly have to know how to handle
clangd.applyFix. Therefore, the code to handle clangd.applyFix in the VS
Code extension (and any other Clangd client) is not required anymore.
Reviewers: ilya-biryukov, sammccall, Nebiroth, hokein
Reviewed By: hokein
Subscribers: ioeric, hokein, rwols, puremourning, bkramer, ilya-biryukov
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D39276
llvm-svn: 317322
2017-11-03 21:39:15 +08:00
|
|
|
|
2017-05-16 17:38:59 +08:00
|
|
|
using namespace clang::clangd;
|
|
|
|
using namespace clang;
|
2018-07-09 22:25:59 +08:00
|
|
|
using namespace llvm;
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
namespace {
|
|
|
|
|
2018-02-01 00:26:27 +08:00
|
|
|
/// \brief Supports a test URI scheme with relaxed constraints for lit tests.
|
|
|
|
/// The path in a test URI will be combined with a platform-specific fake
|
|
|
|
/// directory to form an absolute path. For example, test:///a.cpp is resolved
|
|
|
|
/// C:\clangd-test\a.cpp on Windows and /clangd-test/a.cpp on Unix.
|
|
|
|
class TestScheme : public URIScheme {
|
|
|
|
public:
|
|
|
|
llvm::Expected<std::string>
|
|
|
|
getAbsolutePath(llvm::StringRef /*Authority*/, llvm::StringRef Body,
|
|
|
|
llvm::StringRef /*HintPath*/) const override {
|
|
|
|
using namespace llvm::sys;
|
|
|
|
// Still require "/" in body to mimic file scheme, as we want lengths of an
|
|
|
|
// equivalent URI in both schemes to be the same.
|
|
|
|
if (!Body.startswith("/"))
|
|
|
|
return llvm::make_error<llvm::StringError>(
|
|
|
|
"Expect URI body to be an absolute path starting with '/': " + Body,
|
|
|
|
llvm::inconvertibleErrorCode());
|
|
|
|
Body = Body.ltrim('/');
|
2018-04-10 21:14:03 +08:00
|
|
|
#ifdef _WIN32
|
2018-02-01 00:26:27 +08:00
|
|
|
constexpr char TestDir[] = "C:\\clangd-test";
|
|
|
|
#else
|
|
|
|
constexpr char TestDir[] = "/clangd-test";
|
|
|
|
#endif
|
|
|
|
llvm::SmallVector<char, 16> Path(Body.begin(), Body.end());
|
|
|
|
path::native(Path);
|
|
|
|
auto Err = fs::make_absolute(TestDir, Path);
|
2018-02-01 20:44:52 +08:00
|
|
|
if (Err)
|
|
|
|
llvm_unreachable("Failed to make absolute path in test scheme.");
|
2018-02-01 00:26:27 +08:00
|
|
|
return std::string(Path.begin(), Path.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Expected<URI>
|
|
|
|
uriFromAbsolutePath(llvm::StringRef AbsolutePath) const override {
|
|
|
|
llvm_unreachable("Clangd must never create a test URI.");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static URISchemeRegistry::Add<TestScheme>
|
|
|
|
X("test", "Test scheme for clangd lit tests.");
|
|
|
|
|
[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
|
|
|
SymbolKindBitset defaultSymbolKinds() {
|
|
|
|
SymbolKindBitset Defaults;
|
|
|
|
for (size_t I = SymbolKindMin; I <= static_cast<size_t>(SymbolKind::Array);
|
|
|
|
++I)
|
|
|
|
Defaults.set(I);
|
|
|
|
return Defaults;
|
|
|
|
}
|
|
|
|
|
2018-08-24 21:09:41 +08:00
|
|
|
std::string NormalizeRequestID(const json::Value &ID) {
|
|
|
|
auto NormalizedID = parseNumberOrString(&ID);
|
|
|
|
assert(NormalizedID && "Was not able to parse request id.");
|
|
|
|
return std::move(*NormalizedID);
|
|
|
|
}
|
2017-05-16 22:40:30 +08:00
|
|
|
} // namespace
|
|
|
|
|
[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 ClangdLSPServer::onInitialize(InitializeParams &Params) {
|
2018-08-01 19:28:49 +08:00
|
|
|
if (Params.initializationOptions)
|
|
|
|
applyConfiguration(*Params.initializationOptions);
|
|
|
|
|
2018-02-16 20:20:47 +08:00
|
|
|
if (Params.rootUri && *Params.rootUri)
|
|
|
|
Server.setRootPath(Params.rootUri->file());
|
2018-02-15 22:32:57 +08:00
|
|
|
else if (Params.rootPath && !Params.rootPath->empty())
|
|
|
|
Server.setRootPath(*Params.rootPath);
|
|
|
|
|
|
|
|
CCOpts.EnableSnippets =
|
|
|
|
Params.capabilities.textDocument.completion.completionItem.snippetSupport;
|
2018-08-11 01:25:07 +08:00
|
|
|
DiagOpts.EmbedFixesInDiagnostics =
|
|
|
|
Params.capabilities.textDocument.publishDiagnostics.clangdFixSupport;
|
2018-08-23 04:30:06 +08:00
|
|
|
DiagOpts.SendDiagnosticCategory =
|
|
|
|
Params.capabilities.textDocument.publishDiagnostics.categorySupport;
|
2018-02-15 22:32:57 +08:00
|
|
|
|
[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
|
|
|
if (Params.capabilities.workspace && Params.capabilities.workspace->symbol &&
|
|
|
|
Params.capabilities.workspace->symbol->symbolKind) {
|
|
|
|
for (SymbolKind Kind :
|
|
|
|
*Params.capabilities.workspace->symbol->symbolKind->valueSet) {
|
|
|
|
SupportedSymbolKinds.set(static_cast<size_t>(Kind));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-09 22:25:59 +08:00
|
|
|
reply(json::Object{
|
2017-11-07 23:49:35 +08:00
|
|
|
{{"capabilities",
|
2018-07-09 22:25:59 +08:00
|
|
|
json::Object{
|
[clangd] Support incremental document syncing
Summary:
This patch adds support for incremental document syncing, as described
in the LSP spec. The protocol specifies ranges in terms of Position (a
line and a character), and our drafts are stored as plain strings. So I
see two things that may not be super efficient for very large files:
- Converting a Position to an offset (the positionToOffset function)
requires searching for end of lines until we reach the desired line.
- When we update a range, we construct a new string, which implies
copying the whole document.
However, for the typical size of a C++ document and the frequency of
update (at which a user types), it may not be an issue. This patch aims
at getting the basic feature in, and we can always improve it later if
we find it's too slow.
Signed-off-by: Simon Marchi <simon.marchi@ericsson.com>
Reviewers: malaperle, ilya-biryukov
Reviewed By: ilya-biryukov
Subscribers: MaskRay, klimek, mgorny, ilya-biryukov, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D44272
llvm-svn: 328500
2018-03-26 22:41:40 +08:00
|
|
|
{"textDocumentSync", (int)TextDocumentSyncKind::Incremental},
|
2017-11-07 23:49:35 +08:00
|
|
|
{"documentFormattingProvider", true},
|
|
|
|
{"documentRangeFormattingProvider", true},
|
|
|
|
{"documentOnTypeFormattingProvider",
|
2018-07-09 22:25:59 +08:00
|
|
|
json::Object{
|
2017-11-07 23:49:35 +08:00
|
|
|
{"firstTriggerCharacter", "}"},
|
|
|
|
{"moreTriggerCharacter", {}},
|
|
|
|
}},
|
|
|
|
{"codeActionProvider", true},
|
|
|
|
{"completionProvider",
|
2018-07-09 22:25:59 +08:00
|
|
|
json::Object{
|
2017-11-07 23:49:35 +08:00
|
|
|
{"resolveProvider", false},
|
|
|
|
{"triggerCharacters", {".", ">", ":"}},
|
|
|
|
}},
|
|
|
|
{"signatureHelpProvider",
|
2018-07-09 22:25:59 +08:00
|
|
|
json::Object{
|
2017-11-07 23:49:35 +08:00
|
|
|
{"triggerCharacters", {"(", ","}},
|
|
|
|
}},
|
|
|
|
{"definitionProvider", true},
|
[clangd] Document highlights for clangd
Summary: Implementation of Document Highlights Request as described in
LSP.
Contributed by William Enright (nebiroth).
Reviewers: malaperle, krasimir, bkramer, ilya-biryukov
Reviewed By: malaperle
Subscribers: mgrang, sammccall, klimek, ioeric, rwols, cfe-commits, arphaman, ilya-biryukov
Differential Revision: https://reviews.llvm.org/D38425
llvm-svn: 320474
2017-12-12 20:27:47 +08:00
|
|
|
{"documentHighlightProvider", true},
|
[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
|
|
|
{"hoverProvider", true},
|
2017-11-09 19:30:04 +08:00
|
|
|
{"renameProvider", true},
|
2018-07-06 03:35:01 +08:00
|
|
|
{"documentSymbolProvider", true},
|
[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
|
|
|
{"workspaceSymbolProvider", true},
|
2017-11-07 23:49:35 +08:00
|
|
|
{"executeCommandProvider",
|
2018-07-09 22:25:59 +08:00
|
|
|
json::Object{
|
2018-05-15 23:23:53 +08:00
|
|
|
{"commands", {ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND}},
|
2017-11-07 23:49:35 +08:00
|
|
|
}},
|
|
|
|
}}}});
|
2017-05-16 22:40:30 +08:00
|
|
|
}
|
|
|
|
|
[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 ClangdLSPServer::onShutdown(ShutdownParams &Params) {
|
2017-10-25 16:45:41 +08:00
|
|
|
// Do essentially nothing, just say we're ready to exit.
|
|
|
|
ShutdownRequestReceived = true;
|
[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
|
|
|
reply(nullptr);
|
2017-10-12 21:29:58 +08:00
|
|
|
}
|
2017-05-16 22:40:30 +08:00
|
|
|
|
[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 ClangdLSPServer::onExit(ExitParams &Params) { IsDone = true; }
|
2017-10-25 16:45:41 +08:00
|
|
|
|
[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 ClangdLSPServer::onDocumentDidOpen(DidOpenTextDocumentParams &Params) {
|
2018-03-16 22:30:42 +08:00
|
|
|
PathRef File = Params.textDocument.uri.file();
|
2018-08-02 01:39:29 +08:00
|
|
|
if (Params.metadata && !Params.metadata->extraFlags.empty())
|
|
|
|
CDB.setExtraFlagsForFile(File, std::move(Params.metadata->extraFlags));
|
2018-06-13 17:20:41 +08:00
|
|
|
|
2018-03-16 22:30:42 +08:00
|
|
|
std::string &Contents = Params.textDocument.text;
|
|
|
|
|
[clangd] Support incremental document syncing
Summary:
This patch adds support for incremental document syncing, as described
in the LSP spec. The protocol specifies ranges in terms of Position (a
line and a character), and our drafts are stored as plain strings. So I
see two things that may not be super efficient for very large files:
- Converting a Position to an offset (the positionToOffset function)
requires searching for end of lines until we reach the desired line.
- When we update a range, we construct a new string, which implies
copying the whole document.
However, for the typical size of a C++ document and the frequency of
update (at which a user types), it may not be an issue. This patch aims
at getting the basic feature in, and we can always improve it later if
we find it's too slow.
Signed-off-by: Simon Marchi <simon.marchi@ericsson.com>
Reviewers: malaperle, ilya-biryukov
Reviewed By: ilya-biryukov
Subscribers: MaskRay, klimek, mgorny, ilya-biryukov, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D44272
llvm-svn: 328500
2018-03-26 22:41:40 +08:00
|
|
|
DraftMgr.addDraft(File, Contents);
|
2018-03-16 22:30:42 +08:00
|
|
|
Server.addDocument(File, Contents, WantDiagnostics::Yes);
|
2017-05-16 22:40:30 +08:00
|
|
|
}
|
|
|
|
|
[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 ClangdLSPServer::onDocumentDidChange(DidChangeTextDocumentParams &Params) {
|
2018-02-23 02:40:39 +08:00
|
|
|
auto WantDiags = WantDiagnostics::Auto;
|
|
|
|
if (Params.wantDiagnostics.hasValue())
|
|
|
|
WantDiags = Params.wantDiagnostics.getValue() ? WantDiagnostics::Yes
|
|
|
|
: WantDiagnostics::No;
|
2018-03-16 22:30:42 +08:00
|
|
|
|
|
|
|
PathRef File = Params.textDocument.uri.file();
|
[clangd] Support incremental document syncing
Summary:
This patch adds support for incremental document syncing, as described
in the LSP spec. The protocol specifies ranges in terms of Position (a
line and a character), and our drafts are stored as plain strings. So I
see two things that may not be super efficient for very large files:
- Converting a Position to an offset (the positionToOffset function)
requires searching for end of lines until we reach the desired line.
- When we update a range, we construct a new string, which implies
copying the whole document.
However, for the typical size of a C++ document and the frequency of
update (at which a user types), it may not be an issue. This patch aims
at getting the basic feature in, and we can always improve it later if
we find it's too slow.
Signed-off-by: Simon Marchi <simon.marchi@ericsson.com>
Reviewers: malaperle, ilya-biryukov
Reviewed By: ilya-biryukov
Subscribers: MaskRay, klimek, mgorny, ilya-biryukov, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D44272
llvm-svn: 328500
2018-03-26 22:41:40 +08:00
|
|
|
llvm::Expected<std::string> Contents =
|
|
|
|
DraftMgr.updateDraft(File, Params.contentChanges);
|
|
|
|
if (!Contents) {
|
|
|
|
// If this fails, we are most likely going to be not in sync anymore with
|
|
|
|
// the client. It is better to remove the draft and let further operations
|
|
|
|
// fail rather than giving wrong results.
|
|
|
|
DraftMgr.removeDraft(File);
|
|
|
|
Server.removeDocument(File);
|
2018-06-13 17:20:41 +08:00
|
|
|
CDB.invalidate(File);
|
[clangd] Upgrade logging facilities with levels and formatv.
Summary:
log() is split into four functions:
- elog()/log()/vlog() have different severity levels, allowing filtering
- dlog() is a lazy macro which uses LLVM_DEBUG - it logs to the logger, but
conditionally based on -debug-only flag and is omitted in release builds
All logging functions use formatv-style format strings now, e.g:
log("Could not resolve URI {0}: {1}", URI, Result.takeError());
Existing log sites have been split between elog/log/vlog by best guess.
This includes a workaround for passing Error to formatv that can be
simplified when D49170 or similar lands.
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D49008
llvm-svn: 336785
2018-07-11 18:35:11 +08:00
|
|
|
elog("Failed to update {0}: {1}", File, Contents.takeError());
|
[clangd] Support incremental document syncing
Summary:
This patch adds support for incremental document syncing, as described
in the LSP spec. The protocol specifies ranges in terms of Position (a
line and a character), and our drafts are stored as plain strings. So I
see two things that may not be super efficient for very large files:
- Converting a Position to an offset (the positionToOffset function)
requires searching for end of lines until we reach the desired line.
- When we update a range, we construct a new string, which implies
copying the whole document.
However, for the typical size of a C++ document and the frequency of
update (at which a user types), it may not be an issue. This patch aims
at getting the basic feature in, and we can always improve it later if
we find it's too slow.
Signed-off-by: Simon Marchi <simon.marchi@ericsson.com>
Reviewers: malaperle, ilya-biryukov
Reviewed By: ilya-biryukov
Subscribers: MaskRay, klimek, mgorny, ilya-biryukov, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D44272
llvm-svn: 328500
2018-03-26 22:41:40 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-03-16 22:30:42 +08:00
|
|
|
|
[clangd] Support incremental document syncing
Summary:
This patch adds support for incremental document syncing, as described
in the LSP spec. The protocol specifies ranges in terms of Position (a
line and a character), and our drafts are stored as plain strings. So I
see two things that may not be super efficient for very large files:
- Converting a Position to an offset (the positionToOffset function)
requires searching for end of lines until we reach the desired line.
- When we update a range, we construct a new string, which implies
copying the whole document.
However, for the typical size of a C++ document and the frequency of
update (at which a user types), it may not be an issue. This patch aims
at getting the basic feature in, and we can always improve it later if
we find it's too slow.
Signed-off-by: Simon Marchi <simon.marchi@ericsson.com>
Reviewers: malaperle, ilya-biryukov
Reviewed By: ilya-biryukov
Subscribers: MaskRay, klimek, mgorny, ilya-biryukov, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D44272
llvm-svn: 328500
2018-03-26 22:41:40 +08:00
|
|
|
Server.addDocument(File, *Contents, WantDiags);
|
2017-05-16 22:40:30 +08:00
|
|
|
}
|
|
|
|
|
[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 ClangdLSPServer::onFileEvent(DidChangeWatchedFilesParams &Params) {
|
2017-10-03 02:00:37 +08:00
|
|
|
Server.onFileEvent(Params);
|
|
|
|
}
|
|
|
|
|
[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 ClangdLSPServer::onCommand(ExecuteCommandParams &Params) {
|
2018-02-16 22:15:55 +08:00
|
|
|
auto ApplyEdit = [](WorkspaceEdit WE) {
|
|
|
|
ApplyWorkspaceEditParams Edit;
|
|
|
|
Edit.edit = std::move(WE);
|
|
|
|
// We don't need the response so id == 1 is OK.
|
|
|
|
// Ideally, we would wait for the response and if there is no error, we
|
|
|
|
// would reply success/failure to the original RPC.
|
|
|
|
call("workspace/applyEdit", Edit);
|
|
|
|
};
|
[clangd] Handle clangd.applyFix server-side
Summary:
When the user selects a fix-it (or any code action with commands), it is
possible to let the client forward the selected command to the server.
When the clangd.applyFix command is handled on the server, it can send a
workspace/applyEdit request to the client. This has the advantage that
the client doesn't explicitly have to know how to handle
clangd.applyFix. Therefore, the code to handle clangd.applyFix in the VS
Code extension (and any other Clangd client) is not required anymore.
Reviewers: ilya-biryukov, sammccall, Nebiroth, hokein
Reviewed By: hokein
Subscribers: ioeric, hokein, rwols, puremourning, bkramer, ilya-biryukov
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D39276
llvm-svn: 317322
2017-11-03 21:39:15 +08:00
|
|
|
if (Params.command == ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND &&
|
|
|
|
Params.workspaceEdit) {
|
|
|
|
// The flow for "apply-fix" :
|
|
|
|
// 1. We publish a diagnostic, including fixits
|
|
|
|
// 2. The user clicks on the diagnostic, the editor asks us for code actions
|
|
|
|
// 3. We send code actions, with the fixit embedded as context
|
|
|
|
// 4. The user selects the fixit, the editor asks us to apply it
|
|
|
|
// 5. We unwrap the changes and send them back to the editor
|
|
|
|
// 6. The editor applies the changes (applyEdit), and sends us a reply (but
|
|
|
|
// we ignore it)
|
|
|
|
|
[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
|
|
|
reply("Fix applied.");
|
2018-02-16 22:15:55 +08:00
|
|
|
ApplyEdit(*Params.workspaceEdit);
|
[clangd] Handle clangd.applyFix server-side
Summary:
When the user selects a fix-it (or any code action with commands), it is
possible to let the client forward the selected command to the server.
When the clangd.applyFix command is handled on the server, it can send a
workspace/applyEdit request to the client. This has the advantage that
the client doesn't explicitly have to know how to handle
clangd.applyFix. Therefore, the code to handle clangd.applyFix in the VS
Code extension (and any other Clangd client) is not required anymore.
Reviewers: ilya-biryukov, sammccall, Nebiroth, hokein
Reviewed By: hokein
Subscribers: ioeric, hokein, rwols, puremourning, bkramer, ilya-biryukov
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D39276
llvm-svn: 317322
2017-11-03 21:39:15 +08:00
|
|
|
} else {
|
|
|
|
// We should not get here because ExecuteCommandParams would not have
|
|
|
|
// parsed in the first place and this handler should not be called. But if
|
|
|
|
// more commands are added, this will be here has a safe guard.
|
2017-12-13 20:51:22 +08:00
|
|
|
replyError(
|
[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
|
|
|
ErrorCode::InvalidParams,
|
2017-11-07 18:21:02 +08:00
|
|
|
llvm::formatv("Unsupported command \"{0}\".", Params.command).str());
|
[clangd] Handle clangd.applyFix server-side
Summary:
When the user selects a fix-it (or any code action with commands), it is
possible to let the client forward the selected command to the server.
When the clangd.applyFix command is handled on the server, it can send a
workspace/applyEdit request to the client. This has the advantage that
the client doesn't explicitly have to know how to handle
clangd.applyFix. Therefore, the code to handle clangd.applyFix in the VS
Code extension (and any other Clangd client) is not required anymore.
Reviewers: ilya-biryukov, sammccall, Nebiroth, hokein
Reviewed By: hokein
Subscribers: ioeric, hokein, rwols, puremourning, bkramer, ilya-biryukov
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D39276
llvm-svn: 317322
2017-11-03 21:39:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[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 ClangdLSPServer::onWorkspaceSymbol(WorkspaceSymbolParams &Params) {
|
|
|
|
Server.workspaceSymbols(
|
|
|
|
Params.query, CCOpts.Limit,
|
|
|
|
[this](llvm::Expected<std::vector<SymbolInformation>> Items) {
|
|
|
|
if (!Items)
|
|
|
|
return replyError(ErrorCode::InternalError,
|
|
|
|
llvm::toString(Items.takeError()));
|
|
|
|
for (auto &Sym : *Items)
|
|
|
|
Sym.kind = adjustKindToCapability(Sym.kind, SupportedSymbolKinds);
|
|
|
|
|
2018-07-09 22:25:59 +08:00
|
|
|
reply(json::Array(*Items));
|
[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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
[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 ClangdLSPServer::onRename(RenameParams &Params) {
|
2018-02-16 20:20:47 +08:00
|
|
|
Path File = Params.textDocument.uri.file();
|
2018-03-16 22:30:42 +08:00
|
|
|
llvm::Optional<std::string> Code = DraftMgr.getDraft(File);
|
2018-01-17 20:30:24 +08:00
|
|
|
if (!Code)
|
[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
|
|
|
return replyError(ErrorCode::InvalidParams,
|
2018-01-17 20:30:24 +08:00
|
|
|
"onRename called for non-added file");
|
|
|
|
|
2018-02-15 21:15:47 +08:00
|
|
|
Server.rename(
|
|
|
|
File, Params.position, Params.newName,
|
|
|
|
[File, Code,
|
|
|
|
Params](llvm::Expected<std::vector<tooling::Replacement>> Replacements) {
|
|
|
|
if (!Replacements)
|
|
|
|
return replyError(ErrorCode::InternalError,
|
|
|
|
llvm::toString(Replacements.takeError()));
|
|
|
|
|
2018-05-11 20:12:08 +08:00
|
|
|
// Turn the replacements into the format specified by the Language
|
|
|
|
// Server Protocol. Fuse them into one big JSON array.
|
|
|
|
std::vector<TextEdit> Edits;
|
|
|
|
for (const auto &R : *Replacements)
|
|
|
|
Edits.push_back(replacementToEdit(*Code, R));
|
2018-02-15 21:15:47 +08:00
|
|
|
WorkspaceEdit WE;
|
|
|
|
WE.changes = {{Params.textDocument.uri.uri(), Edits}};
|
|
|
|
reply(WE);
|
|
|
|
});
|
2017-11-09 19:30:04 +08:00
|
|
|
}
|
|
|
|
|
[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 ClangdLSPServer::onDocumentDidClose(DidCloseTextDocumentParams &Params) {
|
2018-03-16 22:30:42 +08:00
|
|
|
PathRef File = Params.textDocument.uri.file();
|
|
|
|
DraftMgr.removeDraft(File);
|
|
|
|
Server.removeDocument(File);
|
2018-08-02 01:39:29 +08:00
|
|
|
CDB.invalidate(File);
|
2017-05-16 22:40:30 +08:00
|
|
|
}
|
|
|
|
|
2017-09-30 18:08:52 +08:00
|
|
|
void ClangdLSPServer::onDocumentOnTypeFormatting(
|
[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
|
|
|
DocumentOnTypeFormattingParams &Params) {
|
2018-02-16 20:20:47 +08:00
|
|
|
auto File = Params.textDocument.uri.file();
|
2018-03-16 22:30:42 +08:00
|
|
|
auto Code = DraftMgr.getDraft(File);
|
2018-01-17 20:30:24 +08:00
|
|
|
if (!Code)
|
[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
|
|
|
return replyError(ErrorCode::InvalidParams,
|
2018-01-17 20:30:24 +08:00
|
|
|
"onDocumentOnTypeFormatting called for non-added file");
|
|
|
|
|
|
|
|
auto ReplacementsOrError = Server.formatOnType(*Code, File, Params.position);
|
2017-12-13 04:25:06 +08:00
|
|
|
if (ReplacementsOrError)
|
2018-07-09 22:25:59 +08:00
|
|
|
reply(json::Array(replacementsToEdits(*Code, ReplacementsOrError.get())));
|
2017-12-13 04:25:06 +08:00
|
|
|
else
|
[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
|
|
|
replyError(ErrorCode::UnknownErrorCode,
|
2017-12-13 20:51:22 +08:00
|
|
|
llvm::toString(ReplacementsOrError.takeError()));
|
2017-05-16 22:40:30 +08:00
|
|
|
}
|
|
|
|
|
2017-09-30 18:08:52 +08:00
|
|
|
void ClangdLSPServer::onDocumentRangeFormatting(
|
[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
|
|
|
DocumentRangeFormattingParams &Params) {
|
2018-02-16 20:20:47 +08:00
|
|
|
auto File = Params.textDocument.uri.file();
|
2018-03-16 22:30:42 +08:00
|
|
|
auto Code = DraftMgr.getDraft(File);
|
2018-01-17 20:30:24 +08:00
|
|
|
if (!Code)
|
[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
|
|
|
return replyError(ErrorCode::InvalidParams,
|
2018-01-17 20:30:24 +08:00
|
|
|
"onDocumentRangeFormatting called for non-added file");
|
|
|
|
|
|
|
|
auto ReplacementsOrError = Server.formatRange(*Code, File, Params.range);
|
2017-12-13 04:25:06 +08:00
|
|
|
if (ReplacementsOrError)
|
2018-07-09 22:25:59 +08:00
|
|
|
reply(json::Array(replacementsToEdits(*Code, ReplacementsOrError.get())));
|
2017-12-13 04:25:06 +08:00
|
|
|
else
|
[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
|
|
|
replyError(ErrorCode::UnknownErrorCode,
|
2017-12-13 20:51:22 +08:00
|
|
|
llvm::toString(ReplacementsOrError.takeError()));
|
2017-05-16 22:40:30 +08:00
|
|
|
}
|
|
|
|
|
[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 ClangdLSPServer::onDocumentFormatting(DocumentFormattingParams &Params) {
|
2018-02-16 20:20:47 +08:00
|
|
|
auto File = Params.textDocument.uri.file();
|
2018-03-16 22:30:42 +08:00
|
|
|
auto Code = DraftMgr.getDraft(File);
|
2018-01-17 20:30:24 +08:00
|
|
|
if (!Code)
|
[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
|
|
|
return replyError(ErrorCode::InvalidParams,
|
2018-01-17 20:30:24 +08:00
|
|
|
"onDocumentFormatting called for non-added file");
|
|
|
|
|
|
|
|
auto ReplacementsOrError = Server.formatFile(*Code, File);
|
2017-12-13 04:25:06 +08:00
|
|
|
if (ReplacementsOrError)
|
2018-07-09 22:25:59 +08:00
|
|
|
reply(json::Array(replacementsToEdits(*Code, ReplacementsOrError.get())));
|
2017-12-13 04:25:06 +08:00
|
|
|
else
|
[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
|
|
|
replyError(ErrorCode::UnknownErrorCode,
|
2017-12-13 20:51:22 +08:00
|
|
|
llvm::toString(ReplacementsOrError.takeError()));
|
2017-05-16 22:40:30 +08:00
|
|
|
}
|
|
|
|
|
2018-07-06 03:35:01 +08:00
|
|
|
void ClangdLSPServer::onDocumentSymbol(DocumentSymbolParams &Params) {
|
|
|
|
Server.documentSymbols(
|
|
|
|
Params.textDocument.uri.file(),
|
|
|
|
[this](llvm::Expected<std::vector<SymbolInformation>> Items) {
|
|
|
|
if (!Items)
|
|
|
|
return replyError(ErrorCode::InvalidParams,
|
|
|
|
llvm::toString(Items.takeError()));
|
|
|
|
for (auto &Sym : *Items)
|
|
|
|
Sym.kind = adjustKindToCapability(Sym.kind, SupportedSymbolKinds);
|
2018-07-09 22:25:59 +08:00
|
|
|
reply(json::Array(*Items));
|
2018-07-06 03:35:01 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
[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 ClangdLSPServer::onCodeAction(CodeActionParams &Params) {
|
2017-05-16 22:40:30 +08:00
|
|
|
// We provide a code action for each diagnostic at the requested location
|
|
|
|
// which has FixIts available.
|
2018-03-16 22:30:42 +08:00
|
|
|
auto Code = DraftMgr.getDraft(Params.textDocument.uri.file());
|
2018-01-17 20:30:24 +08:00
|
|
|
if (!Code)
|
[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
|
|
|
return replyError(ErrorCode::InvalidParams,
|
2018-01-17 20:30:24 +08:00
|
|
|
"onCodeAction called for non-added file");
|
|
|
|
|
2018-07-09 22:25:59 +08:00
|
|
|
json::Array Commands;
|
2017-05-16 22:40:30 +08:00
|
|
|
for (Diagnostic &D : Params.context.diagnostics) {
|
2018-03-12 23:28:22 +08:00
|
|
|
for (auto &F : getFixes(Params.textDocument.uri.file(), D)) {
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
WorkspaceEdit WE;
|
2018-03-12 23:28:22 +08:00
|
|
|
std::vector<TextEdit> Edits(F.Edits.begin(), F.Edits.end());
|
2018-01-29 23:37:46 +08:00
|
|
|
WE.changes = {{Params.textDocument.uri.uri(), std::move(Edits)}};
|
2018-07-09 22:25:59 +08:00
|
|
|
Commands.push_back(json::Object{
|
2018-03-12 23:28:22 +08:00
|
|
|
{"title", llvm::formatv("Apply fix: {0}", F.Message)},
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
{"command", ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND},
|
|
|
|
{"arguments", {WE}},
|
|
|
|
});
|
|
|
|
}
|
2017-05-16 22:40:30 +08:00
|
|
|
}
|
[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
|
|
|
reply(std::move(Commands));
|
2017-05-16 22:40:30 +08:00
|
|
|
}
|
|
|
|
|
[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 ClangdLSPServer::onCompletion(TextDocumentPositionParams &Params) {
|
2018-08-24 21:09:41 +08:00
|
|
|
CreateSpaceForTaskHandle();
|
|
|
|
TaskHandle TH = Server.codeComplete(
|
|
|
|
Params.textDocument.uri.file(), Params.position, CCOpts,
|
|
|
|
[this](llvm::Expected<CodeCompleteResult> List) {
|
|
|
|
auto _ = llvm::make_scope_exit([this]() { CleanupTaskHandle(); });
|
|
|
|
|
|
|
|
if (!List)
|
|
|
|
return replyError(List.takeError());
|
|
|
|
CompletionList LSPList;
|
|
|
|
LSPList.isIncomplete = List->HasMore;
|
|
|
|
for (const auto &R : List->Completions)
|
|
|
|
LSPList.items.push_back(R.render(CCOpts));
|
|
|
|
return reply(std::move(LSPList));
|
|
|
|
});
|
|
|
|
StoreTaskHandle(std::move(TH));
|
2017-05-16 22:40:30 +08:00
|
|
|
}
|
|
|
|
|
[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 ClangdLSPServer::onSignatureHelp(TextDocumentPositionParams &Params) {
|
2018-02-16 20:20:47 +08:00
|
|
|
Server.signatureHelp(Params.textDocument.uri.file(), Params.position,
|
2018-03-13 07:22:35 +08:00
|
|
|
[](llvm::Expected<SignatureHelp> SignatureHelp) {
|
2018-02-15 21:15:47 +08:00
|
|
|
if (!SignatureHelp)
|
|
|
|
return replyError(
|
|
|
|
ErrorCode::InvalidParams,
|
|
|
|
llvm::toString(SignatureHelp.takeError()));
|
2018-03-13 07:22:35 +08:00
|
|
|
reply(*SignatureHelp);
|
2018-02-15 21:15:47 +08:00
|
|
|
});
|
2017-10-06 19:54:17 +08:00
|
|
|
}
|
|
|
|
|
[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 ClangdLSPServer::onGoToDefinition(TextDocumentPositionParams &Params) {
|
2018-08-24 21:09:41 +08:00
|
|
|
Server.findDefinitions(Params.textDocument.uri.file(), Params.position,
|
|
|
|
[](llvm::Expected<std::vector<Location>> Items) {
|
|
|
|
if (!Items)
|
|
|
|
return replyError(
|
|
|
|
ErrorCode::InvalidParams,
|
|
|
|
llvm::toString(Items.takeError()));
|
|
|
|
reply(json::Array(*Items));
|
|
|
|
});
|
2017-06-29 00:12:10 +08:00
|
|
|
}
|
|
|
|
|
[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 ClangdLSPServer::onSwitchSourceHeader(TextDocumentIdentifier &Params) {
|
2018-02-16 20:20:47 +08:00
|
|
|
llvm::Optional<Path> Result = Server.switchSourceHeader(Params.uri.file());
|
[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
|
|
|
reply(Result ? URI::createFile(*Result).toString() : "");
|
2017-09-28 11:14:40 +08:00
|
|
|
}
|
|
|
|
|
[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 ClangdLSPServer::onDocumentHighlight(TextDocumentPositionParams &Params) {
|
2018-02-15 21:15:47 +08:00
|
|
|
Server.findDocumentHighlights(
|
2018-02-16 20:20:47 +08:00
|
|
|
Params.textDocument.uri.file(), Params.position,
|
2018-03-13 07:22:35 +08:00
|
|
|
[](llvm::Expected<std::vector<DocumentHighlight>> Highlights) {
|
2018-02-15 21:15:47 +08:00
|
|
|
if (!Highlights)
|
|
|
|
return replyError(ErrorCode::InternalError,
|
|
|
|
llvm::toString(Highlights.takeError()));
|
2018-07-09 22:25:59 +08:00
|
|
|
reply(json::Array(*Highlights));
|
2018-02-15 21:15:47 +08:00
|
|
|
});
|
[clangd] Document highlights for clangd
Summary: Implementation of Document Highlights Request as described in
LSP.
Contributed by William Enright (nebiroth).
Reviewers: malaperle, krasimir, bkramer, ilya-biryukov
Reviewed By: malaperle
Subscribers: mgrang, sammccall, klimek, ioeric, rwols, cfe-commits, arphaman, ilya-biryukov
Differential Revision: https://reviews.llvm.org/D38425
llvm-svn: 320474
2017-12-12 20:27:47 +08:00
|
|
|
}
|
|
|
|
|
[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 ClangdLSPServer::onHover(TextDocumentPositionParams &Params) {
|
|
|
|
Server.findHover(Params.textDocument.uri.file(), Params.position,
|
2018-06-04 18:37:16 +08:00
|
|
|
[](llvm::Expected<llvm::Optional<Hover>> H) {
|
[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
|
|
|
if (!H) {
|
|
|
|
replyError(ErrorCode::InternalError,
|
|
|
|
llvm::toString(H.takeError()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-13 07:22:35 +08:00
|
|
|
reply(*H);
|
[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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-01 19:28:49 +08:00
|
|
|
void ClangdLSPServer::applyConfiguration(
|
|
|
|
const ClangdConfigurationParamsChange &Settings) {
|
[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
|
|
|
// Compilation database change.
|
|
|
|
if (Settings.compilationDatabasePath.hasValue()) {
|
2018-08-02 01:39:29 +08:00
|
|
|
CDB.setCompileCommandsDir(Settings.compilationDatabasePath.getValue());
|
2018-06-13 17:20:41 +08:00
|
|
|
|
2018-03-16 22:30:42 +08:00
|
|
|
reparseOpenedFiles();
|
[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
|
|
|
}
|
2018-08-02 01:39:29 +08:00
|
|
|
|
|
|
|
// Update to the compilation database.
|
|
|
|
if (Settings.compilationDatabaseChanges) {
|
|
|
|
const auto &CompileCommandUpdates = *Settings.compilationDatabaseChanges;
|
|
|
|
bool ShouldReparseOpenFiles = false;
|
|
|
|
for (auto &Entry : CompileCommandUpdates) {
|
|
|
|
/// The opened files need to be reparsed only when some existing
|
|
|
|
/// entries are changed.
|
|
|
|
PathRef File = Entry.first;
|
|
|
|
if (!CDB.setCompilationCommandForFile(
|
|
|
|
File, tooling::CompileCommand(
|
|
|
|
std::move(Entry.second.workingDirectory), File,
|
|
|
|
std::move(Entry.second.compilationCommand),
|
|
|
|
/*Output=*/"")))
|
|
|
|
ShouldReparseOpenFiles = true;
|
|
|
|
}
|
|
|
|
if (ShouldReparseOpenFiles)
|
|
|
|
reparseOpenedFiles();
|
|
|
|
}
|
[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
|
|
|
}
|
|
|
|
|
2018-08-01 19:28:49 +08:00
|
|
|
// FIXME: This function needs to be properly tested.
|
|
|
|
void ClangdLSPServer::onChangeConfiguration(
|
|
|
|
DidChangeConfigurationParams &Params) {
|
|
|
|
applyConfiguration(Params.settings);
|
|
|
|
}
|
|
|
|
|
2018-03-06 01:28:54 +08:00
|
|
|
ClangdLSPServer::ClangdLSPServer(JSONOutput &Out,
|
2017-11-24 00:58:22 +08:00
|
|
|
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,
|
2018-03-06 01:28:54 +08:00
|
|
|
const ClangdServer::Options &Opts)
|
2018-08-02 01:39:29 +08:00
|
|
|
: Out(Out), CDB(ShouldUseInMemoryCDB ? CompilationDB::makeInMemory()
|
|
|
|
: CompilationDB::makeDirectoryBased(
|
|
|
|
std::move(CompileCommandsDir))),
|
2018-06-13 17:20:41 +08:00
|
|
|
CCOpts(CCOpts), SupportedSymbolKinds(defaultSymbolKinds()),
|
2018-08-02 01:39:29 +08:00
|
|
|
Server(CDB.getCDB(), FSProvider, /*DiagConsumer=*/*this, Opts) {}
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2018-06-05 17:34:46 +08:00
|
|
|
bool ClangdLSPServer::run(std::FILE *In, JSONStreamStyle InputStyle) {
|
2017-05-16 22:40:30 +08:00
|
|
|
assert(!IsDone && "Run was called before");
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
// Set up JSONRPCDispatcher.
|
2018-07-09 22:25:59 +08:00
|
|
|
JSONRPCDispatcher Dispatcher([](const json::Value &Params) {
|
[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
|
|
|
replyError(ErrorCode::MethodNotFound, "method not found");
|
2017-12-13 20:51:22 +08:00
|
|
|
});
|
2018-03-08 05:47:25 +08:00
|
|
|
registerCallbackHandlers(Dispatcher, /*Callbacks=*/*this);
|
2017-05-16 22:40:30 +08:00
|
|
|
|
|
|
|
// Run the Language Server loop.
|
2018-02-06 18:47:30 +08:00
|
|
|
runLanguageServerLoop(In, Out, InputStyle, Dispatcher, IsDone);
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
// Make sure IsDone is set to true after this method exits to ensure assertion
|
|
|
|
// at the start of the method fires if it's ever executed again.
|
|
|
|
IsDone = true;
|
2017-10-25 16:45:41 +08:00
|
|
|
|
|
|
|
return ShutdownRequestReceived;
|
2017-05-16 17:38:59 +08:00
|
|
|
}
|
|
|
|
|
2018-03-12 23:28:22 +08:00
|
|
|
std::vector<Fix> ClangdLSPServer::getFixes(StringRef File,
|
|
|
|
const clangd::Diagnostic &D) {
|
2017-05-16 17:38:59 +08:00
|
|
|
std::lock_guard<std::mutex> Lock(FixItsMutex);
|
|
|
|
auto DiagToFixItsIter = FixItsMap.find(File);
|
|
|
|
if (DiagToFixItsIter == FixItsMap.end())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
const auto &DiagToFixItsMap = DiagToFixItsIter->second;
|
|
|
|
auto FixItsIter = DiagToFixItsMap.find(D);
|
|
|
|
if (FixItsIter == DiagToFixItsMap.end())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
return FixItsIter->second;
|
|
|
|
}
|
|
|
|
|
2018-03-13 07:22:35 +08:00
|
|
|
void ClangdLSPServer::onDiagnosticsReady(PathRef File,
|
|
|
|
std::vector<Diag> Diagnostics) {
|
2018-07-09 22:25:59 +08:00
|
|
|
json::Array DiagnosticsJSON;
|
2017-05-16 17:38:59 +08:00
|
|
|
|
|
|
|
DiagnosticToReplacementMap LocalFixIts; // Temporary storage
|
2018-03-13 07:22:35 +08:00
|
|
|
for (auto &Diag : Diagnostics) {
|
2018-03-12 23:28:22 +08:00
|
|
|
toLSPDiags(Diag, [&](clangd::Diagnostic Diag, llvm::ArrayRef<Fix> Fixes) {
|
2018-08-11 01:25:07 +08:00
|
|
|
json::Object LSPDiag({
|
2018-03-12 23:28:22 +08:00
|
|
|
{"range", Diag.range},
|
|
|
|
{"severity", Diag.severity},
|
|
|
|
{"message", Diag.message},
|
|
|
|
});
|
2018-08-11 01:25:07 +08:00
|
|
|
// LSP extension: embed the fixes in the diagnostic.
|
|
|
|
if (DiagOpts.EmbedFixesInDiagnostics && !Fixes.empty()) {
|
|
|
|
json::Array ClangdFixes;
|
|
|
|
for (const auto &Fix : Fixes) {
|
|
|
|
WorkspaceEdit WE;
|
|
|
|
URIForFile URI{File};
|
|
|
|
WE.changes = {{URI.uri(), std::vector<TextEdit>(Fix.Edits.begin(),
|
|
|
|
Fix.Edits.end())}};
|
|
|
|
ClangdFixes.push_back(
|
|
|
|
json::Object{{"edit", toJSON(WE)}, {"title", Fix.Message}});
|
|
|
|
}
|
|
|
|
LSPDiag["clangd_fixes"] = std::move(ClangdFixes);
|
|
|
|
}
|
2018-08-23 04:30:06 +08:00
|
|
|
if (DiagOpts.SendDiagnosticCategory && !Diag.category.empty())
|
2018-08-15 06:21:40 +08:00
|
|
|
LSPDiag["category"] = Diag.category;
|
2018-08-11 01:25:07 +08:00
|
|
|
DiagnosticsJSON.push_back(std::move(LSPDiag));
|
2018-03-12 23:28:22 +08:00
|
|
|
|
|
|
|
auto &FixItsForDiagnostic = LocalFixIts[Diag];
|
|
|
|
std::copy(Fixes.begin(), Fixes.end(),
|
|
|
|
std::back_inserter(FixItsForDiagnostic));
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
});
|
2017-05-16 17:38:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cache FixIts
|
|
|
|
{
|
|
|
|
// FIXME(ibiryukov): should be deleted when documents are removed
|
|
|
|
std::lock_guard<std::mutex> Lock(FixItsMutex);
|
|
|
|
FixItsMap[File] = LocalFixIts;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Publish diagnostics.
|
2018-07-09 22:25:59 +08:00
|
|
|
Out.writeMessage(json::Object{
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
{"jsonrpc", "2.0"},
|
|
|
|
{"method", "textDocument/publishDiagnostics"},
|
|
|
|
{"params",
|
2018-07-09 22:25:59 +08:00
|
|
|
json::Object{
|
2018-01-29 23:37:46 +08:00
|
|
|
{"uri", URIForFile{File}},
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
{"diagnostics", std::move(DiagnosticsJSON)},
|
|
|
|
}},
|
|
|
|
});
|
2017-05-16 17:38:59 +08:00
|
|
|
}
|
2018-03-16 22:30:42 +08:00
|
|
|
|
|
|
|
void ClangdLSPServer::reparseOpenedFiles() {
|
|
|
|
for (const Path &FilePath : DraftMgr.getActiveFiles())
|
|
|
|
Server.addDocument(FilePath, *DraftMgr.getDraft(FilePath),
|
2018-06-13 17:20:41 +08:00
|
|
|
WantDiagnostics::Auto);
|
2018-03-16 22:30:42 +08:00
|
|
|
}
|
2018-08-02 01:39:29 +08:00
|
|
|
|
|
|
|
ClangdLSPServer::CompilationDB ClangdLSPServer::CompilationDB::makeInMemory() {
|
|
|
|
return CompilationDB(llvm::make_unique<InMemoryCompilationDb>(), nullptr,
|
|
|
|
/*IsDirectoryBased=*/false);
|
|
|
|
}
|
|
|
|
|
|
|
|
ClangdLSPServer::CompilationDB
|
|
|
|
ClangdLSPServer::CompilationDB::makeDirectoryBased(
|
|
|
|
llvm::Optional<Path> CompileCommandsDir) {
|
|
|
|
auto CDB = llvm::make_unique<DirectoryBasedGlobalCompilationDatabase>(
|
|
|
|
std::move(CompileCommandsDir));
|
|
|
|
auto CachingCDB = llvm::make_unique<CachingCompilationDb>(*CDB);
|
|
|
|
return CompilationDB(std::move(CDB), std::move(CachingCDB),
|
|
|
|
/*IsDirectoryBased=*/true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClangdLSPServer::CompilationDB::invalidate(PathRef File) {
|
|
|
|
if (!IsDirectoryBased)
|
|
|
|
static_cast<InMemoryCompilationDb *>(CDB.get())->invalidate(File);
|
|
|
|
else
|
|
|
|
CachingCDB->invalidate(File);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ClangdLSPServer::CompilationDB::setCompilationCommandForFile(
|
|
|
|
PathRef File, tooling::CompileCommand CompilationCommand) {
|
|
|
|
if (IsDirectoryBased) {
|
|
|
|
elog("Trying to set compile command for {0} while using directory-based "
|
|
|
|
"compilation database",
|
|
|
|
File);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return static_cast<InMemoryCompilationDb *>(CDB.get())
|
|
|
|
->setCompilationCommandForFile(File, std::move(CompilationCommand));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClangdLSPServer::CompilationDB::setExtraFlagsForFile(
|
|
|
|
PathRef File, std::vector<std::string> ExtraFlags) {
|
|
|
|
if (!IsDirectoryBased) {
|
|
|
|
elog("Trying to set extra flags for {0} while using in-memory compilation "
|
|
|
|
"database",
|
|
|
|
File);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
static_cast<DirectoryBasedGlobalCompilationDatabase *>(CDB.get())
|
|
|
|
->setExtraFlagsForFile(File, std::move(ExtraFlags));
|
|
|
|
CachingCDB->invalidate(File);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClangdLSPServer::CompilationDB::setCompileCommandsDir(Path P) {
|
|
|
|
if (!IsDirectoryBased) {
|
|
|
|
elog("Trying to set compile commands dir while using in-memory compilation "
|
|
|
|
"database");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
static_cast<DirectoryBasedGlobalCompilationDatabase *>(CDB.get())
|
|
|
|
->setCompileCommandsDir(P);
|
|
|
|
CachingCDB->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
GlobalCompilationDatabase &ClangdLSPServer::CompilationDB::getCDB() {
|
|
|
|
if (CachingCDB)
|
|
|
|
return *CachingCDB;
|
|
|
|
return *CDB;
|
|
|
|
}
|
2018-08-24 21:09:41 +08:00
|
|
|
|
|
|
|
void ClangdLSPServer::onCancelRequest(CancelParams &Params) {
|
|
|
|
std::lock_guard<std::mutex> Lock(TaskHandlesMutex);
|
|
|
|
const auto &It = TaskHandles.find(Params.ID);
|
|
|
|
if (It == TaskHandles.end())
|
|
|
|
return;
|
|
|
|
if (It->second)
|
|
|
|
It->second->cancel();
|
|
|
|
TaskHandles.erase(It);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClangdLSPServer::CleanupTaskHandle() {
|
|
|
|
const json::Value *ID = getRequestId();
|
|
|
|
if (!ID)
|
|
|
|
return;
|
|
|
|
std::string NormalizedID = NormalizeRequestID(*ID);
|
|
|
|
std::lock_guard<std::mutex> Lock(TaskHandlesMutex);
|
|
|
|
TaskHandles.erase(NormalizedID);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClangdLSPServer::CreateSpaceForTaskHandle() {
|
|
|
|
const json::Value *ID = getRequestId();
|
|
|
|
if (!ID)
|
|
|
|
return;
|
|
|
|
std::string NormalizedID = NormalizeRequestID(*ID);
|
|
|
|
std::lock_guard<std::mutex> Lock(TaskHandlesMutex);
|
|
|
|
if (!TaskHandles.insert({NormalizedID, nullptr}).second)
|
|
|
|
elog("Creation of space for task handle: {0} failed.", NormalizedID);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClangdLSPServer::StoreTaskHandle(TaskHandle TH) {
|
|
|
|
const json::Value *ID = getRequestId();
|
|
|
|
if (!ID)
|
|
|
|
return;
|
|
|
|
std::string NormalizedID = NormalizeRequestID(*ID);
|
|
|
|
std::lock_guard<std::mutex> Lock(TaskHandlesMutex);
|
|
|
|
auto It = TaskHandles.find(NormalizedID);
|
|
|
|
if (It == TaskHandles.end()) {
|
|
|
|
elog("CleanupTaskHandle called before store can happen for request:{0}.",
|
|
|
|
NormalizedID);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (It->second != nullptr)
|
|
|
|
elog("TaskHandle didn't get cleared for: {0}.", NormalizedID);
|
|
|
|
It->second = std::move(TH);
|
|
|
|
}
|