Skip to content
This repository was archived by the owner on Sep 3, 2020. It is now read-only.

Commit e00c2ff

Browse files
committed
Add methods even when a template kind is selected.
1 parent c083049 commit e00c2ff

File tree

2 files changed

+78
-3
lines changed

2 files changed

+78
-3
lines changed

src/main/scala/scala/tools/refactoring/implementations/ImplementMethods.scala

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,17 @@ abstract class ImplementMethods extends MultiStageRefactoring with analysis.Inde
3737

3838
override def prepare(s: Selection): Either[PreparationError, PreparationResult] = {
3939

40+
41+
// Expand the selection to the concrete type when a kind was initially selected.
42+
val maybeSelectedTemplate = (s::s.expandToNextEnclosingTree.toList) flatMap { sel: Selection =>
43+
index.declaration(sel.enclosingTree.symbol)
44+
} collectFirst {
45+
case templateDeclaration: ClassDef => templateDeclaration
46+
}
47+
4048
// Get a sequence of methods found in the selected mixed trait.
4149
val methodsToImplement = for {
42-
selectedTemplateDeclaration <- index.declaration(s.enclosingTree.symbol).toSeq collect {
43-
case templateDeclaration: ClassDef => templateDeclaration
44-
}
50+
selectedTemplateDeclaration <- maybeSelectedTemplate.toList
4551
unimplementedMethod <- selectedTemplateDeclaration.impl.body collect {
4652
case methodDeclaration: DefDef if methodDeclaration.rhs.isEmpty =>
4753
methodDeclaration

src/test/scala/scala/tools/refactoring/tests/implementations/ImplementMethodsTest.scala

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,73 @@ class ImplementMethodsTest extends TestHelper with TestRefactoring {
124124
""".stripMargin
125125
} applyRefactoring implementMethods
126126

127+
@Test
128+
def implementMethodsSelectType() = new FileSet() {
129+
"""
130+
|package implementMethods
131+
|
132+
|trait T[T] {
133+
| def f[T]: T
134+
|}
135+
|
136+
|class C extends /*(*/T[Int]/*)*/ {
137+
|
138+
| def g(x: Int): Int = ???
139+
|
140+
|}
141+
""".stripMargin becomes
142+
"""
143+
|package implementMethods
144+
|
145+
|trait T[T] {
146+
| def f[T]: T
147+
|}
148+
|
149+
|class C extends /*(*/T[Int]/*)*/ {
150+
|
151+
| def g(x: Int): Int = ???
152+
|
153+
| def f[T]: T = {
154+
| ???
155+
| }
156+
|
157+
|}
158+
""".stripMargin
159+
} applyRefactoring implementMethods
160+
161+
@Test
162+
def implementMethodsSelectKind() = new FileSet() {
163+
"""
164+
|package implementMethods
165+
|
166+
|trait T[T] {
167+
| def f[T]: T
168+
|}
169+
|
170+
|class C extends /*(*/T/*)*/[Int] {
171+
|
172+
| def g(x: Int): Int = ???
173+
|
174+
|}
175+
""".stripMargin becomes
176+
"""
177+
|package implementMethods
178+
|
179+
|trait T[T] {
180+
| def f[T]: T
181+
|}
182+
|
183+
|class C extends /*(*/T/*)*/[Int] {
184+
|
185+
| def g(x: Int): Int = ???
186+
|
187+
| def f[T]: T = {
188+
| ???
189+
| }
190+
|
191+
|}
192+
""".stripMargin
193+
} applyRefactoring implementMethods
194+
195+
127196
}

0 commit comments

Comments
 (0)