|
| 1 | +/* |
| 2 | + * Minecraft Dev for IntelliJ |
| 3 | + * |
| 4 | + * https://minecraftdev.org |
| 5 | + * |
| 6 | + * Copyright (c) 2021 minecraft-dev |
| 7 | + * |
| 8 | + * MIT License |
| 9 | + */ |
| 10 | + |
| 11 | +package com.demonwav.mcdev.platform.mixin.folding |
| 12 | + |
| 13 | +import com.demonwav.mcdev.platform.mixin.MixinModuleType |
| 14 | +import com.demonwav.mcdev.platform.mixin.util.findAccessorAnnotation |
| 15 | +import com.demonwav.mcdev.platform.mixin.util.findAccessorTarget |
| 16 | +import com.demonwav.mcdev.platform.mixin.util.findInvokerAnnotation |
| 17 | +import com.demonwav.mcdev.platform.mixin.util.findInvokerTarget |
| 18 | +import com.demonwav.mcdev.util.referencedMethod |
| 19 | +import com.intellij.lang.ASTNode |
| 20 | +import com.intellij.lang.folding.CustomFoldingBuilder |
| 21 | +import com.intellij.lang.folding.FoldingDescriptor |
| 22 | +import com.intellij.openapi.editor.Document |
| 23 | +import com.intellij.openapi.editor.FoldingGroup |
| 24 | +import com.intellij.openapi.util.TextRange |
| 25 | +import com.intellij.psi.JavaRecursiveElementWalkingVisitor |
| 26 | +import com.intellij.psi.JavaTokenType |
| 27 | +import com.intellij.psi.PsiElement |
| 28 | +import com.intellij.psi.PsiIdentifier |
| 29 | +import com.intellij.psi.PsiJavaFile |
| 30 | +import com.intellij.psi.PsiMethodCallExpression |
| 31 | +import com.intellij.psi.PsiParenthesizedExpression |
| 32 | +import com.intellij.psi.PsiType |
| 33 | +import com.intellij.psi.PsiTypeCastExpression |
| 34 | +import com.intellij.psi.util.elementType |
| 35 | +import com.intellij.psi.util.parentOfType |
| 36 | + |
| 37 | +class AccessorMixinFoldingBuilder : CustomFoldingBuilder() { |
| 38 | + |
| 39 | + override fun isDumbAware(): Boolean = false |
| 40 | + |
| 41 | + override fun isRegionCollapsedByDefault(node: ASTNode): Boolean { |
| 42 | + val settings = MixinFoldingSettings.instance.state |
| 43 | + val element = node.psi |
| 44 | + val parent = element.parentOfType<PsiMethodCallExpression>() ?: return false |
| 45 | + val refMethod = parent.referencedMethod |
| 46 | + |
| 47 | + return when { |
| 48 | + refMethod == null -> false |
| 49 | + refMethod.findInvokerAnnotation() != null -> when (element) { |
| 50 | + is PsiIdentifier -> settings.foldInvokerMethodCalls |
| 51 | + is PsiParenthesizedExpression -> settings.foldInvokerCasts |
| 52 | + else -> false |
| 53 | + } |
| 54 | + refMethod.findAccessorAnnotation() != null -> when (element) { |
| 55 | + is PsiIdentifier -> settings.foldAccessorMethodCalls |
| 56 | + is PsiParenthesizedExpression -> settings.foldAccessorCasts |
| 57 | + else -> false |
| 58 | + } |
| 59 | + else -> false |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + override fun getLanguagePlaceholderText(node: ASTNode, range: TextRange): String { |
| 64 | + // Accessor parentheses |
| 65 | + if (node.elementType == JavaTokenType.LPARENTH || node.elementType == JavaTokenType.RPARENTH) { |
| 66 | + return "" |
| 67 | + } |
| 68 | + |
| 69 | + return when (val element = node.psi) { |
| 70 | + is PsiParenthesizedExpression -> foldAccessorCastExpression(element) |
| 71 | + is PsiIdentifier -> foldAccessorIdentifier(element) |
| 72 | + else -> null |
| 73 | + } ?: node.text |
| 74 | + } |
| 75 | + |
| 76 | + private fun foldAccessorIdentifier(identifier: PsiIdentifier): String? { |
| 77 | + val expr = identifier.parentOfType<PsiMethodCallExpression>() ?: return null |
| 78 | + val method = expr.referencedMethod ?: return null |
| 79 | + |
| 80 | + if (method.findInvokerAnnotation() != null) { |
| 81 | + return method.findInvokerTarget()?.element?.name |
| 82 | + } |
| 83 | + if (method.findAccessorAnnotation() != null) { |
| 84 | + val name = method.findAccessorTarget()?.element?.name ?: return null |
| 85 | + return if (method.returnType == PsiType.VOID) { |
| 86 | + "$name = " |
| 87 | + } else { |
| 88 | + name |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return null |
| 93 | + } |
| 94 | + |
| 95 | + private fun foldAccessorCastExpression(expr: PsiParenthesizedExpression): String? { |
| 96 | + val castExpression = expr.expression as? PsiTypeCastExpression ?: return null |
| 97 | + return castExpression.operand?.text |
| 98 | + } |
| 99 | + |
| 100 | + override fun buildLanguageFoldRegions( |
| 101 | + descriptors: MutableList<FoldingDescriptor>, |
| 102 | + root: PsiElement, |
| 103 | + document: Document, |
| 104 | + quick: Boolean |
| 105 | + ) { |
| 106 | + if (root !is PsiJavaFile || !MixinModuleType.isInModule(root)) { |
| 107 | + return |
| 108 | + } |
| 109 | + |
| 110 | + root.accept(Visitor(descriptors)) |
| 111 | + } |
| 112 | + |
| 113 | + private class Visitor(private val descriptors: MutableList<FoldingDescriptor>) : |
| 114 | + JavaRecursiveElementWalkingVisitor() { |
| 115 | + |
| 116 | + val settings = MixinFoldingSettings.instance.state |
| 117 | + |
| 118 | + override fun visitMethodCallExpression(expression: PsiMethodCallExpression) { |
| 119 | + super.visitMethodCallExpression(expression) |
| 120 | + |
| 121 | + if ( |
| 122 | + !settings.foldInvokerCasts && !settings.foldInvokerMethodCalls && |
| 123 | + !settings.foldAccessorCasts && !settings.foldAccessorMethodCalls |
| 124 | + ) { |
| 125 | + return |
| 126 | + } |
| 127 | + |
| 128 | + // This is what we're actually going to fold, not the PsiMethodCallExpression |
| 129 | + val referenceExpression = expression.methodExpression |
| 130 | + |
| 131 | + val refMethod = expression.referencedMethod ?: return |
| 132 | + |
| 133 | + val invoker = refMethod.findInvokerAnnotation() != null |
| 134 | + if (invoker && !settings.foldInvokerCasts && !settings.foldInvokerMethodCalls) { |
| 135 | + return |
| 136 | + } |
| 137 | + val accessor = refMethod.findAccessorAnnotation() != null |
| 138 | + if (accessor && !settings.foldAccessorCasts && !settings.foldAccessorMethodCalls) { |
| 139 | + return |
| 140 | + } |
| 141 | + |
| 142 | + // The final element must be a method name (identifier) |
| 143 | + val identifier = referenceExpression.lastChild as? PsiIdentifier ?: return |
| 144 | + if (invoker && settings.foldInvokerMethodCalls) { |
| 145 | + descriptors.add( |
| 146 | + FoldingDescriptor( |
| 147 | + identifier.node, |
| 148 | + identifier.textRange |
| 149 | + ) |
| 150 | + ) |
| 151 | + } |
| 152 | + |
| 153 | + if (accessor && settings.foldAccessorMethodCalls) { |
| 154 | + foldAccessorMethodCall(expression, identifier) |
| 155 | + } |
| 156 | + |
| 157 | + // We have folded the method call by this point |
| 158 | + // Now we need to decide to fold the cast expression |
| 159 | + // Note: There may not be a cast expression |
| 160 | + |
| 161 | + // The pattern we're expecting is something like: |
| 162 | + // ((MixinWorldAccessor) world).callSomeMethod(...) |
| 163 | + // So if there's a cast expression here it'll be inside a parenthisized expression |
| 164 | + val parenthetical = referenceExpression.firstChild as? PsiParenthesizedExpression ?: return |
| 165 | + val castExpression = parenthetical.expression as? PsiTypeCastExpression ?: return |
| 166 | + |
| 167 | + // Can't fold without an operand |
| 168 | + if (castExpression.operand == null) { |
| 169 | + return |
| 170 | + } |
| 171 | + |
| 172 | + if ((invoker && settings.foldInvokerCasts) || (accessor && settings.foldAccessorCasts)) { |
| 173 | + descriptors.add( |
| 174 | + FoldingDescriptor( |
| 175 | + parenthetical.node, |
| 176 | + parenthetical.textRange |
| 177 | + ) |
| 178 | + ) |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + private fun foldAccessorMethodCall( |
| 183 | + expression: PsiMethodCallExpression, |
| 184 | + identifier: PsiIdentifier |
| 185 | + ) { |
| 186 | + val argumentList = expression.argumentList |
| 187 | + val openParen = argumentList.firstChild |
| 188 | + val closeParen = argumentList.lastChild |
| 189 | + if (openParen.elementType !== JavaTokenType.LPARENTH || closeParen.elementType !== JavaTokenType.RPARENTH) { |
| 190 | + return |
| 191 | + } |
| 192 | + |
| 193 | + // All of these folds go together |
| 194 | + val group = FoldingGroup.newGroup("accessMethodCall") |
| 195 | + |
| 196 | + // For a getter we could do both () in a single fold |
| 197 | + // but for a setter we have to do them separate (since there's an expression in side of them) |
| 198 | + // In practice it doesn't really matter, so just always handling them separate reduce's the number of |
| 199 | + // special cases the code has to understand. |
| 200 | + descriptors.add( |
| 201 | + FoldingDescriptor( |
| 202 | + openParen.node, |
| 203 | + openParen.textRange, |
| 204 | + group |
| 205 | + ) |
| 206 | + ) |
| 207 | + descriptors.add( |
| 208 | + FoldingDescriptor( |
| 209 | + closeParen.node, |
| 210 | + closeParen.textRange, |
| 211 | + group |
| 212 | + ) |
| 213 | + ) |
| 214 | + |
| 215 | + descriptors.add( |
| 216 | + FoldingDescriptor( |
| 217 | + identifier.node, |
| 218 | + identifier.textRange, |
| 219 | + group |
| 220 | + ) |
| 221 | + ) |
| 222 | + } |
| 223 | + } |
| 224 | +} |
0 commit comments