Skip to content

Commit

Permalink
Add string literal completion for ME expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
Earthcomputer committed Mar 31, 2024
1 parent 3fe87de commit 77bcf22
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ class MEExpressionCompletionContributor : CompletionContributor() {
Keyword("instanceof", TailType.INSERT_SPACE)
)
)
extend(
CompletionType.BASIC,
MEExpressionCompletionUtil.STRING_LITERAL_PLACE,
object : CompletionProvider<CompletionParameters>() {
override fun addCompletions(
parameters: CompletionParameters,
context: ProcessingContext,
result: CompletionResultSet
) {
result.addAllElements(
MEExpressionCompletionUtil.getStringCompletions(
parameters.originalFile.project,
parameters.position
)
)
}
}
)
extend(
CompletionType.BASIC,
MEExpressionCompletionUtil.FROM_BYTECODE_PLACE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ object MEExpressionCompletionUtil {
NORMAL_ELEMENT,
AFTER_END_EXPRESSION_PATTERN,
)
val STRING_LITERAL_PLACE = PlatformPatterns.psiElement().withElementType(
TokenSet.create(MEExpressionTypes.TOKEN_STRING, MEExpressionTypes.TOKEN_STRING_TERMINATOR)
)
val FROM_BYTECODE_PLACE = PlatformPatterns.psiElement()
.inside(MEStatement::class.java)
.andNot(PlatformPatterns.psiElement().inside(MELitExpression::class.java))
Expand All @@ -201,6 +204,32 @@ object MEExpressionCompletionUtil {
}
}

fun getStringCompletions(project: Project, contextElement: PsiElement): List<LookupElement> {
val expressionAnnotation = contextElement.findMultiInjectionHost()?.parentOfType<PsiAnnotation>()
?: return emptyList()
if (!expressionAnnotation.hasQualifiedName(MixinConstants.MixinExtras.EXPRESSION)) {
return emptyList()
}

val modifierList = expressionAnnotation.findContainingModifierList() ?: return emptyList()

val (handler, handlerAnnotation) = modifierList.annotations.mapFirstNotNull { annotation ->
val qName = annotation.qualifiedName ?: return@mapFirstNotNull null
val handler = MixinAnnotationHandler.forMixinAnnotation(qName, project) ?: return@mapFirstNotNull null
handler to annotation
} ?: return emptyList()

return handler.resolveTarget(handlerAnnotation).flatMap {
(it as? MethodTargetMember)?.classAndMethod?.method?.instructions?.mapNotNull { insn ->
if (insn is LdcInsnNode && insn.cst is String) {
LookupElementBuilder.create(insn.cst)
} else {
null
}
} ?: emptyList()
}
}

fun getCompletionVariantsFromBytecode(project: Project, contextElement: PsiElement): List<LookupElement> {
val statement = contextElement.parentOfType<MEStatement>() ?: return emptyList()

Expand Down Expand Up @@ -641,7 +670,6 @@ object MEExpressionCompletionUtil {
)
}
}
// TODO: string literals?
}
}
is VarInsnNode -> return createLocalVariableLookups(
Expand Down

0 comments on commit 77bcf22

Please sign in to comment.