Skip to content

Commit 5f4408a

Browse files
author
ALX99
committed
gopls: support slices for the clear postfix
1 parent 5a6f0d0 commit 5f4408a

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

gopls/internal/golang/completion/postfix_snippets.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ for {{.VarName .KeyType "k" | .Placeholder}}, {{.VarName .ElemType "v" | .Placeh
173173
{{- end}}`,
174174
}, {
175175
label: "clear",
176-
details: "clear map contents",
177-
body: `{{if and (eq .Kind "map") .StmtOK -}}
176+
details: "clear contents",
177+
body: `{{if and (eq .Kind "map" "slice") .StmtOK -}}
178178
clear({{.X}})
179179
{{- end}}`,
180180
}, {

gopls/internal/test/integration/completion/postfix_snippet_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,25 @@ func _() {
236236
var foo map[string]int
237237
clear(foo)
238238
}
239+
`,
240+
},
241+
{
242+
name: "slice_clear",
243+
before: `
244+
package foo
245+
246+
func _() {
247+
var foo []int
248+
foo.clear
249+
}
250+
`,
251+
after: `
252+
package foo
253+
254+
func _() {
255+
var foo []int
256+
clear(foo)
257+
}
239258
`,
240259
},
241260
{

gopls/internal/test/marker/testdata/completion/postfix.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func _() {
3939

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

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

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

7981
var foo map[int]int

0 commit comments

Comments
 (0)