[tools] gopls/completion: prepend space when completing right after "//" before a declaration

3 views
Skip to first unread message

Gerrit Bot (Gerrit)

unread,
Apr 3, 2026, 11:27:04 AM (6 days ago) Apr 3
to goph...@pubsubhelper.golang.org, Abhay Chaurasiya, golang-co...@googlegroups.com

Gerrit Bot has uploaded the change for review

Commit message

gopls/completion: prepend space when completing right after "//" before a declaration

When a user places the cursor immediately after "//" (no space) on a
line directly above a declaration, gopls already suggests the
declaration name as a completion candidate. However the inserted text
lacked the conventional space, producing "//Name" instead of the
proper Go doc comment form "// Name".

Detect this case in populateCommentCompletions (cursor at slash+2
with comment text exactly "//") and set a new
commentNeedsLeadingSpace flag on the completionContext. item() checks
the flag and prepends a space to the insert text (and snippet), so
accepting the completion yields "// Name".

A marker test is added to comment.txt asserting that a function name
is offered when the cursor is right after "//" directly above its
declaration. All existing completion marker tests continue to pass.

Fixes golang/go#76374
Change-Id: Ibc91170861ad8a142b5cc9bb85d57313d92929b8
GitHub-Last-Rev: 254d6e0eb03bd8b51013fb508a1c95bc222f285e
GitHub-Pull-Request: golang/tools#630

Change diff

diff --git a/gopls/internal/golang/completion/completion.go b/gopls/internal/golang/completion/completion.go
index 314165a..f84bf63 100644
--- a/gopls/internal/golang/completion/completion.go
+++ b/gopls/internal/golang/completion/completion.go
@@ -366,6 +366,12 @@
// commentCompletion is true if we are completing a comment.
commentCompletion bool

+ // commentNeedsLeadingSpace is true when completing a comment whose text
+ // is exactly "//" (cursor placed immediately after the slashes, with no
+ // space). Completion items should prepend a space so that accepting a
+ // suggestion produces "// Name" rather than "//Name".
+ commentNeedsLeadingSpace bool
+
// packageCompletion is true if we are completing a package name.
packageCompletion bool
}
@@ -1049,6 +1055,16 @@
// comment is valid, set surrounding as word boundaries around cursor
c.setSurroundingForComment(comment)

