@@ -1209,18 +1209,19 @@ func formatHover(h *hoverJSON, options *settings.Options, pkgURL func(path Packa
12091209// StdSymbolOf returns the std lib symbol information of the given obj.
12101210// It returns nil if the input obj is not an exported standard library symbol.
12111211func StdSymbolOf (obj types.Object ) * stdlib.Symbol {
1212- if ! obj .Exported () {
1212+ if ! obj .Exported () || obj .Pkg () == nil {
1213+ return nil
1214+ }
1215+
1216+ // Symbols that not defined in standard library should return early.
1217+ // TODO(hxjiang): The returned slices is binary searchable.
1218+ symbols := stdlib .PackageSymbols [obj .Pkg ().Path ()]
1219+ if symbols == nil {
12131220 return nil
12141221 }
12151222
12161223 // Handle Function, Type, Const & Var.
12171224 if isPackageLevel (obj ) {
1218- // Symbols defined not in std lib package should return early.
1219- symbols := stdlib .PackageSymbols [obj .Pkg ().Path ()]
1220- if symbols == nil {
1221- return nil
1222- }
1223- // TODO(hxjiang): This is binary searchable.
12241225 for _ , s := range symbols {
12251226 if s .Kind == stdlib .Method || s .Kind == stdlib .Field {
12261227 continue
@@ -1236,7 +1237,7 @@ func StdSymbolOf(obj types.Object) *stdlib.Symbol {
12361237 if fn , _ := obj .(* types.Func ); fn != nil {
12371238 isPtr , named := typesinternal .ReceiverNamed (fn .Type ().(* types.Signature ).Recv ())
12381239 if isPackageLevel (named .Obj ()) {
1239- for _ , s := range stdlib . PackageSymbols [ obj . Pkg (). Path ()] {
1240+ for _ , s := range symbols {
12401241 if s .Kind != stdlib .Method {
12411242 continue
12421243 }
@@ -1249,7 +1250,30 @@ func StdSymbolOf(obj types.Object) *stdlib.Symbol {
12491250 }
12501251 }
12511252
1252- // TODO(hxjiang): handle exported fields of package level types.
1253+ // Handle Field.
1254+ if v , _ := obj .(* types.Var ); v != nil && v .IsField () {
1255+ for _ , s := range symbols {
1256+ if s .Kind != stdlib .Field {
1257+ continue
1258+ }
1259+
1260+ typeName , fieldName := s .SplitField ()
1261+ if fieldName != v .Name () {
1262+ continue
1263+ }
1264+
1265+ typeObj := obj .Pkg ().Scope ().Lookup (typeName )
1266+ if typeObj == nil {
1267+ continue
1268+ }
1269+
1270+ if fieldObj , _ , _ := types .LookupFieldOrMethod (typeObj .Type (), true , obj .Pkg (), fieldName ); obj == fieldObj {
1271+ return & s
1272+ }
1273+ }
1274+ return nil
1275+ }
1276+
12531277 return nil
12541278}
12551279
0 commit comments