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 <rfindley@google.com>
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Trust: Hyang-Ah Hana Kim <hyangah@gmail.com>
This commit is contained in:
A_D 2022-01-10 02:10:33 +00:00 committed by Robert Findley
parent 52e9527420
commit eb07148636
2 changed files with 50 additions and 0 deletions

View File

@ -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) {

View File

@ -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