Skip to content

Commit

Permalink
gopls: support slices for the clear postfix
Browse files Browse the repository at this point in the history
  • Loading branch information
ALX99 committed Jun 15, 2024
1 parent 5a6f0d0 commit 5f4408a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
4 changes: 2 additions & 2 deletions gopls/internal/golang/completion/postfix_snippets.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ for {{.VarName .KeyType "k" | .Placeholder}}, {{.VarName .ElemType "v" | .Placeh
{{- end}}`,
}, {
label: "clear",
details: "clear map contents",
body: `{{if and (eq .Kind "map") .StmtOK -}}
details: "clear contents",
body: `{{if and (eq .Kind "map" "slice") .StmtOK -}}
clear({{.X}})
{{- end}}`,
}, {
Expand Down
19 changes: 19 additions & 0 deletions gopls/internal/test/integration/completion/postfix_snippet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,25 @@ func _() {
var foo map[string]int
clear(foo)
}
`,
},
{
name: "slice_clear",
before: `
package foo
func _() {
var foo []int
foo.clear
}
`,
after: `
package foo
func _() {
var foo []int
clear(foo)
}
`,
},
{
Expand Down
6 changes: 4 additions & 2 deletions gopls/internal/test/marker/testdata/completion/postfix.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func _() {

func _() {
/* append! */ //@item(postfixAppend, "append!", "append and re-assign slice", "snippet")
/* clear! */ //@item(postfixClearSlice, "clear!", "clear contents", "snippet")
/* copy! */ //@item(postfixCopy, "copy!", "duplicate slice", "snippet")
/* for! */ //@item(postfixFor, "for!", "range over slice by index", "snippet")
/* forr! */ //@item(postfixForr, "forr!", "range over slice by index and value", "snippet")
Expand All @@ -52,10 +53,11 @@ func _() {
/* ifnotnil! */ //@item(postfixIfNotNil, "ifnotnil!", "if expr != nil", "snippet")

var foo []int
foo. //@complete(" //", postfixAppend, postfixCopy, postfixFor, postfixForr, postfixIfNotNil, postfixLast, postfixLen, postfixPrint, postfixRange, postfixReverse, postfixSort, postfixVar)
foo. //@complete(" //", postfixAppend, postfixClearSlice, postfixCopy, postfixFor, postfixForr, postfixIfNotNil, postfixLast, postfixLen, postfixPrint, postfixRange, postfixReverse, postfixSort, postfixVar)
foo = nil

foo.append //@snippet(" //", postfixAppend, "foo = append(foo, $0)")
foo.clear //@snippet(" //", postfixClearSlice, "clear(foo)")
foo.copy //snippet(" //", postfixCopy, "fooCopy := make([]int, len(foo))\ncopy($fooCopy, foo)\n")
foo.fo //@snippet(" //", postfixFor, "for ${1:} := range foo {\n\t$0\n}")
foo.forr //@snippet(" //", postfixForr, "for ${1:}, ${2:} := range foo {\n\t$0\n}")
Expand All @@ -73,7 +75,7 @@ func _() {
/* for! */ //@item(postfixForMap, "for!", "range over map by key", "snippet")
/* forr! */ //@item(postfixForrMap, "forr!", "range over map by key and value", "snippet")
/* range! */ //@item(postfixRangeMap, "range!", "range over map", "snippet")
/* clear! */ //@item(postfixClear, "clear!", "clear map contents", "snippet")
/* clear! */ //@item(postfixClear, "clear!", "clear contents", "snippet")
/* keys! */ //@item(postfixKeys, "keys!", "create slice of keys", "snippet")

var foo map[int]int
Expand Down

0 comments on commit 5f4408a

Please sign in to comment.