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

     1  This test checks the behavior of the 'fill struct' code action.
     2  See fill_struct_resolve.txt for same test with resolve support.
     3  
     4  -- flags --
     5  -ignore_extra_diags
     6  
     7  -- go.mod --
     8  module golang.org/lsptests/fillstruct
     9  
    10  go 1.18
    11  
    12  -- data/data.go --
    13  package data
    14  
    15  type B struct {
    16  	ExportedInt   int
    17  	unexportedInt int
    18  }
    19  
    20  -- a.go --
    21  package fillstruct
    22  
    23  import (
    24  	"golang.org/lsptests/fillstruct/data"
    25  )
    26  
    27  type basicStruct struct {
    28  	foo int
    29  }
    30  
    31  var _ = basicStruct{} //@codeactionedit("}", "refactor.rewrite", a1)
    32  
    33  type twoArgStruct struct {
    34  	foo int
    35  	bar string
    36  }
    37  
    38  var _ = twoArgStruct{} //@codeactionedit("}", "refactor.rewrite", a2)
    39  
    40  type nestedStruct struct {
    41  	bar   string
    42  	basic basicStruct
    43  }
    44  
    45  var _ = nestedStruct{} //@codeactionedit("}", "refactor.rewrite", a3)
    46  
    47  var _ = data.B{} //@codeactionedit("}", "refactor.rewrite", a4)
    48  -- @a1/a.go --
    49  @@ -11 +11,3 @@
    50  -var _ = basicStruct{} //@codeactionedit("}", "refactor.rewrite", a1)
    51  +var _ = basicStruct{
    52  +	foo: 0,
    53  +} //@codeactionedit("}", "refactor.rewrite", a1)
    54  -- @a2/a.go --
    55  @@ -18 +18,4 @@
    56  -var _ = twoArgStruct{} //@codeactionedit("}", "refactor.rewrite", a2)
    57  +var _ = twoArgStruct{
    58  +	foo: 0,
    59  +	bar: "",
    60  +} //@codeactionedit("}", "refactor.rewrite", a2)
    61  -- @a3/a.go --
    62  @@ -25 +25,4 @@
    63  -var _ = nestedStruct{} //@codeactionedit("}", "refactor.rewrite", a3)
    64  +var _ = nestedStruct{
    65  +	bar:   "",
    66  +	basic: basicStruct{},
    67  +} //@codeactionedit("}", "refactor.rewrite", a3)
    68  -- @a4/a.go --
    69  @@ -27 +27,3 @@
    70  -var _ = data.B{} //@codeactionedit("}", "refactor.rewrite", a4)
    71  +var _ = data.B{
    72  +	ExportedInt: 0,
    73  +} //@codeactionedit("}", "refactor.rewrite", a4)
    74  -- a2.go --
    75  package fillstruct
    76  
    77  type typedStruct struct {
    78  	m  map[string]int
    79  	s  []int
    80  	c  chan int
    81  	c1 <-chan int
    82  	a  [2]string
    83  }
    84  
    85  var _ = typedStruct{} //@codeactionedit("}", "refactor.rewrite", a21)
    86  
    87  type funStruct struct {
    88  	fn func(i int) int
    89  }
    90  
    91  var _ = funStruct{} //@codeactionedit("}", "refactor.rewrite", a22)
    92  
    93  type funStructComplex struct {
    94  	fn func(i int, s string) (string, int)
    95  }
    96  
    97  var _ = funStructComplex{} //@codeactionedit("}", "refactor.rewrite", a23)
    98  
    99  type funStructEmpty struct {
   100  	fn func()
   101  }
   102  
   103  var _ = funStructEmpty{} //@codeactionedit("}", "refactor.rewrite", a24)
   104  
   105  -- @a21/a2.go --
   106  @@ -11 +11,7 @@
   107  -var _ = typedStruct{} //@codeactionedit("}", "refactor.rewrite", a21)
   108  +var _ = typedStruct{
   109  +	m:  map[string]int{},
   110  +	s:  []int{},
   111  +	c:  make(chan int),
   112  +	c1: make(<-chan int),
   113  +	a:  [2]string{},
   114  +} //@codeactionedit("}", "refactor.rewrite", a21)
   115  -- @a22/a2.go --
   116  @@ -17 +17,4 @@
   117  -var _ = funStruct{} //@codeactionedit("}", "refactor.rewrite", a22)
   118  +var _ = funStruct{
   119  +	fn: func(i int) int {
   120  +	},
   121  +} //@codeactionedit("}", "refactor.rewrite", a22)
   122  -- @a23/a2.go --
   123  @@ -23 +23,4 @@
   124  -var _ = funStructComplex{} //@codeactionedit("}", "refactor.rewrite", a23)
   125  +var _ = funStructComplex{
   126  +	fn: func(i int, s string) (string, int) {
   127  +	},
   128  +} //@codeactionedit("}", "refactor.rewrite", a23)
   129  -- @a24/a2.go --
   130  @@ -29 +29,4 @@
   131  -var _ = funStructEmpty{} //@codeactionedit("}", "refactor.rewrite", a24)
   132  +var _ = funStructEmpty{
   133  +	fn: func() {
   134  +	},
   135  +} //@codeactionedit("}", "refactor.rewrite", a24)
   136  -- a3.go --
   137  package fillstruct
   138  
   139  import (
   140  	"go/ast"
   141  	"go/token"
   142  )
   143  
   144  type Foo struct {
   145  	A int
   146  }
   147  
   148  type Bar struct {
   149  	X *Foo
   150  	Y *Foo
   151  }
   152  
   153  var _ = Bar{} //@codeactionedit("}", "refactor.rewrite", a31)
   154  
   155  type importedStruct struct {
   156  	m  map[*ast.CompositeLit]ast.Field
   157  	s  []ast.BadExpr
   158  	a  [3]token.Token
   159  	c  chan ast.EmptyStmt
   160  	fn func(ast_decl ast.DeclStmt) ast.Ellipsis
   161  	st ast.CompositeLit
   162  }
   163  
   164  var _ = importedStruct{} //@codeactionedit("}", "refactor.rewrite", a32)
   165  
   166  type pointerBuiltinStruct struct {
   167  	b *bool
   168  	s *string
   169  	i *int
   170  }
   171  
   172  var _ = pointerBuiltinStruct{} //@codeactionedit("}", "refactor.rewrite", a33)
   173  
   174  var _ = []ast.BasicLit{
   175  	{}, //@codeactionedit("}", "refactor.rewrite", a34)
   176  }
   177  
   178  var _ = []ast.BasicLit{{}} //@codeactionedit("}", "refactor.rewrite", a35)
   179  -- @a31/a3.go --
   180  @@ -17 +17,4 @@
   181  -var _ = Bar{} //@codeactionedit("}", "refactor.rewrite", a31)
   182  +var _ = Bar{
   183  +	X: &Foo{},
   184  +	Y: &Foo{},
   185  +} //@codeactionedit("}", "refactor.rewrite", a31)
   186  -- @a32/a3.go --
   187  @@ -28 +28,9 @@
   188  -var _ = importedStruct{} //@codeactionedit("}", "refactor.rewrite", a32)
   189  +var _ = importedStruct{
   190  +	m: map[*ast.CompositeLit]ast.Field{},
   191  +	s: []ast.BadExpr{},
   192  +	a: [3]token.Token{},
   193  +	c: make(chan ast.EmptyStmt),
   194  +	fn: func(ast_decl ast.DeclStmt) ast.Ellipsis {
   195  +	},
   196  +	st: ast.CompositeLit{},
   197  +} //@codeactionedit("}", "refactor.rewrite", a32)
   198  -- @a33/a3.go --
   199  @@ -36 +36,5 @@
   200  -var _ = pointerBuiltinStruct{} //@codeactionedit("}", "refactor.rewrite", a33)
   201  +var _ = pointerBuiltinStruct{
   202  +	b: new(bool),
   203  +	s: new(string),
   204  +	i: new(int),
   205  +} //@codeactionedit("}", "refactor.rewrite", a33)
   206  -- @a34/a3.go --
   207  @@ -39 +39,5 @@
   208  -	{}, //@codeactionedit("}", "refactor.rewrite", a34)
   209  +	{
   210  +		ValuePos: 0,
   211  +		Kind:     0,
   212  +		Value:    "",
   213  +	}, //@codeactionedit("}", "refactor.rewrite", a34)
   214  -- @a35/a3.go --
   215  @@ -42 +42,5 @@
   216  -var _ = []ast.BasicLit{{}} //@codeactionedit("}", "refactor.rewrite", a35)
   217  +var _ = []ast.BasicLit{{
   218  +	ValuePos: 0,
   219  +	Kind:     0,
   220  +	Value:    "",
   221  +}} //@codeactionedit("}", "refactor.rewrite", a35)
   222  -- a4.go --
   223  package fillstruct
   224  
   225  import "go/ast"
   226  
   227  type iStruct struct {
   228  	X int
   229  }
   230  
   231  type sStruct struct {
   232  	str string
   233  }
   234  
   235  type multiFill struct {
   236  	num   int
   237  	strin string
   238  	arr   []int
   239  }
   240  
   241  type assignStruct struct {
   242  	n ast.Node
   243  }
   244  
   245  func fill() {
   246  	var x int
   247  	var _ = iStruct{} //@codeactionedit("}", "refactor.rewrite", a41)
   248  
   249  	var s string
   250  	var _ = sStruct{} //@codeactionedit("}", "refactor.rewrite", a42)
   251  
   252  	var n int
   253  	_ = []int{}
   254  	if true {
   255  		arr := []int{1, 2}
   256  	}
   257  	var _ = multiFill{} //@codeactionedit("}", "refactor.rewrite", a43)
   258  
   259  	var node *ast.CompositeLit
   260  	var _ = assignStruct{} //@codeactionedit("}", "refactor.rewrite", a45)
   261  }
   262  
   263  -- @a41/a4.go --
   264  @@ -25 +25,3 @@
   265  -	var _ = iStruct{} //@codeactionedit("}", "refactor.rewrite", a41)
   266  +	var _ = iStruct{
   267  +		X: x,
   268  +	} //@codeactionedit("}", "refactor.rewrite", a41)
   269  -- @a42/a4.go --
   270  @@ -28 +28,3 @@
   271  -	var _ = sStruct{} //@codeactionedit("}", "refactor.rewrite", a42)
   272  +	var _ = sStruct{
   273  +		str: s,
   274  +	} //@codeactionedit("}", "refactor.rewrite", a42)
   275  -- @a43/a4.go --
   276  @@ -35 +35,5 @@
   277  -	var _ = multiFill{} //@codeactionedit("}", "refactor.rewrite", a43)
   278  +	var _ = multiFill{
   279  +		num:   n,
   280  +		strin: s,
   281  +		arr:   []int{},
   282  +	} //@codeactionedit("}", "refactor.rewrite", a43)
   283  -- @a45/a4.go --
   284  @@ -38 +38,3 @@
   285  -	var _ = assignStruct{} //@codeactionedit("}", "refactor.rewrite", a45)
   286  +	var _ = assignStruct{
   287  +		n: node,
   288  +	} //@codeactionedit("}", "refactor.rewrite", a45)
   289  -- fill_struct.go --
   290  package fillstruct
   291  
   292  type StructA struct {
   293  	unexportedIntField int
   294  	ExportedIntField   int
   295  	MapA               map[int]string
   296  	Array              []int
   297  	StructB
   298  }
   299  
   300  type StructA2 struct {
   301  	B *StructB
   302  }
   303  
   304  type StructA3 struct {
   305  	B StructB
   306  }
   307  
   308  func fill() {
   309  	a := StructA{}  //@codeactionedit("}", "refactor.rewrite", fill_struct1)
   310  	b := StructA2{} //@codeactionedit("}", "refactor.rewrite", fill_struct2)
   311  	c := StructA3{} //@codeactionedit("}", "refactor.rewrite", fill_struct3)
   312  	if true {
   313  		_ = StructA3{} //@codeactionedit("}", "refactor.rewrite", fill_struct4)
   314  	}
   315  }
   316  
   317  -- @fill_struct1/fill_struct.go --
   318  @@ -20 +20,7 @@
   319  -	a := StructA{}  //@codeactionedit("}", "refactor.rewrite", fill_struct1)
   320  +	a := StructA{
   321  +		unexportedIntField: 0,
   322  +		ExportedIntField:   0,
   323  +		MapA:               map[int]string{},
   324  +		Array:              []int{},
   325  +		StructB:            StructB{},
   326  +	}  //@codeactionedit("}", "refactor.rewrite", fill_struct1)
   327  -- @fill_struct2/fill_struct.go --
   328  @@ -21 +21,3 @@
   329  -	b := StructA2{} //@codeactionedit("}", "refactor.rewrite", fill_struct2)
   330  +	b := StructA2{
   331  +		B: &StructB{},
   332  +	} //@codeactionedit("}", "refactor.rewrite", fill_struct2)
   333  -- @fill_struct3/fill_struct.go --
   334  @@ -22 +22,3 @@
   335  -	c := StructA3{} //@codeactionedit("}", "refactor.rewrite", fill_struct3)
   336  +	c := StructA3{
   337  +		B: StructB{},
   338  +	} //@codeactionedit("}", "refactor.rewrite", fill_struct3)
   339  -- @fill_struct4/fill_struct.go --
   340  @@ -24 +24,3 @@
   341  -		_ = StructA3{} //@codeactionedit("}", "refactor.rewrite", fill_struct4)
   342  +		_ = StructA3{
   343  +			B: StructB{},
   344  +		} //@codeactionedit("}", "refactor.rewrite", fill_struct4)
   345  -- fill_struct_anon.go --
   346  package fillstruct
   347  
   348  type StructAnon struct {
   349  	a struct{}
   350  	b map[string]interface{}
   351  	c map[string]struct {
   352  		d int
   353  		e bool
   354  	}
   355  }
   356  
   357  func fill() {
   358  	_ := StructAnon{} //@codeactionedit("}", "refactor.rewrite", fill_struct_anon)
   359  }
   360  -- @fill_struct_anon/fill_struct_anon.go --
   361  @@ -13 +13,5 @@
   362  -	_ := StructAnon{} //@codeactionedit("}", "refactor.rewrite", fill_struct_anon)
   363  +	_ := StructAnon{
   364  +		a: struct{}{},
   365  +		b: map[string]interface{}{},
   366  +		c: map[string]struct{d int; e bool}{},
   367  +	} //@codeactionedit("}", "refactor.rewrite", fill_struct_anon)
   368  -- fill_struct_nested.go --
   369  package fillstruct
   370  
   371  type StructB struct {
   372  	StructC
   373  }
   374  
   375  type StructC struct {
   376  	unexportedInt int
   377  }
   378  
   379  func nested() {
   380  	c := StructB{
   381  		StructC: StructC{}, //@codeactionedit("}", "refactor.rewrite", fill_nested)
   382  	}
   383  }
   384  
   385  -- @fill_nested/fill_struct_nested.go --
   386  @@ -13 +13,3 @@
   387  -		StructC: StructC{}, //@codeactionedit("}", "refactor.rewrite", fill_nested)
   388  +		StructC: StructC{
   389  +			unexportedInt: 0,
   390  +		}, //@codeactionedit("}", "refactor.rewrite", fill_nested)
   391  -- fill_struct_package.go --
   392  package fillstruct
   393  
   394  import (
   395  	h2 "net/http"
   396  
   397  	"golang.org/lsptests/fillstruct/data"
   398  )
   399  
   400  func unexported() {
   401  	a := data.B{}   //@codeactionedit("}", "refactor.rewrite", fill_struct_package1)
   402  	_ = h2.Client{} //@codeactionedit("}", "refactor.rewrite", fill_struct_package2)
   403  }
   404  -- @fill_struct_package1/fill_struct_package.go --
   405  @@ -10 +10,3 @@
   406  -	a := data.B{}   //@codeactionedit("}", "refactor.rewrite", fill_struct_package1)
   407  +	a := data.B{
   408  +		ExportedInt: 0,
   409  +	}   //@codeactionedit("}", "refactor.rewrite", fill_struct_package1)
   410  -- @fill_struct_package2/fill_struct_package.go --
   411  @@ -11 +11,7 @@
   412  -	_ = h2.Client{} //@codeactionedit("}", "refactor.rewrite", fill_struct_package2)
   413  +	_ = h2.Client{
   414  +		Transport: nil,
   415  +		CheckRedirect: func(req *h2.Request, via []*h2.Request) error {
   416  +		},
   417  +		Jar:     nil,
   418  +		Timeout: 0,
   419  +	} //@codeactionedit("}", "refactor.rewrite", fill_struct_package2)
   420  -- fill_struct_partial.go --
   421  package fillstruct
   422  
   423  type StructPartialA struct {
   424  	PrefilledInt int
   425  	UnfilledInt  int
   426  	StructPartialB
   427  }
   428  
   429  type StructPartialB struct {
   430  	PrefilledInt int
   431  	UnfilledInt  int
   432  }
   433  
   434  func fill() {
   435  	a := StructPartialA{
   436  		PrefilledInt: 5,
   437  	} //@codeactionedit("}", "refactor.rewrite", fill_struct_partial1)
   438  	b := StructPartialB{
   439  		/* this comment should disappear */
   440  		PrefilledInt: 7, // This comment should be blown away.
   441  		/* As should
   442  		this one */
   443  	} //@codeactionedit("}", "refactor.rewrite", fill_struct_partial2)
   444  }
   445  
   446  -- @fill_struct_partial1/fill_struct_partial.go --
   447  @@ -16 +16,3 @@
   448  -		PrefilledInt: 5,
   449  +		PrefilledInt:   5,
   450  +		UnfilledInt:    0,
   451  +		StructPartialB: StructPartialB{},
   452  -- @fill_struct_partial2/fill_struct_partial.go --
   453  @@ -19,4 +19,2 @@
   454  -		/* this comment should disappear */
   455  -		PrefilledInt: 7, // This comment should be blown away.
   456  -		/* As should
   457  -		this one */
   458  +		PrefilledInt: 7,
   459  +		UnfilledInt:  0,
   460  -- fill_struct_spaces.go --
   461  package fillstruct
   462  
   463  type StructD struct {
   464  	ExportedIntField int
   465  }
   466  
   467  func spaces() {
   468  	d := StructD{} //@codeactionedit("}", "refactor.rewrite", fill_struct_spaces)
   469  }
   470  
   471  -- @fill_struct_spaces/fill_struct_spaces.go --
   472  @@ -8 +8,3 @@
   473  -	d := StructD{} //@codeactionedit("}", "refactor.rewrite", fill_struct_spaces)
   474  +	d := StructD{
   475  +		ExportedIntField: 0,
   476  +	} //@codeactionedit("}", "refactor.rewrite", fill_struct_spaces)
   477  -- fill_struct_unsafe.go --
   478  package fillstruct
   479  
   480  import "unsafe"
   481  
   482  type unsafeStruct struct {
   483  	x int
   484  	p unsafe.Pointer
   485  }
   486  
   487  func fill() {
   488  	_ := unsafeStruct{} //@codeactionedit("}", "refactor.rewrite", fill_struct_unsafe)
   489  }
   490  
   491  -- @fill_struct_unsafe/fill_struct_unsafe.go --
   492  @@ -11 +11,4 @@
   493  -	_ := unsafeStruct{} //@codeactionedit("}", "refactor.rewrite", fill_struct_unsafe)
   494  +	_ := unsafeStruct{
   495  +		x: 0,
   496  +		p: nil,
   497  +	} //@codeactionedit("}", "refactor.rewrite", fill_struct_unsafe)
   498  -- typeparams.go --
   499  package fillstruct
   500  
   501  type emptyStructWithTypeParams[A any] struct{}
   502  
   503  var _ = emptyStructWithTypeParams[int]{} // no suggested fix
   504  
   505  type basicStructWithTypeParams[T any] struct {
   506  	foo T
   507  }
   508  
   509  var _ = basicStructWithTypeParams[int]{} //@codeactionedit("}", "refactor.rewrite", typeparams1)
   510  
   511  type twoArgStructWithTypeParams[F, B any] struct {
   512  	foo F
   513  	bar B
   514  }
   515  
   516  var _ = twoArgStructWithTypeParams[string, int]{} //@codeactionedit("}", "refactor.rewrite", typeparams2)
   517  
   518  var _ = twoArgStructWithTypeParams[int, string]{
   519  	bar: "bar",
   520  } //@codeactionedit("}", "refactor.rewrite", typeparams3)
   521  
   522  type nestedStructWithTypeParams struct {
   523  	bar   string
   524  	basic basicStructWithTypeParams[int]
   525  }
   526  
   527  var _ = nestedStructWithTypeParams{} //@codeactionedit("}", "refactor.rewrite", typeparams4)
   528  
   529  func _[T any]() {
   530  	type S struct{ t T }
   531  	_ = S{} //@codeactionedit("}", "refactor.rewrite", typeparams5)
   532  }
   533  -- @typeparams1/typeparams.go --
   534  @@ -11 +11,3 @@
   535  -var _ = basicStructWithTypeParams[int]{} //@codeactionedit("}", "refactor.rewrite", typeparams1)
   536  +var _ = basicStructWithTypeParams[int]{
   537  +	foo: 0,
   538  +} //@codeactionedit("}", "refactor.rewrite", typeparams1)
   539  -- @typeparams2/typeparams.go --
   540  @@ -18 +18,4 @@
   541  -var _ = twoArgStructWithTypeParams[string, int]{} //@codeactionedit("}", "refactor.rewrite", typeparams2)
   542  +var _ = twoArgStructWithTypeParams[string, int]{
   543  +	foo: "",
   544  +	bar: 0,
   545  +} //@codeactionedit("}", "refactor.rewrite", typeparams2)
   546  -- @typeparams3/typeparams.go --
   547  @@ -21 +21 @@
   548  +	foo: 0,
   549  -- @typeparams4/typeparams.go --
   550  @@ -29 +29,4 @@
   551  -var _ = nestedStructWithTypeParams{} //@codeactionedit("}", "refactor.rewrite", typeparams4)
   552  +var _ = nestedStructWithTypeParams{
   553  +	bar:   "",
   554  +	basic: basicStructWithTypeParams{},
   555  +} //@codeactionedit("}", "refactor.rewrite", typeparams4)
   556  -- @typeparams5/typeparams.go --
   557  @@ -33 +33,3 @@
   558  -	_ = S{} //@codeactionedit("}", "refactor.rewrite", typeparams5)
   559  +	_ = S{
   560  +		t: *new(T),
   561  +	} //@codeactionedit("}", "refactor.rewrite", typeparams5)
   562  -- issue63921.go --
   563  package fillstruct
   564  
   565  // Test for golang/go#63921: fillstruct panicked with invalid fields.
   566  type invalidStruct struct {
   567    F int
   568    Undefined
   569  }
   570  
   571  func _() {
   572    // Note: the golden content for issue63921 is empty: fillstruct produces no
   573    // edits, but does not panic.
   574    invalidStruct{} //@codeactionedit("}", "refactor.rewrite", issue63921)
   575  }