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