2017-02-07 18:28:20 +08:00
|
|
|
//===--- ProtocolHandlers.cpp - LSP callbacks -----------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ProtocolHandlers.h"
|
2017-05-16 22:40:30 +08:00
|
|
|
#include "ClangdLSPServer.h"
|
2017-05-16 17:38:59 +08:00
|
|
|
#include "ClangdServer.h"
|
|
|
|
#include "DraftStore.h"
|
2017-11-02 17:21:51 +08:00
|
|
|
#include "Trace.h"
|
2017-10-12 21:29:58 +08:00
|
|
|
|
2017-02-07 18:28:20 +08:00
|
|
|
using namespace clang;
|
2017-10-12 21:29:58 +08:00
|
|
|
using namespace clang::clangd;
|
2017-02-07 18:28:20 +08:00
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
namespace {
|
2017-02-07 18:28:20 +08:00
|
|
|
|
2017-10-12 21:29:58 +08:00
|
|
|
// Helper for attaching ProtocolCallbacks methods to a JSONRPCDispatcher.
|
|
|
|
// Invoke like: Registerer("foo", &ProtocolCallbacks::onFoo)
|
|
|
|
// onFoo should be: void onFoo(Ctx &C, FooParams &Params)
|
2017-12-01 05:32:29 +08:00
|
|
|
// FooParams should have a fromJSON function.
|
2017-10-12 21:29:58 +08:00
|
|
|
struct HandlerRegisterer {
|
|
|
|
template <typename Param>
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
void operator()(StringRef Method, void (ProtocolCallbacks::*Handler)(Param)) {
|
2017-10-12 21:29:58 +08:00
|
|
|
// Capture pointers by value, as the lambda will outlive this object.
|
|
|
|
auto *Callbacks = this->Callbacks;
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
Dispatcher.registerHandler(Method, [=](const json::Expr &RawParams) {
|
|
|
|
typename std::remove_reference<Param>::type P;
|
|
|
|
if (fromJSON(RawParams, P)) {
|
|
|
|
(Callbacks->*Handler)(P);
|
|
|
|
} else {
|
|
|
|
log("Failed to decode " + Method + " request.");
|
|
|
|
}
|
|
|
|
});
|
2017-10-03 02:00:37 +08:00
|
|
|
}
|
|
|
|
|
2017-10-12 21:29:58 +08:00
|
|
|
JSONRPCDispatcher &Dispatcher;
|
|
|
|
ProtocolCallbacks *Callbacks;
|
2017-10-03 02:00:37 +08:00
|
|
|
};
|
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
} // namespace
|
|
|
|
|
2017-09-30 00:41:23 +08:00
|
|
|
void clangd::registerCallbackHandlers(JSONRPCDispatcher &Dispatcher,
|
2017-10-10 22:21:04 +08:00
|
|
|
ProtocolCallbacks &Callbacks) {
|
2018-03-08 05:47:25 +08:00
|
|
|
HandlerRegisterer Register{Dispatcher, &Callbacks};
|
2017-10-12 21:29:58 +08:00
|
|
|
|
|
|
|
Register("initialize", &ProtocolCallbacks::onInitialize);
|
|
|
|
Register("shutdown", &ProtocolCallbacks::onShutdown);
|
2017-10-25 16:45:41 +08:00
|
|
|
Register("exit", &ProtocolCallbacks::onExit);
|
2017-10-12 21:29:58 +08:00
|
|
|
Register("textDocument/didOpen", &ProtocolCallbacks::onDocumentDidOpen);
|
|
|
|
Register("textDocument/didClose", &ProtocolCallbacks::onDocumentDidClose);
|
|
|
|
Register("textDocument/didChange", &ProtocolCallbacks::onDocumentDidChange);
|
|
|
|
Register("textDocument/rangeFormatting",
|
|
|
|
&ProtocolCallbacks::onDocumentRangeFormatting);
|
|
|
|
Register("textDocument/onTypeFormatting",
|
|
|
|
&ProtocolCallbacks::onDocumentOnTypeFormatting);
|
|
|
|
Register("textDocument/formatting", &ProtocolCallbacks::onDocumentFormatting);
|
|
|
|
Register("textDocument/codeAction", &ProtocolCallbacks::onCodeAction);
|
|
|
|
Register("textDocument/completion", &ProtocolCallbacks::onCompletion);
|
|
|
|
Register("textDocument/signatureHelp", &ProtocolCallbacks::onSignatureHelp);
|
|
|
|
Register("textDocument/definition", &ProtocolCallbacks::onGoToDefinition);
|
|
|
|
Register("textDocument/switchSourceHeader",
|
|
|
|
&ProtocolCallbacks::onSwitchSourceHeader);
|
2017-11-09 19:30:04 +08:00
|
|
|
Register("textDocument/rename", &ProtocolCallbacks::onRename);
|
[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
|
|
|
Register("textDocument/hover", &ProtocolCallbacks::onHover);
|
2017-10-12 21:29:58 +08:00
|
|
|
Register("workspace/didChangeWatchedFiles", &ProtocolCallbacks::onFileEvent);
|
[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
|
|
|
Register("workspace/executeCommand", &ProtocolCallbacks::onCommand);
|
[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
|
|
|
Register("textDocument/documentHighlight",
|
|
|
|
&ProtocolCallbacks::onDocumentHighlight);
|
[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
|
|
|
Register("workspace/didChangeConfiguration",
|
|
|
|
&ProtocolCallbacks::onChangeConfiguration);
|
2017-04-04 17:46:39 +08:00
|
|
|
}
|