[PATCH] D65866: Code completion should not ignore default parameters in functions.
UTKARSH SAXENA via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 7 06:32:37 PDT 2019
usaxena95 created this revision.
usaxena95 added a reviewer: sammccall.
Herald added subscribers: cfe-commits, kadircet, arphaman, jkorous.
Herald added a project: clang.
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.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D65866
Files:
clang-tools-extra/clangd/CodeCompletionStrings.cpp
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
@@ -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<Symbol> IndexSymbols = {}) {
std::unique_ptr<SymbolIndex> Index;
Index: clang-tools-extra/clangd/CodeCompletionStrings.cpp
===================================================================
--- clang-tools-extra/clangd/CodeCompletionStrings.cpp
+++ 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;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D65866.213867.patch
Type: text/x-patch
Size: 2870 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190807/c717133a/attachment-0001.bin>
More information about the cfe-commits
mailing list