Skip to content

Commit

Permalink
Improve pretification of generated code (replacing fully qualified na…
Browse files Browse the repository at this point in the history
…mes with the type names) based on imports/packages)
  • Loading branch information
WojciechMazur committed Apr 26, 2024
1 parent 54d49eb commit 94c7453
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions rules/src/main/scala/org/virtuslab/rewrites/ZIOCodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -174,26 +174,31 @@ trait ZIOCodeGen { self: SemanticRule =>

def cleanupRules(pkgSymbols: Seq[Symbol], topLevelImportedSymbol: Seq[ImportedSymbol])(implicit
doc: SemanticDocument
): List[String => String] =
): List[String => String] = {
val allPkgSymbols = List(Symbol("scala/"), Symbol("java/lang/")) ++ pkgSymbols

List[String => String](
_.replaceAll(",(\\w)", ", $1"),
_.replace(".`package`", ""),
_.replace("scala.Predef.", ""),
_.replaceAll(raw"scala\.(\w+)$$", "$1"),
_.replaceAll(raw"scala\.(\w+)([\[\]\)\,])", "$1$2"),
_.replace("java.lang.", ""),
_.replace("zio.VersionSpecific.", "zio.")
) ++ (pkgSymbols.map(ImportedSymbol.Wildcard(_)) ++ topLevelImportedSymbol)
) ++ (allPkgSymbols.map(ImportedSymbol.Wildcard(_)) ++ topLevelImportedSymbol)
.sortBy(_.symbol.value)
.reverse
.map { importedSymbol =>
val symbol = importedSymbol.symbol
val fqcn = types.termName(symbol).syntax
importedSymbol match {
case _: ImportedSymbol.Wildcard => (s: String) => s.replace(s"$fqcn.", "")
case _: ImportedSymbol.Name => (s: String) => s.replace(s"$fqcn.", symbol.displayName)
case _: ImportedSymbol.Wildcard =>
(s: String) => s.replace(s"$fqcn.", "")
case _: ImportedSymbol.Name =>
(s: String) =>
s
.replaceAll(raw"${fqcn}$$", symbol.displayName)
.replaceAll(raw"${fqcn}([\[\]\)\,\.])", s"${symbol.displayName}$$1")
}
}
}

def cleanupSyntax(pkgSymbols: Seq[Symbol], topLevelImportedSymbol: Seq[ImportedSymbol])(body: String)(implicit doc: SemanticDocument) =
cleanupRules(pkgSymbols, topLevelImportedSymbol)
Expand Down

0 comments on commit 94c7453

Please sign in to comment.