From 5a6f0d04faa1bf0320b7058b455fcf6ca59de566 Mon Sep 17 00:00:00 2001 From: ALX99 Date: Sat, 15 Jun 2024 22:48:10 +0900 Subject: [PATCH 1/2] gopls: use clear function for the clear postfix --- gopls/internal/golang/completion/postfix_snippets.go | 6 ++---- .../test/integration/completion/postfix_snippet_test.go | 5 +---- gopls/internal/test/marker/testdata/completion/postfix.txt | 2 +- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/gopls/internal/golang/completion/postfix_snippets.go b/gopls/internal/golang/completion/postfix_snippets.go index 641fe8746eb..2d91f9721e5 100644 --- a/gopls/internal/golang/completion/postfix_snippets.go +++ b/gopls/internal/golang/completion/postfix_snippets.go @@ -175,10 +175,8 @@ for {{.VarName .KeyType "k" | .Placeholder}}, {{.VarName .ElemType "v" | .Placeh 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}}`, +clear({{.X}}) +{{- end}}`, }, { label: "keys", details: "create slice of keys", diff --git a/gopls/internal/test/integration/completion/postfix_snippet_test.go b/gopls/internal/test/integration/completion/postfix_snippet_test.go index 884be420835..f03af899b9a 100644 --- a/gopls/internal/test/integration/completion/postfix_snippet_test.go +++ b/gopls/internal/test/integration/completion/postfix_snippet_test.go @@ -234,10 +234,7 @@ package foo func _() { var foo map[string]int - for k := range foo { - delete(foo, k) -} - + clear(foo) } `, }, diff --git a/gopls/internal/test/marker/testdata/completion/postfix.txt b/gopls/internal/test/marker/testdata/completion/postfix.txt index 9b54b578f4c..0bce04a2132 100644 --- a/gopls/internal/test/marker/testdata/completion/postfix.txt +++ b/gopls/internal/test/marker/testdata/completion/postfix.txt @@ -84,7 +84,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") } From 5f4408abd065f1cdce76bcab6d886d0ccbb40ecb Mon Sep 17 00:00:00 2001 From: ALX99 Date: Sat, 15 Jun 2024 23:42:41 +0900 Subject: [PATCH 2/2] gopls: support slices for the clear postfix --- .../golang/completion/postfix_snippets.go | 4 ++-- .../completion/postfix_snippet_test.go | 19 +++++++++++++++++++ .../marker/testdata/completion/postfix.txt | 6 ++++-- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/gopls/internal/golang/completion/postfix_snippets.go b/gopls/internal/golang/completion/postfix_snippets.go index 2d91f9721e5..db2877fa85a 100644 --- a/gopls/internal/golang/completion/postfix_snippets.go +++ b/gopls/internal/golang/completion/postfix_snippets.go @@ -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}}`, }, { diff --git a/gopls/internal/test/integration/completion/postfix_snippet_test.go b/gopls/internal/test/integration/completion/postfix_snippet_test.go index f03af899b9a..1753374f15f 100644 --- a/gopls/internal/test/integration/completion/postfix_snippet_test.go +++ b/gopls/internal/test/integration/completion/postfix_snippet_test.go @@ -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) +} `, }, { diff --git a/gopls/internal/test/marker/testdata/completion/postfix.txt b/gopls/internal/test/marker/testdata/completion/postfix.txt index 0bce04a2132..cc9c54a7b68 100644 --- a/gopls/internal/test/marker/testdata/completion/postfix.txt +++ b/gopls/internal/test/marker/testdata/completion/postfix.txt @@ -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") @@ -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}") @@ -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