aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/com/replaymod/gradle/remap/PsiMapper.kt
diff options
context:
space:
mode:
authorJonas Herzig <me@johni0702.de>2021-03-14 11:25:33 +0100
committerJonas Herzig <me@johni0702.de>2021-03-14 11:38:19 +0100
commite7bc0828ad53c283ca9048e5b54146bf4e81e057 (patch)
tree03a0196556fae719a09dac16151ff683d4d7ada8 /src/main/kotlin/com/replaymod/gradle/remap/PsiMapper.kt
parent1dcdcb113251ae78c16f53fc62ece35f07f6caf5 (diff)
downloadRemap-e7bc0828ad53c283ca9048e5b54146bf4e81e057.tar.gz
Remap-e7bc0828ad53c283ca9048e5b54146bf4e81e057.tar.bz2
Remap-e7bc0828ad53c283ca9048e5b54146bf4e81e057.zip
Add new @Pattern feature to centralize version-aware code
That is, most of the business code should not be aware that it is being compiled to multiple versions even when it heavily interacts with MC, preprocessor statements should be an escape hatch, not the norm. Similarly, code should not be forced to do `MCVer.getWindow(mc)` instead of the much more intuitive `mc.getWindow()`, and this new preprocessor (technically remap) feature makes this possible by defining "search and replace"-like patterns (but smarter in that they are type-aware) in one or more central places which then are applied all over the code base. In a way, this is another step in the automatic back-porting process where preprocessor statements are used when we cannot yet do something automatically. Previously we "merely" automatically converted between different mapping, this new feature now also allows us to automatically perform simple refactoring tasks like changing field access to a getter+setter (e.g. `mc.getWindow()`), or changing how a method is called (e.g. `BufferBuilder.begin`), or changing a method call chain (e.g. `dispatcher.camera.getYaw()`), or most other search-and-replace-like changes and any combination of those. The only major limitation is that the replacement itself is not smart, so arguments must be kept in same order (or be temporarily assigned to local variables which then can be used in any order).
Diffstat (limited to 'src/main/kotlin/com/replaymod/gradle/remap/PsiMapper.kt')
-rw-r--r--src/main/kotlin/com/replaymod/gradle/remap/PsiMapper.kt45
1 files changed, 42 insertions, 3 deletions
diff --git a/src/main/kotlin/com/replaymod/gradle/remap/PsiMapper.kt b/src/main/kotlin/com/replaymod/gradle/remap/PsiMapper.kt
index b131478..c51b801 100644
--- a/src/main/kotlin/com/replaymod/gradle/remap/PsiMapper.kt
+++ b/src/main/kotlin/com/replaymod/gradle/remap/PsiMapper.kt
@@ -21,7 +21,11 @@ import org.jetbrains.kotlin.synthetic.SyntheticJavaPropertyDescriptor
import org.jetbrains.kotlin.synthetic.SyntheticJavaPropertyDescriptor.Companion.propertyNameByGetMethodName
import java.util.*
-internal class PsiMapper(private val map: MappingSet, private val file: PsiFile) {
+internal class PsiMapper(
+ private val map: MappingSet,
+ private val file: PsiFile,
+ private val patterns: PsiPatterns?
+) {
private val mixinMappings = mutableMapOf<String, ClassMapping<*, *>>()
private val errors = mutableListOf<Pair<Int, String>>()
private val changes = TreeMap<TextRange, String>(Comparator.comparing<TextRange, Int> { it.startOffset })
@@ -31,8 +35,9 @@ internal class PsiMapper(private val map: MappingSet, private val file: PsiFile)
errors.add(Pair(line, message))
}
- private fun replace(e: PsiElement, with: String) {
- changes[e.textRange] = with
+ private fun replace(e: PsiElement, with: String) = replace(e.textRange, with)
+ private fun replace(textRange: TextRange, with: String) {
+ changes[textRange] = with
}
private fun replaceIdentifier(parent: PsiElement, with: String) {
@@ -49,10 +54,19 @@ internal class PsiMapper(private val map: MappingSet, private val file: PsiFile)
private fun valid(e: PsiElement): Boolean {
val range = e.textRange
+ // FIXME This implementation is technically wrong but some parts of the
+ // remapper now rely on that, so fixing it is non-trivial.
+ // For a proper implementation see the TextRange version below.
val before = changes.ceilingKey(range)
return before == null || !before.intersects(range)
}
+ private fun valid(range: TextRange): Boolean {
+ val before = changes.floorKey(range) ?: TextRange.EMPTY_RANGE
+ val after = changes.ceilingKey(range) ?: TextRange.EMPTY_RANGE
+ return !before.intersectsStrict(range) && !after.intersectsStrict(range)
+ }
+
private fun getResult(text: String): Pair<String, List<Pair<Int, String>>> {
var result = text
for ((key, value) in changes.descendingMap()) {
@@ -427,7 +441,32 @@ internal class PsiMapper(private val map: MappingSet, private val file: PsiFile)
})
}
+ private fun applyPatternMatch(matcher: PsiPattern.Matcher) {
+ val changes = matcher.toChanges()
+ if (changes.all { valid(it.first) }) {
+ changes.forEach { (range, text) -> replace(range, text)}
+ } else if (changes.any { it.first !in this.changes }) {
+ System.err.println("Conflicting pattern changes in $file")
+ System.err.println("Proposed changes:")
+ changes.forEach { println("${it.first}: \"${it.second}\" (${if (valid(it.first)) "accepted" else "rejected"})") }
+ System.err.println("Current changes:")
+ this.changes.forEach { println("${it.key}: \"${it.value}\"") }
+ }
+ }
+
fun remapFile(bindingContext: BindingContext): Pair<String, List<Pair<Int, String>>> {
+ if (patterns != null) {
+ file.accept(object : JavaRecursiveElementVisitor() {
+ override fun visitCodeBlock(block: PsiCodeBlock) {
+ patterns.find(block).forEach { applyPatternMatch(it) }
+ }
+
+ override fun visitExpression(expression: PsiExpression) {
+ patterns.find(expression).forEach { applyPatternMatch(it) }
+ }
+ })
+ }
+
file.accept(object : JavaRecursiveElementVisitor() {
override fun visitClass(psiClass: PsiClass) {
val annotation = psiClass.getAnnotation(CLASS_MIXIN) ?: return