/
MacroCompat.scala
57 lines (49 loc) · 1.78 KB
/
MacroCompat.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package munit.internal
import munit.Clue
import munit.Location
import scala.quoted._
import scala.language.experimental.macros
object MacroCompat {
trait LocationMacro {
inline implicit def generate: Location = ${ locationImpl() }
implicit def generate: Location = macro MacroCompatScala2.locationImpl
}
def locationImpl()(using Quotes): Expr[Location] = {
import quotes.reflect._
val pos = Position.ofMacroExpansion
val path = pos.sourceFile.jpath.toString
val startLine = pos.startLine + 1
'{ new Location(${ Expr(path) }, ${ Expr(startLine) }) }
}
trait ClueMacro {
inline implicit def generate[T](value: T): Clue[T] = ${ clueImpl('value) }
implicit def generate[T](value: T): Clue[T] = macro
MacroCompatScala2.clueImpl
}
def clueImpl[T: Type](value: Expr[T])(using Quotes): Expr[Clue[T]] = {
import quotes.reflect._
val source = value.asTerm.pos.sourceCode.getOrElse("")
val valueType = TypeTree.of[T].show(using Printer.TreeShortCode)
'{ new Clue(${ Expr(source) }, $value, ${ Expr(valueType) }) }
}
trait CompileErrorMacro {
transparent inline def compileErrors(inline code: String): String = {
val errors = scala.compiletime.testing.typeCheckErrors(code)
errors
.map { error =>
val indent = " " * (error.column - 1)
val trimMessage = error.message.linesIterator
.map { line =>
if (line.matches(" +")) ""
else line
}
.mkString("\n")
val separator = if (error.message.contains('\n')) "\n" else " "
s"error:${separator}${trimMessage}\n${error.lineContent}\n${indent}^"
}
.mkString("\n")
}
def compileErrors(code: String): String = macro
MacroCompatScala2.compileErrorsImpl
}
}