From 928647462e8e2fef57aead3e81fc837064b9a23c Mon Sep 17 00:00:00 2001 From: Simon Ogorodnik Date: Sat, 7 Jul 2018 19:11:21 +0300 Subject: [PATCH] New J2K: Support general inspections with intention wrapper as quickFix --- .../idea/intentions/SelfTargetingIntention.kt | 8 +++- .../kotlin/idea/j2k/J2kPostProcessings.kt | 42 +++++++++++++++---- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/intentions/SelfTargetingIntention.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/intentions/SelfTargetingIntention.kt index 72b0f0de1bc..9af8286bc91 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/intentions/SelfTargetingIntention.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/intentions/SelfTargetingIntention.kt @@ -54,8 +54,7 @@ abstract class SelfTargetingIntention( abstract fun applyTo(element: TElement, editor: Editor?) - private fun getTarget(editor: Editor, file: PsiFile): TElement? { - val offset = editor.caretModel.offset + fun getTarget(offset: Int, file: PsiFile): TElement? { val leaf1 = file.findElementAt(offset) val leaf2 = file.findElementAt(offset - 1) val commonParent = if (leaf1 != null && leaf2 != null) PsiTreeUtil.findCommonParent(leaf1, leaf2) else null @@ -81,6 +80,11 @@ abstract class SelfTargetingIntention( return null } + fun getTarget(editor: Editor, file: PsiFile): TElement? { + val offset = editor.caretModel.offset + return getTarget(offset, file) + } + protected open fun allowCaretInsideElement(element: PsiElement): Boolean = element !is KtBlockExpression diff --git a/idea/src/org/jetbrains/kotlin/idea/j2k/J2kPostProcessings.kt b/idea/src/org/jetbrains/kotlin/idea/j2k/J2kPostProcessings.kt index ca9a42655bd..def8510ae8d 100644 --- a/idea/src/org/jetbrains/kotlin/idea/j2k/J2kPostProcessings.kt +++ b/idea/src/org/jetbrains/kotlin/idea/j2k/J2kPostProcessings.kt @@ -16,11 +16,11 @@ package org.jetbrains.kotlin.idea.j2k -import com.intellij.codeInspection.InspectionManager -import com.intellij.codeInspection.LocalInspectionToolSession -import com.intellij.codeInspection.ProblemHighlightType -import com.intellij.codeInspection.ProblemsHolder +import com.intellij.codeInspection.* +import com.intellij.codeInspection.ex.LocalInspectionToolWrapper import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.project.Project +import com.intellij.psi.PsiElement import com.intellij.psi.search.LocalSearchScope import com.intellij.psi.search.searches.ReferencesSearch import org.jetbrains.kotlin.builtins.KotlinBuiltIns @@ -83,6 +83,8 @@ object J2KPostProcessingRegistrar { registerInspectionBasedProcessing(UnnecessaryVariableInspection()) registerGeneralInspectionBasedProcessing(RedundantModalityModifierInspection()) registerGeneralInspectionBasedProcessing(RedundantVisibilityModifierInspection()) + registerGeneralInspectionBasedProcessing(RedundantExplicitTypeInspection()) + registerGeneralInspectionBasedProcessing(RedundantUnitReturnTypeInspection()) registerIntentionBasedProcessing(FoldInitializerAndIfToElvisIntention()) @@ -178,6 +180,30 @@ object J2KPostProcessingRegistrar { _processings.add(object : J2kPostProcessing { override val writeActionNeeded = false + fun QuickFix.applyFixSmart(project: Project, descriptor: D) { + if (descriptor is ProblemDescriptor) { + if (this is IntentionWrapper) { + @Suppress("NOT_YET_SUPPORTED_IN_INLINE") + fun applyIntention() { + val action = this.action as? SelfTargetingIntention ?: return + val target = action.getTarget(descriptor.psiElement.startOffset, descriptor.psiElement.containingFile) ?: return + if (!action.isApplicableTo(target, descriptor.psiElement.startOffset)) return + action.applyTo(target, null) + } + + if (this.startInWriteAction()) { + ApplicationManager.getApplication().runWriteAction(::applyIntention) + } else { + applyIntention() + } + + } + } + + ApplicationManager.getApplication().runWriteAction { + this.applyFix(project, descriptor) + } + } override fun createAction(element: KtElement, diagnostics: Diagnostics): (() -> Unit)? { val holder = ProblemsHolder(InspectionManager.getInstance(element.project), element.containingFile, false) @@ -192,11 +218,9 @@ object J2KPostProcessingRegistrar { holder.results.clear() element.accept(visitor) if (holder.hasResults()) { - ApplicationManager.getApplication().runWriteAction { - holder.results - .filter { acceptInformationLevel || it.highlightType != ProblemHighlightType.INFORMATION } - .forEach { it.fixes?.firstOrNull()?.applyFix(element.project, it) } - } + holder.results + .filter { acceptInformationLevel || it.highlightType != ProblemHighlightType.INFORMATION } + .forEach { it.fixes?.firstOrNull()?.applyFixSmart(element.project, it) } } } }