Skip to content

Commit

Permalink
Merge pull request #145 from xuwei-k/removeModsFields
Browse files Browse the repository at this point in the history
separate `removeModsFields` impl
  • Loading branch information
xuwei-k authored Apr 3, 2024
2 parents bdbb49b + 1e7a5d5 commit 9dafc88
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package scalameta_ast

import scala.annotation.unused
import scala.meta.Term
import scala.meta.Tree

object RemoveNewFields {
def remove(@unused tree: Tree, @unused parsed: Term, str: String): String = str
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package scalameta_ast

import scala.meta.Defn
import scala.meta.Template
import scala.meta.Term
import scala.meta.Tree
import scala.meta.contrib._
import scala.meta.tokens.Token

object RemoveNewFields {
def remove(tree: Tree, parsed: Term, str: String): String = {
if (
tree.collectFirst {
case _: Term.If => ()
case _: Term.Match => ()
case _: Defn.Type => ()
case _: Template => ()
}.nonEmpty
) {
val positions = parsed.collect {
case x @ Term.Apply(
Term.Select(
Term.Name("Term"),
Term.Name("If")
),
_ :: _ :: _ :: last :: Nil
) =>
(x.tokens, last)
case x @ Term.Apply(
Term.Select(
Term.Name("Term"),
Term.Name("Match")
),
_ :: _ :: last :: Nil
) =>
(x.tokens, last)
case x @ Term.Apply(
Term.Select(
Term.Name("Defn"),
Term.Name("Type")
),
_ :: _ :: _ :: _ :: last :: Nil
) =>
(x.tokens, last)
case x @ Term.Apply(
Term.Name("Template"),
_ :: _ :: _ :: _ :: last :: Nil
) =>
(x.tokens, last)
}.flatMap { case (tokens, toRemove) =>
val startOpt =
tokens.reverseIterator.filter(_.is[Token.Comma]).find(_.pos.start < toRemove.pos.start).map(_.start)
val endOpt =
tokens.reverseIterator.find(_.is[Token.RightParen]).map(_.pos.end - 1)
startOpt.zip(endOpt)
}

str.zipWithIndex.flatMap { case (char, pos) =>
Option.unless(positions.exists { case (start, end) => start <= pos && pos < end }) {
char
}
}.mkString
} else {
str
}
}
}
60 changes: 1 addition & 59 deletions core/src/main/scala/scalameta_ast/ScalametaAST.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import java.util.Date
import scala.annotation.tailrec
import scala.meta._
import scala.meta.common.Convert
import scala.meta.contrib.XtensionTreeOps
import scala.meta.tokens.Token
import scala.meta.parsers.Parse
import scala.meta.parsers.Parsed
Expand Down Expand Up @@ -193,63 +192,6 @@ class ScalametaAST {
loop(Nil, str, 0, values).mkString
}

private def removeModsFields(tree: Tree, parsed: Term, str: String): String = {
if (
tree.collectFirst {
case _: Term.If => ()
case _: Term.Match => ()
case _: Defn.Type => ()
case _: Template => ()
}.nonEmpty
) {
val positions = parsed.collect {
case x @ Term.Apply(
Term.Select(
Term.Name("Term"),
Term.Name("If")
),
_ :: _ :: _ :: last :: Nil
) =>
(x.tokens, last)
case x @ Term.Apply(
Term.Select(
Term.Name("Term"),
Term.Name("Match")
),
_ :: _ :: last :: Nil
) =>
(x.tokens, last)
case x @ Term.Apply(
Term.Select(
Term.Name("Defn"),
Term.Name("Type")
),
_ :: _ :: _ :: _ :: last :: Nil
) =>
(x.tokens, last)
case x @ Term.Apply(
Term.Name("Template"),
_ :: _ :: _ :: _ :: last :: Nil
) =>
(x.tokens, last)
}.flatMap { case (tokens, toRemove) =>
val startOpt =
tokens.reverseIterator.filter(_.is[Token.Comma]).find(_.pos.start < toRemove.pos.start).map(_.start)
val endOpt =
tokens.reverseIterator.find(_.is[Token.RightParen]).map(_.pos.end - 1)
startOpt.zip(endOpt)
}

str.zipWithIndex.flatMap { case (char, pos) =>
Option.unless(positions.exists { case (start, end) => start <= pos && pos < end }) {
char
}
}.mkString
} else {
str
}
}

def convert(
src: String,
outputType: String,
Expand Down Expand Up @@ -380,7 +322,7 @@ class ScalametaAST {
addExtractor(parsed = parsed, str = str, identity) -> parsedOpt
} else {
if (a.removeNewFields) {
val str2 = removeModsFields(tree = tree, parsed = parsed, str = str)
val str2 = RemoveNewFields.remove(tree = tree, parsed = parsed, str = str)
if (a.initialExtractor) {
val parsed2 = implicitly[Parse[Term]].apply(Input.String(str2), scala.meta.dialects.Scala3).get
val parsedOpt2 = PartialFunction.condOpt(args) { case x: ScalafixRule =>
Expand Down
9 changes: 9 additions & 0 deletions core/src/main/scalajvm/scalameta_ast/RemoveNewFields.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package scalameta_ast

import scala.annotation.unused
import scala.meta.Term
import scala.meta.Tree

object RemoveNewFields {
def remove(@unused tree: Tree, @unused parsed: Term, str: String): String = str
}

0 comments on commit 9dafc88

Please sign in to comment.