[PATCH] D50835: [clangd] Add parantheses while auto-completing functions.

Kadir Cetinkaya via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 16 06:59:35 PDT 2018


kadircet updated this revision to Diff 161023.
kadircet marked an inline comment as done.
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,55 @@
               ElementsAre(Sig("foo(T, U) -> void", {"T", "U"})));
 }
 
+TEST(CompletionTest, RenderWithSnippetsForFunctionArgsDisabled) {
+  CodeCompleteOptions Opts;
+  Opts.DisableSnippetTemplateForFunctionArgs = 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;
+  {
+    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;
+
+  /// Disables snippet generation for function arguments. Does nothing if
+  /// snippets are not enabled.
+  bool DisableSnippetTemplateForFunctionArgs = false;
 };
 
 // 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.DisableSnippetTemplateForFunctionArgs)
+      // Check whether function has any parameters or not.
+      LSP.textEdit->newText += SnippetSuffix.size() > 2 ? "(${0})" : "()";
+    else
+      LSP.textEdit->newText += SnippetSuffix;
+  }
+
   // 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.161023.patch
Type: text/x-patch
Size: 2953 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180816/bd683f86/attachment-0001.bin>


More information about the cfe-commits mailing list