Closed erik-stripe closed 6 years ago
In the specific case, I would do something like
val richDoubleMin = SymbolMatcher.normalized(Symbol("_root_.scala.runtime.RichDouble.min."))
ctx.tree.collect {
case t @ Term.ApplyInfix(lhs, richDoubleMin(_), targs, List(rhs)) =>
ctx.replaceTree(t, q"_root_.java.lang.Math.min($lhs, $rhs)".syntax)
}.asPatch
}
This will rewrite
1.2 min 2.2
to
_root_.java.lang.Math.min(1.2, 2.2)
In the above example, the quasi-quote is re-using the matched sub-trees. Is that what you were looking for?
Yes, I think I can use that to write the rules I want.
Thanks for your help!
Cool. I'm going to close this, if you have have further problems we can re-open it.
@erik-stripe tree rewriting facilities are still quite primitive, beware that .syntax
on synthetic trees (quasiquotes) will not preserve comments or formatting in the spliced tree nodes. There is no "easy way" to implement a robust rewrite yet, the token api forces you on a much lower level
min
with ,
Math.min(
to left argument)
to right argumentI'm aiming to work on https://github.com/scalacenter/scalafix/issues/502 in the coming few weeks which should hopefully make tree rewriting more robust.
Hi folks,
Apologies if I misunderstood the documentation/examples, but I didn't see an obvious way to pattern match trees and then produce new (potentially unrelated) trees using subtrees that were bound during the match.
Here's a fairly concrete example:
Is there a preexisting rule that does this? If not, do you think it's possible to define a rule like this?