Create from Usage: Prefer member declarations over extensions
#KT-9012 Fixed
This commit is contained in:
@@ -17,12 +17,13 @@
|
|||||||
package org.jetbrains.kotlin.idea.quickfix
|
package org.jetbrains.kotlin.idea.quickfix
|
||||||
|
|
||||||
import com.intellij.codeInsight.intention.IntentionAction
|
import com.intellij.codeInsight.intention.IntentionAction
|
||||||
|
import com.intellij.codeInsight.intention.LowPriorityAction
|
||||||
import com.intellij.openapi.editor.Editor
|
import com.intellij.openapi.editor.Editor
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.psi.PsiFile
|
import com.intellij.psi.PsiFile
|
||||||
import org.jetbrains.annotations.NotNull
|
import org.jetbrains.annotations.NotNull
|
||||||
|
|
||||||
public class QuickFixWithDelegateFactory(
|
public open class QuickFixWithDelegateFactory(
|
||||||
private val delegateFactory: () -> IntentionAction
|
private val delegateFactory: () -> IntentionAction
|
||||||
) : IntentionAction {
|
) : IntentionAction {
|
||||||
private val familyName: String
|
private val familyName: String
|
||||||
@@ -49,3 +50,7 @@ public class QuickFixWithDelegateFactory(
|
|||||||
delegateFactory().invoke(project, editor, file)
|
delegateFactory().invoke(project, editor, file)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class LowPriorityQuickFixWithDelegateFactory(
|
||||||
|
delegateFactory: () -> IntentionAction
|
||||||
|
): QuickFixWithDelegateFactory(delegateFactory), LowPriorityAction
|
||||||
+6
-3
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.idea.quickfix.createFromUsage.createCallable
|
|||||||
|
|
||||||
import com.intellij.psi.SmartPsiElementPointer
|
import com.intellij.psi.SmartPsiElementPointer
|
||||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||||
|
import org.jetbrains.kotlin.idea.quickfix.LowPriorityQuickFixWithDelegateFactory
|
||||||
import org.jetbrains.kotlin.idea.quickfix.NullQuickFix
|
import org.jetbrains.kotlin.idea.quickfix.NullQuickFix
|
||||||
import org.jetbrains.kotlin.idea.quickfix.QuickFixWithDelegateFactory
|
import org.jetbrains.kotlin.idea.quickfix.QuickFixWithDelegateFactory
|
||||||
import org.jetbrains.kotlin.idea.quickfix.createFromUsage.CreateFromUsageFactory
|
import org.jetbrains.kotlin.idea.quickfix.createFromUsage.CreateFromUsageFactory
|
||||||
@@ -30,14 +31,16 @@ public abstract class CreateCallableMemberFromUsageFactory<E : JetElement>(
|
|||||||
) : CreateFromUsageFactory<E, List<CallableInfo>>() {
|
) : CreateFromUsageFactory<E, List<CallableInfo>>() {
|
||||||
private fun newCallableQuickFix(
|
private fun newCallableQuickFix(
|
||||||
originalElementPointer: SmartPsiElementPointer<E>,
|
originalElementPointer: SmartPsiElementPointer<E>,
|
||||||
|
lowPriority: Boolean,
|
||||||
quickFixDataFactory: (SmartPsiElementPointer<E>) -> List<CallableInfo>?,
|
quickFixDataFactory: (SmartPsiElementPointer<E>) -> List<CallableInfo>?,
|
||||||
quickFixFactory: (E, List<CallableInfo>) -> CreateCallableFromUsageFixBase<E>
|
quickFixFactory: (E, List<CallableInfo>) -> CreateCallableFromUsageFixBase<E>
|
||||||
): QuickFixWithDelegateFactory {
|
): QuickFixWithDelegateFactory {
|
||||||
return QuickFixWithDelegateFactory {
|
val delegateFactory = {
|
||||||
val data = quickFixDataFactory(originalElementPointer).orEmpty()
|
val data = quickFixDataFactory(originalElementPointer).orEmpty()
|
||||||
val originalElement = originalElementPointer.element
|
val originalElement = originalElementPointer.element
|
||||||
if (data.isNotEmpty() && originalElement != null) quickFixFactory(originalElement, data) else NullQuickFix
|
if (data.isNotEmpty() && originalElement != null) quickFixFactory(originalElement, data) else NullQuickFix
|
||||||
}
|
}
|
||||||
|
return if (lowPriority) LowPriorityQuickFixWithDelegateFactory(delegateFactory) else QuickFixWithDelegateFactory(delegateFactory)
|
||||||
}
|
}
|
||||||
|
|
||||||
protected open fun createCallableInfo(element: E, diagnostic: Diagnostic): CallableInfo? = null
|
protected open fun createCallableInfo(element: E, diagnostic: Diagnostic): CallableInfo? = null
|
||||||
@@ -50,12 +53,12 @@ public abstract class CreateCallableMemberFromUsageFactory<E : JetElement>(
|
|||||||
diagnostic: Diagnostic,
|
diagnostic: Diagnostic,
|
||||||
quickFixDataFactory: (SmartPsiElementPointer<E>) -> List<CallableInfo>?
|
quickFixDataFactory: (SmartPsiElementPointer<E>) -> List<CallableInfo>?
|
||||||
): List<QuickFixWithDelegateFactory> {
|
): List<QuickFixWithDelegateFactory> {
|
||||||
val memberFix = newCallableQuickFix(originalElementPointer, quickFixDataFactory) { element, data ->
|
val memberFix = newCallableQuickFix(originalElementPointer, false, quickFixDataFactory) { element, data ->
|
||||||
CreateCallableFromUsageFix(element, data)
|
CreateCallableFromUsageFix(element, data)
|
||||||
}
|
}
|
||||||
if (!extensionsSupported) return listOf(memberFix)
|
if (!extensionsSupported) return listOf(memberFix)
|
||||||
|
|
||||||
val extensionFix = newCallableQuickFix(originalElementPointer, quickFixDataFactory) { element, data ->
|
val extensionFix = newCallableQuickFix(originalElementPointer, true, quickFixDataFactory) { element, data ->
|
||||||
CreateExtensionCallableFromUsageFix(element, data)
|
CreateExtensionCallableFromUsageFix(element, data)
|
||||||
}
|
}
|
||||||
return listOf(memberFix, extensionFix)
|
return listOf(memberFix, extensionFix)
|
||||||
|
|||||||
Reference in New Issue
Block a user