@@ -48,36 +48,38 @@ func Highlight(ctx context.Context, snapshot Snapshot, fh FileHandle, position p
48
48
}
49
49
}
50
50
}
51
- result , err := highlightPath (pkg , path )
51
+ result , err := highlightPath (path , pgf . File , pkg . GetTypesInfo () )
52
52
if err != nil {
53
53
return nil , err
54
54
}
55
55
var ranges []protocol.Range
56
56
for rng := range result {
57
- mRng , err := posToMappedRange ( ctx , snapshot , pkg , rng .start , rng .end )
57
+ rng , err := pgf . PosRange ( rng .start , rng .end )
58
58
if err != nil {
59
59
return nil , err
60
60
}
61
- ranges = append (ranges , mRng . Range () )
61
+ ranges = append (ranges , rng )
62
62
}
63
63
return ranges , nil
64
64
}
65
65
66
- func highlightPath (pkg Package , path []ast.Node ) (map [posRange ]struct {}, error ) {
66
+ func highlightPath (path []ast.Node , file * ast. File , info * types. Info ) (map [posRange ]struct {}, error ) {
67
67
result := make (map [posRange ]struct {})
68
68
switch node := path [0 ].(type ) {
69
69
case * ast.BasicLit :
70
70
if len (path ) > 1 {
71
71
if _ , ok := path [1 ].(* ast.ImportSpec ); ok {
72
- err := highlightImportUses (pkg , path , result )
72
+ err := highlightImportUses (path , info , result )
73
73
return result , err
74
74
}
75
75
}
76
76
highlightFuncControlFlow (path , result )
77
77
case * ast.ReturnStmt , * ast.FuncDecl , * ast.FuncType :
78
78
highlightFuncControlFlow (path , result )
79
79
case * ast.Ident :
80
- highlightIdentifiers (pkg , path , result )
80
+ // Check if ident is inside return or func decl.
81
+ highlightFuncControlFlow (path , result )
82
+ highlightIdentifier (node , file , info , result )
81
83
case * ast.ForStmt , * ast.RangeStmt :
82
84
highlightLoopControlFlow (path , result )
83
85
case * ast.SwitchStmt :
@@ -426,7 +428,7 @@ func labelDecl(n *ast.Ident) *ast.Ident {
426
428
return stmt .Label
427
429
}
428
430
429
- func highlightImportUses (pkg Package , path []ast.Node , result map [posRange ]struct {}) error {
431
+ func highlightImportUses (path []ast.Node , info * types. Info , result map [posRange ]struct {}) error {
430
432
basicLit , ok := path [0 ].(* ast.BasicLit )
431
433
if ! ok {
432
434
return fmt .Errorf ("highlightImportUses called with an ast.Node of type %T" , basicLit )
@@ -440,7 +442,7 @@ func highlightImportUses(pkg Package, path []ast.Node, result map[posRange]struc
440
442
if ! ok {
441
443
return true
442
444
}
443
- obj , ok := pkg . GetTypesInfo () .ObjectOf (n ).(* types.PkgName )
445
+ obj , ok := info .ObjectOf (n ).(* types.PkgName )
444
446
if ! ok {
445
447
return true
446
448
}
@@ -453,19 +455,16 @@ func highlightImportUses(pkg Package, path []ast.Node, result map[posRange]struc
453
455
return nil
454
456
}
455
457
456
- func highlightIdentifiers (pkg Package , path []ast.Node , result map [posRange ]struct {}) error {
457
- id , ok := path [0 ].(* ast.Ident )
458
- if ! ok {
459
- return fmt .Errorf ("highlightIdentifiers called with an ast.Node of type %T" , id )
460
- }
461
- // Check if ident is inside return or func decl.
462
- highlightFuncControlFlow (path , result )
463
-
464
- // TODO: maybe check if ident is a reserved word, if true then don't continue and return results.
465
-
466
- idObj := pkg .GetTypesInfo ().ObjectOf (id )
458
+ func highlightIdentifier (id * ast.Ident , file * ast.File , info * types.Info , result map [posRange ]struct {}) {
459
+ // TODO(rfindley): idObj may be nil. Note that returning early in this case
460
+ // causes tests to fail (because the nObj == idObj check below was succeeded
461
+ // for nil == nil!)
462
+ //
463
+ // Revisit this. If ObjectOf is nil, there are type errors, and it seems
464
+ // reasonable for identifier highlighting not to work.
465
+ idObj := info .ObjectOf (id )
467
466
pkgObj , isImported := idObj .(* types.PkgName )
468
- ast .Inspect (path [ len ( path ) - 1 ] , func (node ast.Node ) bool {
467
+ ast .Inspect (file , func (node ast.Node ) bool {
469
468
if imp , ok := node .(* ast.ImportSpec ); ok && isImported {
470
469
highlightImport (pkgObj , imp , result )
471
470
}
@@ -476,12 +475,11 @@ func highlightIdentifiers(pkg Package, path []ast.Node, result map[posRange]stru
476
475
if n .Name != id .Name {
477
476
return false
478
477
}
479
- if nObj := pkg . GetTypesInfo () .ObjectOf (n ); nObj == idObj {
478
+ if nObj := info .ObjectOf (n ); nObj == idObj {
480
479
result [posRange {start : n .Pos (), end : n .End ()}] = struct {}{}
481
480
}
482
481
return false
483
482
})
484
- return nil
485
483
}
486
484
487
485
func highlightImport (obj * types.PkgName , imp * ast.ImportSpec , result map [posRange ]struct {}) {
0 commit comments