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>
diff --git a/internal/lsp/analysis/fillstruct/fillstruct.go b/internal/lsp/analysis/fillstruct/fillstruct.go
index 36a63a1..0547288 100644
--- a/internal/lsp/analysis/fillstruct/fillstruct.go
+++ b/internal/lsp/analysis/fillstruct/fillstruct.go
@@ -13,6 +13,7 @@
 	"go/format"
 	"go/token"
 	"go/types"
+	"strings"
 	"unicode"
 
 	"golang.org/x/tools/go/analysis"
@@ -87,6 +88,7 @@
 		}
 
 		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 @@
 				continue
 			}
 			fillable = true
+			fillableFields = append(fillableFields, fmt.Sprintf("%s: %s", field.Name(), field.Type().String()))
 		}
 		if !fillable {
 			return
@@ -105,7 +108,21 @@
 		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),