From 8f1b74eef37946d89b31a304744c0f26054b7610 Mon Sep 17 00:00:00 2001 From: Muir Manders Date: Tue, 22 Oct 2019 14:13:24 -0700 Subject: [PATCH] internal/lsp: fix label completion in range and type switch stmts Fix label detection to know about *ast.RangeStmt and *ast.TypeSwitchStmt. Change-Id: I4061e165884f7064fe486249fe3664d572b7b628 Reviewed-on: https://go-review.googlesource.com/c/tools/+/202621 Reviewed-by: Rebecca Stambler Run-TryBot: Rebecca Stambler TryBot-Result: Gobot Gobot --- internal/lsp/source/completion_labels.go | 4 ++-- internal/lsp/testdata/labels/labels.go | 18 ++++++++++++++---- internal/lsp/testdata/summary.txt.golden | 2 +- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/internal/lsp/source/completion_labels.go b/internal/lsp/source/completion_labels.go index 887b333aa1..e1314bb824 100644 --- a/internal/lsp/source/completion_labels.go +++ b/internal/lsp/source/completion_labels.go @@ -71,10 +71,10 @@ func (c *completer) labels(lt labelType) { return case *ast.LabeledStmt: switch p.Stmt.(type) { - case *ast.ForStmt: + case *ast.ForStmt, *ast.RangeStmt: // Loop labels can be used for "break" or "continue". addLabel(p) - case *ast.SwitchStmt, *ast.SelectStmt: + case *ast.SwitchStmt, *ast.SelectStmt, *ast.TypeSwitchStmt: // Switch and select labels can be used only for "break". if lt == labelBreak { addLabel(p) diff --git a/internal/lsp/testdata/labels/labels.go b/internal/lsp/testdata/labels/labels.go index 2162fecb95..b9effb6d0e 100644 --- a/internal/lsp/testdata/labels/labels.go +++ b/internal/lsp/testdata/labels/labels.go @@ -1,10 +1,10 @@ package labels func _() { - goto F //@complete(" //", label1, label4) + goto F //@complete(" //", label1, label5) Foo1: //@item(label1, "Foo1", "label", "const") - for { + for a, b := range []int{} { Foo2: //@item(label2, "Foo2", "label", "const") switch { case true: @@ -16,7 +16,7 @@ Foo1: //@item(label1, "Foo1", "label", "const") FooUnjumpable: } - goto F //@complete(" //", label1, label2, label4) + goto F //@complete(" //", label1, label2, label4, label5) func() { goto F //@complete(" //", label3) @@ -28,12 +28,22 @@ Foo1: //@item(label1, "Foo1", "label", "const") Foo3: //@item(label3, "Foo3", "label", "const") }() } + + Foo4: //@item(label4, "Foo4", "label", "const") + switch interface{}(a).(type) { + case int: + break F //@complete(" //", label4, label1) + } } break F //@complete(" //") continue F //@complete(" //") -Foo4: //@item(label4, "Foo4", "label", "const") +Foo5: //@item(label5, "Foo5", "label", "const") + for { + break F //@complete(" //", label5) + } + return } diff --git a/internal/lsp/testdata/summary.txt.golden b/internal/lsp/testdata/summary.txt.golden index 9d4910b79d..a1d225e22f 100644 --- a/internal/lsp/testdata/summary.txt.golden +++ b/internal/lsp/testdata/summary.txt.golden @@ -1,5 +1,5 @@ -- summary -- -CompletionsCount = 209 +CompletionsCount = 211 CompletionSnippetCount = 39 UnimportedCompletionsCount = 1 DeepCompletionsCount = 5