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