2019-03-25 18:56:05 -06:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package source
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"go/ast"
|
|
|
|
"go/token"
|
|
|
|
|
|
|
|
"golang.org/x/tools/go/ast/astutil"
|
|
|
|
"golang.org/x/tools/internal/span"
|
|
|
|
)
|
|
|
|
|
2019-05-03 22:04:18 -06:00
|
|
|
func Highlight(ctx context.Context, f GoFile, pos token.Pos) []span.Span {
|
2019-05-20 13:23:02 -06:00
|
|
|
file := f.GetAST(ctx)
|
|
|
|
if file == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2019-03-25 18:56:05 -06:00
|
|
|
fset := f.GetFileSet(ctx)
|
2019-05-20 13:23:02 -06:00
|
|
|
path, _ := astutil.PathEnclosingInterval(file, pos, pos)
|
2019-03-25 18:56:05 -06:00
|
|
|
if len(path) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
id, ok := path[0].(*ast.Ident)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var result []span.Span
|
|
|
|
if id.Obj != nil {
|
|
|
|
ast.Inspect(path[len(path)-1], func(n ast.Node) bool {
|
|
|
|
if n, ok := n.(*ast.Ident); ok && n.Obj == id.Obj {
|
|
|
|
s, err := nodeSpan(n, fset)
|
|
|
|
if err == nil {
|
|
|
|
result = append(result, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|