mirror of https://github.com/golang/go.git
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 <suzmue@golang.org> Run-TryBot: Suzy Mueller <suzmue@golang.org> gopls-CI: kokoro <noreply+kokoro@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
parent
939195fa01
commit
ba6b94c735
|
|
@ -13,6 +13,7 @@ import (
|
|||
"go/format"
|
||||
"go/token"
|
||||
"go/types"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"golang.org/x/tools/go/analysis"
|
||||
|
|
@ -87,6 +88,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
|
|||
}
|
||||
|
||||
var fillable bool
|
||||
var fillableFields []string
|
||||
for i := 0; i < fieldCount; i++ {
|
||||
field := obj.Field(i)
|
||||
// Ignore fields that are not accessible in the current package.
|
||||
|
|
@ -94,6 +96,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
|
|||
continue
|
||||
}
|
||||
fillable = true
|
||||
fillableFields = append(fillableFields, fmt.Sprintf("%s: %s", field.Name(), field.Type().String()))
|
||||
}
|
||||
if !fillable {
|
||||
return
|
||||
|
|
@ -105,7 +108,21 @@ func run(pass *analysis.Pass) (interface{}, error) {
|
|||
case *ast.SelectorExpr:
|
||||
name = fmt.Sprintf("%s.%s", typ.X, typ.Sel.Name)
|
||||
default:
|
||||
name = "anonymous struct"
|
||||
totalFields := len(fillableFields)
|
||||
maxLen := 20
|
||||
// Find the index to cut off printing of fields.
|
||||
var i, fieldLen int
|
||||
for i = range fillableFields {
|
||||
if fieldLen > maxLen {
|
||||
break
|
||||
}
|
||||
fieldLen += len(fillableFields[i])
|
||||
}
|
||||
fillableFields = fillableFields[:i]
|
||||
if i < totalFields {
|
||||
fillableFields = append(fillableFields, "...")
|
||||
}
|
||||
name = fmt.Sprintf("anonymous struct { %s }", strings.Join(fillableFields, ", "))
|
||||
}
|
||||
pass.Report(analysis.Diagnostic{
|
||||
Message: fmt.Sprintf("Fill %s", name),
|
||||
|
|
|
|||
Loading…
Reference in New Issue