[clang-tools-extra] r372102 - Add SemanticRanges to Clangd server.
Utkarsh Saxena via cfe-commits
cfe-commits at lists.llvm.org
Tue Sep 17 03:28:05 PDT 2019
Author: usaxena95
Date: Tue Sep 17 03:28:05 2019
New Revision: 372102
URL: http://llvm.org/viewvc/llvm-project?rev=372102&view=rev
Log:
Add SemanticRanges to Clangd server.
Summary:
Adds Semantic Ranges capabilities to Clangd server.
Also adds tests for running it via clangd server.
This differs from the LSP spec as the spec needs this to be evaluated on multiple 'pos' and the expected output is an list of list of semantic ranges.
This is majorly for multi cursor and assuming this is a rare thing, we don't want to optimize make things complicated just for this.
This should be done in the LSP level by queueing one request per 'pos' in the input.
LSP 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/D67650
Modified:
clang-tools-extra/trunk/clangd/ClangdServer.cpp
clang-tools-extra/trunk/clangd/ClangdServer.h
clang-tools-extra/trunk/clangd/unittests/SemanticSelectionTests.cpp
clang-tools-extra/trunk/clangd/unittests/SyncAPI.cpp
clang-tools-extra/trunk/clangd/unittests/SyncAPI.h
Modified: clang-tools-extra/trunk/clangd/ClangdServer.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdServer.cpp?rev=372102&r1=372101&r2=372102&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/ClangdServer.cpp (original)
+++ clang-tools-extra/trunk/clangd/ClangdServer.cpp Tue Sep 17 03:28:05 2019
@@ -17,6 +17,7 @@
#include "Preamble.h"
#include "Protocol.h"
#include "SemanticHighlighting.h"
+#include "SemanticSelection.h"
#include "SourceCode.h"
#include "TUScheduler.h"
#include "Trace.h"
@@ -125,8 +126,8 @@ ClangdServer::ClangdServer(const GlobalC
// critical paths.
WorkScheduler(
CDB, Opts.AsyncThreadsCount, Opts.StorePreamblesInMemory,
- std::make_unique<UpdateIndexCallbacks>(
- DynamicIdx.get(), DiagConsumer, Opts.SemanticHighlighting),
+ std::make_unique<UpdateIndexCallbacks>(DynamicIdx.get(), DiagConsumer,
+ Opts.SemanticHighlighting),
Opts.UpdateDebounce, Opts.RetentionPolicy) {
// Adds an index to the stack, at higher priority than existing indexes.
auto AddIndex = [&](SymbolIndex *Idx) {
@@ -620,6 +621,17 @@ void ClangdServer::symbolInfo(PathRef Fi
WorkScheduler.runWithAST("SymbolInfo", File, std::move(Action));
}
+void ClangdServer::semanticRanges(PathRef File, Position Pos,
+ Callback<std::vector<Range>> CB) {
+ auto Action =
+ [Pos, CB = std::move(CB)](llvm::Expected<InputsAndAST> InpAST) mutable {
+ if (!InpAST)
+ return CB(InpAST.takeError());
+ CB(clangd::getSemanticRanges(InpAST->AST, Pos));
+ };
+ WorkScheduler.runWithAST("SemanticRanges", File, std::move(Action));
+}
+
std::vector<std::pair<Path, std::size_t>>
ClangdServer::getUsedBytesPerFile() const {
return WorkScheduler.getUsedBytesPerFile();
Modified: clang-tools-extra/trunk/clangd/ClangdServer.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdServer.h?rev=372102&r1=372101&r2=372102&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/ClangdServer.h (original)
+++ clang-tools-extra/trunk/clangd/ClangdServer.h Tue Sep 17 03:28:05 2019
@@ -277,6 +277,10 @@ public:
void symbolInfo(PathRef File, Position Pos,
Callback<std::vector<SymbolDetails>> CB);
+ /// Get semantic ranges around a specified position in a file.
+ void semanticRanges(PathRef File, Position Pos,
+ Callback<std::vector<Range>> CB);
+
/// Returns estimated memory usage for each of the currently open files.
/// The order of results is unspecified.
/// Overall memory usage of clangd may be significantly more than reported
Modified: clang-tools-extra/trunk/clangd/unittests/SemanticSelectionTests.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/SemanticSelectionTests.cpp?rev=372102&r1=372101&r2=372102&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/unittests/SemanticSelectionTests.cpp (original)
+++ clang-tools-extra/trunk/clangd/unittests/SemanticSelectionTests.cpp Tue Sep 17 03:28:05 2019
@@ -7,10 +7,13 @@
//===----------------------------------------------------------------------===//
#include "Annotations.h"
+#include "ClangdServer.h"
#include "Matchers.h"
#include "Protocol.h"
#include "SemanticSelection.h"
#include "SourceCode.h"
+#include "SyncAPI.h"
+#include "TestFS.h"
#include "TestTU.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
@@ -23,6 +26,11 @@ namespace clangd {
namespace {
using ::testing::ElementsAreArray;
+class IgnoreDiagnostics : public DiagnosticsConsumer {
+ void onDiagnosticsReady(PathRef File,
+ std::vector<Diag> Diagnostics) override {}
+};
+
TEST(SemanticSelection, All) {
const char *Tests[] = {
R"cpp( // Single statement in a function body.
@@ -138,6 +146,36 @@ TEST(SemanticSelection, All) {
<< Test;
}
}
+
+TEST(SemanticSelection, RunViaClangDServer) {
+ MockFSProvider FS;
+ IgnoreDiagnostics DiagConsumer;
+ MockCompilationDatabase CDB;
+ ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
+
+ auto FooH = testPath("foo.h");
+ FS.Files[FooH] = R"cpp(
+ int foo(int x);
+ #define HASH(x) ((x) % 10)
+ )cpp";
+
+ auto FooCpp = testPath("Foo.cpp");
+ const char *SourceContents = R"cpp(
+ #include "foo.h"
+ [[void bar(int& inp) [[{
+ // inp = HASH(foo(inp));
+ [[inp = [[HASH([[foo([[in^p]])]])]]]];
+ }]]]]
+ )cpp";
+ Annotations SourceAnnotations(SourceContents);
+ FS.Files[FooCpp] = SourceAnnotations.code();
+ Server.addDocument(FooCpp, SourceAnnotations.code());
+
+ auto Ranges = runSemanticRanges(Server, FooCpp, SourceAnnotations.point());
+ ASSERT_TRUE(bool(Ranges))
+ << "getSemanticRange returned an error: " << Ranges.takeError();
+ EXPECT_THAT(*Ranges, ElementsAreArray(SourceAnnotations.ranges()));
+}
} // namespace
} // namespace clangd
} // namespace clang
Modified: clang-tools-extra/trunk/clangd/unittests/SyncAPI.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/SyncAPI.cpp?rev=372102&r1=372101&r2=372102&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/unittests/SyncAPI.cpp (original)
+++ clang-tools-extra/trunk/clangd/unittests/SyncAPI.cpp Tue Sep 17 03:28:05 2019
@@ -145,5 +145,12 @@ RefSlab getRefs(const SymbolIndex &Index
return std::move(Slab).build();
}
+llvm::Expected<std::vector<Range>>
+runSemanticRanges(ClangdServer &Server, PathRef File, Position Pos) {
+ llvm::Optional<llvm::Expected<std::vector<Range>>> Result;
+ Server.semanticRanges(File, Pos, capture(Result));
+ return std::move(*Result);
+}
+
} // namespace clangd
} // namespace clang
Modified: clang-tools-extra/trunk/clangd/unittests/SyncAPI.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/SyncAPI.h?rev=372102&r1=372101&r2=372102&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/unittests/SyncAPI.h (original)
+++ clang-tools-extra/trunk/clangd/unittests/SyncAPI.h Tue Sep 17 03:28:05 2019
@@ -53,6 +53,9 @@ SymbolSlab runFuzzyFind(const SymbolInde
SymbolSlab runFuzzyFind(const SymbolIndex &Index, const FuzzyFindRequest &Req);
RefSlab getRefs(const SymbolIndex &Index, SymbolID ID);
+llvm::Expected<std::vector<Range>>
+runSemanticRanges(ClangdServer &Server, PathRef File, Position Pos);
+
} // namespace clangd
} // namespace clang
More information about the cfe-commits
mailing list