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 appendCodeCompletionString(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."); + appendCodeCompletionString(*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,10 @@ *Snippet += Chunk.Text; break; case CodeCompletionString::CK_Optional: + assert(Chunk.Optional && + "Expected the optional code completion string to be non-null."); + // No need to create placeholders for default arguments in Snippet. + appendCodeCompletionString(*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 @@ -10,6 +10,7 @@ #include "ClangdServer.h" #include "CodeComplete.h" #include "Compiler.h" +#include "Logger.h" #include "Matchers.h" #include "Protocol.h" #include "Quality.h" @@ -939,6 +940,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;