Skip to content

Commit

Permalink
internal/lsp: suppress parameter hint when argument matches parameter
Browse files Browse the repository at this point in the history
Suppress the parameter hint when it would present redundant
information.

Fixes golang/go#2361

Change-Id: I4340a903046f212f8a035eab847da665e2692f1a
Reviewed-on: https://go-review.googlesource.com/c/tools/+/419497
gopls-CI: kokoro <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Run-TryBot: Suzy Mueller <[email protected]>
Reviewed-by: Robert Findley <[email protected]>
  • Loading branch information
suzmue committed Jul 26, 2022
1 parent c83f42d commit 6c8a6c4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
15 changes: 11 additions & 4 deletions internal/lsp/source/inlay_hint.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,24 @@ func parameterNames(node ast.Node, tmap *lsppos.TokenMapper, info *types.Info, _
if i > params.Len()-1 {
break
}
value := params.At(i).Name()
param := params.At(i)
// param.Name is empty for built-ins like append
if value == "" {
if param.Name() == "" {
continue
}
// Skip the parameter name hint if the arg matches the
// the parameter name.
if i, ok := v.(*ast.Ident); ok && i.Name == param.Name() {
continue
}

label := param.Name()
if signature.Variadic() && i == params.Len()-1 {
value = value + "..."
label = label + "..."
}
hints = append(hints, protocol.InlayHint{
Position: &start,
Label: buildLabel(value + ":"),
Label: buildLabel(label + ":"),
Kind: protocol.Parameter,
PaddingRight: true,
})
Expand Down
5 changes: 5 additions & 0 deletions internal/lsp/testdata/inlay_hint/parameter_names.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ func foobar() {
kipp("a", "b", "c")
plex("a", "b", "c")
tars("a", "b", "c")
foo, bar, baz := "a", "b", "c"
kipp(foo, bar, baz)
plex("a", bar, baz)
tars(foo+foo, (bar), "c")

}
5 changes: 5 additions & 0 deletions internal/lsp/testdata/inlay_hint/parameter_names.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,10 @@ func foobar() {
kipp(<foo: >"a", <bar: >"b", <baz: >"c")
plex(<foo: >"a", <bar: >"b", <baz: >"c")
tars(<foo: >"a", <bar: >"b", <baz: >"c")
foo< string>, bar< string>, baz< string> := "a", "b", "c"
kipp(foo, bar, baz)
plex(<foo: >"a", bar, baz)
tars(<foo: >foo+foo, <bar: >(bar), <baz: >"c")

}

0 comments on commit 6c8a6c4

Please sign in to comment.