mirror of
https://github.com/golang/go
synced 2024-11-05 21:36:12 -07:00
ca36ab2721
Change-Id: I232dbb0b66d690e45079808fd0dbf026c4459400 Reviewed-on: https://go-review.googlesource.com/c/tools/+/169277 Reviewed-by: Rebecca Stambler <rstambler@golang.org> Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
43 lines
864 B
Go
43 lines
864 B
Go
// 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"
|
|
)
|
|
|
|
func Highlight(ctx context.Context, f File, pos token.Pos) []span.Span {
|
|
fAST := f.GetAST(ctx)
|
|
fset := f.GetFileSet(ctx)
|
|
path, _ := astutil.PathEnclosingInterval(fAST, pos, pos)
|
|
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
|
|
}
|