2019-03-25 18:56:05 -06:00
|
|
|
// 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"
|
2019-12-04 11:45:53 -07:00
|
|
|
"fmt"
|
2019-03-25 18:56:05 -06:00
|
|
|
"go/ast"
|
2019-11-21 17:26:14 -07:00
|
|
|
"go/token"
|
2020-01-17 08:54:55 -07:00
|
|
|
"go/types"
|
|
|
|
"strings"
|
2019-03-25 18:56:05 -06:00
|
|
|
|
|
|
|
"golang.org/x/tools/go/ast/astutil"
|
2019-09-05 16:54:05 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2020-03-07 19:28:21 -07:00
|
|
|
"golang.org/x/tools/internal/telemetry/event"
|
2019-08-06 13:13:11 -06:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-03-25 18:56:05 -06:00
|
|
|
)
|
|
|
|
|
2019-12-17 16:57:54 -07:00
|
|
|
func Highlight(ctx context.Context, snapshot Snapshot, fh FileHandle, pos protocol.Position) ([]protocol.Range, error) {
|
2020-03-07 19:28:21 -07:00
|
|
|
ctx, done := event.StartSpan(ctx, "source.Highlight")
|
2019-06-26 20:46:12 -06:00
|
|
|
defer done()
|
2019-07-11 19:05:55 -06:00
|
|
|
|
2020-01-14 16:29:21 -07:00
|
|
|
pkg, pgh, err := getParsedFile(ctx, snapshot, fh, WidestPackageHandle)
|
2019-11-19 12:18:53 -07:00
|
|
|
if err != nil {
|
2019-12-04 11:45:53 -07:00
|
|
|
return nil, fmt.Errorf("getting file for Highlight: %v", err)
|
2019-11-19 12:18:53 -07:00
|
|
|
}
|
2020-02-10 21:10:59 -07:00
|
|
|
file, _, m, _, err := pgh.Parse(ctx)
|
2019-09-17 09:19:11 -06:00
|
|
|
if err != nil {
|
2019-09-16 16:17:51 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-05 16:54:05 -06:00
|
|
|
spn, err := m.PointSpan(pos)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rng, err := spn.Range(m.Converter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
path, _ := astutil.PathEnclosingInterval(file, rng.Start, rng.Start)
|
2019-03-25 18:56:05 -06:00
|
|
|
if len(path) == 0 {
|
2020-03-30 10:45:15 -06:00
|
|
|
return nil, fmt.Errorf("no enclosing position found for %v:%v", int(pos.Line), int(pos.Character))
|
2019-03-25 18:56:05 -06:00
|
|
|
}
|
2019-12-16 15:42:56 -07:00
|
|
|
// If start==end for astutil.PathEnclosingInterval, the 1-char interval following start is used instead.
|
|
|
|
// As a result, we might not get an exact match so we should check the 1-char interval to the left of the
|
|
|
|
// passed in position to see if that is an exact match.
|
|
|
|
if _, ok := path[0].(*ast.Ident); !ok {
|
|
|
|
if p, _ := astutil.PathEnclosingInterval(file, rng.Start-1, rng.Start-1); p != nil {
|
|
|
|
switch p[0].(type) {
|
|
|
|
case *ast.Ident, *ast.SelectorExpr:
|
|
|
|
path = p // use preceding ident/selector
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 09:26:10 -07:00
|
|
|
switch path[0].(type) {
|
2020-01-17 08:54:55 -07:00
|
|
|
case *ast.BasicLit:
|
|
|
|
if len(path) > 1 {
|
|
|
|
if _, ok := path[1].(*ast.ImportSpec); ok {
|
|
|
|
return highlightImportUses(ctx, snapshot.View(), pkg, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return highlightFuncControlFlow(ctx, snapshot.View(), pkg, path)
|
|
|
|
case *ast.ReturnStmt, *ast.FuncDecl, *ast.FuncType:
|
|
|
|
return highlightFuncControlFlow(ctx, snapshot.View(), pkg, path)
|
2019-11-21 09:26:10 -07:00
|
|
|
case *ast.Ident:
|
2020-01-17 08:54:55 -07:00
|
|
|
return highlightIdentifiers(ctx, snapshot.View(), pkg, path)
|
2019-11-21 09:26:10 -07:00
|
|
|
case *ast.BranchStmt, *ast.ForStmt, *ast.RangeStmt:
|
2020-01-17 08:54:55 -07:00
|
|
|
return highlightLoopControlFlow(ctx, snapshot.View(), pkg, path)
|
2019-11-21 09:26:10 -07:00
|
|
|
}
|
|
|
|
// If the cursor is in an unidentified area, return empty results.
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-01-17 08:54:55 -07:00
|
|
|
func highlightFuncControlFlow(ctx context.Context, view View, pkg Package, path []ast.Node) ([]protocol.Range, error) {
|
2019-11-21 17:26:14 -07:00
|
|
|
var enclosingFunc ast.Node
|
|
|
|
var returnStmt *ast.ReturnStmt
|
|
|
|
var resultsList *ast.FieldList
|
|
|
|
inReturnList := false
|
|
|
|
Outer:
|
|
|
|
// Reverse walk the path till we get to the func block.
|
2019-12-13 14:06:33 -07:00
|
|
|
for i, n := range path {
|
2019-11-21 17:26:14 -07:00
|
|
|
switch node := n.(type) {
|
2019-12-05 14:03:59 -07:00
|
|
|
case *ast.KeyValueExpr:
|
|
|
|
// If cursor is in a key: value expr, we don't want control flow highlighting
|
|
|
|
return nil, nil
|
2019-12-13 14:06:33 -07:00
|
|
|
case *ast.CallExpr:
|
|
|
|
// If cusor is an arg in a callExpr, we don't want control flow highlighting.
|
|
|
|
if i > 0 {
|
|
|
|
for _, arg := range node.Args {
|
|
|
|
if arg == path[i-1] {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-21 17:26:14 -07:00
|
|
|
case *ast.Field:
|
|
|
|
inReturnList = true
|
|
|
|
case *ast.FuncLit:
|
|
|
|
enclosingFunc = n
|
|
|
|
resultsList = node.Type.Results
|
|
|
|
break Outer
|
|
|
|
case *ast.FuncDecl:
|
|
|
|
enclosingFunc = n
|
|
|
|
resultsList = node.Type.Results
|
|
|
|
break Outer
|
|
|
|
case *ast.ReturnStmt:
|
|
|
|
returnStmt = node
|
|
|
|
// If the cursor is not directly in a *ast.ReturnStmt, then
|
|
|
|
// we need to know if it is within one of the values that is being returned.
|
|
|
|
inReturnList = inReturnList || path[0] != returnStmt
|
|
|
|
}
|
|
|
|
}
|
2019-12-02 13:10:25 -07:00
|
|
|
// Cursor is not in a function.
|
|
|
|
if enclosingFunc == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2019-11-21 17:26:14 -07:00
|
|
|
// If the cursor is on a "return" or "func" keyword, we should highlight all of the exit
|
|
|
|
// points of the function, including the "return" and "func" keywords.
|
|
|
|
highlightAllReturnsAndFunc := path[0] == returnStmt || path[0] == enclosingFunc
|
|
|
|
switch path[0].(type) {
|
|
|
|
case *ast.Ident, *ast.BasicLit:
|
|
|
|
// Cursor is in an identifier and not in a return statement or in the results list.
|
|
|
|
if returnStmt == nil && !inReturnList {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
case *ast.FuncType:
|
|
|
|
highlightAllReturnsAndFunc = true
|
|
|
|
}
|
|
|
|
// The user's cursor may be within the return statement of a function,
|
|
|
|
// or within the result section of a function's signature.
|
|
|
|
// index := -1
|
|
|
|
var nodes []ast.Node
|
|
|
|
if returnStmt != nil {
|
|
|
|
for _, n := range returnStmt.Results {
|
|
|
|
nodes = append(nodes, n)
|
|
|
|
}
|
|
|
|
} else if resultsList != nil {
|
|
|
|
for _, n := range resultsList.List {
|
|
|
|
nodes = append(nodes, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, index := nodeAtPos(nodes, path[0].Pos())
|
|
|
|
|
|
|
|
result := make(map[protocol.Range]bool)
|
|
|
|
// Highlight the correct argument in the function declaration return types.
|
|
|
|
if resultsList != nil && -1 < index && index < len(resultsList.List) {
|
2020-01-17 08:54:55 -07:00
|
|
|
rng, err := nodeToProtocolRange(view, pkg, resultsList.List[index])
|
2019-11-21 17:26:14 -07:00
|
|
|
if err != nil {
|
2020-03-30 10:45:15 -06:00
|
|
|
return nil, err
|
2019-11-21 17:26:14 -07:00
|
|
|
}
|
2020-03-30 10:45:15 -06:00
|
|
|
result[rng] = true
|
2019-11-21 17:26:14 -07:00
|
|
|
}
|
|
|
|
// Add the "func" part of the func declaration.
|
|
|
|
if highlightAllReturnsAndFunc {
|
2020-01-17 08:54:55 -07:00
|
|
|
funcStmt, err := posToMappedRange(view, pkg, enclosingFunc.Pos(), enclosingFunc.Pos()+token.Pos(len("func")))
|
2019-11-21 17:26:14 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rng, err := funcStmt.Range()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result[rng] = true
|
|
|
|
}
|
|
|
|
// Traverse the AST to highlight the other relevant return statements in the function.
|
|
|
|
ast.Inspect(enclosingFunc, func(n ast.Node) bool {
|
|
|
|
// Don't traverse any other functions.
|
|
|
|
switch n.(type) {
|
|
|
|
case *ast.FuncDecl, *ast.FuncLit:
|
|
|
|
return enclosingFunc == n
|
|
|
|
}
|
|
|
|
if n, ok := n.(*ast.ReturnStmt); ok {
|
|
|
|
var toAdd ast.Node
|
|
|
|
// Add the entire return statement, applies when highlight the word "return" or "func".
|
|
|
|
if highlightAllReturnsAndFunc {
|
|
|
|
toAdd = n
|
|
|
|
}
|
|
|
|
// Add the relevant field within the entire return statement.
|
|
|
|
if -1 < index && index < len(n.Results) {
|
|
|
|
toAdd = n.Results[index]
|
|
|
|
}
|
|
|
|
if toAdd != nil {
|
2020-01-17 08:54:55 -07:00
|
|
|
rng, err := nodeToProtocolRange(view, pkg, toAdd)
|
2019-11-21 17:26:14 -07:00
|
|
|
if err != nil {
|
2020-03-07 19:28:21 -07:00
|
|
|
event.Error(ctx, "Error getting range for node", err)
|
2020-03-30 10:45:15 -06:00
|
|
|
return false
|
2019-11-21 17:26:14 -07:00
|
|
|
}
|
2020-03-30 10:45:15 -06:00
|
|
|
result[rng] = true
|
2019-11-21 17:26:14 -07:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return rangeMapToSlice(result), nil
|
|
|
|
}
|
|
|
|
|
2020-01-17 08:54:55 -07:00
|
|
|
func highlightLoopControlFlow(ctx context.Context, view View, pkg Package, path []ast.Node) ([]protocol.Range, error) {
|
2019-11-21 09:26:10 -07:00
|
|
|
var loop ast.Node
|
|
|
|
Outer:
|
2019-11-21 17:26:14 -07:00
|
|
|
// Reverse walk the path till we get to the for loop.
|
2019-11-21 09:26:10 -07:00
|
|
|
for _, n := range path {
|
|
|
|
switch n.(type) {
|
|
|
|
case *ast.ForStmt, *ast.RangeStmt:
|
|
|
|
loop = n
|
|
|
|
break Outer
|
|
|
|
}
|
|
|
|
}
|
2019-11-21 17:26:14 -07:00
|
|
|
// Cursor is not in a for loop.
|
2019-11-21 09:26:10 -07:00
|
|
|
if loop == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2019-11-21 17:26:14 -07:00
|
|
|
result := make(map[protocol.Range]bool)
|
2019-11-21 09:26:10 -07:00
|
|
|
// Add the for statement.
|
2020-01-17 08:54:55 -07:00
|
|
|
forStmt, err := posToMappedRange(view, pkg, loop.Pos(), loop.Pos()+token.Pos(len("for")))
|
2019-11-21 09:26:10 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rng, err := forStmt.Range()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-21 17:26:14 -07:00
|
|
|
result[rng] = true
|
2019-11-21 09:26:10 -07:00
|
|
|
|
|
|
|
ast.Inspect(loop, func(n ast.Node) bool {
|
|
|
|
// Don't traverse any other for loops.
|
|
|
|
switch n.(type) {
|
|
|
|
case *ast.ForStmt, *ast.RangeStmt:
|
|
|
|
return loop == n
|
|
|
|
}
|
2019-11-21 17:26:14 -07:00
|
|
|
// Add all branch statements in same scope as the identified one.
|
2019-11-21 09:26:10 -07:00
|
|
|
if n, ok := n.(*ast.BranchStmt); ok {
|
2020-01-17 08:54:55 -07:00
|
|
|
rng, err := nodeToProtocolRange(view, pkg, n)
|
2019-11-21 09:26:10 -07:00
|
|
|
if err != nil {
|
2020-03-07 19:28:21 -07:00
|
|
|
event.Error(ctx, "Error getting range for node", err)
|
2019-11-21 09:26:10 -07:00
|
|
|
return false
|
|
|
|
}
|
2019-11-21 17:26:14 -07:00
|
|
|
result[rng] = true
|
2019-11-21 09:26:10 -07:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
2019-11-21 17:26:14 -07:00
|
|
|
return rangeMapToSlice(result), nil
|
2019-11-21 09:26:10 -07:00
|
|
|
}
|
|
|
|
|
2020-01-17 08:54:55 -07:00
|
|
|
func highlightImportUses(ctx context.Context, view View, pkg Package, path []ast.Node) ([]protocol.Range, error) {
|
|
|
|
result := make(map[protocol.Range]bool)
|
|
|
|
basicLit, ok := path[0].(*ast.BasicLit)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("highlightImportUses called with an ast.Node of type %T", basicLit)
|
|
|
|
}
|
|
|
|
|
|
|
|
ast.Inspect(path[len(path)-1], func(node ast.Node) bool {
|
|
|
|
if imp, ok := node.(*ast.ImportSpec); ok && imp.Path == basicLit {
|
|
|
|
if rng, err := nodeToProtocolRange(view, pkg, node); err == nil {
|
|
|
|
result[rng] = true
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n, ok := node.(*ast.Ident)
|
|
|
|
if !ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
obj, ok := pkg.GetTypesInfo().ObjectOf(n).(*types.PkgName)
|
|
|
|
if !ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if !strings.Contains(basicLit.Value, obj.Name()) {
|
|
|
|
return true
|
|
|
|
}
|
2020-03-30 10:45:15 -06:00
|
|
|
rng, err := nodeToProtocolRange(view, pkg, n)
|
|
|
|
if err != nil {
|
2020-03-07 19:28:21 -07:00
|
|
|
event.Error(ctx, "Error getting range for node", err)
|
2020-03-30 10:45:15 -06:00
|
|
|
return false
|
2020-01-17 08:54:55 -07:00
|
|
|
}
|
2020-03-30 10:45:15 -06:00
|
|
|
result[rng] = true
|
2020-01-17 08:54:55 -07:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
return rangeMapToSlice(result), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func highlightIdentifiers(ctx context.Context, view View, pkg Package, path []ast.Node) ([]protocol.Range, error) {
|
2019-11-21 17:26:14 -07:00
|
|
|
result := make(map[protocol.Range]bool)
|
2019-03-25 18:56:05 -06:00
|
|
|
id, ok := path[0].(*ast.Ident)
|
|
|
|
if !ok {
|
2019-11-21 09:26:10 -07:00
|
|
|
return nil, errors.Errorf("highlightIdentifiers called with an ast.Node of type %T", id)
|
2019-03-25 18:56:05 -06:00
|
|
|
}
|
2019-11-21 17:26:14 -07:00
|
|
|
// Check if ident is inside return or func decl.
|
2020-01-17 08:54:55 -07:00
|
|
|
if toAdd, err := highlightFuncControlFlow(ctx, view, pkg, path); toAdd != nil && err == nil {
|
2019-11-21 17:26:14 -07:00
|
|
|
for _, r := range toAdd {
|
|
|
|
result[r] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: maybe check if ident is a reserved word, if true then don't continue and return results.
|
2019-11-19 12:18:53 -07:00
|
|
|
|
2019-11-21 09:26:10 -07:00
|
|
|
idObj := pkg.GetTypesInfo().ObjectOf(id)
|
2020-01-17 08:54:55 -07:00
|
|
|
pkgObj, isImported := idObj.(*types.PkgName)
|
2019-11-19 12:18:53 -07:00
|
|
|
ast.Inspect(path[len(path)-1], func(node ast.Node) bool {
|
2020-01-17 08:54:55 -07:00
|
|
|
if imp, ok := node.(*ast.ImportSpec); ok && isImported {
|
|
|
|
if rng, err := highlightImport(view, pkg, pkgObj, imp); rng != nil && err == nil {
|
|
|
|
result[*rng] = true
|
|
|
|
}
|
|
|
|
}
|
2019-11-19 12:18:53 -07:00
|
|
|
n, ok := node.(*ast.Ident)
|
|
|
|
if !ok {
|
|
|
|
return true
|
|
|
|
}
|
2019-12-05 14:03:59 -07:00
|
|
|
if n.Name != id.Name {
|
2019-11-21 09:26:10 -07:00
|
|
|
return false
|
2019-11-19 12:18:53 -07:00
|
|
|
}
|
2019-11-21 09:26:10 -07:00
|
|
|
if nObj := pkg.GetTypesInfo().ObjectOf(n); nObj != idObj {
|
2019-11-19 12:18:53 -07:00
|
|
|
return false
|
|
|
|
}
|
2020-03-30 10:45:15 -06:00
|
|
|
rng, err := nodeToProtocolRange(view, pkg, n)
|
|
|
|
if err != nil {
|
2020-03-07 19:28:21 -07:00
|
|
|
event.Error(ctx, "Error getting range for node", err)
|
2020-03-30 10:45:15 -06:00
|
|
|
return false
|
2019-11-19 12:18:53 -07:00
|
|
|
}
|
2020-03-30 10:45:15 -06:00
|
|
|
result[rng] = true
|
2019-11-21 09:26:10 -07:00
|
|
|
return false
|
2019-11-19 12:18:53 -07:00
|
|
|
})
|
2019-11-21 17:26:14 -07:00
|
|
|
return rangeMapToSlice(result), nil
|
|
|
|
}
|
|
|
|
|
2020-01-17 08:54:55 -07:00
|
|
|
func highlightImport(view View, pkg Package, obj *types.PkgName, imp *ast.ImportSpec) (*protocol.Range, error) {
|
|
|
|
if imp.Name != nil || imp.Path == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if !strings.Contains(imp.Path.Value, obj.Name()) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
rng, err := nodeToProtocolRange(view, pkg, imp.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &rng, nil
|
|
|
|
}
|
|
|
|
|
2019-11-21 17:26:14 -07:00
|
|
|
func rangeMapToSlice(rangeMap map[protocol.Range]bool) []protocol.Range {
|
|
|
|
var list []protocol.Range
|
|
|
|
for i := range rangeMap {
|
|
|
|
list = append(list, i)
|
|
|
|
}
|
|
|
|
return list
|
2019-03-25 18:56:05 -06:00
|
|
|
}
|