Skip to content

Commit

Permalink
Merge pull request #518 from scalameta/scalafmt-scala3
Browse files Browse the repository at this point in the history
Format scala-3 MacroCompat with scala3 runner
  • Loading branch information
valencik committed May 19, 2022
2 parents bd86dfa + 5b224a0 commit 95ee168
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
8 changes: 6 additions & 2 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = "3.5.1"
version = "3.5.4"

assumeStandardLibraryStripMargin = true

Expand All @@ -8,7 +8,11 @@ docstrings.style = Asterisk

project.git=true
project.excludeFilters = [
".*scala-3*"
"LinesSuite.scala"
]
runner.dialect = scala212
fileOverride {
"glob:**/src/{main,test}/scala-3/**" {
runner.dialect = scala3
}
}
12 changes: 8 additions & 4 deletions munit/shared/src/main/scala-2/munit/internal/MacroCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,21 @@ object MacroCompat {
def locationImpl(c: Context): c.Tree = MacroCompatScala2.locationImpl(c)

trait ClueMacro {
implicit def generate[T](value: T): Clue[T] = macro MacroCompatScala2.clueImpl
implicit def generate[T](value: T): Clue[T] =
macro MacroCompatScala2.clueImpl
}

@deprecated("Use MacroCompatScala2.clueImpl instead", "2020-01-06")
def clueImpl(c: Context)(value: c.Tree): c.Tree = MacroCompatScala2.clueImpl(c)(value)
def clueImpl(c: Context)(value: c.Tree): c.Tree =
MacroCompatScala2.clueImpl(c)(value)

trait CompileErrorMacro {
def compileErrors(code: String): String = macro MacroCompatScala2.compileErrorsImpl
def compileErrors(code: String): String =
macro MacroCompatScala2.compileErrorsImpl
}

@deprecated("Use MacroCompatScala2.compileErrorsImpl instead", "2020-01-06")
def compileErrorsImpl(c: Context)(value: c.Tree): c.Tree = MacroCompatScala2.compileErrorsImpl(c)(value)
def compileErrorsImpl(c: Context)(value: c.Tree): c.Tree =
MacroCompatScala2.compileErrorsImpl(c)(value)

}
33 changes: 19 additions & 14 deletions munit/shared/src/main/scala-3/munit/internal/MacroCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,41 @@ object MacroCompat {
val pos = Position.ofMacroExpansion
val path = pos.sourceFile.jpath.toString
val startLine = pos.startLine + 1
'{ new Location(${Expr(path)}, ${Expr(startLine)}) }
'{ 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
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)}) }
'{ new Clue(${ Expr(source) }, $value, ${ Expr(valueType) }) }
}


trait CompileErrorMacro {
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")
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
def compileErrors(code: String): String = macro
MacroCompatScala2.compileErrorsImpl
}

}

0 comments on commit 95ee168

Please sign in to comment.