Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import com.demonwav.mcdev.util.constantValue
import com.demonwav.mcdev.util.countIsAtLeast
import com.demonwav.mcdev.util.descriptor
import com.demonwav.mcdev.util.equivalentTo
import com.demonwav.mcdev.util.findContainingClass
import com.demonwav.mcdev.util.findMethods
import com.demonwav.mcdev.util.internalName
import com.intellij.codeInsight.lookup.LookupElementBuilder
Expand Down Expand Up @@ -240,9 +241,26 @@ class AtResolver(

val project = at.project

val injector = findInjectorAnnotation(at)
val methodAttr = injector?.findAttributeValue("method")
val selectors = methodAttr?.computeStringArray()?.mapNotNull { parseMixinSelector(it, methodAttr) }.orEmpty()
val sourceElement = selectors.firstNotNullOfOrNull { selector ->
if (selector.matchMethod(targetMethod, targetClass)) {
selector.findSourceMethod(
targetClass,
targetMethod,
project,
at.resolveScope,
canDecompile = true,
)
} else {
null
}
}

// Resolve the target source class
val targetPsiClass = getTargetClass(target)
.findSourceClass(project, GlobalSearchScope.allScope(project), canDecompile = true)
val targetPsiClass = sourceElement?.findContainingClass()
?: getTargetClass(target).findSourceClass(project, GlobalSearchScope.allScope(project), canDecompile = true)
?: return emptyList()
val targetPsiFile = targetPsiClass.containingFile ?: return emptyList()

Expand All @@ -252,13 +270,16 @@ class AtResolver(

// Find the element in the desugared class, first by directly searching and then by searching in the original
// and reverse mapping it into the desugared class.
val desugaredTargetElement = when {
val desugaredTargetElement = sourceElement?.let { original ->
DesugarUtil.getOriginalToDesugaredMap(desugaredTargetClass)[original]
?.firstOrNull { it is PsiParameterListOwner }
} ?: when {
targetMethod.isClinit -> desugaredTargetClass.initializers.firstOrNull {
it.hasModifierProperty(PsiModifier.STATIC)
}
else -> desugaredTargetClass.findMethods(targetMethod.memberReference).firstOrNull()
} ?: run {
val originalTargetElement = targetMethod.findSourceElement(
val originalTargetElement = sourceElement ?: targetMethod.findSourceElement(
getTargetClass(target),
project,
GlobalSearchScope.allScope(project),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.demonwav.mcdev.platform.mixin.util.fakeResolve
import com.demonwav.mcdev.platform.mixin.util.findOrConstructSourceField
import com.demonwav.mcdev.util.Quantifier
import com.demonwav.mcdev.util.constantValue
import com.demonwav.mcdev.util.descriptor
import com.intellij.codeInsight.completion.JavaLookupElementBuilder
import com.intellij.codeInsight.lookup.LookupElementBuilder
import com.intellij.openapi.editor.Editor
Expand Down Expand Up @@ -126,16 +127,24 @@ class FieldInjectionPoint : QualifiedInjectionPoint<PsiField>() {
private val opcode: Int,
private val arrayAccess: ArrayAccessType?,
) : NavigationVisitor() {
private var bytecodeOwner: String? = null

override fun configureBytecodeTarget(classNode: ClassNode, methodNode: MethodNode) {
bytecodeOwner = classNode.name
}

override fun visitReferenceExpression(expression: PsiReferenceExpression) {
if (expression !is PsiMethodReferenceExpression) {
// early out for if the name does not match
val name = expression.referenceName
if (name == null || selector.canEverMatch(name)) {
(expression.resolve() as? PsiField)?.let { resolved ->
var matches = selector.matchField(
resolved,
QualifiedMember.resolveQualifier(expression) ?: targetClass,
)
val qualifier = QualifiedMember.resolveQualifier(expression) ?: targetClass
var matches = if (qualifier.qualifiedName == targetClass.qualifiedName && bytecodeOwner != null) {
resolved.descriptor?.let { selector.matchField(bytecodeOwner!!, resolved.name, it) } == true
} else {
selector.matchField(resolved, qualifier)
}
if (matches && opcode != -1) {
// check if we match the opcode
val isStatic = opcode == Opcodes.GETSTATIC || opcode == Opcodes.PUTSTATIC
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/platform/mixin/util/AsmUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,9 @@ fun MethodNode.findSourceElement(
return psiClass.childrenOfType<PsiClassInitializer>().firstOrNull { it.hasModifierProperty(PsiModifier.STATIC) }
?: psiClass
}
if (hasAccess(Opcodes.ACC_SYNTHETIC) && psiClass !is PsiCompiledElement) {
findAssociatedLambda(project, scope, clazz, this)?.let { return it }
}
psiClass.findMethods(memberReference).firstOrNull()?.let { return it }
if (psiClass is PsiCompiledElement) {
// don't walk into stub compiled elements to look for lambdas
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class SourceCodeLocationInfo(val index: Int, val lineNumber: Int?, val indexInLi
private var currentLine: Int? = null
private var countThisLine = 0
private var myResult: T? = null
private var indexedResult: T? = null

override fun accept(t: T): Boolean {
val line = document?.let(t::lineNumber)
Expand All @@ -70,6 +71,7 @@ class SourceCodeLocationInfo(val index: Int, val lineNumber: Int?, val indexInLi
}

if (count++ == index) {
indexedResult = t
if (lineNumber == null) {
myResult = t
return true
Expand All @@ -79,7 +81,7 @@ class SourceCodeLocationInfo(val index: Int, val lineNumber: Int?, val indexInLi
return false
}

override val result get() = myResult
override val result get() = myResult ?: indexedResult
}
}
}
Loading