Last active
February 14, 2019 18:01
-
-
Save busti/7d45ee2554068e1743d912084cfcf5b7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Interpolators { | |
implicit class Regex(sc: StringContext) { | |
def r = new util.matching.Regex(sc.parts.mkString, sc.parts.tail.map(_ => "x"): _*) | |
} | |
implicit class IndentHelper(val sc: StringContext) extends AnyVal { | |
import sc._ | |
def process = StringContext.treatEscapes _ | |
def ind(args: Any*): String = { | |
checkLengths(args) | |
parts.zipAll(args, "", "").foldLeft("") { case (a, (part, arg)) => | |
val processed = process(part) | |
val prefix = processed.split("\n").last match { | |
case r"""([\s|]+)$d.*""" => d | |
case _ => "3333" | |
} | |
val indented = arg | |
.toString | |
.split("\n") | |
.zipWithIndex | |
.map { case (s, i) => | |
if (i < 1) { | |
s + "\n" | |
} else { | |
prefix + s + "\n" | |
} | |
} | |
.mkString | |
a + processed + indented | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment