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

Commit 5231769

Browse files
committed
Unfinished perform stage
1 parent 057be89 commit 5231769

File tree

2 files changed

+54
-25
lines changed

2 files changed

+54
-25
lines changed
Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,63 @@
11
package scala.tools.refactoring.implementations
22

33
import scala.tools.refactoring.common.InteractiveScalaCompiler
4+
import scala.tools.refactoring.transformation.TreeFactory
45
import scala.tools.refactoring.{MultiStageRefactoring, analysis}
56

6-
abstract class ImplementMethods extends MultiStageRefactoring with analysis.Indexes with InteractiveScalaCompiler {
7+
abstract class ImplementMethods extends MultiStageRefactoring with analysis.Indexes with TreeFactory with InteractiveScalaCompiler {
78

89
import global._
910

10-
override type PreparationResult = Seq[DefDef]
11+
case class PreparationResult(targetTemplate: Template, methodsToImplement: Seq[DefDef])
1112

1213
override def prepare(s: Selection): Either[PreparationError, PreparationResult] = {
14+
1315
val methodsToImplement = for {
14-
selectedClassDeclaration <- index.declaration(s.enclosingTree.symbol).toSeq collect {
16+
selectedTemplateDeclaration <- index.declaration(s.enclosingTree.symbol).toSeq collect {
1517
case traitDeclaration: ClassDef => traitDeclaration
1618
}
17-
unimplementedMethod <- selectedClassDeclaration.impl.body collect {
19+
unimplementedMethod <- selectedTemplateDeclaration.impl.body collect {
1820
case methodDeclaration: DefDef if methodDeclaration.rhs.isEmpty => methodDeclaration
1921
}
2022
} yield unimplementedMethod
21-
if(methodsToImplement.isEmpty) Left {
23+
24+
val targetTemplate = s.expandToNextEnclosingTree.flatMap {
25+
_.selectedSymbolTree collect {
26+
case target: Template => target
27+
}
28+
}
29+
30+
if(targetTemplate.isEmpty) Left {
31+
PreparationError("No target class in selection")
32+
}
33+
else if(methodsToImplement.isEmpty) Left {
2234
PreparationError("There are not methods to implement")
2335
}
24-
else Right(methodsToImplement)
36+
else Right(PreparationResult(targetTemplate.get, methodsToImplement))
2537
}
2638

2739
override type RefactoringParameters = Unit
2840

2941
override def perform(selection: Selection, prepared: PreparationResult, params: RefactoringParameters) = {
30-
???
42+
import prepared._
43+
44+
val findTemplate = filter {
45+
case t: Template => t == targetTemplate
46+
}
47+
48+
val addMethods = transform {
49+
case tpl @ Template(_, _, body) if tpl == prepared.targetTemplate =>
50+
val methodsBody = Block(Ident("???") :: Nil, EmptyTree)
51+
val methodWithRhs = methodsToImplement.map(_ copy (rhs = methodsBody))
52+
tpl.copy(body = body ++ methodWithRhs).replaces(tpl)
53+
}
54+
55+
val transformation = topdown {
56+
matchingChildren {
57+
findTemplate &>
58+
addMethods
59+
}
60+
}
61+
Right(transformFile(selection.file, transformation))
3162
}
3263
}

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

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,38 @@ class ImplementMethodsTest extends TestHelper with TestRefactoring {
99

1010
val fromSource =
1111
"""
12+
|package implementMethods
13+
|
1214
|trait T {
13-
| def f(x: Int): String
15+
| def f(x: Int): String
1416
|}
1517
|
1618
|object Obj extends /*(*/T/*)*/ {
19+
|
20+
| def g(x: Int): Int = 1
21+
|
1722
|}
1823
""".stripMargin
1924

2025
val toSource =
2126
"""
27+
|package implementMethods
28+
|
2229
|trait T {
23-
| def f(x: Int): String
30+
| def f(x: Int): String
2431
|}
2532
|
26-
|object Obj extends T {
27-
| def g(): Int = {
28-
| ??? Left(PreparationError("crash!"))
29-
33+
|object Obj extends /*(*/T/*)*/ {
34+
|
35+
| def g(x: Int): Int = 1
36+
|
37+
| def f(x: Int): String = {
38+
| ???
3039
| }
40+
|
3141
|}
3242
""".stripMargin
3343

34-
/*@Test
35-
def addMethod() = {
36-
global.ask { () =>
37-
val refactoring = new AddMethod {
38-
val global: Global = outer.global
39-
val file = addToCompiler(UniqueNames.basename(), fromSource)
40-
val change = addMethod(file, "Obj", "g", List(Nil), Nil, Some("Int"), AddToObject)
41-
}
42-
assertEquals(toSource, Change.applyChanges(refactoring.change, fromSource))
43-
}
44-
}*/
45-
4644
def implementMethods(pro: FileSet) = new TestRefactoringImpl(pro) {
4745
override val refactoring = new ImplementMethods with TestProjectIndex
4846
val changes = performRefactoring(())

0 commit comments

Comments
 (0)