从某种意义上说,这是一个相差一的错误Position.column
and Position.line
是基于 1 的。
从某种意义上说,这是一个文档错误,因为他们费心记录 API,但没有费心提及这一点。
你可以编译-Yrangepos
and:
val n = pos.column - (pos.point - pos.startOrPoint) - 1
println(" " * n + "^")
或类似的指示树中最早的位置。
println(M.f(x + y))
^
Update:
让宏返回给定的表达式,然后编译-Xprint:typer -Yshow-trees
,树是内部Apply
节点,它位于+
:
Apply( // def println(x: Any): Unit in object Predef, tree.tpe=Unit
scala.this."Predef"."println" // def println(x: Any): Unit in object Predef, tree.tpe=(x: Any)Unit
Apply( // def +(x: Int): Int in class Int, tree.tpe=Int
"x"."$plus" // def +(x: Int): Int in class Int, tree.tpe=(x: Int)Int
"y" // val y: Int, tree.tpe=Int
)
)
对于“范围”位置,树顶部的位置包括其下方的所有内容。所以虽然point
是哪里+
是个start
范围位置的最早位置是范围位置所包围的所有内容的最早位置,即树中较低的所有内容。在这种情况下,左叶是x
.
所以区别point - start
告诉您要备份多远。
(我没有考虑如果由于字符编码的差异导致源文件的偏移量与列偏移量不同怎么办。)