-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace java reflection with a macro based solution
Necessary for cross compilation with scala native, since it does not offer any reflection functionalities. Instead of the previous method, we create a mapping between strings (pointed out by the dialectOverride in scalafmt.conf) and methods that allow us to replace dialect values.
- Loading branch information
Showing
4 changed files
with
129 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
scalafmt-macros/shared/src/main/scala/org/scalafmt/config/DialectMacro.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.scalafmt.config | ||
|
||
import scala.meta.Dialect | ||
|
||
import scala.language.experimental.macros | ||
import scala.reflect.macros.blackbox | ||
|
||
// Builds a map between string (the scalafmt method name) | ||
// and dialect method application | ||
private[scalafmt] object DialectMacro { | ||
def dialectMap: Map[String, ((Dialect, Any) => Dialect)] = | ||
macro dialectMap_impl | ||
|
||
def dialectMap_impl( | ||
c: blackbox.Context, | ||
): c.Expr[Map[String, ((Dialect, Any) => Dialect)]] = { | ||
import c.universe._ | ||
val methods = typeOf[Dialect].members.flatMap { | ||
case v: MethodSymbol => v.paramLists match { | ||
case (param :: Nil) :: Nil => // single parameter | ||
val methodName = v.name | ||
val methodNameStr = methodName.toString | ||
if (methodNameStr.startsWith("with")) { | ||
val tpe = param.typeSignature | ||
Some(q"$methodNameStr -> ((dialect: scala.meta.Dialect, v: Any) => dialect.$methodName(v.asInstanceOf[$tpe]))") | ||
} else None | ||
case _ => None | ||
} | ||
case _ => None | ||
} | ||
c.Expr[Map[String, ((Dialect, Any) => Dialect)]]( | ||
q"""scala.collection.immutable.Map(..$methods)""", | ||
) | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
scalafmt-tests/src/test/scala/org/scalafmt/config/ConfigDialectOverrideTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package org.scalafmt.config | ||
|
||
import scala.meta.Dialect | ||
|
||
import munit.FunSuite | ||
|
||
class ConfigDialectOverrideTest extends FunSuite { | ||
|
||
// toplevelSeparator is never actually used, | ||
// but as the only non-boolean Dialect value it makes for a good test | ||
test("dialect override - non boolean setting") { | ||
ScalafmtConfig.fromHoconString( | ||
"""| | ||
|runner.dialectOverride.toplevelSeparator = ">" | ||
|runner.dialect = scala213 | ||
|""".stripMargin, | ||
).get | ||
} | ||
|
||
test("throws on an incorrect type of setting") { | ||
intercept[java.util.NoSuchElementException] { | ||
ScalafmtConfig.fromHoconString( | ||
"""| | ||
|runner.dialectOverride.toplevelSeparator = true | ||
|runner.dialect = scala213 | ||
|""".stripMargin, | ||
).get | ||
} | ||
} | ||
|
||
def testBooleanFlag(methodName: String, getter: Dialect => Boolean): Unit = { | ||
def makeBooleanConfig(setting: String, value: Boolean) = ScalafmtConfig | ||
.fromHoconString( | ||
s"""| | ||
|runner.dialectOverride.$setting = $value | ||
|runner.dialect = scala213 | ||
|""".stripMargin, | ||
).get | ||
Seq(true, false).foreach { flag => | ||
test(s"boolean flag: $methodName($flag)") { | ||
assertEquals( | ||
getter(makeBooleanConfig(methodName, flag).runner.getDialect), | ||
flag, | ||
) | ||
} | ||
} | ||
} | ||
|
||
testBooleanFlag("allowFewerBraces", _.allowFewerBraces) | ||
testBooleanFlag("withAllowFewerBraces", _.allowFewerBraces) | ||
testBooleanFlag("useInfixTypePrecedence", _.useInfixTypePrecedence) | ||
testBooleanFlag("withUseInfixTypePrecedence", _.useInfixTypePrecedence) | ||
testBooleanFlag( | ||
"allowImplicitByNameParameters", | ||
_.allowImplicitByNameParameters, | ||
) | ||
testBooleanFlag( | ||
"withAllowImplicitByNameParameters", | ||
_.allowImplicitByNameParameters, | ||
) | ||
testBooleanFlag("allowSignificantIndentation", _.allowSignificantIndentation) | ||
testBooleanFlag( | ||
"withAllowSignificantIndentation", | ||
_.allowSignificantIndentation, | ||
) | ||
|
||
test("applying generated boolean map elements does not result in errors") { | ||
val omittedMethods = Set( | ||
"withToplevelSeparator", // non-boolean | ||
"withAllowMultilinePrograms", // unimplemented in scalameta (???) | ||
"withAllowTermUnquotes", // unimplemented in scalameta (???) | ||
"withAllowPatUnquotes", // unimplemented in scalameta (???) | ||
) | ||
val generatedMap = DialectMacro.dialectMap | ||
val baseDialect = scala.meta.dialects.Scala213 | ||
generatedMap.keys.filter(!omittedMethods.contains(_)).foreach { key => | ||
generatedMap(key)(baseDialect, true) | ||
} | ||
} | ||
} |