Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/tools/gopls: DocumentHighlight incorrectly highlights outer scope #39275

Closed
leitzler opened this issue May 27, 2020 · 3 comments
Closed

x/tools/gopls: DocumentHighlight incorrectly highlights outer scope #39275

leitzler opened this issue May 27, 2020 · 3 comments

Comments

@leitzler
Copy link
Contributor

@leitzler leitzler commented May 27, 2020

What version of Go are you using (go version)?

$ go version
go version go1.14.3 darwin/amd64
$ go list -m golang.org/x/tools golang.org/x/tools/gopls
golang.org/x/tools v0.0.0-20200509030707-2212a7e161a5
golang.org/x/tools/gopls v0.0.0-20200509030707-2212a7e161a5

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
n/a

What did you do?

1: package main
2: func main() {
3: 	var foo int
4: 	for {
5: 		switch foo {
6: 		case 1:
7: 			break
8: 		}
9: 	}
10: }

When calling textDocument/documentHighlight with the cursor on break in the code above, it incorrectly returns the outer for-loop:

[Trace - 16:44:47.947 PM] Sending request 'textDocument/documentHighlight - (6)'.
Params: {"textDocument":{"uri":"file:///private/var/folders/xm/ls208xd174v95pgd20rn_qbh0000gn/T/tmp.QM3GDnFr/main.go"},"position":{"line":7,"character":3}}


[Trace - 16:44:47.948 PM] Received response 'textDocument/documentHighlight - (6)' in 1ms.
Result: [{"range":{"start":{"line":7,"character":3},"end":{"line":7,"character":8}},"kind":1},{"range":{"start":{"line":4,"character":1},"end":{"line":4,"character":4}},"kind":1}]

What did you expect to see?

I expect the switch statement to be highlighted instead of for.

@gopherbot gopherbot added this to the Unreleased milestone May 27, 2020
@stamblerre stamblerre modified the milestones: Unreleased, gopls/v0.5.0 May 27, 2020
@leitzler
Copy link
Contributor Author

@leitzler leitzler commented May 29, 2020

I intend to submit a fix for this.

@gopherbot
Copy link

@gopherbot gopherbot commented Jun 6, 2020

Change https://golang.org/cl/236524 mentions this issue: x/tools/internal/lsp/source: support labeled statements when highlighting loops

gopherbot pushed a commit to golang/tools that referenced this issue Jun 10, 2020
When document.Highlight is called with the cursor on a loop statement or
branch statement, gopls doesn't look for labels. Placing the cursor at
the break statement below highlights the inner for loop:

Outer:
for {
    for {
        break Outer
    }
}

By making highlight label aware, and ensure that unlabeled "break" in
"switch"/"select" doesn't highlight the outer loop, this change fixes
loop highlighting.

Adding support for highlight of "switch" and "select" will be handled in
a separate CL.

Updates golang/go#39275

Change-Id: I7014aa7b0dfb1da871863ced611623be995f3944
Reviewed-on: https://go-review.googlesource.com/c/tools/+/236524
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
@gopherbot
Copy link

@gopherbot gopherbot commented Jun 17, 2020

Change https://golang.org/cl/238478 mentions this issue: internal/lsp/source: support highlight of switch statements

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.