gopls: add range over channel postfix completion

This adds a snippet that applies to variables of type chan.

When used, it replaces `channel.range!` with the following snippet:
```
for e := range channel {
   |
}
```
Where `|` indicates the location of the cursor.

Change-Id: I8b2f889b22b9f2c292041e5ca5f63c5d0ca98f11
GitHub-Last-Rev: 9cb894be80d0c5243a5e42779c3e96ba79aa66b5
GitHub-Pull-Request: golang/tools#386
Reviewed-on: https://go-review.googlesource.com/c/tools/+/414194
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
This commit is contained in:
Davide Masserut 2022-06-25 20:55:23 +00:00 committed by Robert Findley
parent 79fefdf61d
commit 93bf1fcc7c
2 changed files with 29 additions and 0 deletions

View File

@ -264,6 +264,27 @@ for k := range foo {
keys = append(keys, k)
}
}
`,
},
{
name: "channel_range",
before: `
package foo
func _() {
foo := make(chan int)
foo.range
}
`,
after: `
package foo
func _() {
foo := make(chan int)
for e := range foo {
$0
}
}
`,
},

View File

@ -149,6 +149,14 @@ for {{.VarName .KeyType "k"}}, {{.VarName .ElemType "v"}} := range {{.X}} {
{{$keysVar}} = append({{$keysVar}}, {{$k}})
}
{{end}}`,
}, {
label: "range",
details: "range over channel",
body: `{{if and (eq .Kind "chan") .StmtOK -}}
for {{.VarName .ElemType "e"}} := range {{.X}} {
{{.Cursor}}
}
{{- end}}`,
}, {
label: "var",
details: "assign to variables",