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

Commit efef6a7

Browse files
committed
Implement methods not only from the selected template but also from its ancestry.
1 parent 2cc2155 commit efef6a7

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed

Diff for: src/main/scala/scala/tools/refactoring/implementations/ImplementMethods.scala

+15-3
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,32 @@ abstract class ImplementMethods extends MultiStageRefactoring with analysis.Inde
3535
}
3636
}
3737

38+
private def templateAncestry(template: Template): List[Template] =
39+
template :: {
40+
for {
41+
parent <- template.parents
42+
parentImp <- index.declaration(parent.symbol).toList collect {
43+
case ClassDef(_, _, _, impl) => impl
44+
}
45+
ancenstor <- templateAncestry(parentImp)
46+
} yield ancenstor
47+
}
48+
3849
override def prepare(s: Selection): Either[PreparationError, PreparationResult] = {
3950

4051

4152
// Expand the selection to the concrete type when a kind was initially selected.
4253
val maybeSelectedTemplate = (s::s.expandToNextEnclosingTree.toList) flatMap { sel: Selection =>
4354
index.declaration(sel.enclosingTree.symbol)
4455
} collectFirst {
45-
case templateDeclaration: ClassDef => templateDeclaration
56+
case templateDeclaration: ClassDef => templateDeclaration.impl
4657
}
4758

4859
// Get a sequence of methods found in the selected mixed trait.
4960
val methodsToImplement = for {
50-
selectedTemplateDeclaration <- maybeSelectedTemplate.toList
51-
unimplementedMethod <- selectedTemplateDeclaration.impl.body collect {
61+
selectedTemplate <- maybeSelectedTemplate.toList
62+
selectedDeclaration <- templateAncestry(selectedTemplate)
63+
unimplementedMethod <- selectedDeclaration.body collect {
5264
case methodDeclaration: DefDef if methodDeclaration.rhs.isEmpty =>
5365
methodDeclaration
5466
}

Diff for: src/test/scala/scala/tools/refactoring/tests/implementations/ImplementMethodsTest.scala

+51
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,55 @@ class ImplementMethodsTest extends TestHelper with TestRefactoring {
223223
} applyRefactoring implementMethods
224224

225225

226+
@Test
227+
def implementMethodFromAncestry() = new FileSet() {
228+
"""
229+
|package implementMethods
230+
|
231+
|trait R {
232+
| def k: Unit
233+
|}
234+
|
235+
|trait T {
236+
| def f(x: Int): String
237+
|}
238+
|
239+
|trait S extends T {
240+
| def g(x: Int): Int
241+
|}
242+
|
243+
|object Obj extends /*(*/S/*)*/ with R {
244+
| val x: Int = ???
245+
|}
246+
""".stripMargin becomes
247+
"""
248+
|package implementMethods
249+
|
250+
|trait R {
251+
| def k: Unit
252+
|}
253+
|
254+
|trait T {
255+
| def f(x: Int): String
256+
|}
257+
|
258+
|trait S extends T {
259+
| def g(x: Int): Int
260+
|}
261+
|
262+
|object Obj extends /*(*/S/*)*/ with R {
263+
| val x: Int = ???
264+
|
265+
| def g(x: Int): Int = {
266+
| ???
267+
| }
268+
|
269+
| def f(x: Int): String = {
270+
| ???
271+
| }
272+
|}
273+
""".stripMargin
274+
275+
} applyRefactoring implementMethods
276+
226277
}

0 commit comments

Comments
 (0)