From 115af5e89bf7a8d9b991baffd7b86ec92ee403ab Mon Sep 17 00:00:00 2001 From: Rohan Challa Date: Thu, 5 Dec 2019 16:03:59 -0500 Subject: [PATCH] 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 Run-TryBot: Rohan Challa TryBot-Result: Gobot Gobot --- internal/lsp/source/highlight.go | 5 ++++- internal/lsp/testdata/highlights/highlights.go | 12 +++++++++--- internal/lsp/testdata/summary.txt.golden | 2 +- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/internal/lsp/source/highlight.go b/internal/lsp/source/highlight.go index d8a72bdac9..b17a1696af 100644 --- a/internal/lsp/source/highlight.go +++ b/internal/lsp/source/highlight.go @@ -62,6 +62,9 @@ Outer: // Reverse walk the path till we get to the func block. for _, 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.Field: inReturnList = true case *ast.FuncLit: @@ -232,7 +235,7 @@ func highlightIdentifiers(ctx context.Context, snapshot Snapshot, m *protocol.Co if !ok { return true } - if n.Name != id.Name || n.Obj != id.Obj { + if n.Name != id.Name { return false } if nObj := pkg.GetTypesInfo().ObjectOf(n); nObj != idObj { diff --git a/internal/lsp/testdata/highlights/highlights.go b/internal/lsp/testdata/highlights/highlights.go index ff435307d2..39acf23bdb 100644 --- a/internal/lsp/testdata/highlights/highlights.go +++ b/internal/lsp/testdata/highlights/highlights.go @@ -7,9 +7,15 @@ import ( "golang.org/x/tools/internal/lsp/protocol" ) -type F struct{ bar int } +type F struct{ bar int } //@mark(barDeclaration, "bar"),highlight(barDeclaration, barDeclaration, bar1, bar2, bar3) -var foo = F{bar: 52} //@mark(fooDeclaration, "foo"),highlight(fooDeclaration, fooDeclaration, fooUse) +func _() F { + return F{ + bar: 123, //@mark(bar1, "bar"),highlight(bar1, barDeclaration, bar1, bar2, bar3) + } +} + +var foo = F{bar: 52} //@mark(fooDeclaration, "foo"),mark(bar2, "bar"),highlight(fooDeclaration, fooDeclaration, fooUse),highlight(bar2, barDeclaration, bar1, bar2, bar3) func Print() { //@mark(printFunc, "Print"),highlight(printFunc, printFunc, printTest) fmt.Println(foo) //@mark(fooUse, "foo"),highlight(fooUse, fooDeclaration, fooUse) @@ -17,7 +23,7 @@ func Print() { //@mark(printFunc, "Print"),highlight(printFunc, printFunc, print } func (x *F) Inc() { //@mark(xDeclaration, "x"),highlight(xDeclaration, xDeclaration, xUse) - x.bar++ //@mark(xUse, "x"),highlight(xUse, xDeclaration, xUse) + x.bar++ //@mark(xUse, "x"),mark(bar3, "bar"),highlight(xUse, xDeclaration, xUse),highlight(bar3, barDeclaration, bar1, bar2, bar3) } func testFunctions() { diff --git a/internal/lsp/testdata/summary.txt.golden b/internal/lsp/testdata/summary.txt.golden index 19f615c12a..1ab98b8347 100644 --- a/internal/lsp/testdata/summary.txt.golden +++ b/internal/lsp/testdata/summary.txt.golden @@ -13,7 +13,7 @@ ImportCount = 7 SuggestedFixCount = 1 DefinitionsCount = 38 TypeDefinitionsCount = 2 -HighlightsCount = 37 +HighlightsCount = 41 ReferencesCount = 7 RenamesCount = 22 PrepareRenamesCount = 8