gotools/gopls/internal/test/marker/testdata/codeaction/extract_method.txt

This test exercises function and method extraction.

-- flags --
-ignore_extra_diags

-- basic.go --
package extract

//@codeactionedit(A_XLessThanYP, "refactor.extract.method", meth1)
//@codeactionedit(A_XLessThanYP, "refactor.extract.function", func1)
//@codeactionedit(A_AddP1, "refactor.extract.method", meth2)
//@codeactionedit(A_AddP1, "refactor.extract.function", func2)
//@codeactionedit(A_AddP2, "refactor.extract.method", meth3)
//@codeactionedit(A_AddP2, "refactor.extract.function", func3)
//@codeactionedit(A_XLessThanY, "refactor.extract.method", meth4)
//@codeactionedit(A_XLessThanY, "refactor.extract.function", func4)
//@codeactionedit(A_Add1, "refactor.extract.method", meth5)
//@codeactionedit(A_Add1, "refactor.extract.function", func5)
//@codeactionedit(A_Add2, "refactor.extract.method", meth6)
//@codeactionedit(A_Add2, "refactor.extract.function", func6)

type A struct {
	x int
	y int
}

func (a *A) XLessThanYP() bool {
	return a.x < a.y //@loc(A_XLessThanYP, re`return.*a\.y`)
}

func (a *A) AddP() int {
	sum := a.x + a.y //@loc(A_AddP1, re`sum.*a\.y`)
	return sum       //@loc(A_AddP2, re`return.*?sum`)
}

func (a A) XLessThanY() bool {
	return a.x < a.y //@loc(A_XLessThanY, re`return.*a\.y`)
}

func (a A) Add() int {
	sum := a.x + a.y //@loc(A_Add1, re`sum.*a\.y`)
	return sum       //@loc(A_Add2, re`return.*?sum`)
}

-- @func1/basic.go --
@@ -22 +22 @@
-	return a.x < a.y //@loc(A_XLessThanYP, re`return.*a\.y`)
+	return newFunction(a) //@loc(A_XLessThanYP, re`return.*a\.y`)
@@ -25 +25,4 @@
+func newFunction(a *A) bool {
+	return a.x < a.y
+}
+
-- @func2/basic.go --
@@ -26 +26 @@
-	sum := a.x + a.y //@loc(A_AddP1, re`sum.*a\.y`)
+	sum := newFunction(a) //@loc(A_AddP1, re`sum.*a\.y`)
@@ -30 +30,5 @@
+func newFunction(a *A) int {
+	sum := a.x + a.y
+	return sum
+}
+
-- @func3/basic.go --
@@ -27 +27 @@
-	return sum       //@loc(A_AddP2, re`return.*?sum`)
+	return newFunction(sum)       //@loc(A_AddP2, re`return.*?sum`)
@@ -30 +30,4 @@
+func newFunction(sum int) int {
+	return sum
+}
+
-- @func4/basic.go --
@@ -31 +31 @@
-	return a.x < a.y //@loc(A_XLessThanY, re`return.*a\.y`)
+	return newFunction(a) //@loc(A_XLessThanY, re`return.*a\.y`)
@@ -34 +34,4 @@
+func newFunction(a A) bool {
+	return a.x < a.y
+}
+
-- @func5/basic.go --
@@ -35 +35 @@
-	sum := a.x + a.y //@loc(A_Add1, re`sum.*a\.y`)
+	sum := newFunction(a) //@loc(A_Add1, re`sum.*a\.y`)
@@ -39 +39,5 @@
+func newFunction(a A) int {
+	sum := a.x + a.y
+	return sum
+}
+
-- @func6/basic.go --
@@ -36 +36 @@
-	return sum       //@loc(A_Add2, re`return.*?sum`)
+	return newFunction(sum)       //@loc(A_Add2, re`return.*?sum`)
@@ -39 +39,4 @@
+func newFunction(sum int) int {
+	return sum
+}
+
-- @meth1/basic.go --
@@ -22 +22 @@
-	return a.x < a.y //@loc(A_XLessThanYP, re`return.*a\.y`)
+	return a.newMethod() //@loc(A_XLessThanYP, re`return.*a\.y`)
@@ -25 +25,4 @@
+func (a *A) newMethod() bool {
+	return a.x < a.y
+}
+
-- @meth2/basic.go --
@@ -26 +26 @@
-	sum := a.x + a.y //@loc(A_AddP1, re`sum.*a\.y`)
+	sum := a.newMethod() //@loc(A_AddP1, re`sum.*a\.y`)
@@ -30 +30,5 @@
+func (a *A) newMethod() int {
+	sum := a.x + a.y
+	return sum
+}
+
-- @meth3/basic.go --
@@ -27 +27 @@
-	return sum       //@loc(A_AddP2, re`return.*?sum`)
+	return a.newMethod(sum)       //@loc(A_AddP2, re`return.*?sum`)
@@ -30 +30,4 @@
+func (*A) newMethod(sum int) int {
+	return sum
+}
+
-- @meth4/basic.go --
@@ -31 +31 @@
-	return a.x < a.y //@loc(A_XLessThanY, re`return.*a\.y`)
+	return a.newMethod() //@loc(A_XLessThanY, re`return.*a\.y`)
@@ -34 +34,4 @@
+func (a A) newMethod() bool {
+	return a.x < a.y
+}
+
-- @meth5/basic.go --
@@ -35 +35 @@
-	sum := a.x + a.y //@loc(A_Add1, re`sum.*a\.y`)
+	sum := a.newMethod() //@loc(A_Add1, re`sum.*a\.y`)
@@ -39 +39,5 @@
+func (a A) newMethod() int {
+	sum := a.x + a.y
+	return sum
+}
+
-- @meth6/basic.go --
@@ -36 +36 @@
-	return sum       //@loc(A_Add2, re`return.*?sum`)
+	return a.newMethod(sum)       //@loc(A_Add2, re`return.*?sum`)
@@ -39 +39,4 @@
+func (A) newMethod(sum int) int {
+	return sum
+}
+
-- context.go --
package extract

