Skip to content

allow implicit completion inside empty template argument list #138846

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tom-anders
Copy link
Contributor

I noticed that when triggering completion inside something like std::vector<^> via the '<' trigger character, no results are returned. This PR adds a small heuristic to detect this case and offer completions

@llvmbot
Copy link
Member

llvmbot commented May 7, 2025

@llvm/pr-subscribers-clang-tools-extra

Author: Tom Praschan (tom-anders)

Changes

I noticed that when triggering completion inside something like std::vector&lt;^&gt; via the '<' trigger character, no results are returned. This PR adds a small heuristic to detect this case and offer completions


Full diff: https://github.com/llvm/llvm-project/pull/138846.diff

2 Files Affected:

  • (modified) clang-tools-extra/clangd/CodeComplete.cpp (+5)
  • (modified) clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp (+2)
diff --git a/clang-tools-extra/clangd/CodeComplete.cpp b/clang-tools-extra/clangd/CodeComplete.cpp
index 0eb196fbad46a..cc35e03959332 100644
--- a/clang-tools-extra/clangd/CodeComplete.cpp
+++ b/clang-tools-extra/clangd/CodeComplete.cpp
@@ -2455,6 +2455,11 @@ bool isIncludeFile(llvm::StringRef Line) {
 }
 
 bool allowImplicitCompletion(llvm::StringRef Content, unsigned Offset) {
+  // Check if we're inside an empty template argument list
+  if (Content.size() > 2 && Content[Offset - 1] == '<' &&
+      Content[Offset] == '>')
+    return true;
+
   // Look at last line before completion point only.
   Content = Content.take_front(Offset);
   auto Pos = Content.rfind('\n');
diff --git a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
index 718bee2e40b11..62e0f8627b7c7 100644
--- a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
+++ b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
@@ -3978,6 +3978,7 @@ TEST(AllowImplicitCompletion, All) {
       "foo.^bar",
       "foo->^bar",
       "foo::^bar",
+      "std::vector<std::vector<^>>",
       "  #  include <^foo.h>",
       "#import <foo/^bar.h>",
       "#include_next \"^",
@@ -3990,6 +3991,7 @@ TEST(AllowImplicitCompletion, All) {
       "#include \"foo.h\"^",
       "#error <^",
       "#<^",
+      "a <^b",
   };
   for (const char *Test : Yes) {
     llvm::Annotations A(Test);

@llvmbot
Copy link
Member

llvmbot commented May 7, 2025

@llvm/pr-subscribers-clangd

Author: Tom Praschan (tom-anders)

Changes

I noticed that when triggering completion inside something like std::vector&lt;^&gt; via the '<' trigger character, no results are returned. This PR adds a small heuristic to detect this case and offer completions


Full diff: https://github.com/llvm/llvm-project/pull/138846.diff

2 Files Affected:

  • (modified) clang-tools-extra/clangd/CodeComplete.cpp (+5)
  • (modified) clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp (+2)
diff --git a/clang-tools-extra/clangd/CodeComplete.cpp b/clang-tools-extra/clangd/CodeComplete.cpp
index 0eb196fbad46a..cc35e03959332 100644
--- a/clang-tools-extra/clangd/CodeComplete.cpp
+++ b/clang-tools-extra/clangd/CodeComplete.cpp
@@ -2455,6 +2455,11 @@ bool isIncludeFile(llvm::StringRef Line) {
 }
 
 bool allowImplicitCompletion(llvm::StringRef Content, unsigned Offset) {
+  // Check if we're inside an empty template argument list
+  if (Content.size() > 2 && Content[Offset - 1] == '<' &&
+      Content[Offset] == '>')
+    return true;
+
   // Look at last line before completion point only.
   Content = Content.take_front(Offset);
   auto Pos = Content.rfind('\n');
diff --git a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
index 718bee2e40b11..62e0f8627b7c7 100644
--- a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
+++ b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
@@ -3978,6 +3978,7 @@ TEST(AllowImplicitCompletion, All) {
       "foo.^bar",
       "foo->^bar",
       "foo::^bar",
+      "std::vector<std::vector<^>>",
       "  #  include <^foo.h>",
       "#import <foo/^bar.h>",
       "#include_next \"^",
@@ -3990,6 +3991,7 @@ TEST(AllowImplicitCompletion, All) {
       "#include \"foo.h\"^",
       "#error <^",
       "#<^",
+      "a <^b",
   };
   for (const char *Test : Yes) {
     llvm::Annotations A(Test);

Copy link

github-actions bot commented May 7, 2025

⚠️ We detected that you are using a GitHub private e-mail address to contribute to the repo.
Please turn off Keep my email addresses private setting in your account.
See LLVM Discourse for more information.

Comment on lines +2458 to +2462
// Check if we're inside an empty template argument list
if (Content.size() > 2 && Content[Offset - 1] == '<' &&
Content[Offset] == '>')
return true;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it also pop up a completion within a template parameter list?

template <^> class foo {};

@zyn0217 zyn0217 requested a review from HighCommander4 May 7, 2025 12:04
Copy link
Collaborator

@HighCommander4 HighCommander4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable to me, but the buildkite run is showing AllowImplicitCompletions.All is currently failing

@@ -2455,6 +2455,11 @@ bool isIncludeFile(llvm::StringRef Line) {
}

bool allowImplicitCompletion(llvm::StringRef Content, unsigned Offset) {
// Check if we're inside an empty template argument list
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: for consistency with the other checks in this function, could you:

  • move this after the "Look at last line before completion only" check; and
  • formulate the check using ends_with?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants