golang.org/x/tools/gopls@v0.15.3/internal/test/marker/testdata/completion/postfix.txt (about)

     1  These tests check that postfix completions do and do not show up in certain
     2  cases. Tests for the postfix completion contents are implemented as ad-hoc
     3  integration tests.
     4  
     5  -- flags --
     6  -ignore_extra_diags
     7  
     8  -- go.mod --
     9  module golang.org/lsptests/snippets
    10  
    11  go 1.18
    12  
    13  -- postfix.go --
    14  package snippets
    15  
    16  import (
    17  	"strconv"
    18  )
    19  
    20  func _() {
    21  	var foo []int
    22  	foo.append //@rank(" //", postfixAppend)
    23  
    24  	[]int{}.append //@complete(" //")
    25  
    26  	[]int{}.last //@complete(" //")
    27  
    28  
    29  	foo.copy //@rank(" //", postfixCopy)
    30  
    31  	var s struct{ i []int }
    32  	s.i.copy //@rank(" //", postfixCopy)
    33  
    34  	var _ []int = s.i.copy //@complete(" //")
    35  
    36  	var blah func() []int
    37  	blah().append //@complete(" //")
    38  }
    39  
    40  func _() {
    41  	/* append! */ //@item(postfixAppend, "append!", "append and re-assign slice", "snippet")
    42  	/* copy! */ //@item(postfixCopy, "copy!", "duplicate slice", "snippet")
    43  	/* for! */ //@item(postfixFor, "for!", "range over slice by index", "snippet")
    44  	/* forr! */ //@item(postfixForr, "forr!", "range over slice by index and value", "snippet")
    45  	/* last! */ //@item(postfixLast, "last!", "s[len(s)-1]", "snippet")
    46  	/* len! */ //@item(postfixLen, "len!", "len(s)", "snippet")
    47  	/* print! */ //@item(postfixPrint, "print!", "print to stdout", "snippet")
    48  	/* range! */ //@item(postfixRange, "range!", "range over slice", "snippet")
    49  	/* reverse! */ //@item(postfixReverse, "reverse!", "reverse slice", "snippet")
    50  	/* sort! */ //@item(postfixSort, "sort!", "sort.Slice()", "snippet")
    51  	/* var! */ //@item(postfixVar, "var!", "assign to variable", "snippet")
    52  	/* ifnotnil! */ //@item(postfixIfNotNil, "ifnotnil!", "if expr != nil", "snippet")
    53  
    54  	var foo []int
    55  	foo. //@complete(" //", postfixAppend, postfixCopy, postfixFor, postfixForr, postfixIfNotNil, postfixLast, postfixLen, postfixPrint, postfixRange, postfixReverse, postfixSort, postfixVar)
    56  	foo = nil
    57  
    58  	foo.append //@snippet(" //", postfixAppend, "foo = append(foo, $0)")
    59  	foo.copy //snippet(" //", postfixCopy, "fooCopy := make([]int, len(foo))\ncopy($fooCopy, foo)\n")
    60  	foo.fo //@snippet(" //", postfixFor, "for ${1:} := range foo {\n\t$0\n}")
    61  	foo.forr //@snippet(" //", postfixForr, "for ${1:}, ${2:} := range foo {\n\t$0\n}")
    62  	foo.last //@snippet(" //", postfixLast, "foo[len(foo)-1]")
    63  	foo.len //@snippet(" //", postfixLen, "len(foo)")
    64  	foo.print //@snippet(" //", postfixPrint, `fmt.Printf("foo: %v\n", foo)`)
    65  	foo.rang //@snippet(" //", postfixRange, "for ${1:}, ${2:} := range foo {\n\t$0\n}")
    66  	foo.reverse //@snippet(" //", postfixReverse, "for i, j := 0, len(foo)-1; i < j; i, j = i+1, j-1 {\n\tfoo[i], foo[j] = foo[j], foo[i]\n}\n")
    67  	foo.sort //@snippet(" //", postfixSort, "sort.Slice(foo, func(i, j int) bool {\n\t$0\n})")
    68  	foo.va //@snippet(" //", postfixVar, "${1:} := foo")
    69  	foo.ifnotnil //@snippet(" //", postfixIfNotNil, "if foo != nil {\n\t$0\n}")
    70  }
    71  
    72  func _() {
    73  	/* for! */ //@item(postfixForMap, "for!", "range over map by key", "snippet")
    74  	/* forr! */ //@item(postfixForrMap, "forr!", "range over map by key and value", "snippet")
    75  	/* range! */ //@item(postfixRangeMap, "range!", "range over map", "snippet")
    76  	/* clear! */ //@item(postfixClear, "clear!", "clear map contents", "snippet")
    77  	/* keys! */ //@item(postfixKeys, "keys!", "create slice of keys", "snippet")
    78  
    79  	var foo map[int]int
    80  	foo. //@complete(" //", postfixClear, postfixForMap, postfixForrMap, postfixIfNotNil, postfixKeys, postfixLen, postfixPrint, postfixRangeMap, postfixVar)
    81  
    82  	foo = nil
    83  
    84  	foo.fo //@snippet(" //", postfixFor, "for ${1:} := range foo {\n\t$0\n}")
    85  	foo.forr //@snippet(" //", postfixForr, "for ${1:}, ${2:} := range foo {\n\t$0\n}")
    86  	foo.rang //@snippet(" //", postfixRange, "for ${1:}, ${2:} := range foo {\n\t$0\n}")
    87  	foo.clear //@snippet(" //", postfixClear, "for k := range foo {\n\tdelete(foo, k)\n}\n")
    88  	foo.keys //@snippet(" //", postfixKeys, "keys := make([]int, 0, len(foo))\nfor k := range foo {\n\tkeys = append(keys, k)\n}\n")
    89  }
    90  
    91  func _() {
    92  	/* for! */ //@item(postfixForChannel, "for!", "range over channel", "snippet")
    93  	/* range! */ //@item(postfixRangeChannel, "range!", "range over channel", "snippet")
    94  
    95  	var foo chan int
    96  	foo. //@complete(" //", postfixForChannel, postfixIfNotNil, postfixLen, postfixPrint, postfixRangeChannel, postfixVar)
    97  
    98  	foo = nil
    99  
   100  	foo.fo //@snippet(" //", postfixForChannel, "for ${1:} := range foo {\n\t$0\n}")
   101  	foo.rang //@snippet(" //", postfixRangeChannel, "for ${1:} := range foo {\n\t$0\n}")
   102  }
   103  
   104  type T struct {
   105  	Name string
   106  }
   107  
   108  func _() (string, T, map[string]string, error) {
   109  	/* iferr! */ //@item(postfixIfErr, "iferr!", "check error and return", "snippet")
   110  	/* variferr! */ //@item(postfixVarIfErr, "variferr!", "assign variables and check error", "snippet")
   111  	/* var! */ //@item(postfixVars, "var!", "assign to variables", "snippet")
   112  
   113  	strconv.Atoi("32"). //@complete(" //", postfixIfErr, postfixPrint, postfixVars, postfixVarIfErr)
   114  
   115  	var err error
   116  	err.iferr //@snippet(" //", postfixIfErr, "if err != nil {\n\treturn \"\", T{}, nil, ${1:}\n}\n")
   117  
   118  	strconv.Atoi("32").iferr //@snippet(" //", postfixIfErr, "if _, err := strconv.Atoi(\"32\"); err != nil {\n\treturn \"\", T{}, nil, ${1:}\n}\n")
   119  
   120  	strconv.Atoi("32").variferr //@snippet(" //", postfixVarIfErr, "${1:}, ${2:} := strconv.Atoi(\"32\")\nif ${2:} != nil {\n\treturn \"\", T{}, nil, ${3:}\n}\n")
   121  
   122  	// test function return multiple errors
   123  	var foo func() (error, error)
   124  	foo().iferr //@snippet(" //", postfixIfErr, "if _, err := foo(); err != nil {\n\treturn \"\", T{}, nil, ${1:}\n}\n")
   125  	foo().variferr //@snippet(" //", postfixVarIfErr, "${1:}, ${2:} := foo()\nif ${2:} != nil {\n\treturn \"\", T{}, nil, ${3:}\n}\n")
   126  
   127  	// test function just return error
   128  	var bar func() error
   129  	bar().iferr //@snippet(" //", postfixIfErr, "if err := bar(); err != nil {\n\treturn \"\", T{}, nil, ${1:}\n}\n")
   130  	bar().variferr //@snippet(" //", postfixVarIfErr, "${1:} := bar()\nif ${1:} != nil {\n\treturn \"\", T{}, nil, ${2:}\n}\n")
   131  }