mirror of
https://github.com/golang/go
synced 2024-11-18 23:14:43 -07:00
156eb2ae29
This is a straight move of some code with no changes. It splits the part of the telemetry code that will become a standalone library from the bit that belongs in the lsp. Change-Id: Icedb6bf1f3711da9251450531729984df6df7787 Reviewed-on: https://go-review.googlesource.com/c/tools/+/190403 Run-TryBot: Ian Cottrell <iancottrell@google.com> Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
49 lines
1.2 KiB
Go
49 lines
1.2 KiB
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"
|
|
"golang.org/x/tools/internal/telemetry/trace"
|
|
errors "golang.org/x/xerrors"
|
|
)
|
|
|
|
func Highlight(ctx context.Context, f GoFile, pos token.Pos) ([]span.Span, error) {
|
|
ctx, done := trace.StartSpan(ctx, "source.Highlight")
|
|
defer done()
|
|
|
|
file, err := f.GetAST(ctx, ParseFull)
|
|
if file == nil {
|
|
return nil, err
|
|
}
|
|
fset := f.FileSet()
|
|
path, _ := astutil.PathEnclosingInterval(file, pos, pos)
|
|
if len(path) == 0 {
|
|
return nil, errors.Errorf("no enclosing position found for %s", fset.Position(pos))
|
|
}
|
|
id, ok := path[0].(*ast.Ident)
|
|
if !ok {
|
|
return nil, errors.Errorf("%s is not an identifier", fset.Position(pos))
|
|
}
|
|
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, nil
|
|
}
|