| 1 |
This test checks the behavior of the 'Add methods to TYPE to implement an |
| 2 |
interface...' code action. |
| 3 |
|
| 4 |
-- capabilities.json -- |
| 5 |
{ |
| 6 |
"experimental":{"interactiveResolve":{"inputTypes":["string"]}} |
| 7 |
} |
| 8 |
|
| 9 |
-- flags -- |
| 10 |
-ignore_extra_diags |
| 11 |
-errors_ok |
| 12 |
|
| 13 |
-- go.mod -- |
| 14 |
module golang.org/lsptests/implementinterface |
| 15 |
|
| 16 |
go 1.18 |
| 17 |
|
| 18 |
-- bad/bad.go -- |
| 19 |
package bad |
| 20 |
|
| 21 |
type foo struct{} |
| 22 |
|
| 23 |
// Named |
| 24 |
type namedOfPointer *foo //@codeaction("Pointer", "refactor.rewrite.implementInterface", err=re"found 0 CodeActions", answers=`{"interface":"error"}`) |
| 25 |
|
| 26 |
type namedOfInterface error //@codeaction("Interface", "refactor.rewrite.implementInterface", err=re"found 0 CodeActions", answers=`{"interface":"error"}`) |
| 27 |
|
| 28 |
// Alias |
| 29 |
type aliasOfBasic = int //@codeaction("Basic", "refactor.rewrite.implementInterface", err=re"found 0 CodeActions", answers=`{"interface":"error"}`) |
| 30 |
|
| 31 |
type aliasOfInterface = error //@codeaction("Interface", "refactor.rewrite.implementInterface", err=re"found 0 CodeActions", answers=`{"interface":"error"}`) |
| 32 |
|
| 33 |
type aliasOfPointer = *foo //@codeaction("Pointer", "refactor.rewrite.implementInterface", err=re"found 0 CodeActions", answers=`{"interface":"error"}`) |
| 34 |
|
| 35 |
type aliasOfNamedPointer namedOfPointer //@codeaction("Pointer", "refactor.rewrite.implementInterface", err=re"found 0 CodeActions", answers=`{"interface":"error"}`) |
| 36 |
|
| 37 |
type aliasOfNameInterface namedOfInterface //@codeaction("Interface", "refactor.rewrite.implementInterface", err=re"found 0 CodeActions", answers=`{"interface":"error"}`) |
| 38 |
|
| 39 |
// Interface |
| 40 |
type iface interface {} //@codeaction("iface", "refactor.rewrite.implementInterface", err=re"found 0 CodeActions", answers=`{"interface":"error"}`) |
| 41 |
|
| 42 |
// Not package level |
| 43 |
func _ () { |
| 44 |
type NamedOfBasic int //@codeaction("Basic", "refactor.rewrite.implementInterface", err=re"found 0 CodeActions", answers=`{"interface":"error"}`) |
| 45 |
|
| 46 |
type NamedOfStruct foo //@codeaction("Struct", "refactor.rewrite.implementInterface", err=re"found 0 CodeActions", answers=`{"interface":"error"}`) |
| 47 |
} |
| 48 |
|
| 49 |
-- good/good.go -- |
| 50 |
package good |
| 51 |
|
| 52 |
type foo struct{} |
| 53 |
|
| 54 |
// Named |
| 55 |
type namedOfBasic int //@codeaction("Basic", "refactor.rewrite.implementInterface", edit=namedBasic, answers=`{"interface":"error"}`) |
| 56 |
|
| 57 |
type namedOfStruct foo //@codeaction("Struct", "refactor.rewrite.implementInterface", edit=namedStruct, answers=`{"interface":"error"}`) |
| 58 |
|
| 59 |
type namedOfFunc func(string) bool //@codeaction("Func", "refactor.rewrite.implementInterface", edit=namedFunc, answers=`{"interface":"error"}`) |
| 60 |
|
| 61 |
type namedOfChannel chan struct{} //@codeaction("Channel", "refactor.rewrite.implementInterface", edit=namedChannel, answers=`{"interface":"error"}`) |
| 62 |
|
| 63 |
// Struct |
| 64 |
type structType struct{} //@codeaction("Type", "refactor.rewrite.implementInterface", edit=struct, answers=`{"interface":"error"}`) |
| 65 |
|
| 66 |
type genericStructType[T any] struct{} //@codeaction("Type", "refactor.rewrite.implementInterface", edit=genericStruct, answers=`{"interface":"error"}`) |
| 67 |
|
| 68 |
// Alias |
| 69 |
type aliasOfNamedBasic namedOfBasic //@codeaction("Basic", "refactor.rewrite.implementInterface", edit=aliasNamedBasic, answers=`{"interface":"error"}`) |
| 70 |
|
| 71 |
type aliasOfNamedStruct namedOfStruct //@codeaction("Struct", "refactor.rewrite.implementInterface", edit=aliasNamedStruct, answers=`{"interface":"error"}`) |
| 72 |
|
| 73 |
-- @aliasNamedBasic/good/good.go -- |
| 74 |
@@ -20 +20,8 @@ |
| 75 |
-type aliasOfNamedBasic namedOfBasic //@codeaction("Basic", "refactor.rewrite.implementInterface", edit=aliasNamedBasic, answers=`{"interface":"error"}`) |
| 76 |
+type aliasOfNamedBasic namedOfBasic |
| 77 |
+ |
| 78 |
+// Error implements [error]. |
| 79 |
+func (a *aliasOfNamedBasic) Error() string { |
| 80 |
+ panic("unimplemented") |
| 81 |
+} |
| 82 |
+ |
| 83 |
+//@codeaction("Basic", "refactor.rewrite.implementInterface", edit=aliasNamedBasic, answers=`{"interface":"error"}`) |
| 84 |
@@ -23 +30 @@ |
| 85 |
- |
| 86 |
-- @aliasNamedStruct/good/good.go -- |
| 87 |
@@ -22 +22,6 @@ |
| 88 |
-type aliasOfNamedStruct namedOfStruct //@codeaction("Struct", "refactor.rewrite.implementInterface", edit=aliasNamedStruct, answers=`{"interface":"error"}`) |
| 89 |
+type aliasOfNamedStruct namedOfStruct |
| 90 |
+ |
| 91 |
+// Error implements [error]. |
| 92 |
+func (a *aliasOfNamedStruct) Error() string { |
| 93 |
+ panic("unimplemented") |
| 94 |
+} |
| 95 |
@@ -24 +29 @@ |
| 96 |
+//@codeaction("Struct", "refactor.rewrite.implementInterface", edit=aliasNamedStruct, answers=`{"interface":"error"}`) |
| 97 |
-- @genericStruct/good/good.go -- |
| 98 |
@@ -17 +17,8 @@ |
| 99 |
-type genericStructType[T any] struct{} //@codeaction("Type", "refactor.rewrite.implementInterface", edit=genericStruct, answers=`{"interface":"error"}`) |
| 100 |
+type genericStructType[T any] struct{} |
| 101 |
+ |
| 102 |
+// Error implements [error]. |
| 103 |
+func (g *genericStructType[T]) Error() string { |
| 104 |
+ panic("unimplemented") |
| 105 |
+} |
| 106 |
+ |
| 107 |
+//@codeaction("Type", "refactor.rewrite.implementInterface", edit=genericStruct, answers=`{"interface":"error"}`) |
| 108 |
@@ -23 +30 @@ |
| 109 |
- |
| 110 |
-- @namedBasic/good/good.go -- |
| 111 |
@@ -6 +6,8 @@ |
| 112 |
-type namedOfBasic int //@codeaction("Basic", "refactor.rewrite.implementInterface", edit=namedBasic, answers=`{"interface":"error"}`) |
| 113 |
+type namedOfBasic int |
| 114 |
+ |
| 115 |
+// Error implements [error]. |
| 116 |
+func (n *namedOfBasic) Error() string { |
| 117 |
+ panic("unimplemented") |
| 118 |
+} |
| 119 |
+ |
| 120 |
+//@codeaction("Basic", "refactor.rewrite.implementInterface", edit=namedBasic, answers=`{"interface":"error"}`) |
| 121 |
@@ -23 +30 @@ |
| 122 |
- |
| 123 |
-- @namedChannel/good/good.go -- |
| 124 |
@@ -12 +12,8 @@ |
| 125 |
-type namedOfChannel chan struct{} //@codeaction("Channel", "refactor.rewrite.implementInterface", edit=namedChannel, answers=`{"interface":"error"}`) |
| 126 |
+type namedOfChannel chan struct{} |
| 127 |
+ |
| 128 |
+// Error implements [error]. |
| 129 |
+func (n *namedOfChannel) Error() string { |
| 130 |
+ panic("unimplemented") |
| 131 |
+} |
| 132 |
+ |
| 133 |
+//@codeaction("Channel", "refactor.rewrite.implementInterface", edit=namedChannel, answers=`{"interface":"error"}`) |
| 134 |
@@ -23 +30 @@ |
| 135 |
- |
| 136 |
-- @namedFunc/good/good.go -- |
| 137 |
@@ -10 +10,8 @@ |
| 138 |
-type namedOfFunc func(string) bool //@codeaction("Func", "refactor.rewrite.implementInterface", edit=namedFunc, answers=`{"interface":"error"}`) |
| 139 |
+type namedOfFunc func(string) bool |
| 140 |
+ |
| 141 |
+// Error implements [error]. |
| 142 |
+func (n *namedOfFunc) Error() string { |
| 143 |
+ panic("unimplemented") |
| 144 |
+} |
| 145 |
+ |
| 146 |
+//@codeaction("Func", "refactor.rewrite.implementInterface", edit=namedFunc, answers=`{"interface":"error"}`) |
| 147 |
@@ -23 +30 @@ |
| 148 |
- |
| 149 |
-- @namedStruct/good/good.go -- |
| 150 |
@@ -8 +8,8 @@ |
| 151 |
-type namedOfStruct foo //@codeaction("Struct", "refactor.rewrite.implementInterface", edit=namedStruct, answers=`{"interface":"error"}`) |
| 152 |
+type namedOfStruct foo |
| 153 |
+ |
| 154 |
+// Error implements [error]. |
| 155 |
+func (n *namedOfStruct) Error() string { |
| 156 |
+ panic("unimplemented") |
| 157 |
+} |
| 158 |
+ |
| 159 |
+//@codeaction("Struct", "refactor.rewrite.implementInterface", edit=namedStruct, answers=`{"interface":"error"}`) |
| 160 |
@@ -23 +30 @@ |
| 161 |
- |
| 162 |
-- @struct/good/good.go -- |
| 163 |
@@ -15 +15,8 @@ |
| 164 |
-type structType struct{} //@codeaction("Type", "refactor.rewrite.implementInterface", edit=struct, answers=`{"interface":"error"}`) |
| 165 |
+type structType struct{} |
| 166 |
+ |
| 167 |
+// Error implements [error]. |
| 168 |
+func (s *structType) Error() string { |
| 169 |
+ panic("unimplemented") |
| 170 |
+} |
| 171 |
+ |
| 172 |
+//@codeaction("Type", "refactor.rewrite.implementInterface", edit=struct, answers=`{"interface":"error"}`) |
| 173 |
@@ -23 +30 @@ |
| 174 |
- |
| 175 |
-- cycle/a/a.go -- |
| 176 |
package a |
| 177 |
|
| 178 |
import "golang.org/lsptests/implementinterface/cycle/b" |
| 179 |
|
| 180 |
var _ int = b.B |
| 181 |
|
| 182 |
type Bar struct{} |
| 183 |
|
| 184 |
type A interface { |
| 185 |
Foo(b Bar) |
| 186 |
} |
| 187 |
|
| 188 |
-- cycle/b/b.go -- |
| 189 |
package b |
| 190 |
|
| 191 |
import "golang.org/lsptests/implementinterface/cycle/named" |
| 192 |
|
| 193 |
var _ int = named.C |
| 194 |
|
| 195 |
const B = 1 |
| 196 |
|
| 197 |
-- cycle/named/named.go -- |
| 198 |
package named |
| 199 |
|
| 200 |
const C = 1 |
| 201 |
|
| 202 |
// Package a -> b -> named -x-> a. |
| 203 |
type Named struct {} //@codeaction("Named", "refactor.rewrite.implementInterface", err=re"import cycle", answers=`{"interface":"golang.org/lsptests/implementinterface/cycle/a.A"}`) |
| 204 |
|
| 205 |
-- cycle/alias/alias.go -- |
| 206 |
package alias |
| 207 |
|
| 208 |
import "golang.org/lsptests/implementinterface/cycle/named" |
| 209 |
|
| 210 |
var _ int = named.C |
| 211 |
|
| 212 |
// Method should be added to the named type [named.Named]. |
| 213 |
// Package a -> b -> named -x-> a. |
| 214 |
type Alias = named.Named //@codeaction("Alias", "refactor.rewrite.implementInterface", err=re"import cycle", answers=`{"interface":"golang.org/lsptests/implementinterface/cycle/a.A"}`) |
| 215 |
|
| 216 |
-- visibility/foo/ok.go -- |
| 217 |
package foo |
| 218 |
|
| 219 |
// We can implement an internal interface without importing its package, as long |
| 220 |
// as its methods only use built-in or accessible types. |
| 221 |
type Ok struct{} //@codeaction("Ok", "refactor.rewrite.implementInterface", edit=visible, answers=`{"interface":"golang.org/lsptests/implementinterface/visibility/bar/internal/bar.VisibleTypes"}`) |
| 222 |
|
| 223 |
-- visibility/foo/notok.go -- |
| 224 |
package foo |
| 225 |
|
| 226 |
// We cannot implement this interface because its method signature requires |
| 227 |
// importing the internal, inaccessible type bar.Bar. |
| 228 |
type NotOk struct{} //@codeaction("NotOk", "refactor.rewrite.implementInterface", err=re"inaccessible package", answers=`{"interface":"golang.org/lsptests/implementinterface/visibility/bar/internal/bar.InvisibleTypes"}`) |
| 229 |
|
| 230 |
-- visibility/bar/internal/bar/bar.go -- |
| 231 |
package bar |
| 232 |
|
| 233 |
type VisibleTypes interface { |
| 234 |
Bar(string) error |
| 235 |
} |
| 236 |
|
| 237 |
type Bar struct{} |
| 238 |
|
| 239 |
type InvisibleTypes interface { |
| 240 |
Bar(Bar) error |
| 241 |
} |
| 242 |
|
| 243 |
-- @visible/visibility/foo/ok.go -- |
| 244 |
@@ -5 +5,6 @@ |
| 245 |
-type Ok struct{} //@codeaction("Ok", "refactor.rewrite.implementInterface", edit=visible, answers=`{"interface":"golang.org/lsptests/implementinterface/visibility/bar/internal/bar.VisibleTypes"}`) |
| 246 |
+type Ok struct{} |
| 247 |
+ |
| 248 |
+// Bar implements [bar.VisibleTypes]. |
| 249 |
+func (o *Ok) Bar(string) error { |
| 250 |
+ panic("unimplemented") |
| 251 |
+} |
| 252 |
@@ -7 +12 @@ |
| 253 |
+//@codeaction("Ok", "refactor.rewrite.implementInterface", edit=visible, answers=`{"interface":"golang.org/lsptests/implementinterface/visibility/bar/internal/bar.VisibleTypes"}`) |