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

Commit 057be89

Browse files
committed
ImplementMethods refactor: Minimum test & prepare refactor stage
1 parent af879be commit 057be89

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package scala.tools.refactoring.implementations
2+
3+
import scala.tools.refactoring.common.InteractiveScalaCompiler
4+
import scala.tools.refactoring.{MultiStageRefactoring, analysis}
5+
6+
abstract class ImplementMethods extends MultiStageRefactoring with analysis.Indexes with InteractiveScalaCompiler {
7+
8+
import global._
9+
10+
override type PreparationResult = Seq[DefDef]
11+
12+
override def prepare(s: Selection): Either[PreparationError, PreparationResult] = {
13+
val methodsToImplement = for {
14+
selectedClassDeclaration <- index.declaration(s.enclosingTree.symbol).toSeq collect {
15+
case traitDeclaration: ClassDef => traitDeclaration
16+
}
17+
unimplementedMethod <- selectedClassDeclaration.impl.body collect {
18+
case methodDeclaration: DefDef if methodDeclaration.rhs.isEmpty => methodDeclaration
19+
}
20+
} yield unimplementedMethod
21+
if(methodsToImplement.isEmpty) Left {
22+
PreparationError("There are not methods to implement")
23+
}
24+
else Right(methodsToImplement)
25+
}
26+
27+
override type RefactoringParameters = Unit
28+
29+
override def perform(selection: Selection, prepared: PreparationResult, params: RefactoringParameters) = {
30+
???
31+
}
32+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package scala.tools.refactoring
2+
package tests.implementations
3+
4+
import scala.tools.refactoring.implementations.ImplementMethods
5+
import scala.tools.refactoring.tests.util.{TestHelper, TestRefactoring}
6+
7+
class ImplementMethodsTest extends TestHelper with TestRefactoring {
8+
outer =>
9+
10+
val fromSource =
11+
"""
12+
|trait T {
13+
| def f(x: Int): String
14+
|}
15+
|
16+
|object Obj extends /*(*/T/*)*/ {
17+
|}
18+
""".stripMargin
19+
20+
val toSource =
21+
"""
22+
|trait T {
23+
| def f(x: Int): String
24+
|}
25+
|
26+
|object Obj extends T {
27+
| def g(): Int = {
28+
| ??? Left(PreparationError("crash!"))
29+
30+
| }
31+
|}
32+
""".stripMargin
33+
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+
46+
def implementMethods(pro: FileSet) = new TestRefactoringImpl(pro) {
47+
override val refactoring = new ImplementMethods with TestProjectIndex
48+
val changes = performRefactoring(())
49+
}.changes
50+
51+
@Test
52+
def doRefactor() = new FileSet() {
53+
fromSource becomes toSource
54+
} applyRefactoring implementMethods
55+
56+
}

0 commit comments

Comments
 (0)