golang.org/x/tools/gopls@v0.15.3/internal/test/marker/testdata/codeaction/functionextraction.txt (about) 1 This test verifies various behaviors of function extraction. 2 3 -- go.mod -- 4 module mod.test/extract 5 6 go 1.18 7 8 -- basic.go -- 9 package extract 10 11 func _() { //@codeaction("{", closeBracket, "refactor.extract", outer) 12 a := 1 //@codeaction("a", end, "refactor.extract", inner) 13 _ = a + 4 //@loc(end, "4") 14 } //@loc(closeBracket, "}") 15 16 -- @inner/basic.go -- 17 package extract 18 19 func _() { //@codeaction("{", closeBracket, "refactor.extract", outer) 20 //@codeaction("a", end, "refactor.extract", inner) 21 newFunction() //@loc(end, "4") 22 } 23 24 func newFunction() { 25 a := 1 26 _ = a + 4 27 } //@loc(closeBracket, "}") 28 29 -- @outer/basic.go -- 30 package extract 31 32 func _() { //@codeaction("{", closeBracket, "refactor.extract", outer) 33 //@codeaction("a", end, "refactor.extract", inner) 34 newFunction() //@loc(end, "4") 35 } 36 37 func newFunction() { 38 a := 1 39 _ = a + 4 40 } //@loc(closeBracket, "}") 41 42 -- return.go -- 43 package extract 44 45 func _() bool { 46 x := 1 47 if x == 0 { //@codeaction("if", ifend, "refactor.extract", return) 48 return true 49 } //@loc(ifend, "}") 50 return false 51 } 52 53 -- @return/return.go -- 54 package extract 55 56 func _() bool { 57 x := 1 58 //@codeaction("if", ifend, "refactor.extract", return) 59 shouldReturn, returnValue := newFunction(x) 60 if shouldReturn { 61 return returnValue 62 } //@loc(ifend, "}") 63 return false 64 } 65 66 func newFunction(x int) (bool, bool) { 67 if x == 0 { 68 return true, true 69 } 70 return false, false 71 } 72 73 -- return_nonnested.go -- 74 package extract 75 76 func _() bool { 77 x := 1 //@codeaction("x", rnnEnd, "refactor.extract", rnn) 78 if x == 0 { 79 return true 80 } 81 return false //@loc(rnnEnd, "false") 82 } 83 84 -- @rnn/return_nonnested.go -- 85 package extract 86 87 func _() bool { 88 //@codeaction("x", rnnEnd, "refactor.extract", rnn) 89 return newFunction() //@loc(rnnEnd, "false") 90 } 91 92 func newFunction() bool { 93 x := 1 94 if x == 0 { 95 return true 96 } 97 return false 98 } 99 100 -- return_complex.go -- 101 package extract 102 103 import "fmt" 104 105 func _() (int, string, error) { 106 x := 1 107 y := "hello" 108 z := "bye" //@codeaction("z", rcEnd, "refactor.extract", rc) 109 if y == z { 110 return x, y, fmt.Errorf("same") 111 } else if false { 112 z = "hi" 113 return x, z, nil 114 } //@loc(rcEnd, "}") 115 return x, z, nil 116 } 117 118 -- @rc/return_complex.go -- 119 package extract 120 121 import "fmt" 122 123 func _() (int, string, error) { 124 x := 1 125 y := "hello" 126 //@codeaction("z", rcEnd, "refactor.extract", rc) 127 z, shouldReturn, returnValue, returnValue1, returnValue2 := newFunction(y, x) 128 if shouldReturn { 129 return returnValue, returnValue1, returnValue2 130 } //@loc(rcEnd, "}") 131 return x, z, nil 132 } 133 134 func newFunction(y string, x int) (string, bool, int, string, error) { 135 z := "bye" 136 if y == z { 137 return "", true, x, y, fmt.Errorf("same") 138 } else if false { 139 z = "hi" 140 return "", true, x, z, nil 141 } 142 return z, false, 0, "", nil 143 } 144 145 -- return_complex_nonnested.go -- 146 package extract 147 148 import "fmt" 149 150 func _() (int, string, error) { 151 x := 1 152 y := "hello" 153 z := "bye" //@codeaction("z", rcnnEnd, "refactor.extract", rcnn) 154 if y == z { 155 return x, y, fmt.Errorf("same") 156 } else if false { 157 z = "hi" 158 return x, z, nil 159 } 160 return x, z, nil //@loc(rcnnEnd, "nil") 161 } 162 163 -- @rcnn/return_complex_nonnested.go -- 164 package extract 165 166 import "fmt" 167 168 func _() (int, string, error) { 169 x := 1 170 y := "hello" 171 //@codeaction("z", rcnnEnd, "refactor.extract", rcnn) 172 return newFunction(y, x) //@loc(rcnnEnd, "nil") 173 } 174 175 func newFunction(y string, x int) (int, string, error) { 176 z := "bye" 177 if y == z { 178 return x, y, fmt.Errorf("same") 179 } else if false { 180 z = "hi" 181 return x, z, nil 182 } 183 return x, z, nil 184 } 185 186 -- return_func_lit.go -- 187 package extract 188 189 import "go/ast" 190 191 func _() { 192 ast.Inspect(ast.NewIdent("a"), func(n ast.Node) bool { 193 if n == nil { //@codeaction("if", rflEnd, "refactor.extract", rfl) 194 return true 195 } //@loc(rflEnd, "}") 196 return false 197 }) 198 } 199 200 -- @rfl/return_func_lit.go -- 201 package extract 202 203 import "go/ast" 204 205 func _() { 206 ast.Inspect(ast.NewIdent("a"), func(n ast.Node) bool { 207 //@codeaction("if", rflEnd, "refactor.extract", rfl) 208 shouldReturn, returnValue := newFunction(n) 209 if shouldReturn { 210 return returnValue 211 } //@loc(rflEnd, "}") 212 return false 213 }) 214 } 215 216 func newFunction(n ast.Node) (bool, bool) { 217 if n == nil { 218 return true, true 219 } 220 return false, false 221 } 222 223 -- return_func_lit_nonnested.go -- 224 package extract 225 226 import "go/ast" 227 228 func _() { 229 ast.Inspect(ast.NewIdent("a"), func(n ast.Node) bool { 230 if n == nil { //@codeaction("if", rflnnEnd, "refactor.extract", rflnn) 231 return true 232 } 233 return false //@loc(rflnnEnd, "false") 234 }) 235 } 236 237 -- @rflnn/return_func_lit_nonnested.go -- 238 package extract 239 240 import "go/ast" 241 242 func _() { 243 ast.Inspect(ast.NewIdent("a"), func(n ast.Node) bool { 244 //@codeaction("if", rflnnEnd, "refactor.extract", rflnn) 245 return newFunction(n) //@loc(rflnnEnd, "false") 246 }) 247 } 248 249 func newFunction(n ast.Node) bool { 250 if n == nil { 251 return true 252 } 253 return false 254 } 255 256 -- return_init.go -- 257 package extract 258 259 func _() string { 260 x := 1 261 if x == 0 { //@codeaction("if", riEnd, "refactor.extract", ri) 262 x = 3 263 return "a" 264 } //@loc(riEnd, "}") 265 x = 2 266 return "b" 267 } 268 269 -- @ri/return_init.go -- 270 package extract 271 272 func _() string { 273 x := 1 274 //@codeaction("if", riEnd, "refactor.extract", ri) 275 shouldReturn, returnValue := newFunction(x) 276 if shouldReturn { 277 return returnValue 278 } //@loc(riEnd, "}") 279 x = 2 280 return "b" 281 } 282 283 func newFunction(x int) (bool, string) { 284 if x == 0 { 285 x = 3 286 return true, "a" 287 } 288 return false, "" 289 } 290 291 -- return_init_nonnested.go -- 292 package extract 293 294 func _() string { 295 x := 1 296 if x == 0 { //@codeaction("if", rinnEnd, "refactor.extract", rinn) 297 x = 3 298 return "a" 299 } 300 x = 2 301 return "b" //@loc(rinnEnd, "\"b\"") 302 } 303 304 -- @rinn/return_init_nonnested.go -- 305 package extract 306 307 func _() string { 308 x := 1 309 //@codeaction("if", rinnEnd, "refactor.extract", rinn) 310 return newFunction(x) //@loc(rinnEnd, "\"b\"") 311 } 312 313 func newFunction(x int) string { 314 if x == 0 { 315 x = 3 316 return "a" 317 } 318 x = 2 319 return "b" 320 } 321 322 -- args_returns.go -- 323 package extract 324 325 func _() { 326 a := 1 327 a = 5 //@codeaction("a", araend, "refactor.extract", ara) 328 a = a + 2 //@loc(araend, "2") 329 330 b := a * 2 //@codeaction("b", arbend, "refactor.extract", arb) 331 _ = b + 4 //@loc(arbend, "4") 332 } 333 334 -- @ara/args_returns.go -- 335 package extract 336 337 func _() { 338 a := 1 339 //@codeaction("a", araend, "refactor.extract", ara) 340 a = newFunction(a) //@loc(araend, "2") 341 342 b := a * 2 //@codeaction("b", arbend, "refactor.extract", arb) 343 _ = b + 4 //@loc(arbend, "4") 344 } 345 346 func newFunction(a int) int { 347 a = 5 348 a = a + 2 349 return a 350 } 351 352 -- @arb/args_returns.go -- 353 package extract 354 355 func _() { 356 a := 1 357 a = 5 //@codeaction("a", araend, "refactor.extract", ara) 358 a = a + 2 //@loc(araend, "2") 359 360 //@codeaction("b", arbend, "refactor.extract", arb) 361 newFunction(a) //@loc(arbend, "4") 362 } 363 364 func newFunction(a int) { 365 b := a * 2 366 _ = b + 4 367 } 368 369 -- scope.go -- 370 package extract 371 372 func _() { 373 newFunction := 1 374 a := newFunction //@codeaction("a", "newFunction", "refactor.extract", scope) 375 _ = a // avoid diagnostic 376 } 377 378 func newFunction1() int { 379 return 1 380 } 381 382 -- @scope/scope.go -- 383 package extract 384 385 func _() { 386 newFunction := 1 387 a := newFunction2(newFunction) //@codeaction("a", "newFunction", "refactor.extract", scope) 388 _ = a // avoid diagnostic 389 } 390 391 func newFunction2(newFunction int) int { 392 a := newFunction 393 return a 394 } 395 396 func newFunction1() int { 397 return 1 398 } 399 400 -- smart_initialization.go -- 401 package extract 402 403 func _() { 404 var a []int 405 a = append(a, 2) //@codeaction("a", siEnd, "refactor.extract", si) 406 b := 4 //@loc(siEnd, "4") 407 a = append(a, b) 408 } 409 410 -- @si/smart_initialization.go -- 411 package extract 412 413 func _() { 414 var a []int 415 //@codeaction("a", siEnd, "refactor.extract", si) 416 a, b := newFunction(a) //@loc(siEnd, "4") 417 a = append(a, b) 418 } 419 420 func newFunction(a []int) ([]int, int) { 421 a = append(a, 2) 422 b := 4 423 return a, b 424 } 425 426 -- smart_return.go -- 427 package extract 428 429 func _() { 430 var b []int 431 var a int 432 a = 2 //@codeaction("a", srEnd, "refactor.extract", sr) 433 b = []int{} 434 b = append(b, a) //@loc(srEnd, ")") 435 b[0] = 1 436 } 437 438 -- @sr/smart_return.go -- 439 package extract 440 441 func _() { 442 var b []int 443 var a int 444 //@codeaction("a", srEnd, "refactor.extract", sr) 445 b = newFunction(a, b) //@loc(srEnd, ")") 446 b[0] = 1 447 } 448 449 func newFunction(a int, b []int) []int { 450 a = 2 451 b = []int{} 452 b = append(b, a) 453 return b 454 } 455 456 -- unnecessary_param.go -- 457 package extract 458 459 func _() { 460 var b []int 461 a := 2 //@codeaction("a", upEnd, "refactor.extract", up) 462 b = []int{} 463 b = append(b, a) //@loc(upEnd, ")") 464 b[0] = 1 465 if a == 2 { 466 return 467 } 468 } 469 470 -- @up/unnecessary_param.go -- 471 package extract 472 473 func _() { 474 var b []int 475 //@codeaction("a", upEnd, "refactor.extract", up) 476 a, b := newFunction(b) //@loc(upEnd, ")") 477 b[0] = 1 478 if a == 2 { 479 return 480 } 481 } 482 483 func newFunction(b []int) (int, []int) { 484 a := 2 485 b = []int{} 486 b = append(b, a) 487 return a, b 488 } 489 490 -- comment.go -- 491 package extract 492 493 func _() { 494 a := /* comment in the middle of a line */ 1 //@codeaction("a", commentEnd, "refactor.extract", comment1) 495 // Comment on its own line //@codeaction("Comment", commentEnd, "refactor.extract", comment2) 496 _ = a + 4 //@loc(commentEnd, "4"),codeaction("_", lastComment, "refactor.extract", comment3) 497 // Comment right after 3 + 4 498 499 // Comment after with space //@loc(lastComment, "Comment") 500 } 501 502 -- @comment1/comment.go -- 503 package extract 504 505 func _() { 506 /* comment in the middle of a line */ 507 //@codeaction("a", commentEnd, "refactor.extract", comment1) 508 // Comment on its own line //@codeaction("Comment", commentEnd, "refactor.extract", comment2) 509 newFunction() //@loc(commentEnd, "4"),codeaction("_", lastComment, "refactor.extract", comment3) 510 // Comment right after 3 + 4 511 512 // Comment after with space //@loc(lastComment, "Comment") 513 } 514 515 func newFunction() { 516 a := 1 517 518 _ = a + 4 519 } 520 521 -- @comment2/comment.go -- 522 package extract 523 524 func _() { 525 a := /* comment in the middle of a line */ 1 //@codeaction("a", commentEnd, "refactor.extract", comment1) 526 // Comment on its own line //@codeaction("Comment", commentEnd, "refactor.extract", comment2) 527 newFunction(a) //@loc(commentEnd, "4"),codeaction("_", lastComment, "refactor.extract", comment3) 528 // Comment right after 3 + 4 529 530 // Comment after with space //@loc(lastComment, "Comment") 531 } 532 533 func newFunction(a int) { 534 _ = a + 4 535 } 536 537 -- @comment3/comment.go -- 538 package extract 539 540 func _() { 541 a := /* comment in the middle of a line */ 1 //@codeaction("a", commentEnd, "refactor.extract", comment1) 542 // Comment on its own line //@codeaction("Comment", commentEnd, "refactor.extract", comment2) 543 newFunction(a) //@loc(commentEnd, "4"),codeaction("_", lastComment, "refactor.extract", comment3) 544 // Comment right after 3 + 4 545 546 // Comment after with space //@loc(lastComment, "Comment") 547 } 548 549 func newFunction(a int) { 550 _ = a + 4 551 } 552 553 -- redefine.go -- 554 package extract 555 556 import "strconv" 557 558 func _() { 559 i, err := strconv.Atoi("1") 560 u, err := strconv.Atoi("2") //@codeaction("u", ")", "refactor.extract", redefine) 561 if i == u || err == nil { 562 return 563 } 564 } 565 566 -- @redefine/redefine.go -- 567 package extract 568 569 import "strconv" 570 571 func _() { 572 i, err := strconv.Atoi("1") 573 u, err := newFunction() //@codeaction("u", ")", "refactor.extract", redefine) 574 if i == u || err == nil { 575 return 576 } 577 } 578 579 func newFunction() (int, error) { 580 u, err := strconv.Atoi("2") 581 return u, err 582 } 583