[tools] gopls: add function end name inlay hints

1 view
Skip to first unread message

Gerrit Bot (Gerrit)

unread,
Sep 3, 2026, 11:38:08 PMSep 3
to goph...@pubsubhelper.golang.org, 郭根源(genyuan), golang-co...@googlegroups.com

Gerrit Bot has uploaded the change for review

Commit message

gopls: add function end name inlay hints

Add an opt-in functionEndNames inlay hint that annotates the closing
brace of named function and method declarations with their declared
names. Function literals are intentionally excluded.

Add marker coverage and document the setting in the feature index,
generated API reference, and v0.24.0 release notes.

Fixes golang/go#80465
Change-Id: Ie2a0d429f08809dd96f8991e331ad3c2f4b227f2
GitHub-Last-Rev: e0895f94412006fd6ecf3416343ac3501e72d19c
GitHub-Pull-Request: golang/tools#676

Change diff

diff --git a/gopls/doc/features/index.md b/gopls/doc/features/index.md
index 56b66eb..5210f04 100644
--- a/gopls/doc/features/index.md
+++ b/gopls/doc/features/index.md
@@ -30,7 +30,7 @@
- [Hover](passive.md#hover): information about the symbol under the cursor
- [Signature Help](passive.md#signature-help): type information about the enclosing function call
- [Document Highlight](passive.md#document-highlight): highlight identifiers referring to the same symbol
- - [Inlay Hint](passive.md#inlay-hint): show implicit names of struct fields and parameter names
+ - [Inlay Hint](passive.md#inlay-hint): show implicit names, types, values, and function ends
- [Semantic Tokens](passive.md#semantic-tokens): report syntax information used by editors to color the text
- [Folding Range](passive.md#folding-range): report text regions that can be "folded" (expanded/collapsed) in an editor
- [Document Link](passive.md#document-link): extracts URLs from doc comments, strings in current file so client can linkify
diff --git a/gopls/doc/features/passive.md b/gopls/doc/features/passive.md
index 309c935..9724057 100644
--- a/gopls/doc/features/passive.md
+++ b/gopls/doc/features/passive.md
@@ -181,6 +181,8 @@
variables k and v (`rangeVariableTypes`).
- For a constant expression (perhaps using `iota`), a hint provides
its computed value (`constantValues`).
+- At the closing brace of a function or method declaration, a hint provides
+ its name (`functionEndNames`).

See [Inlay hints](../inlayHints.md) for a complete list with examples.

diff --git a/gopls/doc/inlayHints.md b/gopls/doc/inlayHints.md
index 74207a1..810df4e 100644
--- a/gopls/doc/inlayHints.md
+++ b/gopls/doc/inlayHints.md
@@ -60,6 +60,17 @@

**Disabled by default. Enable it by setting `"hints": {"constantValues": true}`.**

+## **functionEndNames**
+
+`"functionEndNames"` controls inlay hints for function and method names at their closing braces:
+```go
+ func foo() {
+ }« // foo»
+```
+
+
+**Disabled by default. Enable it by setting `"hints": {"functionEndNames": true}`.**
+
## **functionTypeParameters**

`"functionTypeParameters"` inlay hints for implicit type parameters on generic functions:
diff --git a/gopls/doc/release/v0.24.0.md b/gopls/doc/release/v0.24.0.md
index 880e479..fb2687e 100644
--- a/gopls/doc/release/v0.24.0.md
+++ b/gopls/doc/release/v0.24.0.md
@@ -37,6 +37,12 @@

## Editing features

+### `functionEndNames` inlay hint
+
+The new `functionEndNames` inlay hint shows the name of a function or method
+at its closing brace. Enable it using this configuration:
+`{"hints": {"functionEndNames": true}}`.
+
Gopls now supports the Hover request in Go assembly files: hovering
over a symbol reports the signature and doc comment of its Go
declaration.
diff --git a/gopls/internal/doc/api.json b/gopls/internal/doc/api.json
index 30a736e..c88e1e9 100644
--- a/gopls/internal/doc/api.json
+++ b/gopls/internal/doc/api.json
@@ -2088,6 +2088,12 @@
"Status": ""
},
{
+ "Name": "\"functionEndNames\"",
+ "Doc": "`\"functionEndNames\"` controls inlay hints for function and method names at their closing braces:\n```go\n\tfunc foo() {\n\t}« // foo»\n```\n",
+ "Default": "false",
+ "Status": ""
+ },
+ {
"Name": "\"functionTypeParameters\"",
"Doc": "`\"functionTypeParameters\"` inlay hints for implicit type parameters on generic functions:\n```go\n\tmyFoo«[int, string]»(1, \"hello\")\n```\n",
"Default": "false",
@@ -3992,6 +3998,12 @@
"Status": ""
},
{
+ "Name": "functionEndNames",
+ "Doc": "`\"functionEndNames\"` controls inlay hints for function and method names at their closing braces:\n```go\n\tfunc foo() {\n\t}« // foo»\n```\n",
+ "Default": false,
+ "Status": ""
+ },
+ {
"Name": "functionTypeParameters",
"Doc": "`\"functionTypeParameters\"` inlay hints for implicit type parameters on generic functions:\n```go\n\tmyFoo«[int, string]»(1, \"hello\")\n```\n",
"Default": false,
diff --git a/gopls/internal/golang/inlay_hint.go b/gopls/internal/golang/inlay_hint.go
index 74a6634..0876bb6 100644
--- a/gopls/internal/golang/inlay_hint.go
+++ b/gopls/internal/golang/inlay_hint.go
@@ -88,9 +88,27 @@
settings.CompositeLiteralTypes: compositeLiteralTypes,
settings.CompositeLiteralFieldNames: compositeLiteralFields,
settings.FunctionTypeParameters: funcTypeParams,
+ settings.FunctionEndNames: functionEndNames,
settings.IgnoredError: ignoredError,
}

+func functionEndNames(info *types.Info, pgf *parsego.File, qual types.Qualifier, cur inspector.Cursor, add func(protocol.InlayHint)) {
+ for curFunc := range cur.Preorder((*ast.FuncDecl)(nil)) {
+ decl := curFunc.Node().(*ast.FuncDecl)
+ if decl.Body == nil {
+ continue
+ }
+ pos, err := pgf.PosPosition(decl.Body.End())
+ if err != nil {
+ continue
+ }
+ add(protocol.InlayHint{
+ Position: pos,
+ Label: labelPart(" // " + decl.Name.Name),
+ })
+ }
+}
+
func parameterNames(info *types.Info, pgf *parsego.File, qual types.Qualifier, cur inspector.Cursor, add func(protocol.InlayHint)) {
for curCall := range cur.Preorder((*ast.CallExpr)(nil)) {
callExpr := curCall.Node().(*ast.CallExpr)
diff --git a/gopls/internal/settings/settings.go b/gopls/internal/settings/settings.go
index 987223a..695fc80 100644
--- a/gopls/internal/settings/settings.go
+++ b/gopls/internal/settings/settings.go
@@ -631,6 +631,13 @@
// ```
FunctionTypeParameters InlayHint = "functionTypeParameters"

+ // FunctionEndNames controls inlay hints for function and method names at their closing braces:
+ // ```go
+ // func foo() {
+ // }« // foo»
+ // ```
+ FunctionEndNames InlayHint = "functionEndNames"
+
// IgnoredError inlay hints for implicitly discarded errors:
// ```go
// f.Close()« // ignore error»
diff --git a/gopls/internal/test/marker/testdata/inlayhints/function-end-names.txt b/gopls/internal/test/marker/testdata/inlayhints/function-end-names.txt
new file mode 100644
index 0000000..967952c
--- /dev/null
+++ b/gopls/internal/test/marker/testdata/inlayhints/function-end-names.txt
@@ -0,0 +1,26 @@
+Test of the "function end names" inlay hint (#80465).
+
+-- settings.json --
+{"hints": {"functionEndNames": true}}
+
+-- p/p.go --
+package p //@inlayhints(out)
+
+type T struct{}
+
+func foo() {
+ func() {}
+}
+
+func (T) method() {}
+
+-- @out --
+package p //@inlayhints(out)
+
+type T struct{}
+
+func foo() {
+ func() {}
+}< // foo>
+
+func (T) method() {}< // method>

Change information

Files:
  • M gopls/doc/features/index.md
  • M gopls/doc/features/passive.md
  • M gopls/doc/inlayHints.md
  • M gopls/doc/release/v0.24.0.md
  • M gopls/internal/doc/api.json
  • M gopls/internal/golang/inlay_hint.go
  • M gopls/internal/settings/settings.go
  • A gopls/internal/test/marker/testdata/inlayhints/function-end-names.txt
Change size: M
Delta: 8 files changed, 83 insertions(+), 1 deletion(-)
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: Ie2a0d429f08809dd96f8991e331ad3c2f4b227f2
Gerrit-Change-Number: 827564
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-CC: 郭根源(genyuan) <genyu...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy
Reply all
Reply to author
Forward
0 new messages