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

leitzler opened this issue May 27, 2020 · 3 comments

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

leitzler opened this issue May 27, 2020 · 3 comments


Copy link

@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 v0.0.0-20200509030707-2212a7e161a5 v0.0.0-20200509030707-2212a7e161a5

Does this issue reproduce with the latest release?


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

go env Output
$ go env

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.

Copy link
Contributor Author

@leitzler leitzler commented May 29, 2020

I intend to submit a fix for this.

Copy link

@gopherbot gopherbot commented Jun 6, 2020

Change 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:

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-by: Rebecca Stambler <>
Run-TryBot: Rebecca Stambler <>
TryBot-Result: Gobot Gobot <>
Copy link

@gopherbot gopherbot commented Jun 17, 2020

Change mentions this issue: internal/lsp/source: support highlight of switch statements

@stamblerre stamblerre modified the milestones: gopls/v0.5.0, gopls/v0.4.2 Jun 19, 2020
@golang golang locked and limited conversation to collaborators Jun 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants