1
0
mirror of https://github.com/golang/go synced 2024-11-18 23:14:43 -07:00
go/internal/lsp/testdata/signature/signature.go
Muir Manders 7e40e1726e internal/lsp: improve signatureHelp in various cases
- show signature for function calls whose function expression is not
  an object (e.g. the second call in foo()()). since the function name
  is not available, we use the generic "func"
- only provide signature help when the position is on or within the
  call expression parens. this is consistent with the one other lsp
  server i tried (java). this improves the gopls experience in emacs
  where lsp-mode is constantly calling "hover" and
  "signatureHelp" ("hover" should be preferred unless you are inside
  the function params list)
- use the entire signature type string as the label since that includes
  the return values, which are useful to see
- don't qualify the function name with its package. it looks funny to
  see "bytes.Cap()" as the help when you are in a call
  to (*bytes.Buffer).Cap(). it could be useful to include invocant
  type info, but leave it out for now since signature help is meant to
  focus on the function parameters.
- don't turn variadic args "foo ...int" into "foo []int" for the
  parameter information (i.e. maintain it as "foo ...int")
- when determining active parameter, count the space before a
  parameter name as being part of that parameter (e.g. the space
  before "b" in "func(a int, b int)")
- handle variadic params when determining the active param (i.e.
  highlight "foo(a int, *b ...string*)" on signature help for final
  param in `foo(123, "a", "b", "c")`
- don't generate an extra space in formatParams() for unnamed
  arguments

I also tweaked the signatureHelp server log message to include the
error message itself, and populated the server's logger in lsp_test.go
to aid in development.

Fixes golang/go#31448

Change-Id: Iefe0e1e3c531d17197c0fa997b949174475a276c
GitHub-Last-Rev: 5c0b8ebd87a8c05d5d8f519ea096f94e89c77e2c
GitHub-Pull-Request: golang/tools#82
Reviewed-on: https://go-review.googlesource.com/c/tools/+/172439
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-04-17 18:10:49 +00:00

62 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, 0x1337) //@signature("13.37", "Bar(float64, ...byte)", 0)
Bar(13.37, 0x1337) //@signature("0x1337", "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 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)
panic("oops!") //@signature("oops", "panic(interface{})", 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)
}