import (
	"context"
	"testing"
)

//@codeactionedit(B_AddP, "refactor.extract.method", contextMeth1)
//@codeactionedit(B_AddP, "refactor.extract.function", contextFunc1)
//@codeactionedit(B_LongList, "refactor.extract.method", contextMeth2)
//@codeactionedit(B_LongList, "refactor.extract.function", contextFunc2)
//@codeactionedit(B_AddPWithB, "refactor.extract.function", contextFuncB)
//@codeactionedit(B_LongListWithT, "refactor.extract.function", contextFuncT)

type B struct {
	x int
	y int
}
	
func (b *B) AddP(ctx context.Context) (int, error) {
	sum := b.x + b.y
	return sum, ctx.Err() //@loc(B_AddP, re`return.*ctx\.Err\(\)`)
}

func (b *B) LongList(ctx context.Context) (int, error) {
	p1 := 1
	p2 := 1
	p3 := 1
	return p1 + p2 + p3, ctx.Err() //@loc(B_LongList, re`return.*ctx\.Err\(\)`)
}

func (b *B) AddPWithB(ctx context.Context, tB *testing.B) (int, error) {
	sum := b.x + b.y //@loc(B_AddPWithB, re`(?s:^.*?Err\(\))`)
	tB.Skip()
	return sum, ctx.Err()
}

func (b *B) LongListWithT(ctx context.Context, t *testing.T) (int, error) {
	p1 := 1
	p2 := 1
	p3 := 1
	p4 := p1 + p2 //@loc(B_LongListWithT, re`(?s:^.*?Err\(\))`)
	t.Skip()
	return p4 + p3, ctx.Err()
}
-- @contextMeth1/context.go --
@@ -22 +22 @@
-	return sum, ctx.Err() //@loc(B_AddP, re`return.*ctx\.Err\(\)`)
+	return b.newMethod(ctx, sum) //@loc(B_AddP, re`return.*ctx\.Err\(\)`)
@@ -25 +25,4 @@
+func (*B) newMethod(ctx context.Context, sum int) (int, error) {
+	return sum, ctx.Err()
+}
+
-- @contextMeth2/context.go --
@@ -29 +29 @@
-	return p1 + p2 + p3, ctx.Err() //@loc(B_LongList, re`return.*ctx\.Err\(\)`)
+	return b.newMethod(ctx, p1, p2, p3) //@loc(B_LongList, re`return.*ctx\.Err\(\)`)
@@ -32 +32,4 @@
+func (*B) newMethod(ctx context.Context, p1 int, p2 int, p3 int) (int, error) {
+	return p1 + p2 + p3, ctx.Err()
+}
+
-- @contextFunc2/context.go --
@@ -29 +29 @@
-	return p1 + p2 + p3, ctx.Err() //@loc(B_LongList, re`return.*ctx\.Err\(\)`)
+	return newFunction(ctx, p1, p2, p3) //@loc(B_LongList, re`return.*ctx\.Err\(\)`)
@@ -32 +32,4 @@
+func newFunction(ctx context.Context, p1 int, p2 int, p3 int) (int, error) {
+	return p1 + p2 + p3, ctx.Err()
+}
+
-- @contextFunc1/context.go --
@@ -22 +22 @@
-	return sum, ctx.Err() //@loc(B_AddP, re`return.*ctx\.Err\(\)`)
+	return newFunction(ctx, sum) //@loc(B_AddP, re`return.*ctx\.Err\(\)`)
@@ -25 +25,4 @@
+func newFunction(ctx context.Context, sum int) (int, error) {
+	return sum, ctx.Err()
+}
+
-- @contextFuncB/context.go --
@@ -33 +33,6 @@
-	sum := b.x + b.y //@loc(B_AddPWithB, re`(?s:^.*?Err\(\))`)
+	//@loc(B_AddPWithB, re`(?s:^.*?Err\(\))`)
+	return newFunction(ctx, tB, b)
+}
+
+func newFunction(ctx context.Context, tB *testing.B, b *B) (int, error) {
+	sum := b.x + b.y
-- @contextFuncT/context.go --
@@ -42 +42,6 @@
-	p4 := p1 + p2 //@loc(B_LongListWithT, re`(?s:^.*?Err\(\))`)
+	//@loc(B_LongListWithT, re`(?s:^.*?Err\(\))`)
+	return newFunction(ctx, t, p1, p2, p3)
+}
+
+func newFunction(ctx context.Context, t *testing.T, p1 int, p2 int, p3 int) (int, error) {
+	p4 := p1 + p2