github.com/jd-ly/tools@v0.5.7/internal/lsp/testdata/snippets/literal_snippets.go.in (about)

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