mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[kotlin] KTIJ-29133 common interface used
GitOrigin-RevId: cdb4fba131a30819468fc347ccfad6d27e1e7770
This commit is contained in:
committed by
intellij-monorepo-bot
parent
1a19a9f717
commit
0d2955e41a
+5
-17
@@ -1,19 +1,16 @@
|
||||
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package org.jetbrains.kotlin.idea.codeinsight.api.applicable
|
||||
|
||||
import org.jetbrains.kotlin.analysis.api.KtAllowAnalysisFromWriteAction
|
||||
import org.jetbrains.kotlin.analysis.api.KtAllowAnalysisOnEdt
|
||||
import org.jetbrains.kotlin.analysis.api.KtAnalysisSession
|
||||
import org.jetbrains.kotlin.analysis.api.analyze
|
||||
import org.jetbrains.kotlin.analysis.api.lifetime.allowAnalysisFromWriteAction
|
||||
import org.jetbrains.kotlin.analysis.api.lifetime.allowAnalysisOnEdt
|
||||
import org.jetbrains.kotlin.psi.KtElement
|
||||
|
||||
/**
|
||||
* A common base interface for [org.jetbrains.kotlin.idea.codeinsight.api.applicable.intentions.AbstractKotlinApplicableModCommandIntention] and
|
||||
* [org.jetbrains.kotlin.idea.codeinsight.api.applicable.inspections.AbstractKotlinApplicableInspection].
|
||||
*/
|
||||
interface KotlinApplicableTool<ELEMENT : KtElement> : KotlinApplicableToolBase<ELEMENT> {
|
||||
/**
|
||||
* A common base interface for [org.jetbrains.kotlin.idea.codeinsight.api.applicable.intentions.AbstractKotlinApplicableModCommandIntention],
|
||||
* [org.jetbrains.kotlin.idea.codeinsight.api.applicable.intentions.AbstractKotlinModCommandWithContext], and
|
||||
* [org.jetbrains.kotlin.idea.codeinsight.api.applicable.inspections.AbstractKotlinApplicableInspection].
|
||||
*/
|
||||
|
||||
/**
|
||||
* Whether this tool is applicable to [element] by performing some resolution with the Analysis API. Any checks which don't require the
|
||||
@@ -27,12 +24,3 @@ internal fun <ELEMENT : KtElement> KotlinApplicableTool<ELEMENT>.isApplicableWit
|
||||
analyze(element) {
|
||||
isApplicableByAnalyze(element)
|
||||
}
|
||||
|
||||
@OptIn(KtAllowAnalysisOnEdt::class)
|
||||
internal fun <ELEMENT : KtElement> KotlinApplicableTool<ELEMENT>.isApplicableWithAnalyzeAllowEdt(element: ELEMENT): Boolean =
|
||||
allowAnalysisOnEdt {
|
||||
@OptIn(KtAllowAnalysisFromWriteAction::class)
|
||||
allowAnalysisFromWriteAction {
|
||||
isApplicableWithAnalyze(element)
|
||||
}
|
||||
}
|
||||
|
||||
+7
-11
@@ -3,7 +3,6 @@ package org.jetbrains.kotlin.idea.codeinsight.api.applicable.intentions
|
||||
|
||||
import com.intellij.modcommand.ActionContext
|
||||
import com.intellij.modcommand.ModPsiUpdater
|
||||
import org.jetbrains.kotlin.analysis.api.analyze
|
||||
import org.jetbrains.kotlin.idea.codeinsight.api.applicable.KotlinApplicableTool
|
||||
import org.jetbrains.kotlin.idea.codeinsight.api.applicable.isApplicableWithAnalyze
|
||||
import org.jetbrains.kotlin.psi.KtElement
|
||||
@@ -11,17 +10,14 @@ import kotlin.reflect.KClass
|
||||
|
||||
abstract class AbstractKotlinApplicableModCommandIntention<ELEMENT : KtElement>(
|
||||
elementType: KClass<ELEMENT>
|
||||
) : AbstractKotlinApplicableModCommandIntentionBase<ELEMENT>(elementType), KotlinApplicableTool<ELEMENT> {
|
||||
) : AbstractKotlinApplicableModCommandIntentionBase<ELEMENT>(elementType),
|
||||
KotlinApplicableTool<ELEMENT> {
|
||||
|
||||
override fun isElementApplicable(element: ELEMENT, context: ActionContext): Boolean {
|
||||
if (!super.isElementApplicable(element, context)) return false
|
||||
|
||||
val applicableByAnalyze = analyze(element) { isApplicableByAnalyze(element) }
|
||||
return applicableByAnalyze
|
||||
}
|
||||
|
||||
final override fun isApplicableTo(element: ELEMENT, caretOffset: Int): Boolean =
|
||||
super.isApplicableTo(element, caretOffset) && isApplicableWithAnalyze(element)
|
||||
override fun isElementApplicable(
|
||||
element: ELEMENT,
|
||||
context: ActionContext,
|
||||
): Boolean = super.isElementApplicable(element, context)
|
||||
&& isApplicableWithAnalyze(element)
|
||||
|
||||
abstract fun apply(element: ELEMENT, context: ActionContext, updater: ModPsiUpdater)
|
||||
|
||||
|
||||
+4
-20
@@ -8,10 +8,8 @@ import com.intellij.modcommand.Presentation
|
||||
import com.intellij.modcommand.PsiUpdateModCommandAction
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiFile
|
||||
import com.intellij.refactoring.suggested.startOffset
|
||||
import org.jetbrains.kotlin.idea.codeinsight.api.applicable.KotlinApplicableToolBase
|
||||
import org.jetbrains.kotlin.idea.codeinsight.api.applicable.isApplicableToElement
|
||||
import org.jetbrains.kotlin.idea.codeinsight.api.applicators.KotlinApplicabilityRange
|
||||
import org.jetbrains.kotlin.psi.KtBlockExpression
|
||||
import org.jetbrains.kotlin.psi.KtElement
|
||||
import kotlin.reflect.KClass
|
||||
@@ -26,24 +24,10 @@ abstract class AbstractKotlinApplicableModCommandIntentionBase<ELEMENT : KtEleme
|
||||
*/
|
||||
protected abstract fun getActionName(element: ELEMENT): @IntentionName String
|
||||
|
||||
override fun isElementApplicable(element: ELEMENT, context: ActionContext): Boolean {
|
||||
if (!isApplicableByPsi(element)) return false
|
||||
|
||||
val applicabilityRanges = getApplicabilityRange().getApplicabilityRanges(element)
|
||||
if (applicabilityRanges.isEmpty()) return false
|
||||
// A KotlinApplicabilityRange should be relative to the element, while `caretOffset` is absolute.
|
||||
val relativeCaretOffset = context.offset - element.startOffset
|
||||
return applicabilityRanges.any { it.containsOffset(relativeCaretOffset) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the intention's applicability based on [isApplicableByPsi] and [KotlinApplicabilityRange].
|
||||
*
|
||||
* To be invoked on a background thread only.
|
||||
*
|
||||
* @param element is a non-physical [PsiElement]
|
||||
*/
|
||||
open fun isApplicableTo(element: ELEMENT, caretOffset: Int): Boolean = isApplicableToElement(element, caretOffset)
|
||||
override fun isElementApplicable(
|
||||
element: ELEMENT,
|
||||
context: ActionContext,
|
||||
): Boolean = isApplicableToElement(element, context.offset)
|
||||
|
||||
protected open val isKotlinOnlyIntention: Boolean = true
|
||||
|
||||
|
||||
+8
-17
@@ -11,9 +11,7 @@ import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiFile
|
||||
import org.jetbrains.kotlin.analysis.api.KtAnalysisSession
|
||||
import org.jetbrains.kotlin.analysis.api.analyze
|
||||
import org.jetbrains.kotlin.idea.codeinsight.api.applicable.KotlinApplicableToolWithContext
|
||||
import org.jetbrains.kotlin.idea.codeinsight.api.applicable.isApplicableToElement
|
||||
import org.jetbrains.kotlin.idea.codeinsight.api.applicable.prepareContextWithAnalyze
|
||||
import org.jetbrains.kotlin.idea.codeinsight.api.applicable.*
|
||||
import org.jetbrains.kotlin.psi.KtBlockExpression
|
||||
import org.jetbrains.kotlin.psi.KtElement
|
||||
import kotlin.reflect.KClass
|
||||
@@ -21,7 +19,8 @@ import kotlin.reflect.KClass
|
||||
abstract class AbstractKotlinModCommandWithContext<ELEMENT : KtElement, CONTEXT>(
|
||||
clazz: KClass<ELEMENT>
|
||||
) : PsiUpdateModCommandAction<ELEMENT>(clazz.java),
|
||||
KotlinApplicableToolWithContext<ELEMENT, CONTEXT> {
|
||||
KotlinApplicableToolWithContext<ELEMENT, CONTEXT>,
|
||||
KotlinApplicableTool<ELEMENT> {
|
||||
|
||||
/**
|
||||
* @see com.intellij.codeInsight.intention.IntentionAction.getText
|
||||
@@ -37,19 +36,11 @@ abstract class AbstractKotlinModCommandWithContext<ELEMENT : KtElement, CONTEXT>
|
||||
*
|
||||
* @param element is a non-physical [PsiElement]
|
||||
*/
|
||||
override fun isElementApplicable(element: ELEMENT, context: ActionContext): Boolean {
|
||||
return isApplicableToElement(element, context.offset) && analyze(element) { isApplicableByAnalyze(element) }
|
||||
}
|
||||
|
||||
/*
|
||||
* Checks if the element is applicable performing analysis.
|
||||
*
|
||||
* To be invoked on a background thread only.
|
||||
*
|
||||
* @param element is a non-physical [PsiElement]
|
||||
*/
|
||||
context(KtAnalysisSession)
|
||||
protected open fun isApplicableByAnalyze(element: ELEMENT): Boolean = true
|
||||
override fun isElementApplicable(
|
||||
element: ELEMENT,
|
||||
context: ActionContext,
|
||||
): Boolean = isApplicableToElement(element, context.offset)
|
||||
&& isApplicableWithAnalyze(element)
|
||||
|
||||
protected open val isKotlinOnlyIntention: Boolean = true
|
||||
|
||||
|
||||
Reference in New Issue
Block a user