aboutsummaryrefslogtreecommitdiff
path: root/gopls/internal/lsp/testdata/extract/extract_function
diff options
context:
space:
mode:
Diffstat (limited to 'gopls/internal/lsp/testdata/extract/extract_function')
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_args_returns.go11
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_args_returns.go.golden37
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_basic.go8
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_basic.go.golden30
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_basic_comment.go12
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_basic_comment.go.golden57
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_issue_44813.go13
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_issue_44813.go.golden21
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_redefine.go11
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_redefine.go.golden18
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_return_basic.go10
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_return_basic.go.golden21
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_return_basic_nonnested.go10
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_return_basic_nonnested.go.golden17
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_return_complex.go17
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_return_complex.go.golden28
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_return_complex_nonnested.go17
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_return_complex_nonnested.go.golden24
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_return_func_lit.go13
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_return_func_lit.go.golden24
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_return_func_lit_nonnested.go13
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_return_func_lit_nonnested.go.golden20
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_return_init.go12
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_return_init.go.golden23
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_return_init_nonnested.go12
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_return_init_nonnested.go.golden19
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_scope.go10
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_scope.go.golden16
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_smart_initialization.go9
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_smart_initialization.go.golden17
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_smart_return.go11
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_smart_return.go.golden19
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_unnecessary_param.go14
-rw-r--r--gopls/internal/lsp/testdata/extract/extract_function/extract_unnecessary_param.go.golden22
34 files changed, 616 insertions, 0 deletions
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_args_returns.go b/gopls/internal/lsp/testdata/extract/extract_function/extract_args_returns.go
new file mode 100644
index 000000000..63d24df00
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_args_returns.go
@@ -0,0 +1,11 @@
+package extract
+
+func _() {
+ a := 1
+ a = 5 //@mark(exSt0, "a")
+ a = a + 2 //@mark(exEn0, "2")
+ //@extractfunc(exSt0, exEn0)
+ b := a * 2 //@mark(exB, " b")
+ _ = 3 + 4 //@mark(exEnd, "4")
+ //@extractfunc(exB, exEnd)
+}
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_args_returns.go.golden b/gopls/internal/lsp/testdata/extract/extract_function/extract_args_returns.go.golden
new file mode 100644
index 000000000..b15345e23
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_args_returns.go.golden
@@ -0,0 +1,37 @@
+-- functionextraction_extract_args_returns_5_2 --
+package extract
+
+func _() {
+ a := 1
+ //@mark(exSt0, "a")
+ a = newFunction(a) //@mark(exEn0, "2")
+ //@extractfunc(exSt0, exEn0)
+ b := a * 2 //@mark(exB, " b")
+ _ = 3 + 4 //@mark(exEnd, "4")
+ //@extractfunc(exB, exEnd)
+}
+
+func newFunction(a int) int {
+ a = 5
+ a = a + 2
+ return a
+}
+
+-- functionextraction_extract_args_returns_8_1 --
+package extract
+
+func _() {
+ a := 1
+ a = 5 //@mark(exSt0, "a")
+ a = a + 2 //@mark(exEn0, "2")
+ //@extractfunc(exSt0, exEn0)
+ //@mark(exB, " b")
+ newFunction(a) //@mark(exEnd, "4")
+ //@extractfunc(exB, exEnd)
+}
+
+func newFunction(a int) {
+ b := a * 2
+ _ = 3 + 4
+}
+
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_basic.go b/gopls/internal/lsp/testdata/extract/extract_function/extract_basic.go
new file mode 100644
index 000000000..5e44de26f
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_basic.go
@@ -0,0 +1,8 @@
+package extract
+
+func _() { //@mark(exSt25, "{")
+ a := 1 //@mark(exSt1, "a")
+ _ = 3 + 4 //@mark(exEn1, "4")
+ //@extractfunc(exSt1, exEn1)
+ //@extractfunc(exSt25, exEn25)
+} //@mark(exEn25, "}")
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_basic.go.golden b/gopls/internal/lsp/testdata/extract/extract_function/extract_basic.go.golden
new file mode 100644
index 000000000..18adc4db4
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_basic.go.golden
@@ -0,0 +1,30 @@
+-- functionextraction_extract_basic_3_10 --
+package extract
+
+func _() { //@mark(exSt25, "{")
+ //@mark(exSt1, "a")
+ newFunction() //@mark(exEn1, "4")
+ //@extractfunc(exSt1, exEn1)
+ //@extractfunc(exSt25, exEn25)
+}
+
+func newFunction() {
+ a := 1
+ _ = 3 + 4
+} //@mark(exEn25, "}")
+
+-- functionextraction_extract_basic_4_2 --
+package extract
+
+func _() { //@mark(exSt25, "{")
+ //@mark(exSt1, "a")
+ newFunction() //@mark(exEn1, "4")
+ //@extractfunc(exSt1, exEn1)
+ //@extractfunc(exSt25, exEn25)
+}
+
+func newFunction() {
+ a := 1
+ _ = 3 + 4
+} //@mark(exEn25, "}")
+
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_basic_comment.go b/gopls/internal/lsp/testdata/extract/extract_function/extract_basic_comment.go
new file mode 100644
index 000000000..71f969e48
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_basic_comment.go
@@ -0,0 +1,12 @@
+package extract
+
+func _() {
+ a := /* comment in the middle of a line */ 1 //@mark(exSt18, "a")
+ // Comment on its own line //@mark(exSt19, "Comment")
+ _ = 3 + 4 //@mark(exEn18, "4"),mark(exEn19, "4"),mark(exSt20, "_")
+ // Comment right after 3 + 4
+
+ // Comment after with space //@mark(exEn20, "Comment")
+
+ //@extractfunc(exSt18, exEn18),extractfunc(exSt19, exEn19),extractfunc(exSt20, exEn20)
+}
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_basic_comment.go.golden b/gopls/internal/lsp/testdata/extract/extract_function/extract_basic_comment.go.golden
new file mode 100644
index 000000000..1b2869ef7
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_basic_comment.go.golden
@@ -0,0 +1,57 @@
+-- functionextraction_extract_basic_comment_4_2 --
+package extract
+
+func _() {
+ /* comment in the middle of a line */
+ //@mark(exSt18, "a")
+ // Comment on its own line //@mark(exSt19, "Comment")
+ newFunction() //@mark(exEn18, "4"),mark(exEn19, "4"),mark(exSt20, "_")
+ // Comment right after 3 + 4
+
+ // Comment after with space //@mark(exEn20, "Comment")
+
+ //@extractfunc(exSt18, exEn18),extractfunc(exSt19, exEn19),extractfunc(exSt20, exEn20)
+}
+
+func newFunction() {
+ a := 1
+
+ _ = 3 + 4
+}
+
+-- functionextraction_extract_basic_comment_5_5 --
+package extract
+
+func _() {
+ a := /* comment in the middle of a line */ 1 //@mark(exSt18, "a")
+ // Comment on its own line //@mark(exSt19, "Comment")
+ newFunction() //@mark(exEn18, "4"),mark(exEn19, "4"),mark(exSt20, "_")
+ // Comment right after 3 + 4
+
+ // Comment after with space //@mark(exEn20, "Comment")
+
+ //@extractfunc(exSt18, exEn18),extractfunc(exSt19, exEn19),extractfunc(exSt20, exEn20)
+}
+
+func newFunction() {
+ _ = 3 + 4
+}
+
+-- functionextraction_extract_basic_comment_6_2 --
+package extract
+
+func _() {
+ a := /* comment in the middle of a line */ 1 //@mark(exSt18, "a")
+ // Comment on its own line //@mark(exSt19, "Comment")
+ newFunction() //@mark(exEn18, "4"),mark(exEn19, "4"),mark(exSt20, "_")
+ // Comment right after 3 + 4
+
+ // Comment after with space //@mark(exEn20, "Comment")
+
+ //@extractfunc(exSt18, exEn18),extractfunc(exSt19, exEn19),extractfunc(exSt20, exEn20)
+}
+
+func newFunction() {
+ _ = 3 + 4
+}
+
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_issue_44813.go b/gopls/internal/lsp/testdata/extract/extract_function/extract_issue_44813.go
new file mode 100644
index 000000000..9713b9101
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_issue_44813.go
@@ -0,0 +1,13 @@
+package extract
+
+import "fmt"
+
+func main() {
+ x := []rune{} //@mark(exSt9, "x")
+ s := "HELLO"
+ for _, c := range s {
+ x = append(x, c)
+ } //@mark(exEn9, "}")
+ //@extractfunc(exSt9, exEn9)
+ fmt.Printf("%x\n", x)
+}
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_issue_44813.go.golden b/gopls/internal/lsp/testdata/extract/extract_function/extract_issue_44813.go.golden
new file mode 100644
index 000000000..3198c9fa2
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_issue_44813.go.golden
@@ -0,0 +1,21 @@
+-- functionextraction_extract_issue_44813_6_2 --
+package extract
+
+import "fmt"
+
+func main() {
+ //@mark(exSt9, "x")
+ x := newFunction() //@mark(exEn9, "}")
+ //@extractfunc(exSt9, exEn9)
+ fmt.Printf("%x\n", x)
+}
+
+func newFunction() []rune {
+ x := []rune{}
+ s := "HELLO"
+ for _, c := range s {
+ x = append(x, c)
+ }
+ return x
+}
+
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_redefine.go b/gopls/internal/lsp/testdata/extract/extract_function/extract_redefine.go
new file mode 100644
index 000000000..604f4757c
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_redefine.go
@@ -0,0 +1,11 @@
+package extract
+
+import "strconv"
+
+func _() {
+ i, err := strconv.Atoi("1")
+ u, err := strconv.Atoi("2") //@extractfunc("u", ")")
+ if i == u || err == nil {
+ return
+ }
+}
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_redefine.go.golden b/gopls/internal/lsp/testdata/extract/extract_function/extract_redefine.go.golden
new file mode 100644
index 000000000..e2ee217d1
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_redefine.go.golden
@@ -0,0 +1,18 @@
+-- functionextraction_extract_redefine_7_2 --
+package extract
+
+import "strconv"
+
+func _() {
+ i, err := strconv.Atoi("1")
+ u, err := newFunction() //@extractfunc("u", ")")
+ if i == u || err == nil {
+ return
+ }
+}
+
+func newFunction() (int, error) {
+ u, err := strconv.Atoi("2")
+ return u, err
+}
+
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_return_basic.go b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_basic.go
new file mode 100644
index 000000000..1ff24daeb
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_basic.go
@@ -0,0 +1,10 @@
+package extract
+
+func _() bool {
+ x := 1
+ if x == 0 { //@mark(exSt2, "if")
+ return true
+ } //@mark(exEn2, "}")
+ return false
+ //@extractfunc(exSt2, exEn2)
+}
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_return_basic.go.golden b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_basic.go.golden
new file mode 100644
index 000000000..6103d1ee9
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_basic.go.golden
@@ -0,0 +1,21 @@
+-- functionextraction_extract_return_basic_5_2 --
+package extract
+
+func _() bool {
+ x := 1
+ //@mark(exSt2, "if")
+ shouldReturn, returnValue := newFunction(x)
+ if shouldReturn {
+ return returnValue
+ } //@mark(exEn2, "}")
+ return false
+ //@extractfunc(exSt2, exEn2)
+}
+
+func newFunction(x int) (bool, bool) {
+ if x == 0 {
+ return true, true
+ }
+ return false, false
+}
+
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_return_basic_nonnested.go b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_basic_nonnested.go
new file mode 100644
index 000000000..08573acdd
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_basic_nonnested.go
@@ -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)
+}
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_return_basic_nonnested.go.golden b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_basic_nonnested.go.golden
new file mode 100644
index 000000000..19e48da01
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_basic_nonnested.go.golden
@@ -0,0 +1,17 @@
+-- functionextraction_extract_return_basic_nonnested_4_2 --
+package extract
+
+func _() bool {
+ //@mark(exSt13, "x")
+ return newFunction() //@mark(exEn13, "false")
+ //@extractfunc(exSt13, exEn13)
+}
+
+func newFunction() bool {
+ x := 1
+ if x == 0 {
+ return true
+ }
+ return false
+}
+
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_return_complex.go b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_complex.go
new file mode 100644
index 000000000..605c5ec2e
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_complex.go
@@ -0,0 +1,17 @@
+package extract
+
+import "fmt"
+
+func _() (int, string, error) {
+ x := 1
+ y := "hello"
+ z := "bye" //@mark(exSt3, "z")
+ if y == z {
+ return x, y, fmt.Errorf("same")
+ } else {
+ z = "hi"
+ return x, z, nil
+ } //@mark(exEn3, "}")
+ return x, z, nil
+ //@extractfunc(exSt3, exEn3)
+}
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_return_complex.go.golden b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_complex.go.golden
new file mode 100644
index 000000000..4d201227a
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_complex.go.golden
@@ -0,0 +1,28 @@
+-- functionextraction_extract_return_complex_8_2 --
+package extract
+
+import "fmt"
+
+func _() (int, string, error) {
+ x := 1
+ y := "hello"
+ //@mark(exSt3, "z")
+ z, shouldReturn, returnValue, returnValue1, returnValue2 := newFunction(y, x)
+ if shouldReturn {
+ return returnValue, returnValue1, returnValue2
+ } //@mark(exEn3, "}")
+ return x, z, nil
+ //@extractfunc(exSt3, exEn3)
+}
+
+func newFunction(y string, x int) (string, bool, int, string, error) {
+ z := "bye"
+ if y == z {
+ return "", true, x, y, fmt.Errorf("same")
+ } else {
+ z = "hi"
+ return "", true, x, z, nil
+ }
+ return z, false, 0, "", nil
+}
+
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_return_complex_nonnested.go b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_complex_nonnested.go
new file mode 100644
index 000000000..6b2a4d8c0
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_complex_nonnested.go
@@ -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)
+}
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_return_complex_nonnested.go.golden b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_complex_nonnested.go.golden
new file mode 100644
index 000000000..de54b1534
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_complex_nonnested.go.golden
@@ -0,0 +1,24 @@
+-- functionextraction_extract_return_complex_nonnested_8_2 --
+package extract
+
+import "fmt"
+
+func _() (int, string, error) {
+ x := 1
+ y := "hello"
+ //@mark(exSt10, "z")
+ return newFunction(y, x) //@mark(exEn10, "nil")
+ //@extractfunc(exSt10, exEn10)
+}
+
+func newFunction(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
+}
+
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_return_func_lit.go b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_func_lit.go
new file mode 100644
index 000000000..b3fb4fd21
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_func_lit.go
@@ -0,0 +1,13 @@
+package extract
+
+import "go/ast"
+
+func _() {
+ ast.Inspect(ast.NewIdent("a"), func(n ast.Node) bool {
+ if n == nil { //@mark(exSt4, "if")
+ return true
+ } //@mark(exEn4, "}")
+ return false
+ })
+ //@extractfunc(exSt4, exEn4)
+}
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_return_func_lit.go.golden b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_func_lit.go.golden
new file mode 100644
index 000000000..3af747c22
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_func_lit.go.golden
@@ -0,0 +1,24 @@
+-- functionextraction_extract_return_func_lit_7_3 --
+package extract
+
+import "go/ast"
+
+func _() {
+ ast.Inspect(ast.NewIdent("a"), func(n ast.Node) bool {
+ //@mark(exSt4, "if")
+ shouldReturn, returnValue := newFunction(n)
+ if shouldReturn {
+ return returnValue
+ } //@mark(exEn4, "}")
+ return false
+ })
+ //@extractfunc(exSt4, exEn4)
+}
+
+func newFunction(n ast.Node) (bool, bool) {
+ if n == nil {
+ return true, true
+ }
+ return false, false
+}
+
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_return_func_lit_nonnested.go b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_func_lit_nonnested.go
new file mode 100644
index 000000000..c22db2a6d
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_func_lit_nonnested.go
@@ -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)
+}
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_return_func_lit_nonnested.go.golden b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_func_lit_nonnested.go.golden
new file mode 100644
index 000000000..efa22ba2b
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_func_lit_nonnested.go.golden
@@ -0,0 +1,20 @@
+-- functionextraction_extract_return_func_lit_nonnested_7_3 --
+package extract
+
+import "go/ast"
+
+func _() {
+ ast.Inspect(ast.NewIdent("a"), func(n ast.Node) bool {
+ //@mark(exSt11, "if")
+ return newFunction(n) //@mark(exEn11, "false")
+ })
+ //@extractfunc(exSt11, exEn11)
+}
+
+func newFunction(n ast.Node) bool {
+ if n == nil {
+ return true
+ }
+ return false
+}
+
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_return_init.go b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_init.go
new file mode 100644
index 000000000..c1994c1c1
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_init.go
@@ -0,0 +1,12 @@
+package extract
+
+func _() string {
+ x := 1
+ if x == 0 { //@mark(exSt5, "if")
+ x = 3
+ return "a"
+ } //@mark(exEn5, "}")
+ x = 2
+ return "b"
+ //@extractfunc(exSt5, exEn5)
+}
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_return_init.go.golden b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_init.go.golden
new file mode 100644
index 000000000..31d1b2ddf
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_init.go.golden
@@ -0,0 +1,23 @@
+-- functionextraction_extract_return_init_5_2 --
+package extract
+
+func _() string {
+ x := 1
+ //@mark(exSt5, "if")
+ shouldReturn, returnValue := newFunction(x)
+ if shouldReturn {
+ return returnValue
+ } //@mark(exEn5, "}")
+ x = 2
+ return "b"
+ //@extractfunc(exSt5, exEn5)
+}
+
+func newFunction(x int) (bool, string) {
+ if x == 0 {
+ x = 3
+ return true, "a"
+ }
+ return false, ""
+}
+
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_return_init_nonnested.go b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_init_nonnested.go
new file mode 100644
index 000000000..bb5ed083c
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_init_nonnested.go
@@ -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)
+}
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_return_init_nonnested.go.golden b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_init_nonnested.go.golden
new file mode 100644
index 000000000..58bb57325
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_return_init_nonnested.go.golden
@@ -0,0 +1,19 @@
+-- functionextraction_extract_return_init_nonnested_5_2 --
+package extract
+
+func _() string {
+ x := 1
+ //@mark(exSt12, "if")
+ return newFunction(x) //@mark(exEn12, "\"b\"")
+ //@extractfunc(exSt12, exEn12)
+}
+
+func newFunction(x int) string {
+ if x == 0 {
+ x = 3
+ return "a"
+ }
+ x = 2
+ return "b"
+}
+
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_scope.go b/gopls/internal/lsp/testdata/extract/extract_function/extract_scope.go
new file mode 100644
index 000000000..6cc141fd1
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_scope.go
@@ -0,0 +1,10 @@
+package extract
+
+func _() {
+ newFunction := 1
+ a := newFunction //@extractfunc("a", "newFunction")
+}
+
+func newFunction1() int {
+ return 1
+}
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_scope.go.golden b/gopls/internal/lsp/testdata/extract/extract_function/extract_scope.go.golden
new file mode 100644
index 000000000..a4803b4fe
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_scope.go.golden
@@ -0,0 +1,16 @@
+-- functionextraction_extract_scope_5_2 --
+package extract
+
+func _() {
+ newFunction := 1
+ newFunction2(newFunction) //@extractfunc("a", "newFunction")
+}
+
+func newFunction2(newFunction int) {
+ a := newFunction
+}
+
+func newFunction1() int {
+ return 1
+}
+
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_smart_initialization.go b/gopls/internal/lsp/testdata/extract/extract_function/extract_smart_initialization.go
new file mode 100644
index 000000000..da2c669a8
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_smart_initialization.go
@@ -0,0 +1,9 @@
+package extract
+
+func _() {
+ var a []int
+ a = append(a, 2) //@mark(exSt6, "a")
+ b := 4 //@mark(exEn6, "4")
+ //@extractfunc(exSt6, exEn6)
+ a = append(a, b)
+}
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_smart_initialization.go.golden b/gopls/internal/lsp/testdata/extract/extract_function/extract_smart_initialization.go.golden
new file mode 100644
index 000000000..8be5040c4
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_smart_initialization.go.golden
@@ -0,0 +1,17 @@
+-- functionextraction_extract_smart_initialization_5_2 --
+package extract
+
+func _() {
+ var a []int
+ //@mark(exSt6, "a")
+ a, b := newFunction(a) //@mark(exEn6, "4")
+ //@extractfunc(exSt6, exEn6)
+ a = append(a, b)
+}
+
+func newFunction(a []int) ([]int, int) {
+ a = append(a, 2)
+ b := 4
+ return a, b
+}
+
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_smart_return.go b/gopls/internal/lsp/testdata/extract/extract_function/extract_smart_return.go
new file mode 100644
index 000000000..264d680e2
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_smart_return.go
@@ -0,0 +1,11 @@
+package extract
+
+func _() {
+ var b []int
+ var a int
+ a = 2 //@mark(exSt7, "a")
+ b = []int{}
+ b = append(b, a) //@mark(exEn7, ")")
+ b[0] = 1
+ //@extractfunc(exSt7, exEn7)
+}
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_smart_return.go.golden b/gopls/internal/lsp/testdata/extract/extract_function/extract_smart_return.go.golden
new file mode 100644
index 000000000..fdf55ae6d
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_smart_return.go.golden
@@ -0,0 +1,19 @@
+-- functionextraction_extract_smart_return_6_2 --
+package extract
+
+func _() {
+ var b []int
+ var a int
+ //@mark(exSt7, "a")
+ b = newFunction(a, b) //@mark(exEn7, ")")
+ b[0] = 1
+ //@extractfunc(exSt7, exEn7)
+}
+
+func newFunction(a int, b []int) []int {
+ a = 2
+ b = []int{}
+ b = append(b, a)
+ return b
+}
+
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_unnecessary_param.go b/gopls/internal/lsp/testdata/extract/extract_function/extract_unnecessary_param.go
new file mode 100644
index 000000000..a6eb1f872
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_unnecessary_param.go
@@ -0,0 +1,14 @@
+package extract
+
+func _() {
+ var b []int
+ var a int
+ a := 2 //@mark(exSt8, "a")
+ b = []int{}
+ b = append(b, a) //@mark(exEn8, ")")
+ b[0] = 1
+ if a == 2 {
+ return
+ }
+ //@extractfunc(exSt8, exEn8)
+}
diff --git a/gopls/internal/lsp/testdata/extract/extract_function/extract_unnecessary_param.go.golden b/gopls/internal/lsp/testdata/extract/extract_function/extract_unnecessary_param.go.golden
new file mode 100644
index 000000000..4374f3728
--- /dev/null
+++ b/gopls/internal/lsp/testdata/extract/extract_function/extract_unnecessary_param.go.golden
@@ -0,0 +1,22 @@
+-- functionextraction_extract_unnecessary_param_6_2 --
+package extract
+
+func _() {
+ var b []int
+ var a int
+ //@mark(exSt8, "a")
+ a, b = newFunction(b) //@mark(exEn8, ")")
+ b[0] = 1
+ if a == 2 {
+ return
+ }
+ //@extractfunc(exSt8, exEn8)
+}
+
+func newFunction(b []int) (int, []int) {
+ a := 2
+ b = []int{}
+ b = append(b, a)
+ return a, b
+}
+