[clang-tools-extra] r368186 - Code completion should not ignore default parameters in functions.
Sam McCall via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 7 09:52:21 PDT 2019
Author: sammccall
Date: Wed Aug 7 09:52:21 2019
New Revision: 368186
URL: http://llvm.org/viewvc/llvm-project?rev=368186&view=rev
Log:
Code completion should not ignore default parameters in functions.
Summary:
Inorder to display the default arguments we must process the
CK_Optional chunks of CodeCompletionString while creating the Signature.
We do not create placeholders for default arguments.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: jkorous, arphaman, kadircet, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D65866
Modified:
clang-tools-extra/trunk/clangd/CodeCompletionStrings.cpp
clang-tools-extra/trunk/clangd/unittests/CodeCompleteTests.cpp
clang-tools-extra/trunk/clangd/unittests/CodeCompletionStringsTests.cpp
Modified: clang-tools-extra/trunk/clangd/CodeCompletionStrings.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/CodeCompletionStrings.cpp?rev=368186&r1=368185&r2=368186&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/CodeCompletionStrings.cpp (original)
+++ clang-tools-extra/trunk/clangd/CodeCompletionStrings.cpp Wed Aug 7 09:52:21 2019
@@ -32,6 +32,21 @@ void appendEscapeSnippet(const llvm::Str
}
}
+void appendOptionalChunk(const CodeCompletionString &CCS, std::string *Out) {
+ for (const CodeCompletionString::Chunk &C : CCS) {
+ switch (C.Kind) {
+ case CodeCompletionString::CK_Optional:
+ assert(C.Optional &&
+ "Expected the optional code completion string to be non-null.");
+ appendOptionalChunk(*C.Optional, Out);
+ break;
+ default:
+ *Out += C.Text;
+ break;
+ }
+ }
+}
+
bool looksLikeDocComment(llvm::StringRef CommentText) {
// We don't report comments that only contain "special" chars.
// This avoids reporting various delimiters, like:
@@ -138,6 +153,9 @@ void getSignature(const CodeCompletionSt
*Snippet += Chunk.Text;
break;
case CodeCompletionString::CK_Optional:
+ assert(Chunk.Optional);
+ // No need to create placeholders for default arguments in Snippet.
+ appendOptionalChunk(*Chunk.Optional, Signature);
break;
case CodeCompletionString::CK_Placeholder:
*Signature += Chunk.Text;
Modified: clang-tools-extra/trunk/clangd/unittests/CodeCompleteTests.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/CodeCompleteTests.cpp?rev=368186&r1=368185&r2=368186&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/unittests/CodeCompleteTests.cpp (original)
+++ clang-tools-extra/trunk/clangd/unittests/CodeCompleteTests.cpp Wed Aug 7 09:52:21 2019
@@ -939,6 +939,25 @@ TEST(CompletionTest, IgnoreCompleteInExc
EXPECT_TRUE(Results.Completions.empty());
}
+
+TEST(CompletionTest, DefaultArgs) {
+ clangd::CodeCompleteOptions Opts;
+ std::string Context = R"cpp(
+ int X(int A = 0);
+ int Y(int A, int B = 0);
+ int Z(int A, int B = 0, int C = 0, int D = 0);
+ )cpp";
+ EXPECT_THAT(completions(Context + "int y = X^", {}, Opts).Completions,
+ UnorderedElementsAre(Labeled("X(int A = 0)")));
+ EXPECT_THAT(completions(Context + "int y = Y^", {}, Opts).Completions,
+ UnorderedElementsAre(AllOf(Labeled("Y(int A, int B = 0)"),
+ SnippetSuffix("(${1:int A})"))));
+ EXPECT_THAT(completions(Context + "int y = Z^", {}, Opts).Completions,
+ UnorderedElementsAre(
+ AllOf(Labeled("Z(int A, int B = 0, int C = 0, int D = 0)"),
+ SnippetSuffix("(${1:int A})"))));
+}
+
SignatureHelp signatures(llvm::StringRef Text, Position Point,
std::vector<Symbol> IndexSymbols = {}) {
std::unique_ptr<SymbolIndex> Index;
Modified: clang-tools-extra/trunk/clangd/unittests/CodeCompletionStringsTests.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/CodeCompletionStringsTests.cpp?rev=368186&r1=368185&r2=368186&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/unittests/CodeCompletionStringsTests.cpp (original)
+++ clang-tools-extra/trunk/clangd/unittests/CodeCompletionStringsTests.cpp Wed Aug 7 09:52:21 2019
@@ -90,6 +90,30 @@ TEST_F(CompletionStringTest, Function) {
EXPECT_EQ(formatDocumentation(*CCS, "Foo's comment"), "Foo's comment");
}
+TEST_F(CompletionStringTest, FunctionWithDefaultParams) {
+ // return_type foo(p1, p2 = 0, p3 = 0)
+ Builder.AddChunk(CodeCompletionString::CK_Comma);
+ Builder.AddTypedTextChunk("p3 = 0");
+ auto *DefaultParam2 = Builder.TakeString();
+
+ Builder.AddChunk(CodeCompletionString::CK_Comma);
+ Builder.AddTypedTextChunk("p2 = 0");
+ Builder.AddOptionalChunk(DefaultParam2);
+ auto *DefaultParam1 = Builder.TakeString();
+
+ Builder.AddResultTypeChunk("return_type");
+ Builder.AddTypedTextChunk("Foo");
+ Builder.AddChunk(CodeCompletionString::CK_LeftParen);
+ Builder.AddPlaceholderChunk("p1");
+ Builder.AddOptionalChunk(DefaultParam1);
+ Builder.AddChunk(CodeCompletionString::CK_RightParen);
+
+ auto *CCS = Builder.TakeString();
+ computeSignature(*CCS);
+ EXPECT_EQ(Signature, "(p1, p2 = 0, p3 = 0)");
+ EXPECT_EQ(Snippet, "(${1:p1})");
+}
+
TEST_F(CompletionStringTest, EscapeSnippet) {
Builder.AddTypedTextChunk("Foo");
Builder.AddChunk(CodeCompletionString::CK_LeftParen);
More information about the cfe-commits
mailing list