2017-05-16 17:38:59 +08:00
|
|
|
//===--- ClangdLSPServer.h - LSP server --------------------------*- C++-*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-05-16 17:38:59 +08:00
|
|
|
//
|
2018-08-15 00:03:32 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2017-05-16 17:38:59 +08:00
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDLSPSERVER_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDLSPSERVER_H
|
|
|
|
|
|
|
|
#include "ClangdServer.h"
|
2018-03-16 22:30:42 +08:00
|
|
|
#include "DraftStore.h"
|
2019-04-11 00:48:52 +08:00
|
|
|
#include "Features.inc"
|
[clangd] Implementation of workspace/symbol request
Summary:
This is a basic implementation of the "workspace/symbol" request which is
used to find symbols by a string query. Since this is similar to code completion
in terms of result, this implementation reuses the "fuzzyFind" in order to get
matches. For now, the scoring algorithm is the same as code completion and
improvements could be done in the future.
The index model doesn't contain quite enough symbols for this to cover
common symbols like methods, enum class enumerators, functions in unamed
namespaces, etc. The index model will be augmented separately to achieve this.
Reviewers: sammccall, ilya-biryukov
Reviewed By: sammccall
Subscribers: jkorous, hokein, simark, sammccall, klimek, mgorny, ilya-biryukov, mgrang, jkorous-apple, ioeric, MaskRay, cfe-commits
Differential Revision: https://reviews.llvm.org/D44882
llvm-svn: 330637
2018-04-24 04:00:52 +08:00
|
|
|
#include "FindSymbols.h"
|
2017-06-13 23:59:43 +08:00
|
|
|
#include "GlobalCompilationDatabase.h"
|
2017-05-16 17:38:59 +08:00
|
|
|
#include "Path.h"
|
|
|
|
#include "Protocol.h"
|
[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 "Transport.h"
|
2017-05-16 17:38:59 +08:00
|
|
|
#include "clang/Tooling/Core/Replacement.h"
|
2017-07-19 23:43:35 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2018-09-26 13:48:29 +08:00
|
|
|
#include <memory>
|
2017-05-16 17:38:59 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
|
2018-01-10 22:44:34 +08:00
|
|
|
class SymbolIndex;
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2018-09-13 20:58:36 +08:00
|
|
|
/// This class exposes ClangdServer's capabilities via Language Server Protocol.
|
|
|
|
///
|
[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 binds the implemented LSP methods (e.g. onInitialize) to
|
|
|
|
/// corresponding JSON-RPC methods ("initialize").
|
|
|
|
/// The server also supports $/cancelRequest (MessageHandler provides this).
|
|
|
|
class ClangdLSPServer : private DiagnosticsConsumer {
|
2017-05-16 17:38:59 +08:00
|
|
|
public:
|
2017-10-02 23:13:20 +08:00
|
|
|
/// If \p CompileCommandsDir has a value, compile_commands.json will be
|
|
|
|
/// loaded only from \p CompileCommandsDir. Otherwise, clangd will look
|
|
|
|
/// for compile_commands.json in all parent directories of each file.
|
2018-11-02 21:09:36 +08:00
|
|
|
/// If UseDirBasedCDB is false, compile commands are not read from disk.
|
|
|
|
// FIXME: Clean up signature around CDBs.
|
2019-01-22 17:39:05 +08:00
|
|
|
ClangdLSPServer(Transport &Transp, const FileSystemProvider &FSProvider,
|
|
|
|
const clangd::CodeCompleteOptions &CCOpts,
|
2018-11-02 21:09:36 +08:00
|
|
|
llvm::Optional<Path> CompileCommandsDir, bool UseDirBasedCDB,
|
2019-03-28 01:47:49 +08:00
|
|
|
llvm::Optional<OffsetEncoding> ForcedOffsetEncoding,
|
2018-11-02 21:09:36 +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
|
|
|
~ClangdLSPServer();
|
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
|
|
|
/// Run LSP server loop, communicating with the Transport provided in the
|
|
|
|
/// constructor. This method must not be executed more than once.
|
2017-10-25 16:45:41 +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
|
|
|
/// \return Whether we shut down cleanly with a 'shutdown' -> 'exit' sequence.
|
|
|
|
bool run();
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
private:
|
2017-09-30 18:08:52 +08:00
|
|
|
// Implement DiagnosticsConsumer.
|
2018-03-13 07:22:35 +08:00
|
|
|
void onDiagnosticsReady(PathRef File, std::vector<Diag> Diagnostics) override;
|
2018-12-20 23:39:12 +08:00
|
|
|
void onFileUpdated(PathRef File, const TUStatus &Status) override;
|
2019-08-01 16:08:44 +08:00
|
|
|
void onHighlightingsReady(PathRef File,
|
|
|
|
std::vector<HighlightingToken> Highlightings,
|
|
|
|
int NumLines) override;
|
2017-09-30 18:08: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
|
|
|
// LSP methods. Notifications have signature void(const Params&).
|
|
|
|
// Calls have signature void(const Params&, Callback<Response>).
|
|
|
|
void onInitialize(const InitializeParams &, Callback<llvm::json::Value>);
|
|
|
|
void onShutdown(const ShutdownParams &, Callback<std::nullptr_t>);
|
[clangd] Enable auto-index behind a flag.
Summary:
Ownership and configuration:
The auto-index (background index) is maintained by ClangdServer, like Dynamic.
(This means ClangdServer will be able to enqueue preamble indexing in future).
For now it's enabled by a simple boolean flag in ClangdServer::Options, but
we probably want to eventually allow injecting the storage strategy.
New 'sync' command:
In order to meaningfully test the integration (not just unit-test components)
we need a way for tests to ensure the asynchronous index reads/writes occur
before a certain point.
Because these tests and assertions are few, I think exposing an explicit "sync"
command for use in tests is simpler than allowing threading to be completely
disabled in the background index (as we do for TUScheduler).
Bugs:
I fixed a couple of trivial bugs I found while testing, but there's one I can't.
JSONCompilationDatabase::getAllFiles() may return relative paths, and currently
we trigger an assertion that assumes they are absolute.
There's no efficient way to resolve them (you have to retrieve the corresponding
command and then resolve against its directory property). In general I think
this behavior is broken and we should fix it in JSONCompilationDatabase and
require CompilationDatabase::getAllFiles() to be absolute.
Reviewers: kadircet
Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, arphaman, cfe-commits
Differential Revision: https://reviews.llvm.org/D54894
llvm-svn: 347567
2018-11-27 00:00:11 +08:00
|
|
|
void onSync(const NoParams &, Callback<std::nullptr_t>);
|
[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 onDocumentDidOpen(const DidOpenTextDocumentParams &);
|
|
|
|
void onDocumentDidChange(const DidChangeTextDocumentParams &);
|
|
|
|
void onDocumentDidClose(const DidCloseTextDocumentParams &);
|
|
|
|
void onDocumentOnTypeFormatting(const DocumentOnTypeFormattingParams &,
|
|
|
|
Callback<std::vector<TextEdit>>);
|
|
|
|
void onDocumentRangeFormatting(const DocumentRangeFormattingParams &,
|
|
|
|
Callback<std::vector<TextEdit>>);
|
|
|
|
void onDocumentFormatting(const DocumentFormattingParams &,
|
|
|
|
Callback<std::vector<TextEdit>>);
|
2018-11-23 23:21:19 +08:00
|
|
|
// The results are serialized 'vector<DocumentSymbol>' if
|
|
|
|
// SupportsHierarchicalDocumentSymbol is true and 'vector<SymbolInformation>'
|
|
|
|
// otherwise.
|
[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 onDocumentSymbol(const DocumentSymbolParams &,
|
2018-11-23 23:21:19 +08:00
|
|
|
Callback<llvm::json::Value>);
|
[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 onCodeAction(const CodeActionParams &, Callback<llvm::json::Value>);
|
2019-01-03 21:37:12 +08:00
|
|
|
void onCompletion(const CompletionParams &, Callback<CompletionList>);
|
[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 onSignatureHelp(const TextDocumentPositionParams &,
|
|
|
|
Callback<SignatureHelp>);
|
[clangd] Implement textDocument/declaration from LSP 3.14
Summary:
LSP now reflects the declaration/definition distinction.
Language server changes:
- textDocument/definition now returns a definition if one is found, otherwise
the declaration. It no longer returns declaration + definition if they are
distinct.
- textDocument/declaration returns the best declaration we can find.
- For macros, the active macro definition is returned for both methods.
- For include directive, the top of the target file is returned for both.
There doesn't appear to be a discovery mechanism (we can't return everything to
clients that only know about definition), so this changes existing behavior.
In practice, it should greatly reduce the fraction of the time we need to show
the user a menu of options.
C++ API changes:
- findDefinitions is replaced by locateSymbolAt, which returns a
vector<LocatedSymbol> - one for each symbol under the cursor.
- this contains the preferred declaration, the definition (if found), and
the symbol name
This API enables some potentially-neat extensions, like swapping between decl
and def, and exposing the symbol name to the UI in the case of multiple symbols.
Reviewers: hokein
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D57388
llvm-svn: 352864
2019-02-01 19:26:13 +08:00
|
|
|
void onGoToDeclaration(const TextDocumentPositionParams &,
|
|
|
|
Callback<std::vector<Location>>);
|
[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 onGoToDefinition(const TextDocumentPositionParams &,
|
|
|
|
Callback<std::vector<Location>>);
|
|
|
|
void onReference(const ReferenceParams &, Callback<std::vector<Location>>);
|
|
|
|
void onSwitchSourceHeader(const TextDocumentIdentifier &,
|
2019-05-07 16:30:32 +08:00
|
|
|
Callback<llvm::Optional<URIForFile>>);
|
[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 onDocumentHighlight(const TextDocumentPositionParams &,
|
|
|
|
Callback<std::vector<DocumentHighlight>>);
|
|
|
|
void onFileEvent(const DidChangeWatchedFilesParams &);
|
|
|
|
void onCommand(const ExecuteCommandParams &, Callback<llvm::json::Value>);
|
|
|
|
void onWorkspaceSymbol(const WorkspaceSymbolParams &,
|
|
|
|
Callback<std::vector<SymbolInformation>>);
|
2019-07-24 15:49:23 +08:00
|
|
|
void onPrepareRename(const TextDocumentPositionParams &,
|
|
|
|
Callback<llvm::Optional<Range>>);
|
[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 onRename(const RenameParams &, Callback<WorkspaceEdit>);
|
|
|
|
void onHover(const TextDocumentPositionParams &,
|
|
|
|
Callback<llvm::Optional<Hover>>);
|
[clangd] Add support for type hierarchy (super types only for now)
Summary:
Patch by Nathan Ridge(@nridge)!
This is an LSP extension proposed here:
https://github.com/Microsoft/vscode-languageserver-node/pull/426
An example client implementation can be found here:
https://github.com/theia-ide/theia/pull/3802
Reviewers: kadircet, sammccall
Reviewed By: kadircet
Subscribers: jdoerfert, sammccall, cfe-commits, mgorny, dschaefer, simark, ilya-biryukov, ioeric, MaskRay, jkorous, arphaman, kadircet
Tags: #clang
Differential Revision: https://reviews.llvm.org/D56370
llvm-svn: 356445
2019-03-19 17:27:04 +08:00
|
|
|
void onTypeHierarchy(const TypeHierarchyParams &,
|
|
|
|
Callback<llvm::Optional<TypeHierarchyItem>>);
|
2019-07-13 11:24:48 +08:00
|
|
|
void onResolveTypeHierarchy(const ResolveTypeHierarchyItemParams &,
|
|
|
|
Callback<llvm::Optional<TypeHierarchyItem>>);
|
[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 onChangeConfiguration(const DidChangeConfigurationParams &);
|
2018-11-28 00:40:46 +08:00
|
|
|
void onSymbolInfo(const TextDocumentPositionParams &,
|
|
|
|
Callback<std::vector<SymbolDetails>>);
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2018-03-12 23:28:22 +08:00
|
|
|
std::vector<Fix> getFixes(StringRef File, const clangd::Diagnostic &D);
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2019-01-03 21:37:12 +08:00
|
|
|
/// Checks if completion request should be ignored. We need this due to the
|
|
|
|
/// limitation of the LSP. Per LSP, a client sends requests for all "trigger
|
|
|
|
/// character" we specify, but for '>' and ':' we need to check they actually
|
|
|
|
/// produce '->' and '::', respectively.
|
|
|
|
bool shouldRunCompletion(const CompletionParams &Params) const;
|
|
|
|
|
2018-03-16 22:30:42 +08:00
|
|
|
/// Forces a reparse of all currently opened files. As a result, this method
|
|
|
|
/// may be very expensive. This method is normally called when the
|
|
|
|
/// compilation database is changed.
|
|
|
|
void reparseOpenedFiles();
|
2018-10-25 12:22:52 +08:00
|
|
|
void applyConfiguration(const ConfigurationSettings &Settings);
|
2018-03-16 22:30:42 +08:00
|
|
|
|
2019-07-04 15:53:12 +08:00
|
|
|
/// Sends a "publishSemanticHighlighting" notification to the LSP client.
|
|
|
|
void publishSemanticHighlighting(SemanticHighlightingParams Params);
|
|
|
|
|
2019-03-25 18:15:11 +08:00
|
|
|
/// Sends a "publishDiagnostics" notification to the LSP client.
|
|
|
|
void publishDiagnostics(const URIForFile &File,
|
|
|
|
std::vector<clangd::Diagnostic> Diagnostics);
|
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
/// Used to indicate that the 'shutdown' request was received from the
|
|
|
|
/// Language Server client.
|
2017-10-25 16:45:41 +08:00
|
|
|
bool ShutdownRequestReceived = false;
|
|
|
|
|
2019-08-05 20:48:09 +08:00
|
|
|
/// Used to indicate the ClangdLSPServer is being destroyed.
|
|
|
|
std::atomic<bool> IsBeingDestroyed = {false};
|
|
|
|
|
2017-05-16 17:38:59 +08:00
|
|
|
std::mutex FixItsMutex;
|
2018-03-12 23:28:22 +08:00
|
|
|
typedef std::map<clangd::Diagnostic, std::vector<Fix>, LSPDiagnosticCompare>
|
2017-05-16 17:38:59 +08:00
|
|
|
DiagnosticToReplacementMap;
|
|
|
|
/// Caches FixIts per file and diagnostics
|
|
|
|
llvm::StringMap<DiagnosticToReplacementMap> FixItsMap;
|
2019-08-01 16:08:44 +08:00
|
|
|
std::mutex HighlightingsMutex;
|
|
|
|
llvm::StringMap<std::vector<HighlightingToken>> FileToHighlightings;
|
2017-06-13 23:59:43 +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
|
|
|
// Most code should not deal with Transport directly.
|
|
|
|
// MessageHandler deals with incoming messages, use call() etc for outgoing.
|
[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
|
|
|
clangd::Transport &Transp;
|
[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 MessageHandler;
|
|
|
|
std::unique_ptr<MessageHandler> MsgHandler;
|
|
|
|
std::mutex TranspWriter;
|
2019-08-05 20:48:09 +08:00
|
|
|
|
|
|
|
template <typename Response>
|
|
|
|
void call(StringRef Method, llvm::json::Value Params, Callback<Response> CB) {
|
|
|
|
// Wrap the callback with LSP conversion and error-handling.
|
2019-08-15 22:16:06 +08:00
|
|
|
auto HandleReply =
|
|
|
|
[CB = std::move(CB)](
|
|
|
|
llvm::Expected<llvm::json::Value> RawResponse) mutable {
|
|
|
|
Response Rsp;
|
|
|
|
if (!RawResponse) {
|
|
|
|
CB(RawResponse.takeError());
|
|
|
|
} else if (fromJSON(*RawResponse, Rsp)) {
|
|
|
|
CB(std::move(Rsp));
|
|
|
|
} else {
|
|
|
|
elog("Failed to decode {0} response", *RawResponse);
|
|
|
|
CB(llvm::make_error<LSPError>("failed to decode reponse",
|
|
|
|
ErrorCode::InvalidParams));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
callRaw(Method, std::move(Params), std::move(HandleReply));
|
2019-08-05 20:48:09 +08:00
|
|
|
}
|
|
|
|
void callRaw(StringRef Method, llvm::json::Value Params,
|
|
|
|
Callback<llvm::json::Value> CB);
|
[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 notify(StringRef Method, llvm::json::Value Params);
|
|
|
|
|
2019-01-22 17:39:05 +08:00
|
|
|
const FileSystemProvider &FSProvider;
|
2017-12-05 18:42:57 +08:00
|
|
|
/// Options used for code completion
|
|
|
|
clangd::CodeCompleteOptions CCOpts;
|
2018-08-11 01:25:07 +08:00
|
|
|
/// Options used for diagnostics.
|
|
|
|
ClangdDiagnosticOptions DiagOpts;
|
[clangd] Implementation of workspace/symbol request
Summary:
This is a basic implementation of the "workspace/symbol" request which is
used to find symbols by a string query. Since this is similar to code completion
in terms of result, this implementation reuses the "fuzzyFind" in order to get
matches. For now, the scoring algorithm is the same as code completion and
improvements could be done in the future.
The index model doesn't contain quite enough symbols for this to cover
common symbols like methods, enum class enumerators, functions in unamed
namespaces, etc. The index model will be augmented separately to achieve this.
Reviewers: sammccall, ilya-biryukov
Reviewed By: sammccall
Subscribers: jkorous, hokein, simark, sammccall, klimek, mgorny, ilya-biryukov, mgrang, jkorous-apple, ioeric, MaskRay, cfe-commits
Differential Revision: https://reviews.llvm.org/D44882
llvm-svn: 330637
2018-04-24 04:00:52 +08:00
|
|
|
/// The supported kinds of the client.
|
|
|
|
SymbolKindBitset SupportedSymbolKinds;
|
2018-09-28 01:13:07 +08:00
|
|
|
/// The supported completion item kinds of the client.
|
|
|
|
CompletionItemKindBitset SupportedCompletionItemKinds;
|
2018-12-20 23:39:12 +08:00
|
|
|
/// Whether the client supports CodeAction response objects.
|
2018-10-17 00:29:41 +08:00
|
|
|
bool SupportsCodeAction = false;
|
2018-11-23 23:21:19 +08:00
|
|
|
/// From capabilities of textDocument/documentSymbol.
|
|
|
|
bool SupportsHierarchicalDocumentSymbol = false;
|
2018-12-20 23:39:12 +08:00
|
|
|
/// Whether the client supports showing file status.
|
|
|
|
bool SupportFileStatus = false;
|
2019-05-29 18:01:00 +08:00
|
|
|
/// Which kind of markup should we use in textDocument/hover responses.
|
|
|
|
MarkupKind HoverContentFormat = MarkupKind::PlainText;
|
2019-06-04 17:36:59 +08:00
|
|
|
/// Whether the client supports offsets for parameter info labels.
|
|
|
|
bool SupportsOffsetsInSignatureHelp = false;
|
|
|
|
// Store of the current versions of the open documents.
|
2018-03-16 22:30:42 +08:00
|
|
|
DraftStore DraftMgr;
|
|
|
|
|
2018-10-23 22:19:54 +08:00
|
|
|
// The CDB is created by the "initialize" LSP method.
|
2018-11-02 21:09:36 +08:00
|
|
|
bool UseDirBasedCDB; // FIXME: make this a capability.
|
2018-10-25 10:22:53 +08:00
|
|
|
llvm::Optional<Path> CompileCommandsDir; // FIXME: merge with capability?
|
2018-11-02 21:09:36 +08:00
|
|
|
std::unique_ptr<GlobalCompilationDatabase> BaseCDB;
|
|
|
|
// CDB is BaseCDB plus any comands overridden via LSP extensions.
|
|
|
|
llvm::Optional<OverlayCDB> CDB;
|
[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
|
|
|
// The ClangdServer is created by the "initialize" LSP method.
|
|
|
|
// It is destroyed before run() returns, to ensure worker threads exit.
|
|
|
|
ClangdServer::Options ClangdServerOpts;
|
|
|
|
llvm::Optional<ClangdServer> Server;
|
2019-03-28 01:47:49 +08:00
|
|
|
llvm::Optional<OffsetEncoding> NegotiatedOffsetEncoding;
|
2018-08-24 21:09:41 +08:00
|
|
|
};
|
2017-05-16 17:38:59 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|
|
|
|
|
2018-08-15 00:03:32 +08:00
|
|
|
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDLSPSERVER_H
|