+ // If the cursor is placed immediately after "//" with no following text,
+ // completions must prepend a space to produce proper Go doc comment style
+ // ("// Name" rather than "//Name").
+ for _, cmt := range comment.List {
+ if c.pos == cmt.Slash+2 && cmt.Text == "//" {
+ c.completionContext.commentNeedsLeadingSpace = true
+ break
+ }
+ }
+
// Using the next line pos, grab and parse the exported symbol on that line
for _, n := range c.pgf.File.Decls {
declLine := safetoken.Line(file, n.Pos())
diff --git a/gopls/internal/golang/completion/format.go b/gopls/internal/golang/completion/format.go
index 9f5d277..7b2e802 100644
--- a/gopls/internal/golang/completion/format.go
+++ b/gopls/internal/golang/completion/format.go
@@ -229,6 +229,14 @@
if cand.detail != "" {
detail = cand.detail
}
+
+ // When completing right after "//" (cursor at the slashes with no space),
+ // prepend a space so the result is "// Name" rather than "//Name".
+ if c.completionContext.commentNeedsLeadingSpace {
+ insert = " " + insert
+ snip.PrependText(" ")
+ }
+
item := CompletionItem{
Label: label,
InsertText: insert,
diff --git a/gopls/internal/test/marker/testdata/completion/comment.txt b/gopls/internal/test/marker/testdata/completion/comment.txt
index 57a3882..da5e32e 100644
--- a/gopls/internal/test/marker/testdata/completion/comment.txt
+++ b/gopls/internal/test/marker/testdata/completion/comment.txt
@@ -72,6 +72,11 @@
return 0
}

+// This tests that completing right after "//" (no space) above a declaration
+// suggests the identifier name, prepending a space for proper doc comment style.
+//@complete(re"//()", plainFunc)
+func PlainFunc() {} //@item(plainFunc, "PlainFunc", "func()", "func")
+
// This tests multiline block comments and completion with prefix
// Lorem Ipsum Multili//@complete(re"()Multi", multiline)
// Lorem ipsum dolor sit ametom

Change information

Files:
  • M gopls/internal/golang/completion/completion.go
  • M gopls/internal/golang/completion/format.go
  • M gopls/internal/test/marker/testdata/completion/comment.txt
Change size: S
Delta: 3 files changed, 29 insertions(+), 0 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: tools
Gerrit-Branch: master
Gerrit-Change-Id: Ibc91170861ad8a142b5cc9bb85d57313d92929b8
Gerrit-Change-Number: 762540
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-CC: Abhay Chaurasiya <abhaycha...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Alan Donovan (Gerrit)

unread,
Apr 7, 2026, 9:09:18 PM (2 days ago) Apr 7
to Abhay Chaurasiya, Gerrit Bot, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Alan Donovan added 2 comments

Patchset-level comments
File-level comment, Patchset 1 (Latest):
Alan Donovan . resolved

Thanks for tackling this problem.

File gopls/internal/golang/completion/completion.go
Line 373, Patchset 1 (Latest): commentNeedsLeadingSpace bool
Alan Donovan . unresolved

The fix in this CL seems rather narrowly tailored to the specific text of the test case in the attached issue: it works only if completion is triggered on the `//` without additional characters. It might be more useful to check whether the completion's edit range (being replaced) begins immediately after "//" without a space.

Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: tools
    Gerrit-Branch: master
    Gerrit-Change-Id: Ibc91170861ad8a142b5cc9bb85d57313d92929b8
    Gerrit-Change-Number: 762540
    Gerrit-PatchSet: 1
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-CC: Abhay Chaurasiya <abhaycha...@gmail.com>
    Gerrit-CC: Alan Donovan <adon...@google.com>
    Gerrit-Comment-Date: Wed, 08 Apr 2026 01:09:16 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    open
    diffy

    Gerrit Bot (Gerrit)

    unread,
    Apr 8, 2026, 7:09:36 AM (19 hours ago) Apr 8
    to Abhay Chaurasiya, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Gerrit Bot uploaded new patchset

    Gerrit Bot uploaded patch set #2 to this change.
    Open in Gerrit

    Related details

    Attention set is empty
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: newpatchset
    Gerrit-Project: tools
    Gerrit-Branch: master
    Gerrit-Change-Id: Ibc91170861ad8a142b5cc9bb85d57313d92929b8
    Gerrit-Change-Number: 762540
    Gerrit-PatchSet: 2
    unsatisfied_requirement
    open
    diffy

    Abhay Chaurasiya (Gerrit)

    unread,
    Apr 8, 2026, 7:13:34 AM (19 hours ago) Apr 8
    to Gerrit Bot, goph...@pubsubhelper.golang.org, Alan Donovan, golang-co...@googlegroups.com
    Attention needed from Alan Donovan

    Abhay Chaurasiya added 2 comments

    Patchset-level comments
    File-level comment, Patchset 1:
    Abhay Chaurasiya . resolved

    Thanks for the feedback. You're right that the check was too narrow.

    I've updated it to use c.surrounding.start (the start of the edit range, set by setSurroundingForComment) instead of checking c.pos == cmt.Slash+2 && cmt.Text == "//". If the edit range starts immediately at slash+2, there is no space between // and the identifier being replaced, so completions should prepend a space — regardless of how many characters the user has already typed (e.g. //Foo still gets // FooBar).

    Also added a marker test for the partial-prefix case (//Plain → // PlainFunc).

    File gopls/internal/golang/completion/completion.go
    Line 373, Patchset 1: commentNeedsLeadingSpace bool
    Alan Donovan . resolved

    The fix in this CL seems rather narrowly tailored to the specific text of the test case in the attached issue: it works only if completion is triggered on the `//` without additional characters. It might be more useful to check whether the completion's edit range (being replaced) begins immediately after "//" without a space.

    Abhay Chaurasiya

    Done

    Open in Gerrit

    Related details

    Attention is currently required from:
    • Alan Donovan
    Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: tools
      Gerrit-Branch: master
      Gerrit-Change-Id: Ibc91170861ad8a142b5cc9bb85d57313d92929b8
      Gerrit-Change-Number: 762540
      Gerrit-PatchSet: 1
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-CC: Abhay Chaurasiya <abhaycha...@gmail.com>
      Gerrit-CC: Alan Donovan <adon...@google.com>
      Gerrit-Attention: Alan Donovan <adon...@google.com>
      Gerrit-Comment-Date: Wed, 08 Apr 2026 11:13:27 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No
      Comment-In-Reply-To: Alan Donovan <adon...@google.com>
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      Alan Donovan (Gerrit)

      unread,
      Apr 8, 2026, 10:28:59 PM (4 hours ago) Apr 8
      to Abhay Chaurasiya, Gerrit Bot, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

      Alan Donovan voted and added 1 comment

      Votes added by Alan Donovan

      Auto-Submit+1
      Code-Review+2
      Commit-Queue+1

      1 comment

      Patchset-level comments
      File-level comment, Patchset 2 (Latest):
      Alan Donovan . resolved

      Looks good. Thanks for contributing this fix!

      Open in Gerrit

      Related details

      Attention set is empty
      Submit Requirements:
      • requirement satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: tools
      Gerrit-Branch: master
      Gerrit-Change-Id: Ibc91170861ad8a142b5cc9bb85d57313d92929b8
      Gerrit-Change-Number: 762540
      Gerrit-PatchSet: 2
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Alan Donovan <adon...@google.com>
      Gerrit-CC: Abhay Chaurasiya <abhaycha...@gmail.com>
      Gerrit-Comment-Date: Thu, 09 Apr 2026 02:28:54 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: Yes
      satisfied_requirement
      unsatisfied_requirement
      open
      diffy

      Abhay Chaurasiya (Gerrit)

      unread,
      Apr 8, 2026, 11:27:03 PM (3 hours ago) Apr 8
      to Gerrit Bot, goph...@pubsubhelper.golang.org, Go LUCI, Hongxiang Jiang, Alan Donovan, golang-co...@googlegroups.com
      Attention needed from Alan Donovan

      Abhay Chaurasiya added 1 comment

      Patchset-level comments
      Abhay Chaurasiya . resolved

      The CI failure is in gopls/internal/debug with package "bufio" without types — this appears to be a pre-existing infrastructure issue unrelated to this CL. Could you re-add Commit-Queue+1?

      Open in Gerrit

      Related details

      Attention is currently required from:
      • Alan Donovan
      Submit Requirements:
      • requirement satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: tools
      Gerrit-Branch: master
      Gerrit-Change-Id: Ibc91170861ad8a142b5cc9bb85d57313d92929b8
      Gerrit-Change-Number: 762540
      Gerrit-PatchSet: 2
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Alan Donovan <adon...@google.com>
      Gerrit-CC: Abhay Chaurasiya <abhaycha...@gmail.com>
      Gerrit-CC: Hongxiang Jiang <hxj...@golang.org>
      Gerrit-Attention: Alan Donovan <adon...@google.com>
      Gerrit-Comment-Date: Thu, 09 Apr 2026 03:26:55 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No
      satisfied_requirement
      unsatisfied_requirement
      open
      diffy
      Reply all
      Reply to author
      Forward
      0 new messages