From 93bf1fcc7c91fd5ef6c2a85234ab9f6543936707 Mon Sep 17 00:00:00 2001 From: Davide Masserut Date: Sat, 25 Jun 2022 20:55:23 +0000 Subject: [PATCH] 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 Reviewed-by: Robert Findley Run-TryBot: Robert Findley TryBot-Result: Gopher Robot gopls-CI: kokoro --- .../completion/postfix_snippet_test.go | 21 +++++++++++++++++++ .../lsp/source/completion/postfix_snippets.go | 8 +++++++ 2 files changed, 29 insertions(+) diff --git a/gopls/internal/regtest/completion/postfix_snippet_test.go b/gopls/internal/regtest/completion/postfix_snippet_test.go index 2674d555c5..7e595aaad1 100644 --- a/gopls/internal/regtest/completion/postfix_snippet_test.go +++ b/gopls/internal/regtest/completion/postfix_snippet_test.go @@ -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 +} } `, }, diff --git a/internal/lsp/source/completion/postfix_snippets.go b/internal/lsp/source/completion/postfix_snippets.go index d7f0d90da9..aa8454f8e9 100644 --- a/internal/lsp/source/completion/postfix_snippets.go +++ b/internal/lsp/source/completion/postfix_snippets.go @@ -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",