Skip to content

Commit

Permalink
Updated diagrams macro for scala 2 and 3 to render xml literal consis…
Browse files Browse the repository at this point in the history
…tently between scala versions.
  • Loading branch information
cheeseng committed Oct 7, 2022
1 parent 3163a44 commit 89de09f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 43 deletions.
Expand Up @@ -42,6 +42,17 @@ object DiagramsMacro {
case '{ $x: t } => '{ DiagrammedExpr.simpleExpr[t]($x, ${ getAnchor(term) } ) }.asTerm
}

def xmlSugarExpr(term: Term): Term = term.asExpr match {
case '{ $x: t } => '{
DiagrammedExpr.simpleExpr[t]($x, ${
// https://docs.scala-lang.org/scala3/reference/metaprogramming/reflection.html#positions
val anchor = expr.pos.startColumn - Position.ofMacroExpansion.startColumn
val c = expr.pos.sourceCode.getOrElse("<none>").head
Expr(anchor - (if (c == '<') 0 else 1))
} )
}.asTerm
}

def getAnchorForSelect(sel: Select): Expr[Int] = {
if (sel.name == "unary_!")
Expr(sel.pos.startColumn - Position.ofMacroExpansion.startColumn)
Expand All @@ -54,7 +65,8 @@ object DiagramsMacro {
def getAnchor(expr: Term): Expr[Int] = {
// -1 to match scala2 position
// Expr((expr.asTerm.pos.endColumn + expr.asTerm.pos.startColumn - 1) / 2 - Position.ofMacroExpansion.startColumn)
Expr(expr.pos.startColumn - Position.ofMacroExpansion.startColumn)
//val line = expr.pos.sourceCode.getOrElse("").split('\n').apply(expr.pos.startLine - 1)
Expr(expr.pos.startColumn - Position.ofMacroExpansion.startColumn)// - adjustment
}

def handleArgs(argTps: List[TypeRepr], args: List[Term]): (List[Term], List[Term]) =
Expand All @@ -71,9 +83,9 @@ object DiagramsMacro {
}

expr match {
case Apply(Select(New(_), _), _) => default(expr)
case apply: Apply if isXmlSugar(apply) => xmlSugarExpr(expr)

case apply: Apply if isXmlSugar(apply) => default(expr)
case Apply(Select(New(_), _), _) => default(expr)

case apply: Apply if isJavaStatic(apply) => default(expr)

Expand Down
Expand Up @@ -2455,10 +2455,9 @@ class DiagramsSpec extends AnyFunSpec with Matchers with Diagrams {
"""
|
|assert(<person>Dude</person> == <person>Mary</person>)
| | | |
| | | <person>Mary</person>
| | false
| <person>Dude</person>
| | | |
| <person>Dude</person> | <person>Mary</person>
| false
|""".stripMargin
)
)
Expand Down Expand Up @@ -4918,10 +4917,9 @@ class DiagramsSpec extends AnyFunSpec with Matchers with Diagrams {
"""this is a clue
|
|assert(<person>Dude</person> == <person>Mary</person>, "this is a clue")
| | | |
| | | <person>Mary</person>
| | false
| <person>Dude</person>
| | | |
| <person>Dude</person> | <person>Mary</person>
| false
|""".stripMargin
)
)
Expand Down Expand Up @@ -7384,10 +7382,9 @@ class DiagramsSpec extends AnyFunSpec with Matchers with Diagrams {
"""
|
|assume(<person>Dude</person> == <person>Mary</person>)
| | | |
| | | <person>Mary</person>
| | false
| <person>Dude</person>
| | | |
| <person>Dude</person> | <person>Mary</person>
| false
|""".stripMargin
)
)
Expand Down Expand Up @@ -9849,10 +9846,9 @@ class DiagramsSpec extends AnyFunSpec with Matchers with Diagrams {
"""this is a clue
|
|assume(<person>Dude</person> == <person>Mary</person>, "this is a clue")
| | | |
| | | <person>Mary</person>
| | false
| <person>Dude</person>
| | | |
| <person>Dude</person> | <person>Mary</person>
| false
|""".stripMargin
)
)
Expand Down
Expand Up @@ -2455,10 +2455,9 @@ class DirectDiagrammedAssertionsSpec extends AnyFunSpec with org.scalatest.match
"""
|
|org.scalatest.diagrams.Diagrams.assert(<person>Dude</person> == <person>Mary</person>)
| | | |
| | | <person>Mary</person>
| | false
| <person>Dude</person>
| | | |
| <person>Dude</person> | <person>Mary</person>
| false
|""".stripMargin
)
)
Expand Down Expand Up @@ -4914,10 +4913,9 @@ class DirectDiagrammedAssertionsSpec extends AnyFunSpec with org.scalatest.match
"""this is a clue
|
|org.scalatest.diagrams.Diagrams.assert(<person>Dude</person> == <person>Mary</person>, "this is a clue")
| | | |
| | | <person>Mary</person>
| | false
| <person>Dude</person>
| | | |
| <person>Dude</person> | <person>Mary</person>
| false
|""".stripMargin
)
)
Expand Down Expand Up @@ -7373,10 +7371,9 @@ class DirectDiagrammedAssertionsSpec extends AnyFunSpec with org.scalatest.match
"""
|
|org.scalatest.diagrams.Diagrams.assume(<person>Dude</person> == <person>Mary</person>)
| | | |
| | | <person>Mary</person>
| | false
| <person>Dude</person>
| | | |
| <person>Dude</person> | <person>Mary</person>
| false
|""".stripMargin
)
)
Expand Down Expand Up @@ -9832,10 +9829,9 @@ class DirectDiagrammedAssertionsSpec extends AnyFunSpec with org.scalatest.match
"""this is a clue
|
|org.scalatest.diagrams.Diagrams.assume(<person>Dude</person> == <person>Mary</person>, "this is a clue")
| | | |
| | | <person>Mary</person>
| | false
| <person>Dude</person>
| | | |
| <person>Dude</person> | <person>Mary</person>
| false
|""".stripMargin
)
)
Expand Down
Expand Up @@ -38,14 +38,31 @@ private[diagrams] class DiagrammedExprMacro[C <: Context](val context: C) {
private[this] def getPosition(expr: Tree) = expr.pos.asInstanceOf[scala.reflect.internal.util.Position]

// this is taken from expecty and modified, the purpose is to get the anchor for the given expression
private[this] def getAnchor(expr: Tree): Int = expr match {
case Apply(x, ys) => getAnchor(x) + 0
case TypeApply(x, ys) => getAnchor(x) + 0
case _ => {
getPosition(expr) match {
case NoPosition => -1
case pos => pos.point - pos.source.lineToOffset(pos.line - 1)
}
private[this] def getAnchor(expr: Tree): Int = {
expr match {
case apply @ Apply(x, ys) if isXmlSugar(apply) =>
val anchor = getAnchor(x)
val adjustment =
getPosition(expr) match {
case NoPosition => 0
case pos =>
val line = pos.source.lineToString(pos.line - 1)
val c = line.charAt(anchor)
if (c == '<') 0 else 1
}
//val p = getPosition(expr)
//val line = p.source.lineToString(p.line - 1)
//val anchor = getAnchor(x)
//val c = line.charAt(anchor)
anchor - adjustment
case Apply(x, ys) => getAnchor(x) + 0
case TypeApply(x, ys) => ;getAnchor(x) + 0
case _ =>
getPosition(expr) match {
case NoPosition => -1
case pos =>
pos.point - pos.source.lineToOffset(pos.line - 1)
}
}
}

Expand Down

0 comments on commit 89de09f

Please sign in to comment.