Skip to content

Commit

Permalink
Address @olafurpg's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jvican committed Jan 24, 2017
1 parent 4745229 commit 29f6dd6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 37 deletions.
8 changes: 0 additions & 8 deletions core/src/main/scala/scalafix/rewrite/ExplicitImplicit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ case object ExplicitImplicit extends Rewrite {
case m.Term.ApplyType(m.Term.Name("implicitly"), _) => true
case _ => false
}
@scala.annotation.tailrec
final def parents(tree: Tree,
accum: Seq[Tree] = Seq.empty[Tree]): Seq[Tree] = {
tree.parent match {
case Some(parent) => parents(parent, parent +: accum)
case _ => accum
}
}
override def rewrite(ast: m.Tree, ctx: RewriteCtx): Seq[Patch] = {
import scala.meta._
val semantic = getSemanticApi(ctx)
Expand Down
40 changes: 16 additions & 24 deletions core/src/main/scala/scalafix/util/Patch.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,26 @@ import scala.meta._
import scala.meta.tokens.Token
import scala.meta.tokens.Token

sealed abstract class Patch {
def runOn(str: Seq[Token]): Seq[Token]
}
/**
* A patch replaces all tokens between [[from]] and [[to]] with [[replace]].
*/
case class Patch(from: Token, to: Token, replace: String) {
def insideRange(token: Token): Boolean =
token.input.eq(from.input) &&
token.end <= to.end &&
token.start >= from.start

object Patch {
case class AddLeft(tok: Token, toAdd: String) extends Patch {
override def runOn(str: Seq[Token]): Seq[Token] = str.flatMap {
case `tok` => toAdd.tokenize.get.toSeq :+ tok
case t => List(t)
val tokens = replace.tokenize.get.tokens.toSeq
def runOn(str: Seq[Token]): Seq[Token] = {
str.flatMap {
case `from` => tokens
case x if insideRange(x) => Nil
case x => Seq(x)
}
}
case class Replace(from: Token, to: Token, replace: String) extends Patch {
def insideRange(token: Token): Boolean =
token.input.eq(from.input) &&
token.end <= to.end &&
token.start >= from.start
}

val tokens = replace.tokenize.get.tokens.toSeq
def runOn(str: Seq[Token]): Seq[Token] = {
str.flatMap {
case `from` => tokens
case x if insideRange(x) => Nil
case x => Seq(x)
}
}
}
def apply(from: Token, to: Token, replace: String): Patch =
Replace(from, to, replace)
object Patch {
def verifyPatches(patches: Seq[Patch]): Unit = {
// TODO(olafur) assert there's no conflicts.
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/resources/ExplicitImplicit/basic.source
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class A {
class A {
class B { class C }
implicit val x = new B
implicit val y: x.C = new x.C
implicit val y = new x.C
}
>>>
class A {
Expand Down
6 changes: 2 additions & 4 deletions scalafix-nsc/src/main/scala/scalafix/nsc/NscSemanticApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,11 @@ trait NscSemanticApi extends ReflectToolkit {
disambiguatingNamespace: String): g.Symbol = {
val typeName = g.TypeName(name)
val typeNameLookup = in.lookup(typeName)

val termName = g.TermName(name)
val termNameLookup = in.lookup(termName)
logger.elem(termNameLookup, typeNameLookup)
val symbol =
if (typeNameLookup.exists) typeNameLookup
else {
val termName = g.TermName(name)
val termNameLookup = in.lookup(termName)
if (termNameLookup.exists) termNameLookup
else g.NoSymbol
}
Expand Down

0 comments on commit 29f6dd6

Please sign in to comment.