Cleanup in compiler modules
This commit is contained in:
+6
-6
@@ -25,7 +25,7 @@ data class Modification(val range: TextRange, val apply: (String) -> String)
|
||||
class CollectModificationsVisitor(evaluators: List<Evaluator>) : KtTreeVisitorVoid() {
|
||||
|
||||
val elementModifications: Map<Evaluator, MutableList<Modification>> =
|
||||
evaluators.toMap(selector = { it }, transform = { arrayListOf<Modification>() })
|
||||
evaluators.toMapBy(selector = { it }, transform = { arrayListOf<Modification>() })
|
||||
|
||||
override fun visitDeclaration(declaration: KtDeclaration) {
|
||||
super.visitDeclaration(declaration)
|
||||
@@ -40,10 +40,10 @@ class CollectModificationsVisitor(evaluators: List<Evaluator>) : KtTreeVisitorVo
|
||||
|
||||
if (!conditionalResult)
|
||||
modifications.add(Modification(declaration.textRange) { rangeText ->
|
||||
StringBuilder {
|
||||
buildString {
|
||||
append("/* Not available on $evaluator */")
|
||||
repeat(StringUtil.getLineBreakCount(rangeText)) { append("\n") }
|
||||
}.toString()
|
||||
}
|
||||
})
|
||||
else {
|
||||
val targetName = annotations.filterIsInstance<Conditional.TargetName>().singleOrNull()
|
||||
@@ -60,13 +60,13 @@ class CollectModificationsVisitor(evaluators: List<Evaluator>) : KtTreeVisitorVo
|
||||
}
|
||||
|
||||
fun List<Modification>.applyTo(sourceText: String): String {
|
||||
return StringBuilder {
|
||||
return buildString {
|
||||
var prevIndex = 0
|
||||
for ((range, transform) in this@applyTo) {
|
||||
append(sourceText, prevIndex, range.startOffset)
|
||||
append(transform(range.substring(sourceText)))
|
||||
prevIndex = range.endOffset
|
||||
}
|
||||
append(sourceText, prevIndex, sourceText.length())
|
||||
}.toString()
|
||||
append(sourceText, prevIndex, sourceText.length)
|
||||
}
|
||||
}
|
||||
+5
-5
@@ -36,10 +36,10 @@ public fun createJvmProfile(targetRoot: File, version: Int): Profile = Profile("
|
||||
public fun createJsProfile(targetRoot: File): Profile = Profile("JS", JsPlatformEvaluator(), File(targetRoot, "js"))
|
||||
|
||||
public val profileEvaluators: Map<String, () -> Evaluator> =
|
||||
listOf(6, 7, 8).toMap({ version -> "JVM$version" }, { version -> { JvmPlatformEvaluator(version) } }) + ("JS" to { JsPlatformEvaluator() })
|
||||
listOf(6, 7, 8).toMapBy({ version -> "JVM$version" }, { version -> { JvmPlatformEvaluator(version) } }) + ("JS" to { JsPlatformEvaluator() })
|
||||
|
||||
public fun createProfile(name: String, targetRoot: File): Profile {
|
||||
val (profileName, evaluator) = profileEvaluators.entrySet().firstOrNull { it.key.equals(name, ignoreCase = true) } ?: throw IllegalArgumentException("Profile with name '$name' is not supported")
|
||||
val (profileName, evaluator) = profileEvaluators.entries.firstOrNull { it.key.equals(name, ignoreCase = true) } ?: throw IllegalArgumentException("Profile with name '$name' is not supported")
|
||||
return Profile(profileName, evaluator(), targetRoot)
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ public class Preprocessor(val logger: Logger = SystemOutLogger) {
|
||||
class Modify(val sourceText: String, val modifications: List<Modification>) : FileProcessingResult() {
|
||||
fun getModifiedText(): String = modifications.applyTo(sourceText)
|
||||
|
||||
override fun toString(): String = "Modify(${modifications.size()})"
|
||||
override fun toString(): String = "Modify(${modifications.size})"
|
||||
}
|
||||
|
||||
override fun toString() = this.javaClass.simpleName
|
||||
@@ -89,7 +89,7 @@ public class Preprocessor(val logger: Logger = SystemOutLogger) {
|
||||
val visitor = CollectModificationsVisitor(listOf(evaluator))
|
||||
psiFile.accept(visitor)
|
||||
|
||||
val list = visitor.elementModifications.values().single()
|
||||
val list = visitor.elementModifications.values.single()
|
||||
return if (list.isNotEmpty())
|
||||
FileProcessingResult.Modify(sourceText, list)
|
||||
else
|
||||
@@ -150,7 +150,7 @@ fun String.convertLineSeparators(): String = StringUtil.convertLineSeparators(th
|
||||
|
||||
fun File.isTextEqualTo(content: String): Boolean = readText().lines() == content.lines()
|
||||
|
||||
fun File.makeRelativeTo(from: File, to: File) = File(to, relativeTo(from))
|
||||
fun File.makeRelativeTo(from: File, to: File) = File(to, toRelativeString(from))
|
||||
|
||||
fun File.mkdirsOrFail() {
|
||||
if (!mkdirs() && !exists()) {
|
||||
|
||||
+1
-1
@@ -21,7 +21,7 @@ import java.util.concurrent.Executors
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
if (args.size() != 3) {
|
||||
if (args.size != 3) {
|
||||
println("Usage: <path to sources> <output path> <profile>")
|
||||
System.exit(1)
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ public object SystemOutLogger : Logger {
|
||||
public fun Logger.withPrefix(prefix: String): Logger = PrefixedLogger(prefix, this)
|
||||
|
||||
public class PrefixedLogger(val prefix: String, val logger: Logger) : Logger {
|
||||
private fun prefix(msg: CharSequence): CharSequence = StringBuilder {
|
||||
private fun prefix(msg: CharSequence): CharSequence = StringBuilder().apply {
|
||||
append(prefix)
|
||||
append(": ")
|
||||
append(msg)
|
||||
|
||||
Reference in New Issue
Block a user