Extract common pattern in simple intention factories
This commit is contained in:
@@ -25,6 +25,7 @@ import com.intellij.openapi.project.*
|
|||||||
import com.intellij.openapi.editor.*
|
import com.intellij.openapi.editor.*
|
||||||
import com.intellij.psi.*
|
import com.intellij.psi.*
|
||||||
import org.jetbrains.kotlin.idea.project.PluginJetFilesProvider
|
import org.jetbrains.kotlin.idea.project.PluginJetFilesProvider
|
||||||
|
import org.jetbrains.kotlin.idea.quickfix.quickfixUtil.createIntentionForFirstParentOfType
|
||||||
import org.jetbrains.kotlin.lexer.JetTokens
|
import org.jetbrains.kotlin.lexer.JetTokens
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.*
|
import org.jetbrains.kotlin.psi.psiUtil.*
|
||||||
import java.util.ArrayList
|
import java.util.ArrayList
|
||||||
@@ -54,9 +55,7 @@ public class AddInitKeywordFix(element: JetClassInitializer) : JetIntentionActio
|
|||||||
prevLeaf!!.delete()
|
prevLeaf!!.delete()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
override fun createAction(diagnostic: Diagnostic) = diagnostic.createIntentionForFirstParentOfType(::AddInitKeywordFix)
|
||||||
return AddInitKeywordFix(diagnostic.getPsiElement().getNonStrictParentOfType<JetClassInitializer>() ?: return null)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,8 +88,6 @@ public class AddInitKeywordFixInWholeProjectFix(elem: JetClassInitializer)
|
|||||||
|
|
||||||
companion object Factory : JetSingleIntentionActionFactory() {
|
companion object Factory : JetSingleIntentionActionFactory() {
|
||||||
override fun createAction(diagnostic: Diagnostic) =
|
override fun createAction(diagnostic: Diagnostic) =
|
||||||
diagnostic.getPsiElement().getNonStrictParentOfType<JetClassInitializer>()?.let {
|
diagnostic.createIntentionForFirstParentOfType(::AddInitKeywordFixInWholeProjectFix)
|
||||||
AddInitKeywordFixInWholeProjectFix(it)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
|||||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||||
import org.jetbrains.kotlin.idea.JetBundle
|
import org.jetbrains.kotlin.idea.JetBundle
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.analyzeFully
|
import org.jetbrains.kotlin.idea.caches.resolve.analyzeFully
|
||||||
|
import org.jetbrains.kotlin.idea.quickfix.quickfixUtil.createIntentionForFirstParentOfType
|
||||||
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
|
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
|
||||||
import org.jetbrains.kotlin.psi.JetClass
|
import org.jetbrains.kotlin.psi.JetClass
|
||||||
import org.jetbrains.kotlin.psi.JetClassOrObject
|
import org.jetbrains.kotlin.psi.JetClassOrObject
|
||||||
@@ -59,8 +60,8 @@ public class InsertDelegationCallQuickfix(val isThis: Boolean, element: JetSecon
|
|||||||
}
|
}
|
||||||
|
|
||||||
object InsertThisDelegationCallFactory : JetSingleIntentionActionFactory() {
|
object InsertThisDelegationCallFactory : JetSingleIntentionActionFactory() {
|
||||||
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
override fun createAction(diagnostic: Diagnostic) = diagnostic.createIntentionForFirstParentOfType<JetSecondaryConstructor> {
|
||||||
val secondaryConstructor = diagnostic.getPsiElement().getNonStrictParentOfType<JetSecondaryConstructor>() ?: return null
|
secondaryConstructor ->
|
||||||
if (secondaryConstructor.getClassOrObject().getConstructorsCount() <= 1 ||
|
if (secondaryConstructor.getClassOrObject().getConstructorsCount() <= 1 ||
|
||||||
!secondaryConstructor.hasImplicitDelegationCall()) return null
|
!secondaryConstructor.hasImplicitDelegationCall()) return null
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2015 JetBrains s.r.o.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.idea.quickfix.quickfixUtil
|
||||||
|
|
||||||
|
import com.intellij.psi.PsiElement
|
||||||
|
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||||
|
import org.jetbrains.kotlin.idea.quickfix.JetIntentionAction
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
|
||||||
|
|
||||||
|
inline fun <reified T : PsiElement> Diagnostic.createIntentionForFirstParentOfType(
|
||||||
|
factory: (T) -> JetIntentionAction<T>?
|
||||||
|
) = getPsiElement().getNonStrictParentOfType<T>()?.let(factory)
|
||||||
+5
-8
@@ -25,6 +25,7 @@ import org.jetbrains.kotlin.idea.JetBundle
|
|||||||
import org.jetbrains.kotlin.idea.quickfix.JetIntentionAction
|
import org.jetbrains.kotlin.idea.quickfix.JetIntentionAction
|
||||||
import org.jetbrains.kotlin.idea.quickfix.JetSingleIntentionActionFactory
|
import org.jetbrains.kotlin.idea.quickfix.JetSingleIntentionActionFactory
|
||||||
import org.jetbrains.kotlin.idea.quickfix.JetWholeProjectModalAction
|
import org.jetbrains.kotlin.idea.quickfix.JetWholeProjectModalAction
|
||||||
|
import org.jetbrains.kotlin.idea.quickfix.quickfixUtil.createIntentionForFirstParentOfType
|
||||||
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
|
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
|
||||||
import org.jetbrains.kotlin.idea.util.ShortenReferences
|
import org.jetbrains.kotlin.idea.util.ShortenReferences
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
@@ -45,10 +46,8 @@ public class ReplaceJavaClassAsAnnotationArgumentFix(
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object : JetSingleIntentionActionFactory() {
|
companion object : JetSingleIntentionActionFactory() {
|
||||||
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
override fun createAction(diagnostic: Diagnostic) =
|
||||||
val entry = diagnostic.getPsiElement().getNonStrictParentOfType<JetAnnotationEntry>() ?: return null
|
diagnostic.createIntentionForFirstParentOfType(::ReplaceJavaClassAsAnnotationArgumentFix)
|
||||||
return ReplaceJavaClassAsAnnotationArgumentFix(entry)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,9 +79,7 @@ public class ReplaceJavaClassAsAnnotationArgumentInWholeProjectFix(
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object : JetSingleIntentionActionFactory() {
|
companion object : JetSingleIntentionActionFactory() {
|
||||||
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
override fun createAction(diagnostic: Diagnostic) =
|
||||||
val entry = diagnostic.getPsiElement().getNonStrictParentOfType<JetAnnotationEntry>() ?: return null
|
diagnostic.createIntentionForFirstParentOfType<JetAnnotationEntry>(::ReplaceJavaClassAsAnnotationArgumentInWholeProjectFix)
|
||||||
return ReplaceJavaClassAsAnnotationArgumentInWholeProjectFix(entry)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user