Scalaで文字列補間を実行しているときに、インデントを保持する方法があるかどうか疑問に思いました。基本的に、私は自分のStringContextを挿入できるかどうか疑問に思っていました。マクロはこの問題に対処しますが、公式になるまで待ちたいと思います。
これが私が欲しいものです:
val x = "line1 \nline2"
val str = s"> ${x}"
strは次のように評価する必要があります
> line1
line2
Scalaで文字列補間を実行しているときに、インデントを保持する方法があるかどうか疑問に思いました。基本的に、私は自分のStringContextを挿入できるかどうか疑問に思っていました。マクロはこの問題に対処しますが、公式になるまで待ちたいと思います。
これが私が欲しいものです:
val x = "line1 \nline2"
val str = s"> ${x}"
strは次のように評価する必要があります
> line1
line2
私の質問に答え、DanielSobralの非常に役立つ答えをコードに変換します。うまくいけば、それは同じ問題を抱えている他の誰かに役立つでしょう。私はまだ2.10より前なので、暗黙のクラスを使用していません。
import Indenter._
そのように文字列補間を使用しますe" $foo "
import Indenter._
object Ex extends App {
override def main(args: Array[String]) {
val name = "Foo"
val fields = "x: Int\ny:String\nz:Double"
// fields has several lines. All of them will be indented by the same amount.
print (e"""
class $name {
${fields}
}
""")
}
}
印刷する必要があります
class Foo
x: Int
y: String
z: Double
class IndentStringContext(sc: StringContext) {
def e(args: Any*):String = {
val sb = new StringBuilder()
for ((s, a) <- sc.parts zip args) {
sb append s
val ind = getindent(s)
if (ind.size > 0) {
sb append a.toString().replaceAll("\n", "\n" + ind)
} else {
sb append a.toString()
}
}
if (sc.parts.size > args.size)
sb append sc.parts.last
sb.toString()
}
// get white indent after the last new line, if any
def getindent(str: String): String = {
val lastnl = str.lastIndexOf("\n")
if (lastnl == -1) ""
else {
val ind = str.substring(lastnl + 1)
if (ind.trim.isEmpty) ind // ind is all whitespace. Use this
else ""
}
}
}
object Indenter {
// top level implicit defs allowed only in 2.10 and above
implicit def toISC(sc: StringContext) = new IndentStringContext(sc)
}
独自の補間器を作成することも、標準の補間器を独自の補間器でシャドウイングすることもできます。今、私はあなたの例の背後にあるセマンティクスが何であるかわからないので、私は試みるつもりさえありません。
SlideshareまたはSpeakerDeckのScala2.10に関する私のプレゼンテーションをチェックしてください。これらには、補間関数を記述/オーバーライドできるすべての方法の例が含まれています。スライド40から開始します(現時点では、プレゼンテーションは2.10が最終的にリリースされるまで更新される可能性があります)。
投稿2.10の回答をお探しの方へ:
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 _ => ""
}
val argLn = arg.toString
.split("\n")
val len = argLn.length
// Todo: Fix newline bugs
val indented = argLn.zipWithIndex.map {
case (s, i) =>
val res = if (i < 1) { s } else { prefix + s }
if (i == len - 1) { res } else { res + "\n" }
}.mkString
a + processed + indented
}
}
}
}
これが簡単な解決策です。Scastieでの完全なコードとテスト。そこには2つのバージョンがあります。プレーン補間器ですが、もう少し読みやすくするためにindented
少し複雑な補間器もあります。indentedWithStripMargin
assert(indentedWithStripMargin"""abc
|123456${"foo\nbar"}-${"Line1\nLine2"}""" == s"""|abc
|123456foo
| bar-Line1
| Line2""".stripMargin)
コア機能は次のとおりです。
def indentedHelper(parts: List[String], args: List[String]): String = {
// In string interpolation, there is always one more string than argument
assert(parts.size == 1+args.size)
(parts, args) match {
// The simple case is where there is one part (and therefore zero args). In that case,
// we just return the string as-is:
case (part0 :: Nil, Nil) => part0
// If there is more than one part, we can simply take the first two parts and the first arg,
// merge them together into one part, and then use recursion. In other words, we rewrite
// indented"A ${10/10} B ${2} C ${3} D ${4} E"
// as
// indented"A 1 B ${2} C ${3} D ${4} E"
// and then we can rely on recursion to rewrite that further as:
// indented"A 1 B 2 C ${3} D ${4} E"
// then:
// indented"A 1 B 2 C 3 D ${4} E"
// then:
// indented"A 1 B 2 C 3 D 4 E"
case (part0 :: part1 :: tailparts, arg0 :: tailargs) => {
// If 'arg0' has newlines in it, we will need to insert spaces. To decide how many spaces,
// we count many characters after after the last newline in 'part0'. If there is no
// newline, then we just take the length of 'part0':
val i = part0.reverse.indexOf('\n')
val n = if (i == -1)
part0.size // if no newlines in part0, we just take its length
else
i // the number of characters after the last newline
// After every newline in arg0, we must insert 'n' spaces:
val arg0WithPadding = arg0.replaceAll("\n", "\n" + " "*n)
val mergeTwoPartsAndOneArg = part0 + arg0WithPadding + part1
// recurse:
indentedHelper(mergeTwoPartsAndOneArg :: tailparts, tailargs)
}
// The two cases above are exhaustive, but the compiler thinks otherwise, hence we need
// to add this dummy.
case _ => ???
}
}