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

     1  This test checks various ranking of completion results.
     2  
     3  -- flags --
     4  -ignore_extra_diags
     5  
     6  -- settings.json --
     7  {
     8  	"completeUnimported": false,
     9  	"deepCompletion": false
    10  }
    11  
    12  -- go.mod --
    13  module golang.org/lsptests/rank
    14  
    15  go 1.18
    16  
    17  -- struct/struct_rank.go --
    18  package struct_rank
    19  
    20  type foo struct {
    21  	c int //@item(c_rank, "c", "int", "field")
    22  	b int //@item(b_rank, "b", "int", "field")
    23  	a int //@item(a_rank, "a", "int", "field")
    24  }
    25  
    26  func f() {
    27  	foo := foo{} //@rank("}", c_rank, b_rank, a_rank)
    28  }
    29  
    30  -- assign_rank.go --
    31  package rank
    32  
    33  // Literal completion results.
    34  /* int() */ //@item(int, "int()", "int", "var")
    35  /* string() */ //@item(string, "string()", "string", "var")
    36  
    37  var (
    38  	apple int = 3         //@item(apple, "apple", "int", "var")
    39  	pear string = "hello" //@item(pear, "pear", "string", "var")
    40  )
    41  
    42  func _() {
    43  	orange := 1      //@item(orange, "orange", "int", "var")
    44  	grape := "hello" //@item(grape, "grape", "string", "var")
    45  	orange, grape = 2, "hello"  //@complete(" \"", grape, pear, string, orange, apple)
    46  }
    47  
    48  func _() {
    49  	var pineapple int //@item(pineapple, "pineapple", "int", "var")
    50  	pineapple = 1    //@complete(" 1", pineapple, apple, int, pear)
    51  
    52  	y := //@complete(" /", pineapple, apple, pear)
    53  }
    54  
    55  -- binexpr_rank.go --
    56  package rank
    57  
    58  func _() {
    59    _ = 5 +  ; //@complete(" ;", apple, pear)
    60    y :=  + 5; //@complete(" +", apple, pear)
    61  
    62    if 6 ==  {} //@complete(" {", apple, pear)
    63  }
    64  
    65  -- boolexpr_rank.go --
    66  package rank
    67  
    68  func _() {
    69  	someRandomBoolFunc := func() bool { //@item(boolExprFunc, "someRandomBoolFunc", "func() bool", "var")
    70  		return true
    71  	}
    72  
    73  	var foo, bar int     //@item(boolExprBar, "bar", "int", "var")
    74  	if foo == 123 && b { //@rank(" {", boolExprBar, boolExprFunc)
    75  	}
    76  }
    77  
    78  -- convert_rank.go --
    79  package rank
    80  
    81  import "time"
    82  
    83  // Copied from the old builtins.go, which has been ported to the new marker tests.
    84  /* complex(r float64, i float64) */ //@item(complex, "complex", "func(r float64, i float64) complex128", "func")
    85  
    86  func _() {
    87  	type strList []string
    88  	wantsStrList := func(strList) {}
    89  
    90  	var (
    91  		convA string   //@item(convertA, "convA", "string", "var")
    92  		convB []string //@item(convertB, "convB", "[]string", "var")
    93  	)
    94  	wantsStrList(strList(conv)) //@complete("))", convertB, convertA)
    95  }
    96  
    97  func _() {
    98  	type myInt int
    99  
   100  	const (
   101  		convC        = "hi"    //@item(convertC, "convC", "string", "const")
   102  		convD        = 123     //@item(convertD, "convD", "int", "const")
   103  		convE int    = 123     //@item(convertE, "convE", "int", "const")
   104  		convF string = "there" //@item(convertF, "convF", "string", "const")
   105  		convG myInt  = 123     //@item(convertG, "convG", "myInt", "const")
   106  	)
   107  
   108  	var foo int
   109  	foo = conv //@rank(" //", convertE, convertD)
   110  
   111  	var mi myInt
   112  	mi = conv //@rank(" //", convertG, convertD, convertE)
   113  	mi + conv //@rank(" //", convertG, convertD, convertE)
   114  
   115  	1 + conv //@rank(" //", convertD, convertC),rank(" //", convertE, convertC),rank(" //", convertG, convertC)
   116  
   117  	type myString string
   118  	var ms myString
   119  	ms = conv //@rank(" //", convertC, convertF)
   120  
   121  	type myUint uint32
   122  	var mu myUint
   123  	mu = conv //@rank(" //", convertD, convertE)
   124  
   125  	// don't downrank constants when assigning to interface{}
   126  	var _ interface{} = c //@rank(" //", convertD, complex)
   127  
   128  	var _ time.Duration = conv //@rank(" //", convertD, convertE),snippet(" //", convertE, "time.Duration(convE)")
   129  
   130  	var convP myInt   //@item(convertP, "convP", "myInt", "var")
   131  	var _ *int = conv //@snippet(" //", convertP, "(*int)(&convP)")
   132  
   133  	var ff float64 //@item(convertFloat, "ff", "float64", "var")
   134  	f == convD     //@snippet(" =", convertFloat, "ff")
   135  }
   136  
   137  -- switch_rank.go --
   138  package rank
   139  
   140  import "time"
   141  
   142  func _() {
   143  	switch pear {
   144  	case _: //@rank("_", pear, apple)
   145  	}
   146  
   147  	time.Monday //@item(timeMonday, "time.Monday", "time.Weekday", "const"),item(monday ,"Monday", "time.Weekday", "const")
   148  	time.Friday //@item(timeFriday, "time.Friday", "time.Weekday", "const"),item(friday ,"Friday", "time.Weekday", "const")
   149  
   150  	now := time.Now()
   151  	now.Weekday //@item(nowWeekday, "now.Weekday", "func() time.Weekday", "method")
   152  
   153  	then := time.Now()
   154  	then.Weekday //@item(thenWeekday, "then.Weekday", "func() time.Weekday", "method")
   155  
   156  	switch time.Weekday(0) {
   157  	case time.Monday, time.Tuesday:
   158  	case time.Wednesday, time.Thursday:
   159  	case time.Saturday, time.Sunday:
   160  	// TODO: these tests were disabled because they require deep completion
   161  	// (which would break other tests)
   162  	case t: // rank(":", timeFriday, timeMonday)
   163  	case time.: //@rank(":", friday, monday)
   164  
   165  	case now.Weekday():
   166  	case week: // rank(":", thenWeekday, nowWeekday)
   167  	}
   168  }
   169  
   170  -- type_assert_rank.go --
   171  package rank
   172  
   173  func _() {
   174  	type flower int //@item(flower, "flower", "int", "type")
   175  	var fig string  //@item(fig, "fig", "string", "var")
   176  
   177  	_ = interface{}(nil).(f) //@complete(") //", flower)
   178  }
   179  
   180  -- type_switch_rank.go --
   181  package rank
   182  
   183  import (
   184  	"fmt"
   185  	"go/ast"
   186  )
   187  
   188  func _() {
   189  	type basket int   //@item(basket, "basket", "int", "type")
   190  	var banana string //@item(banana, "banana", "string", "var")
   191  
   192  	switch interface{}(pear).(type) {
   193  	case b: //@complete(":", basket)
   194  		b //@complete(" //", banana, basket)
   195  	}
   196  
   197  	Ident  //@item(astIdent, "Ident", "struct{...}", "struct")
   198  	IfStmt //@item(astIfStmt, "IfStmt", "struct{...}", "struct")
   199  
   200  	switch ast.Node(nil).(type) {
   201  	case *ast.Ident:
   202  	case *ast.I: //@rank(":", astIfStmt, astIdent)
   203  	}
   204  
   205  	Stringer   //@item(fmtStringer, "Stringer", "interface{...}", "interface")
   206  	GoStringer //@item(fmtGoStringer, "GoStringer", "interface{...}", "interface")
   207  
   208  	switch interface{}(nil).(type) {
   209  	case fmt.Stringer: //@rank(":", fmtStringer, fmtGoStringer)
   210  	}
   211  }
   212