Skip to content

Commit ba6b94c

Browse files
committed
internal/lsp: add fields to anonymous struct info
Anonymous struct quick fixes should provide more information on which struct they will fill. This adds the first fields of an anonymous struct to the fill title. Updates golang/go#48563 Change-Id: I42cee2e8b1b9405ac2e2e8cfb8deb2c7710c3056 Reviewed-on: https://go-review.googlesource.com/c/tools/+/351591 Trust: Suzy Mueller <[email protected]> Run-TryBot: Suzy Mueller <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]>
1 parent 939195f commit ba6b94c

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

internal/lsp/analysis/fillstruct/fillstruct.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"go/format"
1414
"go/token"
1515
"go/types"
16+
"strings"
1617
"unicode"
1718

1819
"golang.org/x/tools/go/analysis"
@@ -87,13 +88,15 @@ func run(pass *analysis.Pass) (interface{}, error) {
8788
}
8889

8990
var fillable bool
91+
var fillableFields []string
9092
for i := 0; i < fieldCount; i++ {
9193
field := obj.Field(i)
9294
// Ignore fields that are not accessible in the current package.
9395
if field.Pkg() != nil && field.Pkg() != pass.Pkg && !field.Exported() {
9496
continue
9597
}
9698
fillable = true
99+
fillableFields = append(fillableFields, fmt.Sprintf("%s: %s", field.Name(), field.Type().String()))
97100
}
98101
if !fillable {
99102
return
@@ -105,7 +108,21 @@ func run(pass *analysis.Pass) (interface{}, error) {
105108
case *ast.SelectorExpr:
106109
name = fmt.Sprintf("%s.%s", typ.X, typ.Sel.Name)
107110
default:
108-
name = "anonymous struct"
111+
totalFields := len(fillableFields)
112+
maxLen := 20
113+
// Find the index to cut off printing of fields.
114+
var i, fieldLen int
115+
for i = range fillableFields {
116+
if fieldLen > maxLen {
117+
break
118+
}
119+
fieldLen += len(fillableFields[i])
120+
}
121+
fillableFields = fillableFields[:i]
122+
if i < totalFields {
123+
fillableFields = append(fillableFields, "...")
124+
}
125+
name = fmt.Sprintf("anonymous struct { %s }", strings.Join(fillableFields, ", "))
109126
}
110127
pass.Report(analysis.Diagnostic{
111128
Message: fmt.Sprintf("Fill %s", name),

0 commit comments

Comments
 (0)