1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
|
This test ports some arbitrary tests from the old marker framework, that were
*mostly* about completion.
-- flags --
-ignore_extra_diags
-min_go=go1.20
-- settings.json --
{
"completeUnimported": false,
"deepCompletion": false,
"experimentalPostfixCompletions": false
}
-- go.mod --
module foobar.test
go 1.18
-- foo/foo.go --
package foo //@loc(PackageFoo, "foo"),item(PackageFooItem, "foo", "\"foobar.test/foo\"", "package")
type StructFoo struct { //@loc(StructFooLoc, "StructFoo"), item(StructFoo, "StructFoo", "struct{...}", "struct")
Value int //@item(Value, "Value", "int", "field")
}
// Pre-set this marker, as we don't have a "source" for it in this package.
/* Error() */ //@item(Error, "Error", "func() string", "method")
func Foo() { //@item(Foo, "Foo", "func()", "func")
var err error
err.Error() //@complete("E", Error)
}
func _() {
var sFoo StructFoo //@complete("t", StructFoo)
if x := sFoo; x.Value == 1 { //@complete("V", Value), typedef("sFoo", StructFooLoc)
return
}
}
func _() {
shadowed := 123
{
shadowed := "hi" //@item(shadowed, "shadowed", "string", "var")
sha //@complete("a", shadowed), diag("sha", re"(undefined|undeclared)")
_ = shadowed
}
}
type IntFoo int //@loc(IntFooLoc, "IntFoo"), item(IntFoo, "IntFoo", "int", "type")
-- bar/bar.go --
package bar
import (
"foobar.test/foo" //@item(foo, "foo", "\"foobar.test/foo\"", "package")
)
func helper(i foo.IntFoo) {} //@item(helper, "helper", "func(i foo.IntFoo)", "func")
func _() {
help //@complete("l", helper)
_ = foo.StructFoo{} //@complete("S", IntFoo, StructFoo)
}
// Bar is a function.
func Bar() { //@item(Bar, "Bar", "func()", "func", "Bar is a function.")
foo.Foo() //@complete("F", Foo, IntFoo, StructFoo)
var _ foo.IntFoo //@complete("I", IntFoo, StructFoo)
foo.() //@complete("(", Foo, IntFoo, StructFoo), diag(")", re"expected type")
}
// These items weren't present in the old marker tests (due to settings), but
// we may as well include them.
//@item(intConversion, "int()"), item(fooFoo, "foo.Foo")
//@item(fooIntFoo, "foo.IntFoo"), item(fooStructFoo, "foo.StructFoo")
func _() {
var Valentine int //@item(Valentine, "Valentine", "int", "var")
_ = foo.StructFoo{ //@diag("foo", re"unkeyed fields")
Valu //@complete(" //", Value)
}
_ = foo.StructFoo{ //@diag("foo", re"unkeyed fields")
Va //@complete("a", Value, Valentine)
}
_ = foo.StructFoo{
Value: 5, //@complete("a", Value)
}
_ = foo.StructFoo{
//@complete("//", Value, Valentine, intConversion, foo, helper, Bar)
}
_ = foo.StructFoo{
Value: Valen //@complete("le", Valentine)
}
_ = foo.StructFoo{
Value: //@complete(" //", Valentine, intConversion, foo, helper, Bar)
}
_ = foo.StructFoo{
Value: //@complete(" ", Valentine, intConversion, foo, helper, Bar)
}
}
-- baz/baz.go --
package baz
import (
"foobar.test/bar"
f "foobar.test/foo"
)
var FooStruct f.StructFoo
func Baz() {
defer bar.Bar() //@complete("B", Bar)
// TODO: Test completion here.
defer bar.B //@diag(re"bar.B()", re"must be function call")
var x f.IntFoo //@complete("n", IntFoo), typedef("x", IntFooLoc)
bar.Bar() //@complete("B", Bar)
}
func _() {
bob := f.StructFoo{Value: 5}
if x := bob. //@complete(" //", Value)
switch true == false {
case true:
if x := bob. //@complete(" //", Value)
case false:
}
if x := bob.Va //@complete("a", Value)
switch true == true {
default:
}
}
-- arraytype/arraytype.go --
package arraytype
import (
"foobar.test/foo"
)
func _() {
var (
val string //@item(atVal, "val", "string", "var")
)
[] //@complete(" //", atVal, PackageFooItem)
[]val //@complete(" //")
[]foo.StructFoo //@complete(" //", StructFoo)
[]foo.StructFoo(nil) //@complete("(", StructFoo)
[]*foo.StructFoo //@complete(" //", StructFoo)
[...]foo.StructFoo //@complete(" //", StructFoo)
[2][][4]foo.StructFoo //@complete(" //", StructFoo)
[]struct { f []foo.StructFoo } //@complete(" }", StructFoo)
}
func _() {
type myInt int //@item(atMyInt, "myInt", "int", "type")
var mark []myInt //@item(atMark, "mark", "[]myInt", "var")
var s []myInt //@item(atS, "s", "[]myInt", "var")
s = []m //@complete(" //", atMyInt)
var a [1]myInt
a = [1]m //@complete(" //", atMyInt)
var ds [][]myInt
ds = [][]m //@complete(" //", atMyInt)
}
func _() {
var b [0]byte //@item(atByte, "b", "[0]byte", "var")
var _ []byte = b //@snippet(" //", atByte, "b[:]")
}
-- badstmt/badstmt.go --
package badstmt
import (
"foobar.test/foo"
)
// (The syntax error causes suppression of diagnostics for type errors.
// See issue #59888.)
func _(x int) {
defer foo.F //@complete(" //", Foo, IntFoo, StructFoo)
defer foo.F //@complete(" //", Foo, IntFoo, StructFoo)
}
func _() {
switch true {
case true:
go foo.F //@complete(" //", Foo, IntFoo, StructFoo)
}
}
func _() {
defer func() {
foo.F //@complete(" //", Foo, IntFoo, StructFoo), snippet(" //", Foo, "Foo()")
foo. //@rank(" //", Foo)
}
}
-- badstmt/badstmt_2.go --
package badstmt
import (
"foobar.test/foo"
)
func _() {
defer func() { foo. } //@rank(" }", Foo)
}
-- badstmt/badstmt_3.go --
package badstmt
import (
"foobar.test/foo"
)
func _() {
go foo. //@rank(" //", Foo, IntFoo), snippet(" //", Foo, "Foo()")
}
-- badstmt/badstmt_4.go --
package badstmt
import (
"foobar.test/foo"
)
func _() {
go func() {
defer foo. //@rank(" //", Foo, IntFoo)
}
}
-- selector/selector.go --
package selector
import (
"foobar.test/bar"
)
type S struct {
B, A, C int //@item(Bf, "B", "int", "field"),item(Af, "A", "int", "field"),item(Cf, "C", "int", "field")
}
func _() {
_ = S{}.; //@complete(";", Af, Bf, Cf)
}
type bob struct { a int } //@item(a, "a", "int", "field")
type george struct { b int }
type jack struct { c int } //@item(c, "c", "int", "field")
type jill struct { d int }
func (b *bob) george() *george {} //@item(george, "george", "func() *george", "method")
func (g *george) jack() *jack {}
func (j *jack) jill() *jill {} //@item(jill, "jill", "func() *jill", "method")
func _() {
b := &bob{}
y := b.george().
jack();
y.; //@complete(";", c, jill)
}
func _() {
bar. //@complete(" /", Bar)
x := 5
var b *bob
b. //@complete(" /", a, george)
y, z := 5, 6
b. //@complete(" /", a, george)
y, z, a, b, c := 5, 6
}
func _() {
bar. //@complete(" /", Bar)
bar.Bar()
bar. //@complete(" /", Bar)
go f()
}
func _() {
var b *bob
if y != b. //@complete(" /", a, george)
z := 5
if z + y + 1 + b. //@complete(" /", a, george)
r, s, t := 4, 5
if y != b. //@complete(" /", a, george)
z = 5
if z + y + 1 + b. //@complete(" /", a, george)
r = 4
}
-- literal_snippets/literal_snippets.go --
package literal_snippets
import (
"bytes"
"context"
"go/ast"
"net/http"
"sort"
"golang.org/lsptests/foo"
)
func _() {
[]int{} //@item(litIntSlice, "[]int{}", "", "var")
&[]int{} //@item(litIntSliceAddr, "&[]int{}", "", "var")
make([]int, 0) //@item(makeIntSlice, "make([]int, 0)", "", "func")
var _ *[]int = in //@snippet(" //", litIntSliceAddr, "&[]int{$0\\}")
var _ **[]int = in //@complete(" //")
var slice []int
slice = i //@snippet(" //", litIntSlice, "[]int{$0\\}")
slice = m //@snippet(" //", makeIntSlice, "make([]int, ${1:})")
}
func _() {
type namedInt []int
namedInt{} //@item(litNamedSlice, "namedInt{}", "", "var")
make(namedInt, 0) //@item(makeNamedSlice, "make(namedInt, 0)", "", "func")
var namedSlice namedInt
namedSlice = n //@snippet(" //", litNamedSlice, "namedInt{$0\\}")
namedSlice = m //@snippet(" //", makeNamedSlice, "make(namedInt, ${1:})")
}
func _() {
make(chan int) //@item(makeChan, "make(chan int)", "", "func")
var ch chan int
ch = m //@snippet(" //", makeChan, "make(chan int)")
}
func _() {
map[string]struct{}{} //@item(litMap, "map[string]struct{}{}", "", "var")
make(map[string]struct{}) //@item(makeMap, "make(map[string]struct{})", "", "func")
var m map[string]struct{}
m = m //@snippet(" //", litMap, "map[string]struct{\\}{$0\\}")
m = m //@snippet(" //", makeMap, "make(map[string]struct{\\})")
struct{}{} //@item(litEmptyStruct, "struct{}{}", "", "var")
m["hi"] = s //@snippet(" //", litEmptyStruct, "struct{\\}{\\}")
}
func _() {
type myStruct struct{ i int } //@item(myStructType, "myStruct", "struct{...}", "struct")
myStruct{} //@item(litStruct, "myStruct{}", "", "var")
&myStruct{} //@item(litStructPtr, "&myStruct{}", "", "var")
var ms myStruct
ms = m //@snippet(" //", litStruct, "myStruct{$0\\}")
var msPtr *myStruct
msPtr = m //@snippet(" //", litStructPtr, "&myStruct{$0\\}")
msPtr = &m //@snippet(" //", litStruct, "myStruct{$0\\}")
type myStructCopy struct { i int } //@item(myStructCopyType, "myStructCopy", "struct{...}", "struct")
// Don't offer literal completion for convertible structs.
ms = myStruct //@complete(" //", litStruct, myStructType, myStructCopyType)
}
type myImpl struct{}
func (myImpl) foo() {}
func (*myImpl) bar() {}
type myBasicImpl string
func (myBasicImpl) foo() {}
func _() {
type myIntf interface {
foo()
}
myImpl{} //@item(litImpl, "myImpl{}", "", "var")
var mi myIntf
mi = m //@snippet(" //", litImpl, "myImpl{\\}")
myBasicImpl() //@item(litBasicImpl, "myBasicImpl()", "string", "var")
mi = m //@snippet(" //", litBasicImpl, "myBasicImpl($0)")
// only satisfied by pointer to myImpl
type myPtrIntf interface {
bar()
}
&myImpl{} //@item(litImplPtr, "&myImpl{}", "", "var")
var mpi myPtrIntf
mpi = m //@snippet(" //", litImplPtr, "&myImpl{\\}")
}
func _() {
var s struct{ i []int } //@item(litSliceField, "i", "[]int", "field")
var foo []int
// no literal completions after selector
foo = s.i //@complete(" //", litSliceField)
}
func _() {
type myStruct struct{ i int } //@item(litMyStructType, "myStruct", "struct{...}", "struct")
myStruct{} //@item(litMyStruct, "myStruct{}", "", "var")
foo := func(s string, args ...myStruct) {}
// Don't give literal slice candidate for variadic arg.
// Do give literal candidates for variadic element.
foo("", myStruct) //@complete(")", litMyStruct, litMyStructType)
}
func _() {
Buffer{} //@item(litBuffer, "Buffer{}", "", "var")
var b *bytes.Buffer
b = bytes.Bu //@snippet(" //", litBuffer, "Buffer{\\}")
}
func _() {
_ = "func(...) {}" //@item(litFunc, "func(...) {}", "", "var")
// no literal "func" completions
http.Handle("", fun) //@complete(")")
var namedReturn func(s string) (b bool)
namedReturn = f //@snippet(" //", litFunc, "func(s string) (b bool) {$0\\}")
var multiReturn func() (bool, int)
multiReturn = f //@snippet(" //", litFunc, "func() (bool, int) {$0\\}")
var multiNamedReturn func() (b bool, i int)
multiNamedReturn = f //@snippet(" //", litFunc, "func() (b bool, i int) {$0\\}")
var duplicateParams func(myImpl, int, myImpl)
duplicateParams = f //@snippet(" //", litFunc, "func(mi1 myImpl, i int, mi2 myImpl) {$0\\}")
type aliasImpl = myImpl
var aliasParams func(aliasImpl) aliasImpl
aliasParams = f //@snippet(" //", litFunc, "func(ai aliasImpl) aliasImpl {$0\\}")
const two = 2
var builtinTypes func([]int, [two]bool, map[string]string, struct{ i int }, interface{ foo() }, <-chan int)
builtinTypes = f //@snippet(" //", litFunc, "func(i1 []int, b [two]bool, m map[string]string, s struct{ i int \\}, i2 interface{ foo() \\}, c <-chan int) {$0\\}")
var _ func(ast.Node) = f //@snippet(" //", litFunc, "func(n ast.Node) {$0\\}")
var _ func(error) = f //@snippet(" //", litFunc, "func(err error) {$0\\}")
var _ func(context.Context) = f //@snippet(" //", litFunc, "func(ctx context.Context) {$0\\}")
type context struct {}
var _ func(context) = f //@snippet(" //", litFunc, "func(ctx context) {$0\\}")
}
func _() {
float64() //@item(litFloat64, "float64()", "float64", "var")
// don't complete to "&float64()"
var _ *float64 = float64 //@complete(" //")
var f float64
f = fl //@complete(" //", litFloat64),snippet(" //", litFloat64, "float64($0)")
type myInt int
myInt() //@item(litMyInt, "myInt()", "", "var")
var mi myInt
mi = my //@snippet(" //", litMyInt, "myInt($0)")
}
func _() {
type ptrStruct struct {
p *ptrStruct
}
ptrStruct{} //@item(litPtrStruct, "ptrStruct{}", "", "var")
ptrStruct{
p: &ptrSt, //@rank(",", litPtrStruct)
}
&ptrStruct{} //@item(litPtrStructPtr, "&ptrStruct{}", "", "var")
&ptrStruct{
p: ptrSt, //@rank(",", litPtrStructPtr)
}
}
func _() {
f := func(...[]int) {}
f() //@snippet(")", litIntSlice, "[]int{$0\\}")
}
func _() {
// don't complete to "untyped int()"
[]int{}[untyped] //@complete("] //")
}
type Tree[T any] struct{}
func (tree Tree[T]) Do(f func(s T)) {}
func _() {
var t Tree[string]
t.Do(fun) //@complete(")", litFunc), snippet(")", litFunc, "func(s string) {$0\\}")
}
|