1
0
mirror of https://github.com/golang/go synced 2024-10-01 03:38:32 -06:00
go/internal/lsp/source/highlight.go
Ian Cottrell 4f9510c6a1 internal/lsp: prepare for non go files
This abstracts out the concrete file type so that we can support non go files.

Change-Id: I7447daa2ce076ec2867de9e59a0dedfe1a0553f5
Reviewed-on: https://go-review.googlesource.com/c/tools/+/175217
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-05-15 23:59:46 +00:00

43 lines
866 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 GoFile, 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
}