Skip to content

Commit 115af5e

Browse files
committed
internal/lsp: fix control flow highlighting taking precedence
The control flow highlighting is taking precedence when you are highlighting a key:value expression within the return statement. Expected behavior is to just highlight all instances of the key or value and ignore the control flow statement when inside the scope. Fixes golang/go#36057 Change-Id: If4b254151c38d152f337833c55a456f8dce18be7 Reviewed-on: https://go-review.googlesource.com/c/tools/+/210558 Reviewed-by: Rebecca Stambler <[email protected]> Run-TryBot: Rohan Challa <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 9a30a9a commit 115af5e

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

internal/lsp/source/highlight.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ Outer:
6262
// Reverse walk the path till we get to the func block.
6363
for _, n := range path {
6464
switch node := n.(type) {
65+
case *ast.KeyValueExpr:
66+
// If cursor is in a key: value expr, we don't want control flow highlighting
67+
return nil, nil
6568
case *ast.Field:
6669
inReturnList = true
6770
case *ast.FuncLit:
@@ -232,7 +235,7 @@ func highlightIdentifiers(ctx context.Context, snapshot Snapshot, m *protocol.Co
232235
if !ok {
233236
return true
234237
}
235-
if n.Name != id.Name || n.Obj != id.Obj {
238+
if n.Name != id.Name {
236239
return false
237240
}
238241
if nObj := pkg.GetTypesInfo().ObjectOf(n); nObj != idObj {

internal/lsp/testdata/highlights/highlights.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,23 @@ import (
77
"golang.org/x/tools/internal/lsp/protocol"
88
)
99

10-
type F struct{ bar int }
10+
type F struct{ bar int } //@mark(barDeclaration, "bar"),highlight(barDeclaration, barDeclaration, bar1, bar2, bar3)
1111

12-
var foo = F{bar: 52} //@mark(fooDeclaration, "foo"),highlight(fooDeclaration, fooDeclaration, fooUse)
12+
func _() F {
13+
return F{
14+
bar: 123, //@mark(bar1, "bar"),highlight(bar1, barDeclaration, bar1, bar2, bar3)
15+
}
16+
}
17+
18+
var foo = F{bar: 52} //@mark(fooDeclaration, "foo"),mark(bar2, "bar"),highlight(fooDeclaration, fooDeclaration, fooUse),highlight(bar2, barDeclaration, bar1, bar2, bar3)
1319

1420
func Print() { //@mark(printFunc, "Print"),highlight(printFunc, printFunc, printTest)
1521
fmt.Println(foo) //@mark(fooUse, "foo"),highlight(fooUse, fooDeclaration, fooUse)
1622
fmt.Print("yo") //@mark(printSep, "Print"),highlight(printSep, printSep, print1, print2)
1723
}
1824

1925
func (x *F) Inc() { //@mark(xDeclaration, "x"),highlight(xDeclaration, xDeclaration, xUse)
20-
x.bar++ //@mark(xUse, "x"),highlight(xUse, xDeclaration, xUse)
26+
x.bar++ //@mark(xUse, "x"),mark(bar3, "bar"),highlight(xUse, xDeclaration, xUse),highlight(bar3, barDeclaration, bar1, bar2, bar3)
2127
}
2228

2329
func testFunctions() {

internal/lsp/testdata/summary.txt.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ImportCount = 7
1313
SuggestedFixCount = 1
1414
DefinitionsCount = 38
1515
TypeDefinitionsCount = 2
16-
HighlightsCount = 37
16+
HighlightsCount = 41
1717
ReferencesCount = 7
1818
RenamesCount = 22
1919
PrepareRenamesCount = 8

0 commit comments

Comments
 (0)