-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/lsp: add support for extracting non-nested returns
If there is a return statement that is guaranteed to execute in the selection to extract to function, then the result of calling the extracted function can be directly returned. Updates golang/go#37170 Change-Id: I6454e4107d670e4a1bc9048b2e1073fc80fc78ab Reviewed-on: https://go-review.googlesource.com/c/tools/+/312469 Trust: Suzy Mueller <[email protected]> Run-TryBot: Suzy Mueller <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]>
- Loading branch information
Showing
11 changed files
with
206 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
internal/lsp/testdata/extract/extract_function/extract_return_basic_nonnested.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package extract | ||
|
||
func _() bool { | ||
x := 1 //@mark(exSt13, "x") | ||
if x == 0 { | ||
return true | ||
} | ||
return false //@mark(exEn13, "false") | ||
//@extractfunc(exSt13, exEn13) | ||
} |
16 changes: 16 additions & 0 deletions
16
internal/lsp/testdata/extract/extract_function/extract_return_basic_nonnested.go.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
-- functionextraction_extract_return_basic_nonnested_4_2 -- | ||
package extract | ||
|
||
func _() bool { | ||
return fn0() //@mark(exEn13, "false") | ||
//@extractfunc(exSt13, exEn13) | ||
} | ||
|
||
func fn0() bool { | ||
x := 1 | ||
if x == 0 { | ||
return true | ||
} | ||
return false | ||
} | ||
|
17 changes: 17 additions & 0 deletions
17
internal/lsp/testdata/extract/extract_function/extract_return_complex_nonnested.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package extract | ||
|
||
import "fmt" | ||
|
||
func _() (int, string, error) { | ||
x := 1 | ||
y := "hello" | ||
z := "bye" //@mark(exSt10, "z") | ||
if y == z { | ||
return x, y, fmt.Errorf("same") | ||
} else { | ||
z = "hi" | ||
return x, z, nil | ||
} | ||
return x, z, nil //@mark(exEn10, "nil") | ||
//@extractfunc(exSt10, exEn10) | ||
} |
23 changes: 23 additions & 0 deletions
23
internal/lsp/testdata/extract/extract_function/extract_return_complex_nonnested.go.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
-- functionextraction_extract_return_complex_nonnested_8_2 -- | ||
package extract | ||
|
||
import "fmt" | ||
|
||
func _() (int, string, error) { | ||
x := 1 | ||
y := "hello" | ||
return fn0(y, x) //@mark(exEn10, "nil") | ||
//@extractfunc(exSt10, exEn10) | ||
} | ||
|
||
func fn0(y string, x int) (int, string, error) { | ||
z := "bye" | ||
if y == z { | ||
return x, y, fmt.Errorf("same") | ||
} else { | ||
z = "hi" | ||
return x, z, nil | ||
} | ||
return x, z, nil | ||
} | ||
|
13 changes: 13 additions & 0 deletions
13
internal/lsp/testdata/extract/extract_function/extract_return_func_lit_nonnested.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package extract | ||
|
||
import "go/ast" | ||
|
||
func _() { | ||
ast.Inspect(ast.NewIdent("a"), func(n ast.Node) bool { | ||
if n == nil { //@mark(exSt11, "if") | ||
return true | ||
} | ||
return false //@mark(exEn11, "false") | ||
}) | ||
//@extractfunc(exSt11, exEn11) | ||
} |
19 changes: 19 additions & 0 deletions
19
internal/lsp/testdata/extract/extract_function/extract_return_func_lit_nonnested.go.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
-- functionextraction_extract_return_func_lit_nonnested_7_3 -- | ||
package extract | ||
|
||
import "go/ast" | ||
|
||
func _() { | ||
ast.Inspect(ast.NewIdent("a"), func(n ast.Node) bool { | ||
return fn0(n) //@mark(exEn11, "false") | ||
}) | ||
//@extractfunc(exSt11, exEn11) | ||
} | ||
|
||
func fn0(n ast.Node) bool { | ||
if n == nil { | ||
return true | ||
} | ||
return false | ||
} | ||
|
12 changes: 12 additions & 0 deletions
12
internal/lsp/testdata/extract/extract_function/extract_return_init_nonnested.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package extract | ||
|
||
func _() string { | ||
x := 1 | ||
if x == 0 { //@mark(exSt12, "if") | ||
x = 3 | ||
return "a" | ||
} | ||
x = 2 | ||
return "b" //@mark(exEn12, "\"b\"") | ||
//@extractfunc(exSt12, exEn12) | ||
} |
18 changes: 18 additions & 0 deletions
18
internal/lsp/testdata/extract/extract_function/extract_return_init_nonnested.go.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
-- functionextraction_extract_return_init_nonnested_5_2 -- | ||
package extract | ||
|
||
func _() string { | ||
x := 1 | ||
return fn0(x) //@mark(exEn12, "\"b\"") | ||
//@extractfunc(exSt12, exEn12) | ||
} | ||
|
||
func fn0(x int) string { | ||
if x == 0 { | ||
x = 3 | ||
return "a" | ||
} | ||
x = 2 | ||
return "b" | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters