1
0
mirror of https://github.com/golang/go synced 2024-11-19 03:54:42 -07:00
go/internal/lsp/source/highlight.go
Rebecca Stambler 7e7c6e5214 internal/lsp: add nil checks for ASTs and token
Fixes golang/go#32120
Updates golang/go#32132

Change-Id: Ib4bb8a4818be7dec468c46b72afc3dd57b35f155
Reviewed-on: https://go-review.googlesource.com/c/tools/+/178158
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2019-05-20 20:09:54 +00:00

46 lines
900 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 {
file := f.GetAST(ctx)
if file == nil {
return nil
}
fset := f.GetFileSet(ctx)
path, _ := astutil.PathEnclosingInterval(file, 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
}