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-03-12 23:28:22 +08:00
|
|
|
#include "Diagnostics.h"
|
2017-12-19 20:23:48 +08:00
|
|
|
#include "SourceCode.h"
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
#include "Trace.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] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
#include "llvm/Support/ScopedPrinter.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
|
|
|
|
2018-07-09 22:25:59 +08:00
|
|
|
using namespace llvm;
|
2018-10-20 23:30:37 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
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:
|
2018-10-20 23:30:37 +08:00
|
|
|
Expected<std::string> getAbsolutePath(StringRef /*Authority*/, StringRef Body,
|
|
|
|
StringRef /*HintPath*/) const override {
|
2018-02-01 00:26:27 +08:00
|
|
|
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("/"))
|
2018-10-20 23:30:37 +08:00
|
|
|
return make_error<StringError>(
|
2018-02-01 00:26:27 +08:00
|
|
|
"Expect URI body to be an absolute path starting with '/': " + Body,
|
2018-10-20 23:30:37 +08:00
|
|
|
inconvertibleErrorCode());
|
2018-02-01 00:26:27 +08:00
|
|
|
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
|
2018-10-20 23:30:37 +08:00
|
|
|
SmallVector<char, 16> Path(Body.begin(), Body.end());
|
2018-02-01 00:26:27 +08:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
Expected<URI> uriFromAbsolutePath(StringRef AbsolutePath) const override {
|
2018-02-01 00:26:27 +08:00
|
|
|
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-09-28 01:13:07 +08:00
|
|
|
CompletionItemKindBitset defaultCompletionItemKinds() {
|
|
|
|
CompletionItemKindBitset Defaults;
|
|
|
|
for (size_t I = CompletionItemKindMin;
|
|
|
|
I <= static_cast<size_t>(CompletionItemKind::Reference); ++I)
|
|
|
|
Defaults.set(I);
|
|
|
|
return Defaults;
|
|
|
|
}
|
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
} // namespace
|
|
|
|
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
// MessageHandler dispatches incoming LSP messages.
|
|
|
|
// It handles cross-cutting concerns:
|
|
|
|
// - serializes/deserializes protocol objects to JSON
|
|
|
|
// - logging of inbound messages
|
|
|
|
// - cancellation handling
|
|
|
|
// - basic call tracing
|
[clangd] Enforce rules around "initialize" request, and create ClangdServer lazily.
Summary:
LSP is a slightly awkward map to C++ object lifetimes: the initialize request
is part of the protocol and provides information that doesn't change over the
lifetime of the server.
Until now, we handled this by initializing ClangdServer and ClangdLSPServer
right away, and making anything that can be set in the "initialize" request
mutable.
With this patch, we create ClangdLSPServer immediately, but defer creating
ClangdServer until "initialize". This opens the door to passing the relevant
initialize params in the constructor and storing them immutably.
(That change isn't actually done in this patch).
To make this safe, we have the MessageDispatcher enforce that the "initialize"
method is called before any other (as required by LSP). That way each method
handler can assume Server is initialized, as today.
As usual, while implementing this I found places where our test cases violated
the protocol.
Reviewers: ioeric
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53398
llvm-svn: 344741
2018-10-18 22:41:50 +08:00
|
|
|
// MessageHandler ensures that initialize() is called before any other handler.
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
class ClangdLSPServer::MessageHandler : public Transport::MessageHandler {
|
|
|
|
public:
|
|
|
|
MessageHandler(ClangdLSPServer &Server) : Server(Server) {}
|
|
|
|
|
|
|
|
bool onNotify(StringRef Method, json::Value Params) override {
|
|
|
|
log("<-- {0}", Method);
|
|
|
|
if (Method == "exit")
|
|
|
|
return false;
|
[clangd] Enforce rules around "initialize" request, and create ClangdServer lazily.
Summary:
LSP is a slightly awkward map to C++ object lifetimes: the initialize request
is part of the protocol and provides information that doesn't change over the
lifetime of the server.
Until now, we handled this by initializing ClangdServer and ClangdLSPServer
right away, and making anything that can be set in the "initialize" request
mutable.
With this patch, we create ClangdLSPServer immediately, but defer creating
ClangdServer until "initialize". This opens the door to passing the relevant
initialize params in the constructor and storing them immutably.
(That change isn't actually done in this patch).
To make this safe, we have the MessageDispatcher enforce that the "initialize"
method is called before any other (as required by LSP). That way each method
handler can assume Server is initialized, as today.
As usual, while implementing this I found places where our test cases violated
the protocol.
Reviewers: ioeric
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53398
llvm-svn: 344741
2018-10-18 22:41:50 +08:00
|
|
|
if (!Server.Server)
|
|
|
|
elog("Notification {0} before initialization", Method);
|
|
|
|
else if (Method == "$/cancelRequest")
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
onCancel(std::move(Params));
|
|
|
|
else if (auto Handler = Notifications.lookup(Method))
|
|
|
|
Handler(std::move(Params));
|
|
|
|
else
|
|
|
|
log("unhandled notification {0}", Method);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool onCall(StringRef Method, json::Value Params, json::Value ID) override {
|
|
|
|
log("<-- {0}({1})", Method, ID);
|
[clangd] Enforce rules around "initialize" request, and create ClangdServer lazily.
Summary:
LSP is a slightly awkward map to C++ object lifetimes: the initialize request
is part of the protocol and provides information that doesn't change over the
lifetime of the server.
Until now, we handled this by initializing ClangdServer and ClangdLSPServer
right away, and making anything that can be set in the "initialize" request
mutable.
With this patch, we create ClangdLSPServer immediately, but defer creating
ClangdServer until "initialize". This opens the door to passing the relevant
initialize params in the constructor and storing them immutably.
(That change isn't actually done in this patch).
To make this safe, we have the MessageDispatcher enforce that the "initialize"
method is called before any other (as required by LSP). That way each method
handler can assume Server is initialized, as today.
As usual, while implementing this I found places where our test cases violated
the protocol.
Reviewers: ioeric
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53398
llvm-svn: 344741
2018-10-18 22:41:50 +08:00
|
|
|
if (!Server.Server && Method != "initialize") {
|
|
|
|
elog("Call {0} before initialization.", Method);
|
|
|
|
Server.reply(ID, make_error<LSPError>("server not initialized",
|
|
|
|
ErrorCode::ServerNotInitialized));
|
|
|
|
} else if (auto Handler = Calls.lookup(Method))
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
Handler(std::move(Params), std::move(ID));
|
|
|
|
else
|
2018-10-20 23:30:37 +08:00
|
|
|
Server.reply(ID, make_error<LSPError>("method not found",
|
|
|
|
ErrorCode::MethodNotFound));
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool onReply(json::Value ID, Expected<json::Value> Result) override {
|
|
|
|
// We ignore replies, just log them.
|
|
|
|
if (Result)
|
|
|
|
log("<-- reply({0})", ID);
|
|
|
|
else
|
2018-10-20 23:30:37 +08:00
|
|
|
log("<-- reply({0}) error: {1}", ID, toString(Result.takeError()));
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bind an LSP method name to a call.
|
|
|
|
template <typename Param, typename Reply>
|
|
|
|
void bind(const char *Method,
|
|
|
|
void (ClangdLSPServer::*Handler)(const Param &, Callback<Reply>)) {
|
|
|
|
Calls[Method] = [Method, Handler, this](json::Value RawParams,
|
|
|
|
json::Value ID) {
|
|
|
|
Param P;
|
|
|
|
if (!fromJSON(RawParams, P)) {
|
|
|
|
elog("Failed to decode {0} request.", Method);
|
|
|
|
Server.reply(ID, make_error<LSPError>("failed to decode request",
|
|
|
|
ErrorCode::InvalidRequest));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
trace::Span Tracer(Method);
|
|
|
|
SPAN_ATTACH(Tracer, "Params", RawParams);
|
|
|
|
auto *Trace = Tracer.Args; // We attach reply from another thread.
|
|
|
|
// Calls can be canceled by the client. Add cancellation context.
|
|
|
|
WithContext WithCancel(cancelableRequestContext(ID));
|
|
|
|
// FIXME: this function should assert it's called exactly once.
|
2018-10-20 23:30:37 +08:00
|
|
|
(Server.*Handler)(P, [this, ID, Trace](Expected<Reply> Result) {
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
if (Result) {
|
|
|
|
if (Trace)
|
|
|
|
(*Trace)["Reply"] = *Result;
|
|
|
|
Server.reply(ID, json::Value(std::move(*Result)));
|
|
|
|
} else {
|
|
|
|
auto Err = Result.takeError();
|
|
|
|
if (Trace)
|
2018-10-20 23:30:37 +08:00
|
|
|
(*Trace)["Error"] = to_string(Err);
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
Server.reply(ID, std::move(Err));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bind an LSP method name to a notification.
|
|
|
|
template <typename Param>
|
|
|
|
void bind(const char *Method,
|
|
|
|
void (ClangdLSPServer::*Handler)(const Param &)) {
|
|
|
|
Notifications[Method] = [Method, Handler, this](json::Value RawParams) {
|
|
|
|
Param P;
|
|
|
|
if (!fromJSON(RawParams, P)) {
|
|
|
|
elog("Failed to decode {0} request.", Method);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
trace::Span Tracer(Method);
|
|
|
|
SPAN_ATTACH(Tracer, "Params", RawParams);
|
|
|
|
(Server.*Handler)(P);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-10-20 23:30:37 +08:00
|
|
|
StringMap<std::function<void(json::Value)>> Notifications;
|
|
|
|
StringMap<std::function<void(json::Value, json::Value)>> Calls;
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
|
|
|
|
// Method calls may be cancelled by ID, so keep track of their state.
|
|
|
|
// This needs a mutex: handlers may finish on a different thread, and that's
|
|
|
|
// when we clean up entries in the map.
|
|
|
|
mutable std::mutex RequestCancelersMutex;
|
2018-10-20 23:30:37 +08:00
|
|
|
StringMap<std::pair<Canceler, /*Cookie*/ unsigned>> RequestCancelers;
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
unsigned NextRequestCookie = 0; // To disambiguate reused IDs, see below.
|
2018-10-20 23:30:37 +08:00
|
|
|
void onCancel(const json::Value &Params) {
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
const json::Value *ID = nullptr;
|
|
|
|
if (auto *O = Params.getAsObject())
|
|
|
|
ID = O->get("id");
|
|
|
|
if (!ID) {
|
|
|
|
elog("Bad cancellation request: {0}", Params);
|
|
|
|
return;
|
|
|
|
}
|
2018-10-20 23:30:37 +08:00
|
|
|
auto StrID = to_string(*ID);
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
std::lock_guard<std::mutex> Lock(RequestCancelersMutex);
|
|
|
|
auto It = RequestCancelers.find(StrID);
|
|
|
|
if (It != RequestCancelers.end())
|
|
|
|
It->second.first(); // Invoke the canceler.
|
|
|
|
}
|
|
|
|
// We run cancelable requests in a context that does two things:
|
|
|
|
// - allows cancellation using RequestCancelers[ID]
|
|
|
|
// - cleans up the entry in RequestCancelers when it's no longer needed
|
|
|
|
// If a client reuses an ID, the last wins and the first cannot be canceled.
|
|
|
|
Context cancelableRequestContext(const json::Value &ID) {
|
|
|
|
auto Task = cancelableTask();
|
2018-10-20 23:30:37 +08:00
|
|
|
auto StrID = to_string(ID); // JSON-serialize ID for map key.
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
auto Cookie = NextRequestCookie++; // No lock, only called on main thread.
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(RequestCancelersMutex);
|
|
|
|
RequestCancelers[StrID] = {std::move(Task.second), Cookie};
|
|
|
|
}
|
|
|
|
// When the request ends, we can clean up the entry we just added.
|
|
|
|
// The cookie lets us check that it hasn't been overwritten due to ID
|
|
|
|
// reuse.
|
|
|
|
return Task.first.derive(make_scope_exit([this, StrID, Cookie] {
|
|
|
|
std::lock_guard<std::mutex> Lock(RequestCancelersMutex);
|
|
|
|
auto It = RequestCancelers.find(StrID);
|
|
|
|
if (It != RequestCancelers.end() && It->second.second == Cookie)
|
|
|
|
RequestCancelers.erase(It);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
ClangdLSPServer &Server;
|
|
|
|
};
|
|
|
|
|
|
|
|
// call(), notify(), and reply() wrap the Transport, adding logging and locking.
|
|
|
|
void ClangdLSPServer::call(StringRef Method, json::Value Params) {
|
|
|
|
auto ID = NextCallID++;
|
|
|
|
log("--> {0}({1})", Method, ID);
|
|
|
|
// We currently don't handle responses, so no need to store ID anywhere.
|
|
|
|
std::lock_guard<std::mutex> Lock(TranspWriter);
|
|
|
|
Transp.call(Method, std::move(Params), ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClangdLSPServer::notify(StringRef Method, json::Value Params) {
|
|
|
|
log("--> {0}", Method);
|
|
|
|
std::lock_guard<std::mutex> Lock(TranspWriter);
|
|
|
|
Transp.notify(Method, std::move(Params));
|
|
|
|
}
|
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
void ClangdLSPServer::reply(json::Value ID, Expected<json::Value> Result) {
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
if (Result) {
|
|
|
|
log("--> reply({0})", ID);
|
|
|
|
std::lock_guard<std::mutex> Lock(TranspWriter);
|
|
|
|
Transp.reply(std::move(ID), std::move(Result));
|
|
|
|
} else {
|
|
|
|
Error Err = Result.takeError();
|
|
|
|
log("--> reply({0}) error: {1}", ID, Err);
|
|
|
|
std::lock_guard<std::mutex> Lock(TranspWriter);
|
|
|
|
Transp.reply(std::move(ID), std::move(Err));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClangdLSPServer::onInitialize(const InitializeParams &Params,
|
|
|
|
Callback<json::Value> Reply) {
|
2018-10-19 23:42:23 +08:00
|
|
|
if (Params.rootUri && *Params.rootUri)
|
|
|
|
ClangdServerOpts.WorkspaceRoot = Params.rootUri->file();
|
|
|
|
else if (Params.rootPath && !Params.rootPath->empty())
|
|
|
|
ClangdServerOpts.WorkspaceRoot = *Params.rootPath;
|
[clangd] Enforce rules around "initialize" request, and create ClangdServer lazily.
Summary:
LSP is a slightly awkward map to C++ object lifetimes: the initialize request
is part of the protocol and provides information that doesn't change over the
lifetime of the server.
Until now, we handled this by initializing ClangdServer and ClangdLSPServer
right away, and making anything that can be set in the "initialize" request
mutable.
With this patch, we create ClangdLSPServer immediately, but defer creating
ClangdServer until "initialize". This opens the door to passing the relevant
initialize params in the constructor and storing them immutably.
(That change isn't actually done in this patch).
To make this safe, we have the MessageDispatcher enforce that the "initialize"
method is called before any other (as required by LSP). That way each method
handler can assume Server is initialized, as today.
As usual, while implementing this I found places where our test cases violated
the protocol.
Reviewers: ioeric
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53398
llvm-svn: 344741
2018-10-18 22:41:50 +08:00
|
|
|
if (Server)
|
|
|
|
return Reply(make_error<LSPError>("server already initialized",
|
|
|
|
ErrorCode::InvalidRequest));
|
|
|
|
Server.emplace(CDB.getCDB(), FSProvider,
|
|
|
|
static_cast<DiagnosticsConsumer &>(*this), ClangdServerOpts);
|
2018-10-16 23:55:03 +08:00
|
|
|
if (Params.initializationOptions) {
|
|
|
|
const ClangdInitializationOptions &Opts = *Params.initializationOptions;
|
|
|
|
|
|
|
|
// Explicit compilation database path.
|
|
|
|
if (Opts.compilationDatabasePath.hasValue()) {
|
|
|
|
CDB.setCompileCommandsDir(Opts.compilationDatabasePath.getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
applyConfiguration(Opts.ParamsChange);
|
|
|
|
}
|
2018-08-01 19:28:49 +08:00
|
|
|
|
2018-10-17 15:33:42 +08:00
|
|
|
CCOpts.EnableSnippets = Params.capabilities.CompletionSnippets;
|
|
|
|
DiagOpts.EmbedFixesInDiagnostics = Params.capabilities.DiagnosticFixes;
|
|
|
|
DiagOpts.SendDiagnosticCategory = Params.capabilities.DiagnosticCategory;
|
|
|
|
if (Params.capabilities.WorkspaceSymbolKinds)
|
|
|
|
SupportedSymbolKinds |= *Params.capabilities.WorkspaceSymbolKinds;
|
|
|
|
if (Params.capabilities.CompletionItemKinds)
|
|
|
|
SupportedCompletionItemKinds |= *Params.capabilities.CompletionItemKinds;
|
|
|
|
SupportsCodeAction = Params.capabilities.CodeActionStructure;
|
2018-09-28 01:13:07 +08:00
|
|
|
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +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},
|
2018-09-05 19:53:07 +08:00
|
|
|
{"referencesProvider", 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] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
void ClangdLSPServer::onShutdown(const ShutdownParams &Params,
|
|
|
|
Callback<std::nullptr_t> Reply) {
|
2017-10-25 16:45:41 +08:00
|
|
|
// Do essentially nothing, just say we're ready to exit.
|
|
|
|
ShutdownRequestReceived = true;
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
Reply(nullptr);
|
2017-10-12 21:29:58 +08:00
|
|
|
}
|
2017-05-16 22:40:30 +08:00
|
|
|
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
void ClangdLSPServer::onDocumentDidOpen(
|
|
|
|
const 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
|
|
|
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
const std::string &Contents = Params.textDocument.text;
|
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
|
|
|
DraftMgr.addDraft(File, Contents);
|
2018-09-26 13:48:29 +08:00
|
|
|
Server->addDocument(File, Contents, WantDiagnostics::Yes);
|
2017-05-16 22:40:30 +08:00
|
|
|
}
|
|
|
|
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
void ClangdLSPServer::onDocumentDidChange(
|
|
|
|
const 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();
|
2018-10-20 23:30:37 +08:00
|
|
|
Expected<std::string> Contents =
|
[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.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);
|
2018-09-26 13:48:29 +08:00
|
|
|
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
|
|
|
|
2018-09-26 13:48:29 +08:00
|
|
|
Server->addDocument(File, *Contents, WantDiags);
|
2017-05-16 22:40:30 +08:00
|
|
|
}
|
|
|
|
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
void ClangdLSPServer::onFileEvent(const DidChangeWatchedFilesParams &Params) {
|
2018-09-26 13:48:29 +08:00
|
|
|
Server->onFileEvent(Params);
|
2017-10-03 02:00:37 +08:00
|
|
|
}
|
|
|
|
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
void ClangdLSPServer::onCommand(const ExecuteCommandParams &Params,
|
|
|
|
Callback<json::Value> Reply) {
|
|
|
|
auto ApplyEdit = [&](WorkspaceEdit WE) {
|
2018-02-16 22:15:55 +08:00
|
|
|
ApplyWorkspaceEditParams Edit;
|
|
|
|
Edit.edit = std::move(WE);
|
|
|
|
// 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] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +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.
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
Reply(make_error<LSPError>(
|
2018-10-20 23:30:37 +08:00
|
|
|
formatv("Unsupported command \"{0}\".", Params.command).str(),
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
ErrorCode::InvalidParams));
|
[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] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
void ClangdLSPServer::onWorkspaceSymbol(
|
|
|
|
const WorkspaceSymbolParams &Params,
|
|
|
|
Callback<std::vector<SymbolInformation>> Reply) {
|
2018-09-26 13:48:29 +08:00
|
|
|
Server->workspaceSymbols(
|
[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
|
|
|
Params.query, CCOpts.Limit,
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
Bind(
|
|
|
|
[this](decltype(Reply) Reply,
|
2018-10-20 23:30:37 +08:00
|
|
|
Expected<std::vector<SymbolInformation>> Items) {
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
if (!Items)
|
|
|
|
return Reply(Items.takeError());
|
|
|
|
for (auto &Sym : *Items)
|
|
|
|
Sym.kind = adjustKindToCapability(Sym.kind, SupportedSymbolKinds);
|
|
|
|
|
|
|
|
Reply(std::move(*Items));
|
|
|
|
},
|
|
|
|
std::move(Reply)));
|
[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] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
void ClangdLSPServer::onRename(const RenameParams &Params,
|
|
|
|
Callback<WorkspaceEdit> Reply) {
|
2018-02-16 20:20:47 +08:00
|
|
|
Path File = Params.textDocument.uri.file();
|
2018-10-20 23:30:37 +08:00
|
|
|
Optional<std::string> Code = DraftMgr.getDraft(File);
|
2018-01-17 20:30:24 +08:00
|
|
|
if (!Code)
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
return Reply(make_error<LSPError>("onRename called for non-added file",
|
|
|
|
ErrorCode::InvalidParams));
|
2018-01-17 20:30:24 +08:00
|
|
|
|
2018-09-26 13:48:29 +08:00
|
|
|
Server->rename(
|
2018-02-15 21:15:47 +08:00
|
|
|
File, Params.position, Params.newName,
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
Bind(
|
2018-10-20 23:30:37 +08:00
|
|
|
[File, Code,
|
|
|
|
Params](decltype(Reply) Reply,
|
|
|
|
Expected<std::vector<tooling::Replacement>> Replacements) {
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
if (!Replacements)
|
|
|
|
return Reply(Replacements.takeError());
|
|
|
|
|
|
|
|
// 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));
|
|
|
|
WorkspaceEdit WE;
|
|
|
|
WE.changes = {{Params.textDocument.uri.uri(), Edits}};
|
|
|
|
Reply(WE);
|
|
|
|
},
|
|
|
|
std::move(Reply)));
|
2017-11-09 19:30:04 +08:00
|
|
|
}
|
|
|
|
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
void ClangdLSPServer::onDocumentDidClose(
|
|
|
|
const DidCloseTextDocumentParams &Params) {
|
2018-03-16 22:30:42 +08:00
|
|
|
PathRef File = Params.textDocument.uri.file();
|
|
|
|
DraftMgr.removeDraft(File);
|
2018-09-26 13:48:29 +08:00
|
|
|
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] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
const DocumentOnTypeFormattingParams &Params,
|
|
|
|
Callback<std::vector<TextEdit>> Reply) {
|
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] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
return Reply(make_error<LSPError>(
|
|
|
|
"onDocumentOnTypeFormatting called for non-added file",
|
|
|
|
ErrorCode::InvalidParams));
|
2018-01-17 20:30:24 +08:00
|
|
|
|
2018-09-26 13:48:29 +08:00
|
|
|
auto ReplacementsOrError = Server->formatOnType(*Code, File, Params.position);
|
2017-12-13 04:25:06 +08:00
|
|
|
if (ReplacementsOrError)
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
Reply(replacementsToEdits(*Code, ReplacementsOrError.get()));
|
2017-12-13 04:25:06 +08:00
|
|
|
else
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
Reply(ReplacementsOrError.takeError());
|
2017-05-16 22:40:30 +08:00
|
|
|
}
|
|
|
|
|
2017-09-30 18:08:52 +08:00
|
|
|
void ClangdLSPServer::onDocumentRangeFormatting(
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
const DocumentRangeFormattingParams &Params,
|
|
|
|
Callback<std::vector<TextEdit>> Reply) {
|
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] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
return Reply(make_error<LSPError>(
|
|
|
|
"onDocumentRangeFormatting called for non-added file",
|
|
|
|
ErrorCode::InvalidParams));
|
2018-01-17 20:30:24 +08:00
|
|
|
|
2018-09-26 13:48:29 +08:00
|
|
|
auto ReplacementsOrError = Server->formatRange(*Code, File, Params.range);
|
2017-12-13 04:25:06 +08:00
|
|
|
if (ReplacementsOrError)
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
Reply(replacementsToEdits(*Code, ReplacementsOrError.get()));
|
2017-12-13 04:25:06 +08:00
|
|
|
else
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
Reply(ReplacementsOrError.takeError());
|
2017-05-16 22:40:30 +08:00
|
|
|
}
|
|
|
|
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
void ClangdLSPServer::onDocumentFormatting(
|
|
|
|
const DocumentFormattingParams &Params,
|
|
|
|
Callback<std::vector<TextEdit>> Reply) {
|
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] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
return Reply(
|
|
|
|
make_error<LSPError>("onDocumentFormatting called for non-added file",
|
|
|
|
ErrorCode::InvalidParams));
|
2018-01-17 20:30:24 +08:00
|
|
|
|
2018-09-26 13:48:29 +08:00
|
|
|
auto ReplacementsOrError = Server->formatFile(*Code, File);
|
2017-12-13 04:25:06 +08:00
|
|
|
if (ReplacementsOrError)
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
Reply(replacementsToEdits(*Code, ReplacementsOrError.get()));
|
2017-12-13 04:25:06 +08:00
|
|
|
else
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
Reply(ReplacementsOrError.takeError());
|
2017-05-16 22:40:30 +08:00
|
|
|
}
|
|
|
|
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
void ClangdLSPServer::onDocumentSymbol(
|
|
|
|
const DocumentSymbolParams &Params,
|
|
|
|
Callback<std::vector<SymbolInformation>> Reply) {
|
2018-09-26 13:48:29 +08:00
|
|
|
Server->documentSymbols(
|
2018-07-06 03:35:01 +08:00
|
|
|
Params.textDocument.uri.file(),
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
Bind(
|
|
|
|
[this](decltype(Reply) Reply,
|
2018-10-20 23:30:37 +08:00
|
|
|
Expected<std::vector<SymbolInformation>> Items) {
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
if (!Items)
|
|
|
|
return Reply(Items.takeError());
|
|
|
|
for (auto &Sym : *Items)
|
|
|
|
Sym.kind = adjustKindToCapability(Sym.kind, SupportedSymbolKinds);
|
|
|
|
Reply(std::move(*Items));
|
|
|
|
},
|
|
|
|
std::move(Reply)));
|
2018-07-06 03:35:01 +08:00
|
|
|
}
|
|
|
|
|
2018-10-17 00:29:41 +08:00
|
|
|
static Optional<Command> asCommand(const CodeAction &Action) {
|
|
|
|
Command Cmd;
|
|
|
|
if (Action.command && Action.edit)
|
2018-10-20 23:30:37 +08:00
|
|
|
return None; // Not representable. (We never emit these anyway).
|
2018-10-17 00:29:41 +08:00
|
|
|
if (Action.command) {
|
|
|
|
Cmd = *Action.command;
|
|
|
|
} else if (Action.edit) {
|
|
|
|
Cmd.command = Command::CLANGD_APPLY_FIX_COMMAND;
|
|
|
|
Cmd.workspaceEdit = *Action.edit;
|
|
|
|
} else {
|
2018-10-20 23:30:37 +08:00
|
|
|
return None;
|
2018-10-17 00:29:41 +08:00
|
|
|
}
|
|
|
|
Cmd.title = Action.title;
|
|
|
|
if (Action.kind && *Action.kind == CodeAction::QUICKFIX_KIND)
|
|
|
|
Cmd.title = "Apply fix: " + Cmd.title;
|
|
|
|
return Cmd;
|
|
|
|
}
|
|
|
|
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
void ClangdLSPServer::onCodeAction(const CodeActionParams &Params,
|
|
|
|
Callback<json::Value> Reply) {
|
|
|
|
auto Code = DraftMgr.getDraft(Params.textDocument.uri.file());
|
|
|
|
if (!Code)
|
|
|
|
return Reply(make_error<LSPError>("onCodeAction called for non-added file",
|
|
|
|
ErrorCode::InvalidParams));
|
2018-10-17 00:29:41 +08:00
|
|
|
// We provide a code action for Fixes on the specified diagnostics.
|
|
|
|
std::vector<CodeAction> Actions;
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
for (const Diagnostic &D : Params.context.diagnostics) {
|
2018-03-12 23:28:22 +08:00
|
|
|
for (auto &F : getFixes(Params.textDocument.uri.file(), D)) {
|
2018-10-17 00:29:41 +08:00
|
|
|
Actions.emplace_back();
|
|
|
|
Actions.back().title = F.Message;
|
|
|
|
Actions.back().kind = CodeAction::QUICKFIX_KIND;
|
|
|
|
Actions.back().diagnostics = {D};
|
|
|
|
Actions.back().edit.emplace();
|
|
|
|
Actions.back().edit->changes.emplace();
|
|
|
|
(*Actions.back().edit->changes)[Params.textDocument.uri.uri()] = {
|
|
|
|
F.Edits.begin(), F.Edits.end()};
|
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 22:40:30 +08:00
|
|
|
}
|
2018-10-17 00:29:41 +08:00
|
|
|
|
|
|
|
if (SupportsCodeAction)
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
Reply(json::Array(Actions));
|
2018-10-17 00:29:41 +08:00
|
|
|
else {
|
|
|
|
std::vector<Command> Commands;
|
|
|
|
for (const auto &Action : Actions)
|
|
|
|
if (auto Command = asCommand(Action))
|
|
|
|
Commands.push_back(std::move(*Command));
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
Reply(json::Array(Commands));
|
2018-10-17 00:29:41 +08:00
|
|
|
}
|
2017-05-16 22:40:30 +08:00
|
|
|
}
|
|
|
|
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
void ClangdLSPServer::onCompletion(const TextDocumentPositionParams &Params,
|
|
|
|
Callback<CompletionList> Reply) {
|
2018-09-26 13:48:29 +08:00
|
|
|
Server->codeComplete(Params.textDocument.uri.file(), Params.position, CCOpts,
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
Bind(
|
|
|
|
[this](decltype(Reply) Reply,
|
2018-10-20 23:30:37 +08:00
|
|
|
Expected<CodeCompleteResult> List) {
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
if (!List)
|
|
|
|
return Reply(List.takeError());
|
|
|
|
CompletionList LSPList;
|
|
|
|
LSPList.isIncomplete = List->HasMore;
|
|
|
|
for (const auto &R : List->Completions) {
|
|
|
|
CompletionItem C = R.render(CCOpts);
|
|
|
|
C.kind = adjustKindToCapability(
|
|
|
|
C.kind, SupportedCompletionItemKinds);
|
|
|
|
LSPList.items.push_back(std::move(C));
|
|
|
|
}
|
|
|
|
return Reply(std::move(LSPList));
|
|
|
|
},
|
|
|
|
std::move(Reply)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClangdLSPServer::onSignatureHelp(const TextDocumentPositionParams &Params,
|
|
|
|
Callback<SignatureHelp> Reply) {
|
2018-09-26 13:48:29 +08:00
|
|
|
Server->signatureHelp(Params.textDocument.uri.file(), Params.position,
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
std::move(Reply));
|
2017-10-06 19:54:17 +08:00
|
|
|
}
|
|
|
|
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
void ClangdLSPServer::onGoToDefinition(const TextDocumentPositionParams &Params,
|
|
|
|
Callback<std::vector<Location>> Reply) {
|
2018-09-26 13:48:29 +08:00
|
|
|
Server->findDefinitions(Params.textDocument.uri.file(), Params.position,
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
std::move(Reply));
|
2017-06-29 00:12:10 +08:00
|
|
|
}
|
|
|
|
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
void ClangdLSPServer::onSwitchSourceHeader(const TextDocumentIdentifier &Params,
|
|
|
|
Callback<std::string> Reply) {
|
2018-10-20 23:30:37 +08:00
|
|
|
Optional<Path> Result = Server->switchSourceHeader(Params.uri.file());
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
Reply(Result ? URI::createFile(*Result).toString() : "");
|
2017-09-28 11:14:40 +08:00
|
|
|
}
|
|
|
|
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
void ClangdLSPServer::onDocumentHighlight(
|
|
|
|
const TextDocumentPositionParams &Params,
|
|
|
|
Callback<std::vector<DocumentHighlight>> Reply) {
|
|
|
|
Server->findDocumentHighlights(Params.textDocument.uri.file(),
|
|
|
|
Params.position, std::move(Reply));
|
[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] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
void ClangdLSPServer::onHover(const TextDocumentPositionParams &Params,
|
2018-10-20 23:30:37 +08:00
|
|
|
Callback<Optional<Hover>> Reply) {
|
2018-09-26 13:48:29 +08:00
|
|
|
Server->findHover(Params.textDocument.uri.file(), Params.position,
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
std::move(Reply));
|
[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(
|
2018-10-16 23:55:03 +08:00
|
|
|
const ClangdConfigurationParamsChange &Params) {
|
|
|
|
// Per-file update to the compilation database.
|
|
|
|
if (Params.compilationDatabaseChanges) {
|
|
|
|
const auto &CompileCommandUpdates = *Params.compilationDatabaseChanges;
|
2018-08-02 01:39:29 +08:00
|
|
|
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(
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
const DidChangeConfigurationParams &Params) {
|
2018-08-01 19:28:49 +08:00
|
|
|
applyConfiguration(Params.settings);
|
|
|
|
}
|
|
|
|
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
void ClangdLSPServer::onReference(const ReferenceParams &Params,
|
|
|
|
Callback<std::vector<Location>> Reply) {
|
2018-09-26 13:48:29 +08:00
|
|
|
Server->findReferences(Params.textDocument.uri.file(), Params.position,
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
std::move(Reply));
|
2018-09-05 19:53:07 +08:00
|
|
|
}
|
|
|
|
|
[clangd] Refactor JSON-over-stdin/stdout code into Transport abstraction. (re-land r344620)
Summary:
This paves the way for alternative transports (mac XPC, maybe messagepack?),
and also generally improves layering: testing ClangdLSPServer becomes less of
a pipe dream, we split up the JSONOutput monolith, etc.
This isn't a final state, much of what remains in JSONRPCDispatcher can go away,
handlers can call reply() on the transport directly, JSONOutput can be renamed
to StreamLogger and removed, etc. But this patch is sprawling already.
The main observable change (see tests) is that hitting EOF on input is now an
error: the client should send the 'exit' notification.
This is defensible: the protocol doesn't spell this case out. Reproducing the
current behavior for all combinations of shutdown/exit/EOF clutters interfaces.
We can iterate on this if desired.
Reviewers: jkorous, ioeric, hokein
Subscribers: mgorny, ilya-biryukov, MaskRay, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53286
llvm-svn: 344672
2018-10-17 15:32:05 +08:00
|
|
|
ClangdLSPServer::ClangdLSPServer(class Transport &Transp,
|
2017-11-24 00:58:22 +08:00
|
|
|
const clangd::CodeCompleteOptions &CCOpts,
|
2018-10-20 23:30:37 +08:00
|
|
|
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)
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
: Transp(Transp), MsgHandler(new MessageHandler(*this)),
|
[clangd] Refactor JSON-over-stdin/stdout code into Transport abstraction. (re-land r344620)
Summary:
This paves the way for alternative transports (mac XPC, maybe messagepack?),
and also generally improves layering: testing ClangdLSPServer becomes less of
a pipe dream, we split up the JSONOutput monolith, etc.
This isn't a final state, much of what remains in JSONRPCDispatcher can go away,
handlers can call reply() on the transport directly, JSONOutput can be renamed
to StreamLogger and removed, etc. But this patch is sprawling already.
The main observable change (see tests) is that hitting EOF on input is now an
error: the client should send the 'exit' notification.
This is defensible: the protocol doesn't spell this case out. Reproducing the
current behavior for all combinations of shutdown/exit/EOF clutters interfaces.
We can iterate on this if desired.
Reviewers: jkorous, ioeric, hokein
Subscribers: mgorny, ilya-biryukov, MaskRay, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53286
llvm-svn: 344672
2018-10-17 15:32:05 +08:00
|
|
|
CDB(ShouldUseInMemoryCDB ? CompilationDB::makeInMemory()
|
|
|
|
: CompilationDB::makeDirectoryBased(
|
|
|
|
std::move(CompileCommandsDir))),
|
2018-06-13 17:20:41 +08:00
|
|
|
CCOpts(CCOpts), SupportedSymbolKinds(defaultSymbolKinds()),
|
2018-09-28 01:13:07 +08:00
|
|
|
SupportedCompletionItemKinds(defaultCompletionItemKinds()),
|
[clangd] Enforce rules around "initialize" request, and create ClangdServer lazily.
Summary:
LSP is a slightly awkward map to C++ object lifetimes: the initialize request
is part of the protocol and provides information that doesn't change over the
lifetime of the server.
Until now, we handled this by initializing ClangdServer and ClangdLSPServer
right away, and making anything that can be set in the "initialize" request
mutable.
With this patch, we create ClangdLSPServer immediately, but defer creating
ClangdServer until "initialize". This opens the door to passing the relevant
initialize params in the constructor and storing them immutably.
(That change isn't actually done in this patch).
To make this safe, we have the MessageDispatcher enforce that the "initialize"
method is called before any other (as required by LSP). That way each method
handler can assume Server is initialized, as today.
As usual, while implementing this I found places where our test cases violated
the protocol.
Reviewers: ioeric
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53398
llvm-svn: 344741
2018-10-18 22:41:50 +08:00
|
|
|
ClangdServerOpts(Opts) {
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
// clang-format off
|
|
|
|
MsgHandler->bind("initialize", &ClangdLSPServer::onInitialize);
|
|
|
|
MsgHandler->bind("shutdown", &ClangdLSPServer::onShutdown);
|
|
|
|
MsgHandler->bind("textDocument/rangeFormatting", &ClangdLSPServer::onDocumentRangeFormatting);
|
|
|
|
MsgHandler->bind("textDocument/onTypeFormatting", &ClangdLSPServer::onDocumentOnTypeFormatting);
|
|
|
|
MsgHandler->bind("textDocument/formatting", &ClangdLSPServer::onDocumentFormatting);
|
|
|
|
MsgHandler->bind("textDocument/codeAction", &ClangdLSPServer::onCodeAction);
|
|
|
|
MsgHandler->bind("textDocument/completion", &ClangdLSPServer::onCompletion);
|
|
|
|
MsgHandler->bind("textDocument/signatureHelp", &ClangdLSPServer::onSignatureHelp);
|
|
|
|
MsgHandler->bind("textDocument/definition", &ClangdLSPServer::onGoToDefinition);
|
|
|
|
MsgHandler->bind("textDocument/references", &ClangdLSPServer::onReference);
|
|
|
|
MsgHandler->bind("textDocument/switchSourceHeader", &ClangdLSPServer::onSwitchSourceHeader);
|
|
|
|
MsgHandler->bind("textDocument/rename", &ClangdLSPServer::onRename);
|
|
|
|
MsgHandler->bind("textDocument/hover", &ClangdLSPServer::onHover);
|
|
|
|
MsgHandler->bind("textDocument/documentSymbol", &ClangdLSPServer::onDocumentSymbol);
|
|
|
|
MsgHandler->bind("workspace/executeCommand", &ClangdLSPServer::onCommand);
|
|
|
|
MsgHandler->bind("textDocument/documentHighlight", &ClangdLSPServer::onDocumentHighlight);
|
|
|
|
MsgHandler->bind("workspace/symbol", &ClangdLSPServer::onWorkspaceSymbol);
|
|
|
|
MsgHandler->bind("textDocument/didOpen", &ClangdLSPServer::onDocumentDidOpen);
|
|
|
|
MsgHandler->bind("textDocument/didClose", &ClangdLSPServer::onDocumentDidClose);
|
|
|
|
MsgHandler->bind("textDocument/didChange", &ClangdLSPServer::onDocumentDidChange);
|
|
|
|
MsgHandler->bind("workspace/didChangeWatchedFiles", &ClangdLSPServer::onFileEvent);
|
|
|
|
MsgHandler->bind("workspace/didChangeConfiguration", &ClangdLSPServer::onChangeConfiguration);
|
|
|
|
// clang-format on
|
|
|
|
}
|
|
|
|
|
|
|
|
ClangdLSPServer::~ClangdLSPServer() = default;
|
2017-05-16 17:38:59 +08:00
|
|
|
|
[clangd] Refactor JSON-over-stdin/stdout code into Transport abstraction. (re-land r344620)
Summary:
This paves the way for alternative transports (mac XPC, maybe messagepack?),
and also generally improves layering: testing ClangdLSPServer becomes less of
a pipe dream, we split up the JSONOutput monolith, etc.
This isn't a final state, much of what remains in JSONRPCDispatcher can go away,
handlers can call reply() on the transport directly, JSONOutput can be renamed
to StreamLogger and removed, etc. But this patch is sprawling already.
The main observable change (see tests) is that hitting EOF on input is now an
error: the client should send the 'exit' notification.
This is defensible: the protocol doesn't spell this case out. Reproducing the
current behavior for all combinations of shutdown/exit/EOF clutters interfaces.
We can iterate on this if desired.
Reviewers: jkorous, ioeric, hokein
Subscribers: mgorny, ilya-biryukov, MaskRay, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53286
llvm-svn: 344672
2018-10-17 15:32:05 +08:00
|
|
|
bool ClangdLSPServer::run() {
|
2017-05-16 22:40:30 +08:00
|
|
|
// Run the Language Server loop.
|
[clangd] Refactor JSON-over-stdin/stdout code into Transport abstraction. (re-land r344620)
Summary:
This paves the way for alternative transports (mac XPC, maybe messagepack?),
and also generally improves layering: testing ClangdLSPServer becomes less of
a pipe dream, we split up the JSONOutput monolith, etc.
This isn't a final state, much of what remains in JSONRPCDispatcher can go away,
handlers can call reply() on the transport directly, JSONOutput can be renamed
to StreamLogger and removed, etc. But this patch is sprawling already.
The main observable change (see tests) is that hitting EOF on input is now an
error: the client should send the 'exit' notification.
This is defensible: the protocol doesn't spell this case out. Reproducing the
current behavior for all combinations of shutdown/exit/EOF clutters interfaces.
We can iterate on this if desired.
Reviewers: jkorous, ioeric, hokein
Subscribers: mgorny, ilya-biryukov, MaskRay, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53286
llvm-svn: 344672
2018-10-17 15:32:05 +08:00
|
|
|
bool CleanExit = true;
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
if (auto Err = Transp.loop(*MsgHandler)) {
|
[clangd] Refactor JSON-over-stdin/stdout code into Transport abstraction. (re-land r344620)
Summary:
This paves the way for alternative transports (mac XPC, maybe messagepack?),
and also generally improves layering: testing ClangdLSPServer becomes less of
a pipe dream, we split up the JSONOutput monolith, etc.
This isn't a final state, much of what remains in JSONRPCDispatcher can go away,
handlers can call reply() on the transport directly, JSONOutput can be renamed
to StreamLogger and removed, etc. But this patch is sprawling already.
The main observable change (see tests) is that hitting EOF on input is now an
error: the client should send the 'exit' notification.
This is defensible: the protocol doesn't spell this case out. Reproducing the
current behavior for all combinations of shutdown/exit/EOF clutters interfaces.
We can iterate on this if desired.
Reviewers: jkorous, ioeric, hokein
Subscribers: mgorny, ilya-biryukov, MaskRay, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53286
llvm-svn: 344672
2018-10-17 15:32:05 +08:00
|
|
|
elog("Transport error: {0}", std::move(Err));
|
|
|
|
CleanExit = false;
|
|
|
|
}
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2018-09-26 13:48:29 +08:00
|
|
|
// Destroy ClangdServer to ensure all worker threads finish.
|
|
|
|
Server.reset();
|
[clangd] Refactor JSON-over-stdin/stdout code into Transport abstraction. (re-land r344620)
Summary:
This paves the way for alternative transports (mac XPC, maybe messagepack?),
and also generally improves layering: testing ClangdLSPServer becomes less of
a pipe dream, we split up the JSONOutput monolith, etc.
This isn't a final state, much of what remains in JSONRPCDispatcher can go away,
handlers can call reply() on the transport directly, JSONOutput can be renamed
to StreamLogger and removed, etc. But this patch is sprawling already.
The main observable change (see tests) is that hitting EOF on input is now an
error: the client should send the 'exit' notification.
This is defensible: the protocol doesn't spell this case out. Reproducing the
current behavior for all combinations of shutdown/exit/EOF clutters interfaces.
We can iterate on this if desired.
Reviewers: jkorous, ioeric, hokein
Subscribers: mgorny, ilya-biryukov, MaskRay, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53286
llvm-svn: 344672
2018-10-17 15:32:05 +08:00
|
|
|
return CleanExit && 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-10-20 23:30:37 +08:00
|
|
|
toLSPDiags(Diag, [&](clangd::Diagnostic Diag, 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];
|
2018-10-07 22:49:41 +08:00
|
|
|
llvm::copy(Fixes, 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.
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
notify("textDocument/publishDiagnostics",
|
|
|
|
json::Object{
|
|
|
|
{"uri", URIForFile{File}},
|
|
|
|
{"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())
|
2018-09-26 13:48:29 +08:00
|
|
|
Server->addDocument(FilePath, *DraftMgr.getDraft(FilePath),
|
|
|
|
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(
|
2018-10-20 23:30:37 +08:00
|
|
|
Optional<Path> CompileCommandsDir) {
|
2018-08-02 01:39:29 +08:00
|
|
|
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-10-20 23:30:37 +08:00
|
|
|
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|