diff options
| author | Utkarsh Saxena <usx@google.com> | 2019-09-24 13:38:33 +0000 |
|---|---|---|
| committer | Utkarsh Saxena <usx@google.com> | 2019-09-24 13:38:33 +0000 |
| commit | 55925da4c98b3ddac0f28ccc322e8ee52553e022 (patch) | |
| tree | 5bd9e806a55853ac2b3b1b89b8e68e1fae250ecc /clang-tools-extra/clangd/ClangdLSPServer.cpp | |
| parent | 3a415c20ad72f061c38282a49349c6ea31105ff2 (diff) | |
| download | bcm5719-llvm-55925da4c98b3ddac0f28ccc322e8ee52553e022.tar.gz bcm5719-llvm-55925da4c98b3ddac0f28ccc322e8ee52553e022.zip | |
[clangd] Add semantic selection to ClangdLSPServer.
Summary:
This adds semantic selection to the LSP Server.
Adds support for serialization of input request and the output reply.
Also adds regression tests for the feature.
Currently we do not support multi cursor.The LSP Server only accepts single position in the request as opposed to many position in the spec.
Spec:
https://github.com/microsoft/language-server-protocol/blob/dbaeumer/3.15/specification.md#textDocument_selectionRange
Reviewers: hokein
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D67720
llvm-svn: 372753
Diffstat (limited to 'clang-tools-extra/clangd/ClangdLSPServer.cpp')
| -rw-r--r-- | clang-tools-extra/clangd/ClangdLSPServer.cpp | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/clang-tools-extra/clangd/ClangdLSPServer.cpp b/clang-tools-extra/clangd/ClangdLSPServer.cpp index 0930c80da06..fd5b3444f34 100644 --- a/clang-tools-extra/clangd/ClangdLSPServer.cpp +++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp @@ -22,14 +22,18 @@ #include "llvm/ADT/Optional.h" #include "llvm/ADT/ScopeExit.h" #include "llvm/ADT/StringRef.h" +#include "llvm/ADT/iterator_range.h" #include "llvm/Support/Errc.h" #include "llvm/Support/Error.h" #include "llvm/Support/FormatVariadic.h" +#include "llvm/Support/JSON.h" #include "llvm/Support/Path.h" #include "llvm/Support/SHA1.h" #include "llvm/Support/ScopedPrinter.h" #include <cstddef> +#include <memory> #include <string> +#include <vector> namespace clang { namespace clangd { @@ -127,6 +131,21 @@ llvm::Error validateEdits(const DraftStore &DraftMgr, const Tweak::Effect &E) { llvm::to_string(InvalidFileCount - 1) + " others)"); } +// Converts a list of Ranges to a LinkedList of SelectionRange. +SelectionRange render(const std::vector<Range> &Ranges) { + if (Ranges.empty()) + return {}; + SelectionRange Result; + Result.range = Ranges[0]; + auto *Next = &Result.parent; + for (const auto &R : llvm::make_range(Ranges.begin() + 1, Ranges.end())) { + *Next = std::make_unique<SelectionRange>(); + Next->get()->range = R; + Next = &Next->get()->parent; + } + return Result; +} + } // namespace // MessageHandler dispatches incoming LSP messages. @@ -536,6 +555,7 @@ void ClangdLSPServer::onInitialize(const InitializeParams &Params, {"documentHighlightProvider", true}, {"hoverProvider", true}, {"renameProvider", std::move(RenameProvider)}, + {"selectionRangeProvider", true}, {"documentSymbolProvider", true}, {"workspaceSymbolProvider", true}, {"referencesProvider", true}, @@ -1125,6 +1145,30 @@ void ClangdLSPServer::onSymbolInfo(const TextDocumentPositionParams &Params, std::move(Reply)); } +void ClangdLSPServer::onSelectionRange( + const SelectionRangeParams &Params, + Callback<std::vector<SelectionRange>> Reply) { + if (Params.positions.size() != 1) { + elog("{0} positions provided to SelectionRange. Supports exactly one " + "position.", + Params.positions.size()); + return Reply(llvm::make_error<LSPError>( + "SelectionRange supports exactly one position", + ErrorCode::InvalidRequest)); + } + Server->semanticRanges( + Params.textDocument.uri.file(), Params.positions[0], + [Reply = std::move(Reply)]( + llvm::Expected<std::vector<Range>> Ranges) mutable { + if (!Ranges) { + return Reply(Ranges.takeError()); + } + std::vector<SelectionRange> Result; + Result.emplace_back(render(std::move(*Ranges))); + return Reply(std::move(Result)); + }); +} + ClangdLSPServer::ClangdLSPServer( class Transport &Transp, const FileSystemProvider &FSProvider, const clangd::CodeCompleteOptions &CCOpts, @@ -1167,6 +1211,7 @@ ClangdLSPServer::ClangdLSPServer( MsgHandler->bind("textDocument/symbolInfo", &ClangdLSPServer::onSymbolInfo); MsgHandler->bind("textDocument/typeHierarchy", &ClangdLSPServer::onTypeHierarchy); MsgHandler->bind("typeHierarchy/resolve", &ClangdLSPServer::onResolveTypeHierarchy); + MsgHandler->bind("textDocument/selectionRange", &ClangdLSPServer::onSelectionRange); // clang-format on } |

