Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diagrams Macro Update #2169

Merged
merged 2 commits into from Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 Down Expand Up @@ -71,9 +82,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,27 @@ 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
}
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