|
| 1 | +package me.devnatan.inventoryframework.intellij |
| 2 | + |
| 3 | +import com.intellij.psi.PsiField |
| 4 | +import org.jetbrains.uast.UBinaryExpression |
| 5 | +import org.jetbrains.uast.UCallExpression |
| 6 | +import org.jetbrains.uast.UExpression |
| 7 | +import org.jetbrains.uast.UFile |
| 8 | +import org.jetbrains.uast.ULambdaExpression |
| 9 | +import org.jetbrains.uast.ULiteralExpression |
| 10 | +import org.jetbrains.uast.UReferenceExpression |
| 11 | +import org.jetbrains.uast.UUnaryExpression |
| 12 | +import org.jetbrains.uast.UastBinaryOperator |
| 13 | +import org.jetbrains.uast.UastPrefixOperator |
| 14 | +import org.jetbrains.uast.skipParenthesizedExprDown |
| 15 | +import org.jetbrains.uast.visitor.AbstractUastVisitor |
| 16 | + |
| 17 | +private const val FRAMEWORK_PACKAGE_PREFIX = "me.devnatan.inventoryframework" |
| 18 | +private const val ON_CLICK_METHOD = "onClick" |
| 19 | +private val ROW_COLUMN_FACTORY_METHODS = setOf("row", "firstRow", "lastRow", "column", "firstColumn", "lastColumn") |
| 20 | + |
| 21 | +class ClickActionExtractionResult(val indexed: Map<Int, PreviewClickAction>, val layoutBound: Map<Char, PreviewClickAction>) |
| 22 | + |
| 23 | +// Finds `.onClick(handler)` bindings, both the direct chain form ItemExtractor also resolves for |
| 24 | +// withItem (`slot(i).onClick(...)`, possibly with a `.withItem(...)` in between) and the row/column |
| 25 | +// factory-lambda form (`render.firstRow((pos, slot) -> slot.withItem(x).onClick(y))`), then |
| 26 | +// pattern-matches the handler body against a small, fixed vocabulary (matchClickAction below). |
| 27 | +// Anything outside that vocabulary is recorded as Unsupported rather than ignored, so the preview |
| 28 | +// can say "can't simulate this" instead of silently doing nothing when clicked. |
| 29 | +object ClickHandlerExtractor { |
| 30 | + |
| 31 | + fun extract( |
| 32 | + uFile: UFile, |
| 33 | + rows: Int, |
| 34 | + columns: Int, |
| 35 | + stateIndex: Map<PsiField, PreviewStateDeclaration>, |
| 36 | + ): ClickActionExtractionResult { |
| 37 | + val indexed = mutableMapOf<Int, PreviewClickAction>() |
| 38 | + val layoutBound = mutableMapOf<Char, PreviewClickAction>() |
| 39 | + |
| 40 | + fun apply(target: SlotTarget, action: PreviewClickAction) { |
| 41 | + when (target) { |
| 42 | + is SlotTarget.Indices -> target.slots.forEach { indexed[it] = action } |
| 43 | + is SlotTarget.Layout -> layoutBound[target.character] = action |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + uFile.accept(object : AbstractUastVisitor() { |
| 48 | + override fun visitCallExpression(node: UCallExpression): Boolean { |
| 49 | + val method = node.resolve() ?: return false |
| 50 | + val declaringClass = method.containingClass?.qualifiedName |
| 51 | + if (declaringClass == null || !declaringClass.startsWith(FRAMEWORK_PACKAGE_PREFIX)) return false |
| 52 | + val methodName = node.methodName |
| 53 | + |
| 54 | + if (methodName == ON_CLICK_METHOD && node.valueArguments.size == 1) { |
| 55 | + val receiverCall = asCallExpression(node.receiver) ?: return false |
| 56 | + val target = SlotTargetResolver.resolve(receiverCall, rows, columns) ?: return false |
| 57 | + val lambda = node.valueArguments[0].skipParenthesizedExprDown() as? ULambdaExpression ?: return false |
| 58 | + apply(target, matchClickAction(lambda, stateIndex)) |
| 59 | + return false |
| 60 | + } |
| 61 | + |
| 62 | + if (methodName in ROW_COLUMN_FACTORY_METHODS) { |
| 63 | + resolveFactoryClickAction(node, rows, columns, stateIndex)?.let { (target, action) -> |
| 64 | + apply(target, action) |
| 65 | + } |
| 66 | + return false |
| 67 | + } |
| 68 | + |
| 69 | + return false |
| 70 | + } |
| 71 | + }) |
| 72 | + |
| 73 | + return ClickActionExtractionResult(indexed, layoutBound) |
| 74 | + } |
| 75 | + |
| 76 | + private fun resolveFactoryClickAction( |
| 77 | + node: UCallExpression, |
| 78 | + rows: Int, |
| 79 | + columns: Int, |
| 80 | + stateIndex: Map<PsiField, PreviewStateDeclaration>, |
| 81 | + ): Pair<SlotTarget, PreviewClickAction>? { |
| 82 | + val (target, lambda) = SlotTargetResolver.resolveFactoryLambda(node, rows, columns) ?: return null |
| 83 | + val handlerExpr = findCallArgumentInLambda(lambda, ON_CLICK_METHOD) ?: return null |
| 84 | + val handlerLambda = handlerExpr.skipParenthesizedExprDown() as? ULambdaExpression ?: return null |
| 85 | + return target to matchClickAction(handlerLambda, stateIndex) |
| 86 | + } |
| 87 | + |
| 88 | + private fun matchClickAction( |
| 89 | + lambda: ULambdaExpression, |
| 90 | + stateIndex: Map<PsiField, PreviewStateDeclaration>, |
| 91 | + ): PreviewClickAction { |
| 92 | + val statement = singleBodyExpression(lambda.body) ?: return PreviewClickAction.Unsupported |
| 93 | + val call = asCallExpression(statement) ?: return PreviewClickAction.Unsupported |
| 94 | + val receiver = call.receiver?.skipParenthesizedExprDown() as? UReferenceExpression |
| 95 | + ?: return PreviewClickAction.Unsupported |
| 96 | + val field = receiver.resolve() as? PsiField ?: return PreviewClickAction.Unsupported |
| 97 | + val declaration = stateIndex[field] ?: return PreviewClickAction.Unsupported |
| 98 | + |
| 99 | + return when (call.methodName) { |
| 100 | + "set" -> { |
| 101 | + val value = call.valueArguments.getOrNull(0)?.skipParenthesizedExprDown() |
| 102 | + ?: return PreviewClickAction.Unsupported |
| 103 | + matchSetValue(value, field, declaration) ?: PreviewClickAction.Unsupported |
| 104 | + } |
| 105 | + // MutableIntState.increment/decrement(host) - the idiomatic alternative to |
| 106 | + // set(get(ctx) + 1, ctx), which is why both are matched independently. |
| 107 | + "increment" -> if (declaration.kind == PreviewStateKind.INT) { |
| 108 | + PreviewClickAction.Delta(declaration.id, 1) |
| 109 | + } else { |
| 110 | + PreviewClickAction.Unsupported |
| 111 | + } |
| 112 | + "decrement" -> if (declaration.kind == PreviewStateKind.INT) { |
| 113 | + PreviewClickAction.Delta(declaration.id, -1) |
| 114 | + } else { |
| 115 | + PreviewClickAction.Unsupported |
| 116 | + } |
| 117 | + else -> PreviewClickAction.Unsupported |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private fun matchSetValue(value: UExpression, field: PsiField, declaration: PreviewStateDeclaration): PreviewClickAction? { |
| 122 | + val literal = value as? ULiteralExpression |
| 123 | + if (literal != null) { |
| 124 | + return when (declaration.kind) { |
| 125 | + PreviewStateKind.BOOLEAN -> (literal.value as? Boolean)?.let { PreviewClickAction.SetLiteral(declaration.id, it) } |
| 126 | + PreviewStateKind.INT -> (literal.value as? Int)?.let { PreviewClickAction.SetLiteral(declaration.id, it) } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + if (value is UUnaryExpression && value.operator == UastPrefixOperator.LOGICAL_NOT) { |
| 131 | + if (declaration.kind != PreviewStateKind.BOOLEAN) return null |
| 132 | + return if (isGetCallOn(value.operand.skipParenthesizedExprDown(), field)) { |
| 133 | + PreviewClickAction.ToggleBoolean(declaration.id) |
| 134 | + } else { |
| 135 | + null |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + if (value is UBinaryExpression) { |
| 140 | + if (declaration.kind != PreviewStateKind.INT) return null |
| 141 | + val left = value.leftOperand.skipParenthesizedExprDown() |
| 142 | + val right = value.rightOperand.skipParenthesizedExprDown() as? ULiteralExpression |
| 143 | + val delta = right?.value as? Int ?: return null |
| 144 | + if (!isGetCallOn(left, field)) return null |
| 145 | + return when (value.operator) { |
| 146 | + UastBinaryOperator.PLUS -> PreviewClickAction.Delta(declaration.id, delta) |
| 147 | + UastBinaryOperator.MINUS -> PreviewClickAction.Delta(declaration.id, -delta) |
| 148 | + else -> null |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return null |
| 153 | + } |
| 154 | + |
| 155 | + private fun isGetCallOn(expr: UExpression, field: PsiField): Boolean { |
| 156 | + val call = asCallExpression(expr) ?: return false |
| 157 | + if (call.methodName != "get") return false |
| 158 | + val receiver = call.receiver?.skipParenthesizedExprDown() as? UReferenceExpression ?: return false |
| 159 | + return receiver.resolve() == field |
| 160 | + } |
| 161 | +} |
0 commit comments