@@ -147,20 +147,19 @@ func findRhsTypeDecl(ctx context.Context, snapshot *cache.Snapshot, pkg *cache.P
147
147
// we choose Rhs instead of types.Unalias to make the connection between original alias
148
148
// and the corresponding aliased type clearer.
149
149
// types.Unalias brings confusion because it breaks the connection from A to C given
150
- // the alias chain like 'type ( A = B; B =C ; )' except we show all transitive alias
150
+ // the alias chain like 'type ( A = B; B = C ; )' except we show all transitive alias
151
151
// from start to the end. As it's rare, we don't do so.
152
- t := alias .Rhs ()
153
- switch o := t .(type ) {
154
- case * types.Named :
155
- obj = o .Obj ()
156
- declPGF1 , declPos1 , err := parseFull (ctx , snapshot , pkg .FileSet (), obj .Pos ())
152
+ if named , ok := alias .Rhs ().(* types.Named ); ok {
153
+ obj = named .Obj ()
154
+ declPGF1 , declPos1 , err := parseFull (ctx , snapshot , pkg .FileSet (), obj )
157
155
if err != nil {
158
156
return "" , err
159
157
}
160
- realTypeDecl , _ , err := typeDeclContent (declPGF1 , declPos1 , obj )
158
+ realTypeDecl , _ , err := typeDeclContent (declPGF1 , declPos1 , obj . Name () )
161
159
return realTypeDecl , err
162
160
}
163
161
}
162
+
164
163
return "" , nil
165
164
}
166
165
@@ -337,7 +336,7 @@ func hover(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, pp pro
337
336
// order to correctly compute their documentation, signature, and link.
338
337
//
339
338
// Beware: decl{PGF,Pos} are not necessarily associated with pkg.FileSet().
340
- declPGF , declPos , err := parseFull (ctx , snapshot , pkg .FileSet (), obj . Pos () )
339
+ declPGF , declPos , err := parseFull (ctx , snapshot , pkg .FileSet (), obj )
341
340
if err != nil {
342
341
return protocol.Range {}, nil , fmt .Errorf ("re-parsing declaration of %s: %v" , obj .Name (), err )
343
342
}
@@ -468,7 +467,7 @@ func hover(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, pp pro
468
467
_ , isTypeParam := types .Unalias (obj .Type ()).(* types.TypeParam )
469
468
if isTypeName && ! isTypeParam {
470
469
var spec1 * ast.TypeSpec
471
- typeDecl , spec1 , err = typeDeclContent (declPGF , declPos , obj )
470
+ typeDecl , spec1 , err = typeDeclContent (declPGF , declPos , obj . Name () )
472
471
if err != nil {
473
472
return protocol.Range {}, nil , err
474
473
}
@@ -684,7 +683,7 @@ func hover(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, pp pro
684
683
}
685
684
686
685
// typeDeclContent returns a well formatted type definition.
687
- func typeDeclContent (declPGF * parsego.File , declPos token.Pos , obj types. Object ) (string , * ast.TypeSpec , error ) {
686
+ func typeDeclContent (declPGF * parsego.File , declPos token.Pos , name string ) (string , * ast.TypeSpec , error ) {
688
687
_ , spec , _ := findDeclInfo ([]* ast.File {declPGF .File }, declPos ) // may be nil^3
689
688
// Don't duplicate comments.
690
689
spec1 , ok := spec .(* ast.TypeSpec )
@@ -698,7 +697,7 @@ func typeDeclContent(declPGF *parsego.File, declPos token.Pos, obj types.Object)
698
697
if ! declPGF .Fixed () {
699
698
errorf = bug .Errorf
700
699
}
701
- return "" , nil , errorf ("type name %q without type spec" , obj . Name () )
700
+ return "" , nil , errorf ("type name %q without type spec" , name )
702
701
}
703
702
spec2 := * spec1
704
703
spec2 .Doc = nil
@@ -1228,7 +1227,7 @@ func HoverDocForObject(ctx context.Context, snapshot *cache.Snapshot, fset *toke
1228
1227
return nil , nil
1229
1228
}
1230
1229
1231
- pgf , pos , err := parseFull (ctx , snapshot , fset , obj . Pos () )
1230
+ pgf , pos , err := parseFull (ctx , snapshot , fset , obj )
1232
1231
if err != nil {
1233
1232
return nil , fmt .Errorf ("re-parsing: %v" , err )
1234
1233
}
@@ -1273,21 +1272,23 @@ func chooseDocComment(decl ast.Decl, spec ast.Spec, field *ast.Field) *ast.Comme
1273
1272
return nil
1274
1273
}
1275
1274
1276
- // parseFull fully parses the file corresponding to position pos (for
1277
- // which fset provides file/line information).
1278
- //
1279
- // It returns the resulting parsego.File as well as new pos contained
1280
- // in the parsed file.
1275
+ // parseFull fully parses the file containing the declaration of obj
1276
+ // (for which fset provides file/line information). It returns the
1277
+ // parsego.File and the position of the declaration within it.
1281
1278
//
1282
1279
// BEWARE: the provided FileSet is used only to interpret the provided
1283
1280
// pos; the resulting File and Pos may belong to the same or a
1284
1281
// different FileSet, such as one synthesized by the parser cache, if
1285
1282
// parse-caching is enabled.
1286
- //
1287
- // TODO(adonovan): change this function to accept a filename and a
1288
- // byte offset, and eliminate the confusing (fset, pos) parameters.
1289
- // Then simplify stubmethods.StubInfo, which doesn't need a Fset.
1290
- func parseFull (ctx context.Context , snapshot * cache.Snapshot , fset * token.FileSet , pos token.Pos ) (* parsego.File , token.Pos , error ) {
1283
+ func parseFull (ctx context.Context , snapshot * cache.Snapshot , fset * token.FileSet , obj types.Object ) (* parsego.File , token.Pos , error ) {
1284
+ if isBuiltin (obj ) {
1285
+ pgf , id , err := builtinDecl (ctx , snapshot , obj )
1286
+ if err != nil {
1287
+ return nil , 0 , err
1288
+ }
1289
+ return pgf , id .Pos (), err
1290
+ }
1291
+ pos := obj .Pos ()
1291
1292
f := fset .File (pos )
1292
1293
if f == nil {
1293
1294
return nil , 0 , bug .Errorf ("internal error: no file for position %d" , pos )
@@ -1304,11 +1305,11 @@ func parseFull(ctx context.Context, snapshot *cache.Snapshot, fset *token.FileSe
1304
1305
return nil , 0 , err
1305
1306
}
1306
1307
1308
+ // Translate pos from original file to new file.
1307
1309
offset , err := safetoken .Offset (f , pos )
1308
1310
if err != nil {
1309
1311
return nil , 0 , bug .Errorf ("offset out of bounds in %q" , uri )
1310
1312
}
1311
-
1312
1313
fullPos , err := safetoken .Pos (pgf .Tok , offset )
1313
1314
if err != nil {
1314
1315
return nil , 0 , err
0 commit comments