mirror of
https://github.com/golang/go
synced 2024-11-19 03:44:40 -07:00
be5259f298
This relates to https://github.com/golang/go/issues/31374 and should switch all instances within `gopls` to use `x/errors` instead of `fmt` to create new errors. Change-Id: I18339b75d12418d852e0dcc2ba0ed6c2970783b3 GitHub-Last-Rev: f4a55d9b79e7458ef1f1e06cb5eabbabd884f321 GitHub-Pull-Request: golang/tools#108 Reviewed-on: https://go-review.googlesource.com/c/tools/+/179880 Run-TryBot: Rebecca Stambler <rstambler@golang.org> Reviewed-by: Rebecca Stambler <rstambler@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/lsp/telemetry/trace"
|
|
"golang.org/x/tools/internal/span"
|
|
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
|
|
}
|