Text replacement macros are awesome at this kind of thing. Scala doesn't come with them, but consider writing your own! Transforming e.g.
{# case Const(i) => Const(i*i); case x => x #}
to
({ case Const(i) => Const(i*i); case x => x }, """case Const(i) => Const(i*i); case x => x""")
should be pretty easy; then you just have to preprocess before you compile. If you want your IDE to not get confused by different line lengths, you can store the strings in a separate object, e.g.
{# _+7 #}/*STORE*/
goes to
({_+7}, Store.aW4)
...
object Store {
val aW4 = """_+7"""
}
//(EOF)
(For best results, base-64 encode the captured text. Nesting will work fine as long as you work recursively and are aware that nesting might happen.)