Skip to content

Commit

Permalink
internal/lsp: skip return highlighting if cursor is in arglist of func
Browse files Browse the repository at this point in the history
If the cursor is within an argument that is within a callExpr which is
in a return statement, we only want it to highlight the ident that the cursor
is in. We do not want it to highlight the entire function.

Updates golang/go#34496

Change-Id: If4025660a99fd5df90098e0560a5e9e7260e33c8
Reviewed-on: https://go-review.googlesource.com/c/tools/+/211338
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Run-TryBot: Rohan Challa <rohan@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
  • Loading branch information
ridersofrohan committed Dec 13, 2019
1 parent 56463cc commit 04c2e8e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
11 changes: 10 additions & 1 deletion internal/lsp/source/highlight.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,20 @@ func highlightFuncControlFlow(ctx context.Context, snapshot Snapshot, m *protoco
inReturnList := false
Outer:
// Reverse walk the path till we get to the func block.
for _, n := range path {
for i, n := range path {
switch node := n.(type) {
case *ast.KeyValueExpr:
// If cursor is in a key: value expr, we don't want control flow highlighting
return nil, nil
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
}
}
}
case *ast.Field:
inReturnList = true
case *ast.FuncLit:
Expand Down
5 changes: 5 additions & 0 deletions internal/lsp/testdata/highlights/highlights.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,8 @@ func testReturnMultipleFields() (float32, string) { //@mark(retVal31, "float32")
}
return 4.9, "test" //@mark(retVal51, "4.9"),mark(retVal52, "\"test\""),highlight(retVal51, retVal31, retVal41, retVal51),highlight(retVal52, retVal32, retVal42, retVal52)
}

func testReturnFunc() int32 { //@mark(retCall, "int32")
mulch := 1 //@mark(mulchDec, "mulch"),highlight(mulchDec, mulchDec, mulchRet)
return int32(mulch) //@mark(mulchRet, "mulch"),mark(retFunc, "int32"),mark(retTotal, "int32(mulch)"),highlight(mulchRet, mulchDec, mulchRet),highlight(retFunc, retCall, retFunc, retTotal)
}
2 changes: 1 addition & 1 deletion internal/lsp/testdata/summary.txt.golden
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ImportCount = 7
SuggestedFixCount = 1
DefinitionsCount = 39
TypeDefinitionsCount = 2
HighlightsCount = 41
HighlightsCount = 44
ReferencesCount = 7
RenamesCount = 22
PrepareRenamesCount = 8
Expand Down

0 comments on commit 04c2e8e

Please sign in to comment.