Skip to content

Commit c22c001

Browse files
committed
Fix InvalidInjectorMethodSignatureInspection preview
1 parent 8bd000f commit c22c001

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

src/main/kotlin/platform/mixin/inspection/injector/InvalidInjectorMethodSignatureInspection.kt

+34-24
Original file line numberDiff line numberDiff line change
@@ -330,16 +330,17 @@ class InvalidInjectorMethodSignatureInspection : MixinInspection() {
330330
}
331331
val method = startElement as PsiMethod
332332
fixParameters(project, method.parameterList, false)
333-
fixReturnType(method)
334-
fixIntLikeTypes(method, editor ?: return)
333+
fixReturnType(method, editor ?: return, file, false)
334+
fixIntLikeTypes(method, editor, false)
335335
}
336336

337337
override fun generatePreview(project: Project, editor: Editor, file: PsiFile): IntentionPreviewInfo {
338338
val method = PsiTreeUtil.findSameElementInCopy(startElement, file) as? PsiMethod
339339
?: return IntentionPreviewInfo.EMPTY
340340
fixParameters(project, method.parameterList, true)
341-
fixReturnType(method)
342-
fixIntLikeTypes(method, editor)
341+
// Pass the original startElement because the underlying fix gets the preview element itself
342+
fixReturnType(startElement as PsiMethod, editor, file, true)
343+
fixIntLikeTypes(method, editor, true)
343344
return IntentionPreviewInfo.DIFF
344345
}
345346

@@ -386,34 +387,43 @@ class InvalidInjectorMethodSignatureInspection : MixinInspection() {
386387
}
387388
}
388389

389-
private fun fixReturnType(method: PsiMethod) {
390+
private fun fixReturnType(method: PsiMethod, editor: Editor, file: PsiFile, preview: Boolean) {
390391
if (expectedReturnType == null) {
391392
return
392393
}
393-
QuickFixFactory.getInstance()
394-
.createMethodReturnFix(method, expectedReturnType, false)
395-
.applyFix()
394+
val fix = QuickFixFactory.getInstance().createMethodReturnFix(method, expectedReturnType, false)
395+
if (preview) {
396+
fix.generatePreview(file.project, editor, file)
397+
} else {
398+
fix.applyFix()
399+
}
396400
}
397401

398-
private fun fixIntLikeTypes(method: PsiMethod, editor: Editor) {
402+
private fun fixIntLikeTypes(method: PsiMethod, editor: Editor, preview: Boolean) {
399403
if (intLikeTypePositions.isEmpty()) {
400404
return
401405
}
402-
invokeLater {
403-
WriteCommandAction.runWriteCommandAction(
404-
method.project,
405-
"Choose Int-Like Type",
406-
null,
407-
{
408-
val template = makeIntLikeTypeTemplate(method, intLikeTypePositions)
409-
if (template != null) {
410-
editor.caretModel.moveToOffset(method.startOffset)
411-
TemplateManager.getInstance(method.project)
412-
.startTemplate(editor, template)
413-
}
414-
},
415-
method.parentOfType<PsiFile>()!!
416-
)
406+
val runnable = {
407+
val template = makeIntLikeTypeTemplate(method, intLikeTypePositions)
408+
if (template != null) {
409+
editor.caretModel.moveToOffset(method.startOffset)
410+
TemplateManager.getInstance(method.project)
411+
.startTemplate(editor, template)
412+
}
413+
}
414+
415+
if (preview) {
416+
runnable()
417+
} else {
418+
invokeLater {
419+
WriteCommandAction.runWriteCommandAction(
420+
method.project,
421+
"Choose Int-Like Type",
422+
null,
423+
runnable,
424+
method.parentOfType<PsiFile>()!!
425+
)
426+
}
417427
}
418428
}
419429

0 commit comments

Comments
 (0)