-
Notifications
You must be signed in to change notification settings - Fork 186
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is just an alias for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but I like the convention of calling it |
||
def syntax: String = | ||
s"""${tree.input.syntax} | ||
|${logger.revealWhitespace(tree.syntax.take(100))}""".stripMargin | ||
override def toString: String = syntax | ||
|
@@ -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) | ||
} |
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.
I think this makes a lot of sense 👍