[clang-tools-extra] 2eba08f - [clangd] Do not try to use $0 as a placeholder in completion snippets
Nathan Ridge via cfe-commits
cfe-commits at lists.llvm.org
Sun Jul 24 00:01:41 PDT 2022
Author: Nathan Ridge
Date: 2022-07-24T03:01:18-04:00
New Revision: 2eba08fd9a5f42efb64ee8cc5ee0edebd8ce4bc0
URL: https://github.com/llvm/llvm-project/commit/2eba08fd9a5f42efb64ee8cc5ee0edebd8ce4bc0
DIFF: https://github.com/llvm/llvm-project/commit/2eba08fd9a5f42efb64ee8cc5ee0edebd8ce4bc0.diff
LOG: [clangd] Do not try to use $0 as a placeholder in completion snippets
$0 can only be used as a tab stop, not as a placeholder (e.g.
`${0:expression}` is not valid)
Fixes https://github.com/clangd/clangd/issues/1190
Differential Revision: https://reviews.llvm.org/D128621
Added:
Modified:
clang-tools-extra/clangd/CodeCompletionStrings.cpp
clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
clang-tools-extra/clangd/unittests/CodeCompletionStringsTests.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clangd/CodeCompletionStrings.cpp b/clang-tools-extra/clangd/CodeCompletionStrings.cpp
index 1832ee14f2d85..21f83451f7014 100644
--- a/clang-tools-extra/clangd/CodeCompletionStrings.cpp
+++ b/clang-tools-extra/clangd/CodeCompletionStrings.cpp
@@ -192,11 +192,15 @@ void getSignature(const CodeCompletionString &CCS, std::string *Signature,
case CodeCompletionString::CK_Placeholder:
*Signature += Chunk.Text;
++SnippetArg;
- *Snippet +=
- "${" +
- std::to_string(SnippetArg == CursorSnippetArg ? 0 : SnippetArg) + ':';
- appendEscapeSnippet(Chunk.Text, Snippet);
- *Snippet += '}';
+ if (SnippetArg == CursorSnippetArg) {
+ // We'd like to make $0 a placeholder too, but vscode does not support
+ // this (https://github.com/microsoft/vscode/issues/152837).
+ *Snippet += "$0";
+ } else {
+ *Snippet += "${" + std::to_string(SnippetArg) + ':';
+ appendEscapeSnippet(Chunk.Text, Snippet);
+ *Snippet += '}';
+ }
break;
case CodeCompletionString::CK_Informative:
HadInformativeChunks = true;
diff --git a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
index 9291bb7595be0..5050ab203b8db 100644
--- a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
+++ b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
@@ -3231,9 +3231,8 @@ TEST(CompletionTest, CursorInSnippets) {
// Last placeholder in code patterns should be $0 to put the cursor there.
EXPECT_THAT(Results.Completions,
- Contains(AllOf(
- named("while"),
- snippetSuffix(" (${1:condition}) {\n${0:statements}\n}"))));
+ Contains(AllOf(named("while"),
+ snippetSuffix(" (${1:condition}) {\n$0\n}"))));
// However, snippets for functions must *not* end with $0.
EXPECT_THAT(Results.Completions,
Contains(AllOf(named("while_foo"),
diff --git a/clang-tools-extra/clangd/unittests/CodeCompletionStringsTests.cpp b/clang-tools-extra/clangd/unittests/CodeCompletionStringsTests.cpp
index 7aace938b70cb..329a213c66984 100644
--- a/clang-tools-extra/clangd/unittests/CodeCompletionStringsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/CodeCompletionStringsTests.cpp
@@ -150,7 +150,7 @@ TEST_F(CompletionStringTest, SnippetsInPatterns) {
// When completing a pattern, the last placeholder holds the cursor position.
computeSignature(MakeCCS(), /*CompletingPattern=*/true);
- EXPECT_EQ(Snippet, " ${1:name} = ${0:target};");
+ EXPECT_EQ(Snippet, " ${1:name} = $0;");
}
TEST_F(CompletionStringTest, IgnoreInformativeQualifier) {
More information about the cfe-commits
mailing list