-
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
3 changed files
with
47 additions
and
3 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
33 changes: 33 additions & 0 deletions
33
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,33 @@ | ||
package org.scalafmt.config | ||
|
||
import scala.meta.Dialect | ||
|
||
import scala.language.experimental.macros | ||
import scala.reflect.macros.blackbox | ||
|
||
// Builds a map between string and dialect method application | ||
object DialectMacro { | ||
def dialectMap(dialect: Dialect): Map[String, (Any => Dialect)] = | ||
macro dialectMap_impl | ||
|
||
def dialectMap_impl( | ||
c: blackbox.Context, | ||
)(dialect: c.Tree): c.Expr[Map[String, (Any => Dialect)]] = { | ||
import c.universe._ | ||
val mapElements = typeOf[Dialect].members.flatMap { | ||
case v: TermSymbol if v.isVal => | ||
val valName = v.name.decodedName.toString().strip() | ||
val tpe = v.typeSignature | ||
val methodName = "with" + Character.toUpperCase(valName.head) + | ||
valName.tail.strip() | ||
val methodTermName = TermName(methodName) | ||
if (typeOf[Dialect].members.exists(_.name.decodedName == methodTermName)) | ||
Some(q"""$methodName -> ((v: Any) => $dialect.$methodTermName(v.asInstanceOf[$tpe]))""") | ||
else None | ||
case _ => None | ||
} | ||
c.Expr[Map[String, (Any => Dialect)]]( | ||
q"""scala.collection.immutable.Map(..$mapElements)""", | ||
) | ||
} | ||
} |