sammccall created this revision. sammccall added a reviewer: usaxena95. Herald added subscribers: cfe-commits, kadircet, arphaman, jkorous, MaskRay, ilya-biryukov. Herald added a project: clang.
Informative only, useful for positioning UI, interacting with other sources of completion etc. As requested by an embedder of clangd. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D74305 Files: clang-tools-extra/clangd/CodeComplete.cpp clang-tools-extra/clangd/CodeComplete.h clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp Index: clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp +++ clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp @@ -2134,6 +2134,7 @@ "some text [[scope::more::]][[identif]]^ier", "some text [[scope::]][[mor]]^e::identifier", "weird case foo::[[::bar::]][[baz]]^", + "/* [[]][[]]^ */", }) { Annotations F(Case); auto Offset = cantFail(positionToOffset(F.code(), F.point())); @@ -2675,6 +2676,28 @@ EXPECT_THAT(Signatures, Contains(Sig("x() -> auto"))); } +TEST(CompletionTest, CompletionRange) { + const char* WithRange = "auto x = [[abc]]^"; + auto Completions = completions(WithRange); + EXPECT_EQ(Completions.CompletionRange, Annotations(WithRange).range()); + Completions = completionsNoCompile(WithRange); + EXPECT_EQ(Completions.CompletionRange, Annotations(WithRange).range()); + + const char* EmptyRange = "auto x = [[]]^"; + Completions = completions(EmptyRange); + EXPECT_EQ(Completions.CompletionRange, Annotations(EmptyRange).range()); + Completions = completionsNoCompile(EmptyRange); + EXPECT_EQ(Completions.CompletionRange, Annotations(EmptyRange).range()); + + // Sema doesn't trigger at all here, while the no-sema completion runs + // heuristics as normal and reports a range. It'd be nice to be consistent. + const char* NoCompletion = "/* [[]]^ */"; + Completions = completions(NoCompletion); + EXPECT_EQ(Completions.CompletionRange, llvm::None); + Completions = completionsNoCompile(NoCompletion); + EXPECT_EQ(Completions.CompletionRange, Annotations(NoCompletion).range()); +} + TEST(NoCompileCompletionTest, Basic) { auto Results = completionsNoCompile(R"cpp( void func() { Index: clang-tools-extra/clangd/CodeComplete.h =================================================================== --- clang-tools-extra/clangd/CodeComplete.h +++ clang-tools-extra/clangd/CodeComplete.h @@ -216,6 +216,11 @@ std::vector<CodeCompletion> Completions; bool HasMore = false; CodeCompletionContext::Kind Context = CodeCompletionContext::CCC_Other; + // The text that is being directly completed. + // Example: foo.pb^ -> foo.push_back() + // ~~ + // Typically matches the textEdit.range of Completions, but not guaranteed to. + llvm::Optional<Range> CompletionRange; // Usually the source will be parsed with a real C++ parser. // But heuristics may be used instead if e.g. the preamble is not ready. bool RanParser = true; Index: clang-tools-extra/clangd/CodeComplete.cpp =================================================================== --- clang-tools-extra/clangd/CodeComplete.cpp +++ clang-tools-extra/clangd/CodeComplete.cpp @@ -1473,6 +1473,7 @@ } Output.HasMore = Incomplete; Output.Context = CCContextKind; + Output.CompletionRange = ReplacedRange; return Output; }
Index: clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp +++ clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp @@ -2134,6 +2134,7 @@ "some text [[scope::more::]][[identif]]^ier", "some text [[scope::]][[mor]]^e::identifier", "weird case foo::[[::bar::]][[baz]]^", + "/* [[]][[]]^ */", }) { Annotations F(Case); auto Offset = cantFail(positionToOffset(F.code(), F.point())); @@ -2675,6 +2676,28 @@ EXPECT_THAT(Signatures, Contains(Sig("x() -> auto"))); } +TEST(CompletionTest, CompletionRange) { + const char* WithRange = "auto x = [[abc]]^"; + auto Completions = completions(WithRange); + EXPECT_EQ(Completions.CompletionRange, Annotations(WithRange).range()); + Completions = completionsNoCompile(WithRange); + EXPECT_EQ(Completions.CompletionRange, Annotations(WithRange).range()); + + const char* EmptyRange = "auto x = [[]]^"; + Completions = completions(EmptyRange); + EXPECT_EQ(Completions.CompletionRange, Annotations(EmptyRange).range()); + Completions = completionsNoCompile(EmptyRange); + EXPECT_EQ(Completions.CompletionRange, Annotations(EmptyRange).range()); + + // Sema doesn't trigger at all here, while the no-sema completion runs + // heuristics as normal and reports a range. It'd be nice to be consistent. + const char* NoCompletion = "/* [[]]^ */"; + Completions = completions(NoCompletion); + EXPECT_EQ(Completions.CompletionRange, llvm::None); + Completions = completionsNoCompile(NoCompletion); + EXPECT_EQ(Completions.CompletionRange, Annotations(NoCompletion).range()); +} + TEST(NoCompileCompletionTest, Basic) { auto Results = completionsNoCompile(R"cpp( void func() { Index: clang-tools-extra/clangd/CodeComplete.h =================================================================== --- clang-tools-extra/clangd/CodeComplete.h +++ clang-tools-extra/clangd/CodeComplete.h @@ -216,6 +216,11 @@ std::vector<CodeCompletion> Completions; bool HasMore = false; CodeCompletionContext::Kind Context = CodeCompletionContext::CCC_Other; + // The text that is being directly completed. + // Example: foo.pb^ -> foo.push_back() + // ~~ + // Typically matches the textEdit.range of Completions, but not guaranteed to. + llvm::Optional<Range> CompletionRange; // Usually the source will be parsed with a real C++ parser. // But heuristics may be used instead if e.g. the preamble is not ready. bool RanParser = true; Index: clang-tools-extra/clangd/CodeComplete.cpp =================================================================== --- clang-tools-extra/clangd/CodeComplete.cpp +++ clang-tools-extra/clangd/CodeComplete.cpp @@ -1473,6 +1473,7 @@ } Output.HasMore = Incomplete; Output.Context = CCContextKind; + Output.CompletionRange = ReplacedRange; return Output; }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits