golang.org/x/tools/gopls@v0.15.3/internal/test/marker/testdata/codeaction/extract_method.txt (about) 1 This test exercises function and method extraction. 2 3 -- flags -- 4 -ignore_extra_diags 5 6 -- basic.go -- 7 package extract 8 9 //@codeactionedit(A_XLessThanYP, "refactor.extract", meth1, "Extract method") 10 //@codeactionedit(A_XLessThanYP, "refactor.extract", func1, "Extract function") 11 //@codeactionedit(A_AddP1, "refactor.extract", meth2, "Extract method") 12 //@codeactionedit(A_AddP1, "refactor.extract", func2, "Extract function") 13 //@codeactionedit(A_AddP2, "refactor.extract", meth3, "Extract method") 14 //@codeactionedit(A_AddP2, "refactor.extract", func3, "Extract function") 15 //@codeactionedit(A_XLessThanY, "refactor.extract", meth4, "Extract method") 16 //@codeactionedit(A_XLessThanY, "refactor.extract", func4, "Extract function") 17 //@codeactionedit(A_Add1, "refactor.extract", meth5, "Extract method") 18 //@codeactionedit(A_Add1, "refactor.extract", func5, "Extract function") 19 //@codeactionedit(A_Add2, "refactor.extract", meth6, "Extract method") 20 //@codeactionedit(A_Add2, "refactor.extract", func6, "Extract function") 21 22 type A struct { 23 x int 24 y int 25 } 26 27 func (a *A) XLessThanYP() bool { 28 return a.x < a.y //@loc(A_XLessThanYP, re`return.*a\.y`) 29 } 30 31 func (a *A) AddP() int { 32 sum := a.x + a.y //@loc(A_AddP1, re`sum.*a\.y`) 33 return sum //@loc(A_AddP2, re`return.*sum`) 34 } 35 36 func (a A) XLessThanY() bool { 37 return a.x < a.y //@loc(A_XLessThanY, re`return.*a\.y`) 38 } 39 40 func (a A) Add() int { 41 sum := a.x + a.y //@loc(A_Add1, re`sum.*a\.y`) 42 return sum //@loc(A_Add2, re`return.*sum`) 43 } 44 45 -- @func1/basic.go -- 46 @@ -22 +22 @@ 47 - return a.x < a.y //@loc(A_XLessThanYP, re`return.*a\.y`) 48 + return newFunction(a) //@loc(A_XLessThanYP, re`return.*a\.y`) 49 @@ -25 +25,4 @@ 50 +func newFunction(a *A) bool { 51 + return a.x < a.y 52 +} 53 + 54 -- @func2/basic.go -- 55 @@ -26 +26 @@ 56 - sum := a.x + a.y //@loc(A_AddP1, re`sum.*a\.y`) 57 + sum := newFunction(a) //@loc(A_AddP1, re`sum.*a\.y`) 58 @@ -30 +30,5 @@ 59 +func newFunction(a *A) int { 60 + sum := a.x + a.y 61 + return sum 62 +} 63 + 64 -- @func3/basic.go -- 65 @@ -27 +27 @@ 66 - return sum //@loc(A_AddP2, re`return.*sum`) 67 + return newFunction(sum) //@loc(A_AddP2, re`return.*sum`) 68 @@ -30 +30,4 @@ 69 +func newFunction(sum int) int { 70 + return sum 71 +} 72 + 73 -- @func4/basic.go -- 74 @@ -31 +31 @@ 75 - return a.x < a.y //@loc(A_XLessThanY, re`return.*a\.y`) 76 + return newFunction(a) //@loc(A_XLessThanY, re`return.*a\.y`) 77 @@ -34 +34,4 @@ 78 +func newFunction(a A) bool { 79 + return a.x < a.y 80 +} 81 + 82 -- @func5/basic.go -- 83 @@ -35 +35 @@ 84 - sum := a.x + a.y //@loc(A_Add1, re`sum.*a\.y`) 85 + sum := newFunction(a) //@loc(A_Add1, re`sum.*a\.y`) 86 @@ -39 +39,5 @@ 87 +func newFunction(a A) int { 88 + sum := a.x + a.y 89 + return sum 90 +} 91 + 92 -- @func6/basic.go -- 93 @@ -36 +36 @@ 94 - return sum //@loc(A_Add2, re`return.*sum`) 95 + return newFunction(sum) //@loc(A_Add2, re`return.*sum`) 96 @@ -39 +39,4 @@ 97 +func newFunction(sum int) int { 98 + return sum 99 +} 100 + 101 -- @meth1/basic.go -- 102 @@ -22 +22 @@ 103 - return a.x < a.y //@loc(A_XLessThanYP, re`return.*a\.y`) 104 + return a.newMethod() //@loc(A_XLessThanYP, re`return.*a\.y`) 105 @@ -25 +25,4 @@ 106 +func (a *A) newMethod() bool { 107 + return a.x < a.y 108 +} 109 + 110 -- @meth2/basic.go -- 111 @@ -26 +26 @@ 112 - sum := a.x + a.y //@loc(A_AddP1, re`sum.*a\.y`) 113 + sum := a.newMethod() //@loc(A_AddP1, re`sum.*a\.y`) 114 @@ -30 +30,5 @@ 115 +func (a *A) newMethod() int { 116 + sum := a.x + a.y 117 + return sum 118 +} 119 + 120 -- @meth3/basic.go -- 121 @@ -27 +27 @@ 122 - return sum //@loc(A_AddP2, re`return.*sum`) 123 + return a.newMethod(sum) //@loc(A_AddP2, re`return.*sum`) 124 @@ -30 +30,4 @@ 125 +func (*A) newMethod(sum int) int { 126 + return sum 127 +} 128 + 129 -- @meth4/basic.go -- 130 @@ -31 +31 @@ 131 - return a.x < a.y //@loc(A_XLessThanY, re`return.*a\.y`) 132 + return a.newMethod() //@loc(A_XLessThanY, re`return.*a\.y`) 133 @@ -34 +34,4 @@ 134 +func (a A) newMethod() bool { 135 + return a.x < a.y 136 +} 137 + 138 -- @meth5/basic.go -- 139 @@ -35 +35 @@ 140 - sum := a.x + a.y //@loc(A_Add1, re`sum.*a\.y`) 141 + sum := a.newMethod() //@loc(A_Add1, re`sum.*a\.y`) 142 @@ -39 +39,5 @@ 143 +func (a A) newMethod() int { 144 + sum := a.x + a.y 145 + return sum 146 +} 147 + 148 -- @meth6/basic.go -- 149 @@ -36 +36 @@ 150 - return sum //@loc(A_Add2, re`return.*sum`) 151 + return a.newMethod(sum) //@loc(A_Add2, re`return.*sum`) 152 @@ -39 +39,4 @@ 153 +func (A) newMethod(sum int) int { 154 + return sum 155 +} 156 + 157 -- context.go -- 158 package extract 159 160 import "context" 161 162 //@codeactionedit(B_AddP, "refactor.extract", contextMeth1, "Extract method") 163 //@codeactionedit(B_AddP, "refactor.extract", contextFunc1, "Extract function") 164 //@codeactionedit(B_LongList, "refactor.extract", contextMeth2, "Extract method") 165 //@codeactionedit(B_LongList, "refactor.extract", contextFunc2, "Extract function") 166 167 type B struct { 168 x int 169 y int 170 } 171 172 func (b *B) AddP(ctx context.Context) (int, error) { 173 sum := b.x + b.y 174 return sum, ctx.Err() //@loc(B_AddP, re`return.*ctx\.Err\(\)`) 175 } 176 177 func (b *B) LongList(ctx context.Context) (int, error) { 178 p1 := 1 179 p2 := 1 180 p3 := 1 181 return p1 + p2 + p3, ctx.Err() //@loc(B_LongList, re`return.*ctx\.Err\(\)`) 182 } 183 -- @contextMeth1/context.go -- 184 @@ -17 +17 @@ 185 - return sum, ctx.Err() //@loc(B_AddP, re`return.*ctx\.Err\(\)`) 186 + return b.newMethod(ctx, sum) //@loc(B_AddP, re`return.*ctx\.Err\(\)`) 187 @@ -20 +20,4 @@ 188 +func (*B) newMethod(ctx context.Context, sum int) (int, error) { 189 + return sum, ctx.Err() 190 +} 191 + 192 -- @contextMeth2/context.go -- 193 @@ -24 +24 @@ 194 - return p1 + p2 + p3, ctx.Err() //@loc(B_LongList, re`return.*ctx\.Err\(\)`) 195 + return b.newMethod(ctx, p1, p2, p3) //@loc(B_LongList, re`return.*ctx\.Err\(\)`) 196 @@ -26 +26,4 @@ 197 + 198 +func (*B) newMethod(ctx context.Context, p1 int, p2 int, p3 int) (int, error) { 199 + return p1 + p2 + p3, ctx.Err() 200 +} 201 -- @contextFunc2/context.go -- 202 @@ -24 +24 @@ 203 - return p1 + p2 + p3, ctx.Err() //@loc(B_LongList, re`return.*ctx\.Err\(\)`) 204 + return newFunction(ctx, p1, p2, p3) //@loc(B_LongList, re`return.*ctx\.Err\(\)`) 205 @@ -26 +26,4 @@ 206 + 207 +func newFunction(ctx context.Context, p1 int, p2 int, p3 int) (int, error) { 208 + return p1 + p2 + p3, ctx.Err() 209 +} 210 -- @contextFunc1/context.go -- 211 @@ -17 +17 @@ 212 - return sum, ctx.Err() //@loc(B_AddP, re`return.*ctx\.Err\(\)`) 213 + return newFunction(ctx, sum) //@loc(B_AddP, re`return.*ctx\.Err\(\)`) 214 @@ -20 +20,4 @@ 215 +func newFunction(ctx context.Context, sum int) (int, error) { 216 + return sum, ctx.Err() 217 +} 218 +