[clangd] Do not try to use $0 as a placeholder in completion snippets
authorNathan Ridge <zeratul976@hotmail.com>
Mon, 18 Jul 2022 07:48:04 +0000 (03:48 -0400)
committerNathan Ridge <zeratul976@hotmail.com>
Sun, 24 Jul 2022 07:01:18 +0000 (03:01 -0400)
$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

clang-tools-extra/clangd/CodeCompletionStrings.cpp
clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
clang-tools-extra/clangd/unittests/CodeCompletionStringsTests.cpp

index 1832ee1..21f8345 100644 (file)
@@ -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;
index 9291bb7..5050ab2 100644 (file)
@@ -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"),
index 7aace93..329a213 100644 (file)
@@ -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) {