1
0
mirror of https://github.com/golang/go synced 2024-11-19 11:24:51 -07:00
go/internal/lsp/testdata/signature/signature.go
Muir Manders 49ba83114e internal/lsp: improve composite literal completion
When the value of a composite literal key/value pair was unparsable,
you were getting completions for the composite literal keys instead of
values. For example "struct { foo int }{foo: []<>" was completing to
the field name "foo". This was because the leaf ast.Node at the cursor
was the composite literal itself, and our go-back-one-character logic
was not happening because the preceding character's node
was *ast.BadExpr, not *ast.Ident. Fix by always generating the ast
path for the character before the cursor's position. I couldn't find
any cases where this broke completion.

I also added expected type detection for the following composite
literal cases:
- array/slice literals
- struct literals (both implicit and explicit field names)
- map keys and values

Fixes golang/go#29153

Change-Id: If8cf678cbd743a970f52893fcf4a9b83ea06d7e9
GitHub-Last-Rev: f385705cc05eb98132e20561451dbb8c39b68519
GitHub-Pull-Request: golang/tools#86
Reviewed-on: https://go-review.googlesource.com/c/tools/+/173099
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-04-23 22:28:03 +00:00

63 lines
1.9 KiB
Go

package signature
import (
"bytes"
"encoding/json"
"math/big"
)
func Foo(a string, b int) (c bool) {
return
}
func Bar(float64, ...byte) {
}
type myStruct struct{}
func (*myStruct) foo(e *json.Decoder) (*big.Int, error) {
return nil, nil
}
type MyFunc func(foo int) string
func Qux() {
Foo("foo", 123) //@signature("(", "Foo(a string, b int) (c bool)", 2)
Foo("foo", 123) //@signature("123", "Foo(a string, b int) (c bool)", 1)
Foo("foo", 123) //@signature(",", "Foo(a string, b int) (c bool)", 0)
Foo("foo", 123) //@signature(" 1", "Foo(a string, b int) (c bool)", 1)
Foo("foo", 123) //@signature(")", "Foo(a string, b int) (c bool)", 1)
Bar(13.37, 0x13) //@signature("13.37", "Bar(float64, ...byte)", 0)
Bar(13.37, 0x37) //@signature("0x37", "Bar(float64, ...byte)", 1)
Bar(13.37, 1, 2, 3, 4) //@signature("4", "Bar(float64, ...byte)", 1)
fn := func(hi, there string) func(i int) rune {
return func(int) rune { return 0 }
}
fn("hi", "there") //@signature("hi", "fn(hi string, there string) func(i int) rune", 0)
fn("hi", "there")(1) //@signature("1", "func(i int) rune", 0)
fnPtr := &fn
(*fnPtr)("hi", "there") //@signature("hi", "func(hi string, there string) func(i int) rune", 0)
var fnIntf interface{} = Foo
fnIntf.(func(string, int) bool)("hi", 123) //@signature("123", "func(string, int) bool", 1)
(&bytes.Buffer{}).Next(2) //@signature("2", "Next(n int) []byte", 0)
myFunc := MyFunc(func(n int) string { return "" })
myFunc(123) //@signature("123", "myFunc(foo int) string", 0)
var ms myStruct
ms.foo(nil) //@signature("nil", "foo(e *json.Decoder) (*big.Int, error)", 0)
_ = make([]int, 1, 2) //@signature("2", "make([]int, int, int) []int", 2)
Foo(myFunc(123), 456) //@signature("myFunc", "Foo(a string, b int) (c bool)", 0)
Foo(myFunc(123), 456) //@signature("123", "myFunc(foo int) string", 0)
panic("oops!") //@signature("oops", "panic(interface{})", 0)
}