Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use the clear function for the clear postfix and add slices support #500

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions gopls/internal/golang/completion/postfix_snippets.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,10 @@ for {{.VarName .KeyType "k" | .Placeholder}}, {{.VarName .ElemType "v" | .Placeh
{{- end}}`,
}, {
label: "clear",
details: "clear map contents",
body: `{{if and (eq .Kind "map") .StmtOK -}}
{{$k := (.VarName .KeyType "k")}}for {{$k}} := range {{.X}} {
delete({{.X}}, {{$k}})
}
{{end}}`,
details: "clear contents",
body: `{{if and (eq .Kind "map" "slice") .StmtOK -}}
clear({{.X}})
{{- end}}`,
}, {
label: "keys",
details: "create slice of keys",
Expand Down
20 changes: 18 additions & 2 deletions gopls/internal/test/integration/completion/postfix_snippet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,26 @@ package foo

func _() {
var foo map[string]int
for k := range foo {
delete(foo, k)
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
8 changes: 5 additions & 3 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 All @@ -84,7 +86,7 @@ func _() {
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}")
foo.rang //@snippet(" //", postfixRange, "for ${1:}, ${2:} := range foo {\n\t$0\n}")
foo.clear //@snippet(" //", postfixClear, "for k := range foo {\n\tdelete(foo, k)\n}\n")
foo.clear //@snippet(" //", postfixClear, "clear(foo)")
foo.keys //@snippet(" //", postfixKeys, "keys := make([]int, 0, len(foo))\nfor k := range foo {\n\tkeys = append(keys, k)\n}\n")
}

Expand Down