This repository has been archived by the owner on Sep 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
Implement methods #199
Open
pfcoperez
wants to merge
9
commits into
scala-ide:master
Choose a base branch
from
pfcoperez:implement_methods
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Implement methods #199
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
057be89
ImplementMethods refactor: Minimum test & prepare refactor stage
pfcoperez 5231769
Unfinished perform stage
pfcoperez 9f454a0
Avoid adding implemented methods.
pfcoperez c083049
Improved tests
pfcoperez e00c2ff
Add methods even when a template kind is selected.
pfcoperez 2cc2155
Additional tests
pfcoperez 2baf089
Implement methods not only from the selected template but also from i…
pfcoperez 7b0f633
Implement not only methods but also values.
pfcoperez 034a062
"Implement methods" now is able to implement abstract type tags.
pfcoperez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
143 changes: 143 additions & 0 deletions
143
src/main/scala/scala/tools/refactoring/implementations/ImplementMethods.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package scala.tools.refactoring.implementations | ||
|
||
import scala.tools.refactoring.common.InteractiveScalaCompiler | ||
import scala.tools.refactoring.transformation.TreeFactory | ||
import scala.tools.refactoring.{MultiStageRefactoring, analysis} | ||
|
||
abstract class ImplementMethods extends MultiStageRefactoring with analysis.Indexes with TreeFactory with InteractiveScalaCompiler { | ||
|
||
import global._ | ||
|
||
case class PreparationResult(targetTemplate: Template, memberToImplement: Seq[MemberDef]) | ||
|
||
/* Helper class to box members so they can be | ||
compared in terms of their signature. | ||
*/ | ||
implicit class OverloadedMember(val member: MemberDef) { | ||
|
||
private val key = member match { | ||
case method: DefDef => | ||
import method.{name, tparams, vparamss} | ||
|
||
val vparamsTypes = for { | ||
paramList <- vparamss | ||
param <- paramList | ||
} yield param.tpt.tpe | ||
|
||
(name, tparams, vparamsTypes) | ||
|
||
case ValDef(_, name, _, _) => 'valdef -> name.toString.trim | ||
case TypeDef(_, name, _, _) => 'typedef -> name.toString.trim | ||
} | ||
|
||
override def hashCode(): RunId = | ||
key.hashCode() | ||
|
||
override def equals(obj: scala.Any): Boolean = obj match { | ||
case that: OverloadedMember => key == that.key | ||
case _ => false | ||
} | ||
} | ||
|
||
private def templateAncestry(template: Template): List[Template] = | ||
template :: { | ||
for { | ||
parent <- template.parents | ||
parentImp <- index.declaration(parent.symbol).toList collect { | ||
case ClassDef(_, _, _, impl) => impl | ||
} | ||
ancestor <- templateAncestry(parentImp) | ||
} yield ancestor | ||
} | ||
|
||
override def prepare(s: Selection): Either[PreparationError, PreparationResult] = { | ||
|
||
|
||
// Expand the selection to the concrete type when a kind was initially selected. | ||
val maybeSelectedTemplate = (s::s.expandToNextEnclosingTree.toList) flatMap { sel: Selection => | ||
index.declaration(sel.enclosingTree.symbol) | ||
} collectFirst { | ||
case templateDeclaration: ClassDef => templateDeclaration.impl | ||
} | ||
|
||
// Get a sequence of members (types, methods or values) found in the selected mixed trait | ||
val membersToImplement = { | ||
|
||
val rawList = for { | ||
selectedTemplate <- maybeSelectedTemplate.toList | ||
selectedDeclaration <- templateAncestry(selectedTemplate) | ||
unimplementedMember <- selectedDeclaration.body collect { | ||
case defOrValDef: ValOrDefDef if defOrValDef.rhs.isEmpty => defOrValDef | ||
case typeDef: TypeDef if !typeDef.rhs.hasExistingSymbol => | ||
typeDef | ||
} | ||
} yield unimplementedMember | ||
|
||
val (uniqueMembers, _) = | ||
rawList.foldRight((List.empty[MemberDef], Set.empty[OverloadedMember])) { | ||
case (member, (l, visited)) if !visited.contains(member) => | ||
(member::l, visited + member) | ||
case (_, st) => st | ||
} | ||
uniqueMembers | ||
} | ||
|
||
// Use the selection to find the template where the members should be implemented. | ||
val targetTemplate = s.expandToNextEnclosingTree.flatMap { | ||
_.selectedSymbolTree collect { | ||
case target: Template => target | ||
} | ||
} | ||
|
||
targetTemplate map { t => // If the selection has indeed a target template... | ||
if(membersToImplement.isEmpty) Left { //... as long as there are members in the mixed trait... | ||
PreparationError("There are not members to implement") | ||
} else Right { //... these are selected to be defined in the target template. | ||
// If and only if they were not already defined there. | ||
val implementedMembers: Set[OverloadedMember] = { | ||
t.body collect { | ||
case memberDef: MemberDef => new OverloadedMember(memberDef) | ||
} toSet | ||
} | ||
PreparationResult(t, membersToImplement.filterNot(implementedMembers contains _)) | ||
} | ||
} getOrElse Left { | ||
PreparationError("No target class in selection") | ||
} | ||
} | ||
|
||
override type RefactoringParameters = Unit | ||
|
||
override def perform(selection: Selection, prepared: PreparationResult, params: RefactoringParameters) = { | ||
import prepared._ | ||
|
||
val findTemplate = filter { | ||
case t: Template => t == targetTemplate | ||
} | ||
|
||
val addMembers = transform { | ||
case tpl @ Template(_, _, body) if tpl == prepared.targetTemplate => | ||
val unimplementedSentence = Ident("???") | ||
val thisType = SingletonTypeTree(This(TypeName(""))) | ||
|
||
val membersWithRhs = memberToImplement collect { | ||
case methodDef: DefDef => | ||
methodDef copy (rhs = Block(unimplementedSentence :: Nil, EmptyTree)) | ||
case valueDef: ValDef => | ||
valueDef copy (rhs = unimplementedSentence) | ||
case typeDef: TypeDef => | ||
typeDef copy (rhs = thisType) | ||
} | ||
|
||
tpl.copy(body = body ++ membersWithRhs).replaces(tpl) | ||
} | ||
|
||
val transformation = topdown { | ||
matchingChildren { | ||
findTemplate &> | ||
addMembers | ||
} | ||
} | ||
Right(transformFile(selection.file, transformation)) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally I think that it will be quite hard to modify trees. The way how you determine unimplemented methods/fields from tree is done OK, but it is safer to modify source code directly, not its tree. There is
scala.tools.refactoring.util.Movement
util to walk on source. Generally I have to look at this closer.