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.psi.*
|
||||
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.psi.psiUtil.*
|
||||
import java.util.ArrayList
|
||||
@@ -54,9 +55,7 @@ public class AddInitKeywordFix(element: JetClassInitializer) : JetIntentionActio
|
||||
prevLeaf!!.delete()
|
||||
}
|
||||
}
|
||||
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
||||
return AddInitKeywordFix(diagnostic.getPsiElement().getNonStrictParentOfType<JetClassInitializer>() ?: return null)
|
||||
}
|
||||
override fun createAction(diagnostic: Diagnostic) = diagnostic.createIntentionForFirstParentOfType(::AddInitKeywordFix)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,8 +88,6 @@ public class AddInitKeywordFixInWholeProjectFix(elem: JetClassInitializer)
|
||||
|
||||
companion object Factory : JetSingleIntentionActionFactory() {
|
||||
override fun createAction(diagnostic: Diagnostic) =
|
||||
diagnostic.getPsiElement().getNonStrictParentOfType<JetClassInitializer>()?.let {
|
||||
AddInitKeywordFixInWholeProjectFix(it)
|
||||
}
|
||||
diagnostic.createIntentionForFirstParentOfType(::AddInitKeywordFixInWholeProjectFix)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.idea.JetBundle
|
||||
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.psi.JetClass
|
||||
import org.jetbrains.kotlin.psi.JetClassOrObject
|
||||
@@ -59,8 +60,8 @@ public class InsertDelegationCallQuickfix(val isThis: Boolean, element: JetSecon
|
||||
}
|
||||
|
||||
object InsertThisDelegationCallFactory : JetSingleIntentionActionFactory() {
|
||||
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
||||
val secondaryConstructor = diagnostic.getPsiElement().getNonStrictParentOfType<JetSecondaryConstructor>() ?: return null
|
||||
override fun createAction(diagnostic: Diagnostic) = diagnostic.createIntentionForFirstParentOfType<JetSecondaryConstructor> {
|
||||
secondaryConstructor ->
|
||||
if (secondaryConstructor.getClassOrObject().getConstructorsCount() <= 1 ||
|
||||
!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.JetSingleIntentionActionFactory
|
||||
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.ShortenReferences
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
@@ -45,10 +46,8 @@ public class ReplaceJavaClassAsAnnotationArgumentFix(
|
||||
}
|
||||
|
||||
companion object : JetSingleIntentionActionFactory() {
|
||||
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
||||
val entry = diagnostic.getPsiElement().getNonStrictParentOfType<JetAnnotationEntry>() ?: return null
|
||||
return ReplaceJavaClassAsAnnotationArgumentFix(entry)
|
||||
}
|
||||
override fun createAction(diagnostic: Diagnostic) =
|
||||
diagnostic.createIntentionForFirstParentOfType(::ReplaceJavaClassAsAnnotationArgumentFix)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,9 +79,7 @@ public class ReplaceJavaClassAsAnnotationArgumentInWholeProjectFix(
|
||||
}
|
||||
|
||||
companion object : JetSingleIntentionActionFactory() {
|
||||
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
||||
val entry = diagnostic.getPsiElement().getNonStrictParentOfType<JetAnnotationEntry>() ?: return null
|
||||
return ReplaceJavaClassAsAnnotationArgumentInWholeProjectFix(entry)
|
||||
}
|
||||
override fun createAction(diagnostic: Diagnostic) =
|
||||
diagnostic.createIntentionForFirstParentOfType<JetAnnotationEntry>(::ReplaceJavaClassAsAnnotationArgumentInWholeProjectFix)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user