Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move patch operations into RewriteCtx, fixes #193. #235

Merged
merged 3 commits into from
Jun 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion readme/ImplementingRewrites.scalatex
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
on parsed abstract syntax tree nodes. The public API for patch
operations is available in PatchOps.scala

@hl.ref(wd/"scalafix-core"/"shared"/"src"/"main"/"scala"/"scalafix"/"patch"/"PatchOps.scala", start = "class SyntacticPatch")
@hl.ref(wd/"scalafix-core"/"shared"/"src"/"main"/"scala"/"scalafix"/"patch"/"PatchOps.scala", start = "trait PatchOps")

Some things are typically easier to do on the token level and other
things are easier to do on the tree level.
Expand Down
11 changes: 0 additions & 11 deletions scalafix-core/shared/src/main/scala/scalafix/package.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import scala.meta._
import scalafix.patch.SemanticPatchOps
import scalafix.patch.SyntacticPatchOps

package object scalafix {

Expand All @@ -17,15 +15,6 @@ package object scalafix {
type Patch = patch.Patch
val Patch = patch.Patch

implicit class XtensionMirrorRewriteCtx(val ctx: RewriteCtx)(
implicit val mirror: Mirror)
extends SemanticPatchOps(ctx, mirror) {
def semanticOps: SemanticPatchOps = this
}
implicit class XtensionRewriteCtx(val ctx: RewriteCtx)
extends SyntacticPatchOps(ctx) {
def semanticOps: SyntacticPatchOps = this
}
implicit class XtensionSeqPatch(patches: Iterable[Patch]) {
def asPatch: Patch = Patch.fromIterable(patches)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ object ImportPatchOps {
val trailingComma =
if (hadLeadingComma) List(Patch.empty)
else removeFirstComma(ctx.tokenList.trailing(tokens.last))
PatchOps.removeTokens(tokens) ++ trailingComma ++ leadingComma
ctx.removeTokens(tokens) ++ trailingComma ++ leadingComma
}

val leadingNewlines = isRemovedImport.map { i =>
Expand Down
40 changes: 15 additions & 25 deletions scalafix-core/shared/src/main/scala/scalafix/patch/PatchOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,25 @@ package scalafix
package patch

import scala.meta._
import scalafix.patch.TokenPatch._
import scalafix.patch.TreePatch._
import org.scalameta.FileLine
import org.scalameta.logger

object PatchOps {
def removeTokens(tokens: Tokens): Patch = tokens.foldLeft(Patch.empty)(_ + TokenPatch.Remove(_))
}

class SyntacticPatchOps(ctx: RewriteCtx) {
def removeImportee(importee: Importee): Patch = TreePatch.RemoveImportee(importee)
def replaceToken(token: Token, toReplace: String): Patch =
Add(token, "", toReplace, keepTok = false)
def removeTokens(tokens: Tokens): Patch = PatchOps.removeTokens(tokens)
def removeToken(token: Token): Patch = Add(token, "", "", keepTok = false)
def rename(from: Name, to: Name)(implicit fileLine: FileLine): Patch =
ctx.toks(from).headOption.fold(Patch.empty)(tok => Add(tok, "", to.value, keepTok = false))
def addRight(tok: Token, toAdd: String): Patch = Add(tok, "", toAdd)
def addLeft(tok: Token, toAdd: String): Patch = Add(tok, toAdd, "")
}
trait PatchOps {
// Syntactic patch ops.
def removeImportee(importee: Importee): Patch
def replaceToken(token: Token, toReplace: String): Patch
def removeTokens(tokens: Tokens): Patch
def removeToken(token: Token): Patch
def rename(from: Name, to: Name)(implicit fileLine: FileLine): Patch
def addRight(tok: Token, toAdd: String): Patch
def addLeft(tok: Token, toAdd: String): Patch

class SemanticPatchOps(ctx: RewriteCtx, mirror: Mirror) {
def removeGlobalImport(symbol: Symbol): Patch = RemoveGlobalImport(symbol)
def addGlobalImport(importer: Importer): Patch = AddGlobalImport(importer)
// Semantic patch ops.
def removeGlobalImport(symbol: Symbol)(implicit mirror: Mirror): Patch
def addGlobalImport(importer: Importer)(implicit mirror: Mirror): Patch
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this makes a lot of sense 👍

def replace(from: Symbol,
to: Term.Ref,
additionalImports: List[Importer] = Nil,
normalized: Boolean = true): Patch =
Replace(from, to, additionalImports, normalized)
def renameSymbol(from: Symbol, to: Name, normalize: Boolean = false): Patch =
RenameSymbol(from, to, normalize)
normalized: Boolean = true)(implicit mirror: Mirror): Patch
def renameSymbol(from: Symbol, to: Name, normalize: Boolean = true)(
implicit mirror: Mirror): Patch
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
package scalafix
package rewrite
import scala.meta.Tree
import scala.meta._
import scala.meta.contrib.AssociatedComments
import scala.meta.inputs.Input
import scala.meta.io.AbsolutePath
import scala.meta.tokens.Tokens
import scalafix.syntax._
import scalafix.config.ScalafixConfig
import scalafix.config.ScalafixReporter
import scalafix.patch.PatchOps
import scalafix.patch.TokenPatch
import scalafix.patch.TokenPatch.Add
import scalafix.patch.TreePatch
import scalafix.patch.TreePatch._
import scalafix.util.MatchingParens
import scalafix.util.TokenList
import org.scalameta.FileLine
import org.scalameta.logger

/** Bundle of useful things when implementing [[Rewrite]]. */
case class RewriteCtx(tree: Tree, config: ScalafixConfig) {
def syntax =
case class RewriteCtx(tree: Tree, config: ScalafixConfig) extends PatchOps {
ctx =>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is just an alias for this right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but I like the convention of calling it ctx. The ctx is not much used to implement the patches at the moment but I suspect that later we may need it to support configuration etc.

def syntax: String =
s"""${tree.input.syntax}
|${logger.revealWhitespace(tree.syntax.take(100))}""".stripMargin
override def toString: String = syntax
Expand All @@ -24,4 +31,34 @@ case class RewriteCtx(tree: Tree, config: ScalafixConfig) {
lazy val matching: MatchingParens = MatchingParens(tokens)
lazy val comments: AssociatedComments = AssociatedComments(tokens)
val reporter: ScalafixReporter = config.reporter

// Syntactic patch ops.
def removeImportee(importee: Importee): Patch =
TreePatch.RemoveImportee(importee)
def replaceToken(token: Token, toReplace: String): Patch =
Add(token, "", toReplace, keepTok = false)
def removeTokens(tokens: Tokens): Patch =
tokens.foldLeft(Patch.empty)(_ + TokenPatch.Remove(_))
def removeToken(token: Token): Patch = Add(token, "", "", keepTok = false)
def rename(from: Name, to: Name)(implicit fileLine: FileLine): Patch =
ctx
.toks(from)
.headOption
.fold(Patch.empty)(tok => Add(tok, "", to.value, keepTok = false))
def addRight(tok: Token, toAdd: String): Patch = Add(tok, "", toAdd)
def addLeft(tok: Token, toAdd: String): Patch = Add(tok, toAdd, "")

// Semantic patch ops.
def removeGlobalImport(symbol: Symbol)(implicit mirror: Mirror): Patch =
RemoveGlobalImport(symbol)
def addGlobalImport(importer: Importer)(implicit mirror: Mirror): Patch =
AddGlobalImport(importer)
def replace(from: Symbol,
to: Term.Ref,
additionalImports: List[Importer] = Nil,
normalized: Boolean = true)(implicit mirror: Mirror): Patch =
Replace(from, to, additionalImports, normalized)
def renameSymbol(from: Symbol, to: Name, normalize: Boolean = false)(
implicit mirror: Mirror): Patch =
RenameSymbol(from, to, normalize)
}