diff --git a/clang-tools-extra/clangd/CodeCompletionStrings.cpp b/clang-tools-extra/clangd/CodeCompletionStrings.cpp --- a/clang-tools-extra/clangd/CodeCompletionStrings.cpp +++ b/clang-tools-extra/clangd/CodeCompletionStrings.cpp @@ -32,6 +32,21 @@ } } +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 @@ *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; diff --git a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp --- a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp +++ b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp @@ -939,6 +939,25 @@ 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 IndexSymbols = {}) { std::unique_ptr Index; diff --git a/clang-tools-extra/clangd/unittests/CodeCompletionStringsTests.cpp b/clang-tools-extra/clangd/unittests/CodeCompletionStringsTests.cpp --- a/clang-tools-extra/clangd/unittests/CodeCompletionStringsTests.cpp +++ b/clang-tools-extra/clangd/unittests/CodeCompletionStringsTests.cpp @@ -90,6 +90,30 @@ 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);