github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/internal/lsp/testdata/snippets/literal_snippets.go.in (about)

     1  package snippets
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"go/ast"
     7  	"net/http"
     8  	"sort"
     9  
    10  	"github.com/powerman/golang-tools/internal/lsp/foo"
    11  )
    12  
    13  func _() {
    14  	[]int{}        //@item(litIntSlice, "[]int{}", "", "var")
    15  	&[]int{}       //@item(litIntSliceAddr, "&[]int{}", "", "var")
    16  	make([]int, 0) //@item(makeIntSlice, "make([]int, 0)", "", "func")
    17  
    18  	var _ *[]int = in //@snippet(" //", litIntSliceAddr, "&[]int{$0\\}", "&[]int{$0\\}")
    19  	var _ **[]int = in //@complete(" //")
    20  
    21  	var slice []int
    22  	slice = i //@snippet(" //", litIntSlice, "[]int{$0\\}", "[]int{$0\\}")
    23  	slice = m //@snippet(" //", makeIntSlice, "make([]int, ${1:})", "make([]int, ${1:0})")
    24  }
    25  
    26  func _() {
    27  	type namedInt []int
    28  
    29  	namedInt{}        //@item(litNamedSlice, "namedInt{}", "", "var")
    30  	make(namedInt, 0) //@item(makeNamedSlice, "make(namedInt, 0)", "", "func")
    31  
    32  	var namedSlice namedInt
    33  	namedSlice = n //@snippet(" //", litNamedSlice, "namedInt{$0\\}", "namedInt{$0\\}")
    34  	namedSlice = m //@snippet(" //", makeNamedSlice, "make(namedInt, ${1:})", "make(namedInt, ${1:0})")
    35  }
    36  
    37  func _() {
    38  	make(chan int) //@item(makeChan, "make(chan int)", "", "func")
    39  
    40  	var ch chan int
    41  	ch = m //@snippet(" //", makeChan, "make(chan int)", "make(chan int)")
    42  }
    43  
    44  func _() {
    45  	map[string]struct{}{}     //@item(litMap, "map[string]struct{}{}", "", "var")
    46  	make(map[string]struct{}) //@item(makeMap, "make(map[string]struct{})", "", "func")
    47  
    48  	var m map[string]struct{}
    49  	m = m //@snippet(" //", litMap, "map[string]struct{\\}{$0\\}", "map[string]struct{\\}{$0\\}")
    50  	m = m //@snippet(" //", makeMap, "make(map[string]struct{\\})", "make(map[string]struct{\\})")
    51  
    52  	struct{}{} //@item(litEmptyStruct, "struct{}{}", "", "var")
    53  
    54  	m["hi"] = s //@snippet(" //", litEmptyStruct, "struct{\\}{\\}", "struct{\\}{\\}")
    55  }
    56  
    57  func _() {
    58  	type myStruct struct{ i int } //@item(myStructType, "myStruct", "struct{...}", "struct")
    59  
    60  	myStruct{}  //@item(litStruct, "myStruct{}", "", "var")
    61  	&myStruct{} //@item(litStructPtr, "&myStruct{}", "", "var")
    62  
    63  	var ms myStruct
    64  	ms = m //@snippet(" //", litStruct, "myStruct{$0\\}", "myStruct{$0\\}")
    65  
    66  	var msPtr *myStruct
    67  	msPtr = m //@snippet(" //", litStructPtr, "&myStruct{$0\\}", "&myStruct{$0\\}")
    68  
    69  	msPtr = &m //@snippet(" //", litStruct, "myStruct{$0\\}", "myStruct{$0\\}")
    70  
    71  	type myStructCopy struct { i int } //@item(myStructCopyType, "myStructCopy", "struct{...}", "struct")
    72  
    73  	// Don't offer literal completion for convertible structs.
    74  	ms = myStruct //@complete(" //", litStruct, myStructType, myStructCopyType)
    75  }
    76  
    77  type myImpl struct{}
    78  
    79  func (myImpl) foo() {}
    80  
    81  func (*myImpl) bar() {}
    82  
    83  type myBasicImpl string
    84  
    85  func (myBasicImpl) foo() {}
    86  
    87  func _() {
    88  	type myIntf interface {
    89  		foo()
    90  	}
    91  
    92  	myImpl{} //@item(litImpl, "myImpl{}", "", "var")
    93  
    94  	var mi myIntf
    95  	mi = m //@snippet(" //", litImpl, "myImpl{\\}", "myImpl{\\}")
    96  
    97  	myBasicImpl() //@item(litBasicImpl, "myBasicImpl()", "string", "var")
    98  
    99  	mi = m //@snippet(" //", litBasicImpl, "myBasicImpl($0)", "myBasicImpl($0)")
   100  
   101  	// only satisfied by pointer to myImpl
   102  	type myPtrIntf interface {
   103  		bar()
   104  	}
   105  
   106  	&myImpl{} //@item(litImplPtr, "&myImpl{}", "", "var")
   107  
   108  	var mpi myPtrIntf
   109  	mpi = m //@snippet(" //", litImplPtr, "&myImpl{\\}", "&myImpl{\\}")
   110  }
   111  
   112  func _() {
   113  	var s struct{ i []int } //@item(litSliceField, "i", "[]int", "field")
   114  	var foo []int
   115  	// no literal completions after selector
   116  	foo = s.i //@complete(" //", litSliceField)
   117  }
   118  
   119  func _() {
   120  	type myStruct struct{ i int } //@item(litMyStructType, "myStruct", "struct{...}", "struct")
   121  	myStruct{} //@item(litMyStruct, "myStruct{}", "", "var")
   122  
   123  	foo := func(s string, args ...myStruct) {}
   124  	// Don't give literal slice candidate for variadic arg.
   125  	// Do give literal candidates for variadic element.
   126  	foo("", myStruct) //@complete(")", litMyStruct, litMyStructType)
   127  }
   128  
   129  func _() {
   130  	Buffer{} //@item(litBuffer, "Buffer{}", "", "var")
   131  
   132  	var b *bytes.Buffer
   133  	b = bytes.Bu //@snippet(" //", litBuffer, "Buffer{\\}", "Buffer{\\}")
   134  }
   135  
   136  func _() {
   137  	_ = "func(...) {}" //@item(litFunc, "func(...) {}", "", "var")
   138  
   139  	sort.Slice(nil, fun) //@complete(")", litFunc),snippet(")", litFunc, "func(i, j int) bool {$0\\}", "func(i, j int) bool {$0\\}")
   140  
   141  	http.HandleFunc("", f) //@snippet(")", litFunc, "func(w http.ResponseWriter, r *http.Request) {$0\\}", "func(${1:w} http.ResponseWriter, ${2:r} *http.Request) {$0\\}")
   142  
   143  	// no literal "func" completions
   144  	http.Handle("", fun) //@complete(")")
   145  
   146  	http.HandlerFunc() //@item(handlerFunc, "http.HandlerFunc()", "", "var")
   147  	http.Handle("", h) //@snippet(")", handlerFunc, "http.HandlerFunc($0)", "http.HandlerFunc($0)")
   148  	http.Handle("", http.HandlerFunc()) //@snippet("))", litFunc, "func(w http.ResponseWriter, r *http.Request) {$0\\}", "func(${1:w} http.ResponseWriter, ${2:r} *http.Request) {$0\\}")
   149  
   150  	var namedReturn func(s string) (b bool)
   151  	namedReturn = f //@snippet(" //", litFunc, "func(s string) (b bool) {$0\\}", "func(s string) (b bool) {$0\\}")
   152  
   153  	var multiReturn func() (bool, int)
   154  	multiReturn = f //@snippet(" //", litFunc, "func() (bool, int) {$0\\}", "func() (bool, int) {$0\\}")
   155  
   156  	var multiNamedReturn func() (b bool, i int)
   157  	multiNamedReturn = f //@snippet(" //", litFunc, "func() (b bool, i int) {$0\\}", "func() (b bool, i int) {$0\\}")
   158  
   159  	var duplicateParams func(myImpl, int, myImpl)
   160  	duplicateParams = f //@snippet(" //", litFunc, "func(mi1 myImpl, i int, mi2 myImpl) {$0\\}", "func(${1:mi1} myImpl, ${2:i} int, ${3:mi2} myImpl) {$0\\}")
   161  
   162  	type aliasImpl = myImpl
   163  	var aliasParams func(aliasImpl) aliasImpl
   164  	aliasParams = f //@snippet(" //", litFunc, "func(ai aliasImpl) aliasImpl {$0\\}", "func(${1:ai} aliasImpl) aliasImpl {$0\\}")
   165  
   166  	const two = 2
   167  	var builtinTypes func([]int, [two]bool, map[string]string, struct{ i int }, interface{ foo() }, <-chan int)
   168  	builtinTypes = f //@snippet(" //", litFunc, "func(i1 []int, b [two]bool, m map[string]string, s struct{ i int \\}, i2 interface{ foo() \\}, c <-chan int) {$0\\}", "func(${1:i1} []int, ${2:b} [two]bool, ${3:m} map[string]string, ${4:s} struct{ i int \\}, ${5:i2} interface{ foo() \\}, ${6:c} <-chan int) {$0\\}")
   169  
   170  	var _ func(ast.Node) = f //@snippet(" //", litFunc, "func(n ast.Node) {$0\\}", "func(${1:n} ast.Node) {$0\\}")
   171  	var _ func(error) = f //@snippet(" //", litFunc, "func(err error) {$0\\}", "func(${1:err} error) {$0\\}")
   172  	var _ func(context.Context) = f //@snippet(" //", litFunc, "func(ctx context.Context) {$0\\}", "func(${1:ctx} context.Context) {$0\\}")
   173  
   174  	type context struct {}
   175  	var _ func(context) = f //@snippet(" //", litFunc, "func(ctx context) {$0\\}", "func(${1:ctx} context) {$0\\}")
   176  }
   177  
   178  func _() {
   179  	StructFoo{} //@item(litStructFoo, "StructFoo{}", "struct{...}", "struct")
   180  
   181  	var sfp *foo.StructFoo
   182  	// Don't insert the "&" before "StructFoo{}".
   183  	sfp = foo.Str //@snippet(" //", litStructFoo, "StructFoo{$0\\}", "StructFoo{$0\\}")
   184  
   185  	var sf foo.StructFoo
   186  	sf = foo.Str //@snippet(" //", litStructFoo, "StructFoo{$0\\}", "StructFoo{$0\\}")
   187  	sf = foo. //@snippet(" //", litStructFoo, "StructFoo{$0\\}", "StructFoo{$0\\}")
   188  }
   189  
   190  func _() {
   191  	float64() //@item(litFloat64, "float64()", "float64", "var")
   192  
   193  	// don't complete to "&float64()"
   194  	var _ *float64 = float64 //@complete(" //")
   195  
   196  	var f float64
   197  	f = fl //@complete(" //", litFloat64),snippet(" //", litFloat64, "float64($0)", "float64($0)")
   198  
   199  	type myInt int
   200  	myInt() //@item(litMyInt, "myInt()", "", "var")
   201  
   202  	var mi myInt
   203  	mi = my //@snippet(" //", litMyInt, "myInt($0)", "myInt($0)")
   204  }
   205  
   206  func _() {
   207  	type ptrStruct struct {
   208  		p *ptrStruct
   209  	}
   210  
   211  	ptrStruct{} //@item(litPtrStruct, "ptrStruct{}", "", "var")
   212  
   213  	ptrStruct{
   214  		p: &ptrSt, //@rank(",", litPtrStruct)
   215  	}
   216  
   217  	&ptrStruct{} //@item(litPtrStructPtr, "&ptrStruct{}", "", "var")
   218  
   219  	&ptrStruct{
   220  		p: ptrSt, //@rank(",", litPtrStructPtr)
   221  	}
   222  }
   223  
   224  func _() {
   225  	f := func(...[]int) {}
   226  	f() //@snippet(")", litIntSlice, "[]int{$0\\}", "[]int{$0\\}")
   227  }
   228  
   229  
   230  func _() {
   231  	// don't complete to "untyped int()"
   232  	[]int{}[untyped] //@complete("] //")
   233  }