golang.org/x/tools/gopls@v0.15.3/internal/test/marker/testdata/hover/godef.txt (about)

     1  This test was ported from 'godef' in the old marker tests.
     2  It tests various hover and definition requests.
     3  
     4  Requires go1.19+ for the new go/doc/comment package.
     5  
     6  -- flags --
     7  -min_go=go1.19
     8  
     9  -- flags --
    10  -min_go=go1.20
    11  
    12  -- go.mod --
    13  module godef.test
    14  
    15  go 1.18
    16  
    17  -- a/a_x_test.go --
    18  package a_test
    19  
    20  import (
    21  	"testing"
    22  )
    23  
    24  func TestA2(t *testing.T) { //@hover("TestA2", "TestA2", TestA2)
    25  	Nonexistant() //@diag("Nonexistant", re"(undeclared name|undefined): Nonexistant")
    26  }
    27  
    28  -- @TestA2 --
    29  ```go
    30  func TestA2(t *testing.T)
    31  ```
    32  -- @ember --
    33  ```go
    34  field Member string
    35  ```
    36  
    37  @loc(Member, "Member")
    38  
    39  
    40  [`(a.Thing).Member` on pkg.go.dev](https://pkg.go.dev/godef.test/a#Thing.Member)
    41  -- a/d.go --
    42  package a //@hover("a", _, a)
    43  
    44  import "fmt"
    45  
    46  type Thing struct { //@loc(Thing, "Thing")
    47  	Member string //@loc(Member, "Member")
    48  }
    49  
    50  var Other Thing //@loc(Other, "Other")
    51  
    52  func Things(val []string) []Thing { //@loc(Things, "Things")
    53  	return nil
    54  }
    55  
    56  func (t Thing) Method(i int) string { //@loc(Method, "Method")
    57  	return t.Member
    58  }
    59  
    60  func (t Thing) Method3() {
    61  }
    62  
    63  func (t *Thing) Method2(i int, j int) (error, string) {
    64  	return nil, t.Member
    65  }
    66  
    67  func (t *Thing) private() {
    68  }
    69  
    70  func useThings() {
    71  	t := Thing{ //@hover("ing", "Thing", ing)
    72  		Member: "string", //@hover("ember", "Member", ember), def("ember", Member)
    73  	}
    74  	fmt.Print(t.Member) //@hover("ember", "Member", ember), def("ember", Member)
    75  	fmt.Print(Other)    //@hover("ther", "Other", ther), def("ther", Other)
    76  	Things(nil)         //@hover("ings", "Things", ings), def("ings", Things)
    77  	t.Method(0)         //@hover("eth", "Method", eth), def("eth", Method)
    78  }
    79  
    80  type NextThing struct { //@loc(NextThing, "NextThing")
    81  	Thing
    82  	Value int
    83  }
    84  
    85  func (n NextThing) another() string {
    86  	return n.Member
    87  }
    88  
    89  // Shadows Thing.Method3
    90  func (n *NextThing) Method3() int {
    91  	return n.Value
    92  }
    93  
    94  var nextThing NextThing //@hover("NextThing", "NextThing", NextThing), def("NextThing", NextThing)
    95  
    96  -- @ings --
    97  ```go
    98  func Things(val []string) []Thing
    99  ```
   100  
   101  [`a.Things` on pkg.go.dev](https://pkg.go.dev/godef.test/a#Things)
   102  -- @ther --
   103  ```go
   104  var Other Thing
   105  ```
   106  
   107  @loc(Other, "Other")
   108  
   109  
   110  [`a.Other` on pkg.go.dev](https://pkg.go.dev/godef.test/a#Other)
   111  -- @a --
   112  -- @ing --
   113  ```go
   114  type Thing struct {
   115  	Member string //@loc(Member, "Member")
   116  }
   117  ```
   118  
   119  ```go
   120  func (t Thing) Method(i int) string
   121  func (t *Thing) Method2(i int, j int) (error, string)
   122  func (t Thing) Method3()
   123  func (t *Thing) private()
   124  ```
   125  
   126  [`a.Thing` on pkg.go.dev](https://pkg.go.dev/godef.test/a#Thing)
   127  -- @NextThing --
   128  ```go
   129  type NextThing struct {
   130  	Thing
   131  	Value int
   132  }
   133  ```
   134  
   135  ```go
   136  // Embedded fields:
   137  Member string // through Thing 
   138  ```
   139  
   140  ```go
   141  func (t Thing) Method(i int) string
   142  func (t *Thing) Method2(i int, j int) (error, string)
   143  func (n *NextThing) Method3() int
   144  func (n NextThing) another() string
   145  func (t *Thing) private()
   146  ```
   147  
   148  [`a.NextThing` on pkg.go.dev](https://pkg.go.dev/godef.test/a#NextThing)
   149  -- @eth --
   150  ```go
   151  func (t Thing) Method(i int) string
   152  ```
   153  
   154  [`(a.Thing).Method` on pkg.go.dev](https://pkg.go.dev/godef.test/a#Thing.Method)
   155  -- a/f.go --
   156  // Package a is a package for testing go to definition.
   157  package a
   158  
   159  import "fmt"
   160  
   161  func TypeStuff() {
   162  	var x string
   163  
   164  	switch y := interface{}(x).(type) { //@loc(y, "y"), hover("y", "y", y) , def("y", y)
   165  	case int: //@loc(intY, "int")
   166  		fmt.Printf("%v", y) //@hover("y", "y", inty), def("y", y)
   167  	case string: //@loc(stringY, "string")
   168  		fmt.Printf("%v", y) //@hover("y", "y", stringy), def("y", y)
   169  	}
   170  
   171  }
   172  -- @inty --
   173  ```go
   174  var y int
   175  ```
   176  -- @stringy --
   177  ```go
   178  var y string
   179  ```
   180  -- @y --
   181  ```go
   182  var y interface{}
   183  ```
   184  -- a/h.go --
   185  package a
   186  
   187  func _() {
   188  	type s struct {
   189  		nested struct {
   190  			// nested number
   191  			number int64 //@loc(nestedNumber, "number")
   192  		}
   193  		nested2 []struct {
   194  			// nested string
   195  			str string //@loc(nestedString, "str")
   196  		}
   197  		x struct {
   198  			x struct {
   199  				x struct {
   200  					x struct {
   201  						x struct {
   202  							// nested map
   203  							m map[string]float64 //@loc(nestedMap, "m")
   204  						}
   205  					}
   206  				}
   207  			}
   208  		}
   209  	}
   210  
   211  	var t s
   212  	_ = t.nested.number  //@hover("number", "number", nestedNumber), def("number", nestedNumber)
   213  	_ = t.nested2[0].str //@hover("str", "str", nestedString), def("str", nestedString)
   214  	_ = t.x.x.x.x.x.m    //@hover("m", "m", nestedMap), def("m", nestedMap)
   215  }
   216  
   217  func _() {
   218  	var s struct {
   219  		// a field
   220  		a int //@loc(structA, "a")
   221  		// b nested struct
   222  		b struct { //@loc(structB, "b")
   223  			// c field of nested struct
   224  			c int //@loc(structC, "c")
   225  		}
   226  	}
   227  	_ = s.a   //@def("a", structA)
   228  	_ = s.b   //@def("b", structB)
   229  	_ = s.b.c //@def("c", structC)
   230  
   231  	var arr []struct {
   232  		// d field
   233  		d int //@loc(arrD, "d")
   234  		// e nested struct
   235  		e struct { //@loc(arrE, "e")
   236  			// f field of nested struct
   237  			f int //@loc(arrF, "f")
   238  		}
   239  	}
   240  	_ = arr[0].d   //@def("d", arrD)
   241  	_ = arr[0].e   //@def("e", arrE)
   242  	_ = arr[0].e.f //@def("f", arrF)
   243  
   244  	var complex []struct {
   245  		c <-chan map[string][]struct {
   246  			// h field
   247  			h int //@loc(complexH, "h")
   248  			// i nested struct
   249  			i struct { //@loc(complexI, "i")
   250  				// j field of nested struct
   251  				j int //@loc(complexJ, "j")
   252  			}
   253  		}
   254  	}
   255  	_ = (<-complex[0].c)["0"][0].h   //@def("h", complexH)
   256  	_ = (<-complex[0].c)["0"][0].i   //@def("i", complexI)
   257  	_ = (<-complex[0].c)["0"][0].i.j //@def("j", complexJ)
   258  
   259  	var mapWithStructKey map[struct { //@diag("struct", re"invalid map key")
   260  		// X key field
   261  		x []string //@loc(mapStructKeyX, "x")
   262  	}]int
   263  	for k := range mapWithStructKey {
   264  		_ = k.x //@def("x", mapStructKeyX)
   265  	}
   266  
   267  	var mapWithStructKeyAndValue map[struct {
   268  		// Y key field
   269  		y string //@loc(mapStructKeyY, "y")
   270  	}]struct {
   271  		// X value field
   272  		x string //@loc(mapStructValueX, "x")
   273  	}
   274  	for k, v := range mapWithStructKeyAndValue {
   275  		// TODO: we don't show docs for y field because both map key and value
   276  		// are structs. And in this case, we parse only map value
   277  		_ = k.y //@hover("y", "y", hoverStructKeyY), def("y", mapStructKeyY)
   278  		_ = v.x //@hover("x", "x", hoverStructKeyX), def("x", mapStructValueX)
   279  	}
   280  
   281  	var i []map[string]interface {
   282  		// open method comment
   283  		open() error //@loc(openMethod, "open")
   284  	}
   285  	i[0]["1"].open() //@hover("pen","open", openMethod), def("open", openMethod)
   286  }
   287  
   288  func _() {
   289  	test := struct {
   290  		// test description
   291  		desc string //@loc(testDescription, "desc")
   292  	}{}
   293  	_ = test.desc //@def("desc", testDescription)
   294  
   295  	for _, tt := range []struct {
   296  		// test input
   297  		in map[string][]struct { //@loc(testInput, "in")
   298  			// test key
   299  			key string //@loc(testInputKey, "key")
   300  			// test value
   301  			value interface{} //@loc(testInputValue, "value")
   302  		}
   303  		result struct {
   304  			v <-chan struct {
   305  				// expected test value
   306  				value int //@loc(testResultValue, "value")
   307  			}
   308  		}
   309  	}{} {
   310  		_ = tt.in               //@def("in", testInput)
   311  		_ = tt.in["0"][0].key   //@def("key", testInputKey)
   312  		_ = tt.in["0"][0].value //@def("value", testInputValue)
   313  
   314  		_ = (<-tt.result.v).value //@def("value", testResultValue)
   315  	}
   316  }
   317  
   318  func _() {
   319  	getPoints := func() []struct {
   320  		// X coord
   321  		x int //@loc(returnX, "x")
   322  		// Y coord
   323  		y int //@loc(returnY, "y")
   324  	} {
   325  		return nil
   326  	}
   327  
   328  	r := getPoints()
   329  	_ = r[0].x //@def("x", returnX)
   330  	_ = r[0].y //@def("y", returnY)
   331  }
   332  -- @hoverStructKeyX --
   333  ```go
   334  field x string
   335  ```
   336  
   337  X value field
   338  -- @hoverStructKeyY --
   339  ```go
   340  field y string
   341  ```
   342  
   343  Y key field
   344  -- @nestedNumber --
   345  ```go
   346  field number int64
   347  ```
   348  
   349  nested number
   350  -- @nestedString --
   351  ```go
   352  field str string
   353  ```
   354  
   355  nested string
   356  -- @openMethod --
   357  ```go
   358  func (interface) open() error
   359  ```
   360  
   361  open method comment
   362  -- @nestedMap --
   363  ```go
   364  field m map[string]float64
   365  ```
   366  
   367  nested map
   368  -- b/e.go --
   369  package b
   370  
   371  import (
   372  	"fmt"
   373  
   374  	"godef.test/a"
   375  )
   376  
   377  func useThings() {
   378  	t := a.Thing{}      //@loc(bStructType, "ing")
   379  	fmt.Print(t.Member) //@loc(bMember, "ember")
   380  	fmt.Print(a.Other)  //@loc(bVar, "ther")
   381  	a.Things(nil)          //@loc(bFunc, "ings")
   382  }
   383  
   384  /*@
   385  def(bStructType, Thing)
   386  def(bMember, Member)
   387  def(bVar, Other)
   388  def(bFunc, Things)
   389  */
   390  
   391  func _() {
   392  	var x interface{}
   393  	switch x := x.(type) { //@hover("x", "x", xInterface)
   394  	case string: //@loc(eString, "string")
   395  		fmt.Println(x) //@hover("x", "x", xString)
   396  	case int: //@loc(eInt, "int")
   397  		fmt.Println(x) //@hover("x", "x", xInt)
   398  	}
   399  }
   400  -- @xInt --
   401  ```go
   402  var x int
   403  ```
   404  -- @xInterface --
   405  ```go
   406  var x interface{}
   407  ```
   408  -- @xString --
   409  ```go
   410  var x string
   411  ```
   412  -- broken/unclosedIf.go --
   413  package broken
   414  
   415  import "fmt"
   416  
   417  func unclosedIf() {
   418  	if false {
   419  		var myUnclosedIf string              //@loc(myUnclosedIf, "myUnclosedIf")
   420  		fmt.Printf("s = %v\n", myUnclosedIf) //@def("my", myUnclosedIf)
   421  }
   422  
   423  func _() {} //@diag("_", re"expected")