[PATCH] D50835: [clangd] Add parantheses while auto-completing functions.
Kadir Cetinkaya via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Aug 17 07:32:24 PDT 2018
kadircet updated this revision to Diff 161243.
kadircet added a comment.
- Change option name.
Repository:
rCTE Clang Tools Extra
https://reviews.llvm.org/D50835
Files:
clangd/CodeComplete.cpp
clangd/CodeComplete.h
unittests/clangd/CodeCompleteTests.cpp
Index: unittests/clangd/CodeCompleteTests.cpp
===================================================================
--- unittests/clangd/CodeCompleteTests.cpp
+++ unittests/clangd/CodeCompleteTests.cpp
@@ -1590,6 +1590,56 @@
ElementsAre(Sig("foo(T, U) -> void", {"T", "U"})));
}
+TEST(CompletionTest, RenderWithSnippetsForFunctionArgsDisabled) {
+ CodeCompleteOptions Opts;
+ Opts.EnableFunctionArgSnippets = true;
+ {
+ CodeCompletion C;
+ C.RequiredQualifier = "Foo::";
+ C.Name = "x";
+ C.SnippetSuffix = "()";
+
+ auto R = C.render(Opts);
+ EXPECT_EQ(R.textEdit->newText, "Foo::x");
+ EXPECT_EQ(R.insertTextFormat, InsertTextFormat::PlainText);
+ }
+
+ Opts.EnableSnippets = true;
+ Opts.EnableFunctionArgSnippets = false;
+ {
+ CodeCompletion C;
+ C.RequiredQualifier = "Foo::";
+ C.Name = "x";
+ C.SnippetSuffix = "";
+
+ auto R = C.render(Opts);
+ EXPECT_EQ(R.textEdit->newText, "Foo::x");
+ EXPECT_EQ(R.insertTextFormat, InsertTextFormat::Snippet);
+ }
+
+ {
+ CodeCompletion C;
+ C.RequiredQualifier = "Foo::";
+ C.Name = "x";
+ C.SnippetSuffix = "()";
+
+ auto R = C.render(Opts);
+ EXPECT_EQ(R.textEdit->newText, "Foo::x()");
+ EXPECT_EQ(R.insertTextFormat, InsertTextFormat::Snippet);
+ }
+
+ {
+ CodeCompletion C;
+ C.RequiredQualifier = "Foo::";
+ C.Name = "x";
+ C.SnippetSuffix = "(${0:bool})";
+
+ auto R = C.render(Opts);
+ EXPECT_EQ(R.textEdit->newText, "Foo::x(${0})");
+ EXPECT_EQ(R.insertTextFormat, InsertTextFormat::Snippet);
+ }
+}
+
} // namespace
} // namespace clangd
} // namespace clang
Index: clangd/CodeComplete.h
===================================================================
--- clangd/CodeComplete.h
+++ clangd/CodeComplete.h
@@ -82,6 +82,10 @@
/// Include completions that require small corrections, e.g. change '.' to
/// '->' on member access etc.
bool IncludeFixIts = false;
+
+ /// Whether to generate snippets for function arguments on code-completion.
+ /// Needs snippets to be enabled as well.
+ bool EnableFunctionArgSnippets = true;
};
// Semi-structured representation of a code-complete suggestion for our C++ API.
Index: clangd/CodeComplete.cpp
===================================================================
--- clangd/CodeComplete.cpp
+++ clangd/CodeComplete.cpp
@@ -1371,8 +1371,14 @@
LSP.additionalTextEdits.push_back(FixIt);
}
}
- if (Opts.EnableSnippets)
- LSP.textEdit->newText += SnippetSuffix;
+ if (Opts.EnableSnippets && !SnippetSuffix.empty()) {
+ if (Opts.EnableFunctionArgSnippets)
+ LSP.textEdit->newText += SnippetSuffix;
+ else
+ // Check whether function has any parameters or not.
+ LSP.textEdit->newText += SnippetSuffix.size() > 2 ? "(${0})" : "()";
+ }
+
// FIXME(kadircet): Do not even fill insertText after making sure textEdit is
// compatible with most of the editors.
LSP.insertText = LSP.textEdit->newText;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D50835.161243.patch
Type: text/x-patch
Size: 2975 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180817/4b9667f3/attachment.bin>
More information about the cfe-commits
mailing list