internal/lsp: add literal completion candidates
Add support for literal completion candidates such as "[]int{}" or
"make([]int, 0)". We support both named and unnamed types. I used the
existing type matching logic, so, for example, if the expected type is
an interface, we will suggest literal candidates that implement the
interface.
The literal candidates have a lower score than normal matching
candidates, so they shouldn't be disruptive in cases where you don't
want a literal candidate.
This commit adds support for slice, array, struct, map, and channel
literal candidates since they are pretty similar. Functions will be
supported in a subsequent commit.
I also added support for setting a snippet's final tab stop. This is
useful if you want the cursor to end up somewhere other than the
character after the snippet.
Change-Id: Id3b74260fff4d61703989b422267021b00cec005
Reviewed-on: https://go-review.googlesource.com/c/tools/+/193698
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-09-04 17:12:37 -06:00
|
|
|
package snippets
|
|
|
|
|
2019-09-05 13:04:13 -06:00
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"sort"
|
2019-10-15 10:26:11 -06:00
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp/foo"
|
2019-09-05 13:04:13 -06:00
|
|
|
)
|
|
|
|
|
internal/lsp: add literal completion candidates
Add support for literal completion candidates such as "[]int{}" or
"make([]int, 0)". We support both named and unnamed types. I used the
existing type matching logic, so, for example, if the expected type is
an interface, we will suggest literal candidates that implement the
interface.
The literal candidates have a lower score than normal matching
candidates, so they shouldn't be disruptive in cases where you don't
want a literal candidate.
This commit adds support for slice, array, struct, map, and channel
literal candidates since they are pretty similar. Functions will be
supported in a subsequent commit.
I also added support for setting a snippet's final tab stop. This is
useful if you want the cursor to end up somewhere other than the
character after the snippet.
Change-Id: Id3b74260fff4d61703989b422267021b00cec005
Reviewed-on: https://go-review.googlesource.com/c/tools/+/193698
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-09-04 17:12:37 -06:00
|
|
|
func _() {
|
|
|
|
[]int{} //@item(litIntSlice, "[]int{}", "", "var")
|
|
|
|
make([]int, 0) //@item(makeIntSlice, "make([]int, 0)", "", "func")
|
|
|
|
|
|
|
|
var slice []int
|
|
|
|
slice = i //@snippet(" //", litIntSlice, "[]int{$0\\}", "[]int{$0\\}")
|
|
|
|
slice = m //@snippet(" //", makeIntSlice, "make([]int, ${1:})", "make([]int, ${1:0})")
|
|
|
|
}
|
|
|
|
|
|
|
|
func _() {
|
|
|
|
type namedInt []int
|
|
|
|
|
|
|
|
namedInt{} //@item(litNamedSlice, "namedInt{}", "", "var")
|
|
|
|
make(namedInt, 0) //@item(makeNamedSlice, "make(namedInt, 0)", "", "func")
|
|
|
|
|
|
|
|
var namedSlice namedInt
|
|
|
|
namedSlice = n //@snippet(" //", litNamedSlice, "namedInt{$0\\}", "namedInt{$0\\}")
|
|
|
|
namedSlice = m //@snippet(" //", makeNamedSlice, "make(namedInt, ${1:})", "make(namedInt, ${1:0})")
|
|
|
|
}
|
|
|
|
|
|
|
|
func _() {
|
|
|
|
make(chan int) //@item(makeChan, "make(chan int)", "", "func")
|
|
|
|
|
|
|
|
var ch chan int
|
|
|
|
ch = m //@snippet(" //", makeChan, "make(chan int)", "make(chan int)")
|
|
|
|
}
|
|
|
|
|
|
|
|
func _() {
|
|
|
|
map[string]struct{}{} //@item(litMap, "map[string]struct{}{}", "", "var")
|
|
|
|
make(map[string]struct{}) //@item(makeMap, "make(map[string]struct{})", "", "func")
|
|
|
|
|
|
|
|
var m map[string]struct{}
|
|
|
|
m = m //@snippet(" //", litMap, "map[string]struct{\\}{$0\\}", "map[string]struct{\\}{$0\\}")
|
|
|
|
m = m //@snippet(" //", makeMap, "make(map[string]struct{\\})", "make(map[string]struct{\\})")
|
|
|
|
|
|
|
|
struct{}{} //@item(litEmptyStruct, "struct{}{}", "", "var")
|
|
|
|
|
|
|
|
m["hi"] = s //@snippet(" //", litEmptyStruct, "struct{\\}{\\}", "struct{\\}{\\}")
|
|
|
|
}
|
|
|
|
|
|
|
|
func _() {
|
|
|
|
type myStruct struct{ i int }
|
|
|
|
|
|
|
|
myStruct{} //@item(litStruct, "myStruct{}", "", "var")
|
|
|
|
&myStruct{} //@item(litStructPtr, "&myStruct{}", "", "var")
|
|
|
|
|
|
|
|
var ms myStruct
|
|
|
|
ms = m //@snippet(" //", litStruct, "myStruct{$0\\}", "myStruct{$0\\}")
|
|
|
|
|
|
|
|
var msPtr *myStruct
|
|
|
|
msPtr = m //@snippet(" //", litStructPtr, "&myStruct{$0\\}", "&myStruct{$0\\}")
|
|
|
|
|
|
|
|
msPtr = &m //@snippet(" //", litStruct, "myStruct{$0\\}", "myStruct{$0\\}")
|
|
|
|
}
|
|
|
|
|
|
|
|
type myImpl struct{}
|
|
|
|
|
|
|
|
func (myImpl) foo() {}
|
|
|
|
|
|
|
|
func (*myImpl) bar() {}
|
|
|
|
|
2019-09-16 20:07:16 -06:00
|
|
|
type myBasicImpl string
|
|
|
|
|
|
|
|
func (myBasicImpl) foo() {}
|
|
|
|
|
internal/lsp: add literal completion candidates
Add support for literal completion candidates such as "[]int{}" or
"make([]int, 0)". We support both named and unnamed types. I used the
existing type matching logic, so, for example, if the expected type is
an interface, we will suggest literal candidates that implement the
interface.
The literal candidates have a lower score than normal matching
candidates, so they shouldn't be disruptive in cases where you don't
want a literal candidate.
This commit adds support for slice, array, struct, map, and channel
literal candidates since they are pretty similar. Functions will be
supported in a subsequent commit.
I also added support for setting a snippet's final tab stop. This is
useful if you want the cursor to end up somewhere other than the
character after the snippet.
Change-Id: Id3b74260fff4d61703989b422267021b00cec005
Reviewed-on: https://go-review.googlesource.com/c/tools/+/193698
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-09-04 17:12:37 -06:00
|
|
|
func _() {
|
|
|
|
type myIntf interface {
|
|
|
|
foo()
|
|
|
|
}
|
|
|
|
|
|
|
|
myImpl{} //@item(litImpl, "myImpl{}", "", "var")
|
|
|
|
|
|
|
|
var mi myIntf
|
|
|
|
mi = m //@snippet(" //", litImpl, "myImpl{\\}", "myImpl{\\}")
|
|
|
|
|
2019-09-16 20:07:16 -06:00
|
|
|
myBasicImpl() //@item(litBasicImpl, "myBasicImpl()", "string", "var")
|
|
|
|
|
|
|
|
mi = m //@snippet(" //", litBasicImpl, "myBasicImpl($0)", "myBasicImpl($0)")
|
|
|
|
|
internal/lsp: add literal completion candidates
Add support for literal completion candidates such as "[]int{}" or
"make([]int, 0)". We support both named and unnamed types. I used the
existing type matching logic, so, for example, if the expected type is
an interface, we will suggest literal candidates that implement the
interface.
The literal candidates have a lower score than normal matching
candidates, so they shouldn't be disruptive in cases where you don't
want a literal candidate.
This commit adds support for slice, array, struct, map, and channel
literal candidates since they are pretty similar. Functions will be
supported in a subsequent commit.
I also added support for setting a snippet's final tab stop. This is
useful if you want the cursor to end up somewhere other than the
character after the snippet.
Change-Id: Id3b74260fff4d61703989b422267021b00cec005
Reviewed-on: https://go-review.googlesource.com/c/tools/+/193698
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-09-04 17:12:37 -06:00
|
|
|
// only satisfied by pointer to myImpl
|
|
|
|
type myPtrIntf interface {
|
|
|
|
bar()
|
|
|
|
}
|
|
|
|
|
|
|
|
&myImpl{} //@item(litImplPtr, "&myImpl{}", "", "var")
|
|
|
|
|
|
|
|
var mpi myPtrIntf
|
|
|
|
mpi = m //@snippet(" //", litImplPtr, "&myImpl{\\}", "&myImpl{\\}")
|
|
|
|
}
|
|
|
|
|
|
|
|
func _() {
|
|
|
|
var s struct{ i []int } //@item(litSliceField, "i", "[]int", "field")
|
|
|
|
var foo []int
|
|
|
|
// no literal completions after selector
|
|
|
|
foo = s.i //@complete(" //", litSliceField)
|
|
|
|
}
|
|
|
|
|
|
|
|
func _() {
|
|
|
|
type myStruct struct{ i int } //@item(litStructType, "myStruct", "struct{...}", "struct")
|
|
|
|
|
|
|
|
foo := func(s string, args ...myStruct) {}
|
|
|
|
// Don't give literal slice candidate for variadic arg.
|
|
|
|
foo("", myStruct) //@complete(")", litStructType)
|
|
|
|
}
|
2019-09-05 13:04:13 -06:00
|
|
|
|
|
|
|
func _() {
|
2019-09-17 09:10:48 -06:00
|
|
|
_ = "func(...) {}" //@item(litFunc, "func(...) {}", "", "var")
|
2019-09-05 13:04:13 -06:00
|
|
|
|
|
|
|
sort.Slice(nil, f) //@snippet(")", litFunc, "func(i, j int) bool {$0\\}", "func(i, j int) bool {$0\\}")
|
|
|
|
|
|
|
|
http.HandleFunc("", f) //@snippet(")", litFunc, "", "func(${1:rw} http.ResponseWriter, ${2:r} *http.Request) {$0\\}")
|
|
|
|
|
2019-11-03 18:47:21 -07:00
|
|
|
// no literal "func" completions
|
|
|
|
http.Handle("", fun) //@complete(")")
|
|
|
|
|
|
|
|
http.HandlerFunc() //@item(handlerFunc, "http.HandlerFunc()", "", "var")
|
|
|
|
http.Handle("", h) //@snippet(")", handlerFunc, "http.HandlerFunc($0)", "http.HandlerFunc($0)")
|
|
|
|
http.Handle("", http.HandlerFunc()) //@snippet("))", litFunc, "", "func(${1:rw} http.ResponseWriter, ${2:r} *http.Request) {$0\\}")
|
|
|
|
|
2019-09-05 13:04:13 -06:00
|
|
|
var namedReturn func(s string) (b bool)
|
|
|
|
namedReturn = f //@snippet(" //", litFunc, "func(s string) (b bool) {$0\\}", "func(s string) (b bool) {$0\\}")
|
|
|
|
|
|
|
|
var multiReturn func() (bool, int)
|
|
|
|
multiReturn = f //@snippet(" //", litFunc, "func() (bool, int) {$0\\}", "func() (bool, int) {$0\\}")
|
|
|
|
|
|
|
|
var multiNamedReturn func() (b bool, i int)
|
|
|
|
multiNamedReturn = f //@snippet(" //", litFunc, "func() (b bool, i int) {$0\\}", "func() (b bool, i int) {$0\\}")
|
|
|
|
|
|
|
|
var duplicateParams func(myImpl, int, myImpl)
|
|
|
|
duplicateParams = f //@snippet(" //", litFunc, "", "func(${1:mi} myImpl, ${2:_} int, ${3:_} myImpl) {$0\\}")
|
|
|
|
}
|
2019-10-15 10:26:11 -06:00
|
|
|
|
|
|
|
func _() {
|
|
|
|
StructFoo{} //@item(litStructFoo, "StructFoo{}", "struct{...}", "struct")
|
|
|
|
|
|
|
|
var sfp *foo.StructFoo
|
|
|
|
// Don't insert the "&" before "StructFoo{}".
|
|
|
|
sfp = foo.Str //@snippet(" //", litStructFoo, "StructFoo{$0\\}", "StructFoo{$0\\}")
|
|
|
|
|
|
|
|
var sf foo.StructFoo
|
|
|
|
sf = foo.Str //@snippet(" //", litStructFoo, "StructFoo{$0\\}", "StructFoo{$0\\}")
|
|
|
|
sf = foo. //@snippet(" //", litStructFoo, "StructFoo{$0\\}", "StructFoo{$0\\}")
|
|
|
|
}
|