From eb0714863624ce772f7391ed34b4e734b29c5041 Mon Sep 17 00:00:00 2001 From: A_D Date: Mon, 10 Jan 2022 02:10:33 +0000 Subject: [PATCH] gopls: add string join/split postfix completions This adds two new snippets, centred around string manipulation. The first snippet applies to variables of type string, and is called split. When used, it replaces `someStringVar.split!` with `strings.Split(someStringVar, "|")`, where `|` indicates the location of the cursor. The second snippet is essentially the same as the first, but in "reverse". Meaning that rather than going from a string to a slice of strings, it goes from a slice of strings to a single string: `someStringSlice.join!` -> `strings.Join(someStringSlice, "|")`. Change-Id: I0e303a39766463034687f76a5d9dbab419e2021b GitHub-Last-Rev: 935cc418e75d54f0e5b1194cdb576c8829252a26 GitHub-Pull-Request: golang/tools#347 Reviewed-on: https://go-review.googlesource.com/c/tools/+/362474 Reviewed-by: Robert Findley Trust: Robert Findley Run-TryBot: Robert Findley gopls-CI: kokoro TryBot-Result: Gopher Robot Trust: Hyang-Ah Hana Kim --- .../completion/postfix_snippet_test.go | 38 +++++++++++++++++++ .../lsp/source/completion/postfix_snippets.go | 12 ++++++ 2 files changed, 50 insertions(+) diff --git a/gopls/internal/regtest/completion/postfix_snippet_test.go b/gopls/internal/regtest/completion/postfix_snippet_test.go index 1f5b7cfc7a..c10926a667 100644 --- a/gopls/internal/regtest/completion/postfix_snippet_test.go +++ b/gopls/internal/regtest/completion/postfix_snippet_test.go @@ -372,6 +372,44 @@ func _() { } `, }, + { + name: "string split", + before: ` +package foo + +func foo() []string { + x := "test" + return x.split +}`, + after: ` +package foo + +import "strings" + +func foo() []string { + x := "test" + return strings.Split(x, "$0") +}`, + }, + { + name: "string slice join", + before: ` +package foo + +func foo() string { + x := []string{"a", "test"} + return x.join +}`, + after: ` +package foo + +import "strings" + +func foo() string { + x := []string{"a", "test"} + return strings.Join(x, "$0") +}`, + }, } r := WithOptions(Options(func(o *source.Options) { diff --git a/internal/lsp/source/completion/postfix_snippets.go b/internal/lsp/source/completion/postfix_snippets.go index 4c5cb0ef1f..7ea962118b 100644 --- a/internal/lsp/source/completion/postfix_snippets.go +++ b/internal/lsp/source/completion/postfix_snippets.go @@ -174,6 +174,18 @@ for {{.VarName .KeyType "k"}}, {{.VarName .ElemType "v"}} := range {{.X}} { body: `{{if and (eq .Kind "tuple") .StmtOK -}} {{.Import "fmt"}}.Println({{.X}}) {{- end}}`, +}, { + label: "split", + details: "split string", + body: `{{if (eq (.TypeName .Type) "string") -}} +{{.Import "strings"}}.Split({{.X}}, "{{.Cursor}}") +{{- end}}`, +}, { + label: "join", + details: "join string slice", + body: `{{if and (eq .Kind "slice") (eq (.TypeName .ElemType) "string") -}} +{{.Import "strings"}}.Join({{.X}}, "{{.Cursor}}") +{{- end}}`, }} // Cursor indicates where the client's cursor should end up after the