From 46b1afc70b37364aa45b6e89aec6c5b3652ee90a Mon Sep 17 00:00:00 2001 From: tangcent Date: Sun, 30 Jun 2024 17:46:59 +0800 Subject: [PATCH] fix: remove Non-extendable interface usage (#1146) --- gradle.properties | 2 +- .../idea/plugin/DataEventCollector.kt | 38 ----------- .../idea/plugin/actions/BasicAnAction.kt | 2 - .../idea/plugin/actions/SuvExportAction.kt | 38 +++-------- .../idea/plugin/DataEventCollectorTest.kt | 66 ------------------- .../intellij/extend/ActionContextKitTest.kt | 2 +- 6 files changed, 11 insertions(+), 137 deletions(-) delete mode 100644 idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/DataEventCollector.kt delete mode 100644 idea-plugin/src/test/kotlin/com/itangcent/idea/plugin/DataEventCollectorTest.kt diff --git a/gradle.properties b/gradle.properties index 5485d7059..7c711fde9 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,4 +3,4 @@ plugin_version=2.7.1.212.0 kotlin.code.style=official kotlin_version=1.8.0 junit_version=5.9.2 -itangcent_intellij_version=1.6.4 \ No newline at end of file +itangcent_intellij_version=1.6.6 \ No newline at end of file diff --git a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/DataEventCollector.kt b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/DataEventCollector.kt deleted file mode 100644 index 046036702..000000000 --- a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/DataEventCollector.kt +++ /dev/null @@ -1,38 +0,0 @@ -package com.itangcent.idea.plugin - -import com.google.inject.Inject -import com.intellij.openapi.actionSystem.AnActionEvent -import com.intellij.openapi.actionSystem.DataContext -import com.intellij.openapi.actionSystem.DataKey - -class DataEventCollector : DataContext { - - private val cache: LinkedHashMap = LinkedHashMap() - - @Inject - private var anActionEvent: AnActionEvent? = null - - private var dataReachable: Boolean = true - - constructor() - - constructor(anActionEvent: AnActionEvent) { - this.anActionEvent = anActionEvent - } - - override fun getData(dataId: String): Any? { - if (cache.containsKey(dataId)) { - return cache[dataId] - } - if (!dataReachable) { - return null - } - val data = anActionEvent!!.getData(DataKey.create(dataId)) - cache[dataId] = data - return data - } - - fun disableDataReach() { - dataReachable = false - } -} \ No newline at end of file diff --git a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/actions/BasicAnAction.kt b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/actions/BasicAnAction.kt index 58a37be2a..5d5ba7efd 100644 --- a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/actions/BasicAnAction.kt +++ b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/actions/BasicAnAction.kt @@ -7,9 +7,7 @@ import com.itangcent.common.spi.SpiUtils import com.itangcent.idea.config.CachedResourceResolver import com.itangcent.idea.plugin.Initializer import com.itangcent.idea.plugin.log.CustomLogConfig -import com.itangcent.idea.psi.DisableDocSupport import com.itangcent.idea.utils.ConfigurableLogger -import com.itangcent.intellij.actions.ActionEventDataContextAdaptor import com.itangcent.intellij.actions.KotlinAnAction import com.itangcent.intellij.config.resource.ResourceResolver import com.itangcent.intellij.context.ActionContext diff --git a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/actions/SuvExportAction.kt b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/actions/SuvExportAction.kt index cd673f278..430479ada 100644 --- a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/actions/SuvExportAction.kt +++ b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/actions/SuvExportAction.kt @@ -2,9 +2,7 @@ package com.itangcent.idea.plugin.actions import com.intellij.openapi.actionSystem.AnActionEvent import com.intellij.openapi.actionSystem.CommonDataKeys -import com.intellij.openapi.actionSystem.DataContext import com.intellij.openapi.project.Project -import com.itangcent.idea.plugin.DataEventCollector import com.itangcent.idea.plugin.api.export.ExportDoc import com.itangcent.idea.plugin.api.export.condition.markAsSimple import com.itangcent.idea.plugin.api.export.core.ClassExporter @@ -19,28 +17,14 @@ import com.itangcent.intellij.extend.guice.singleton import com.itangcent.intellij.extend.guice.with import com.itangcent.intellij.file.DefaultLocalFileRepository import com.itangcent.intellij.file.LocalFileRepository +import com.itangcent.intellij.psi.DataContextProvider class SuvExportAction : ApiExportAction("Export Api") { - private var dataEventCollector: DataEventCollector? = null - - override fun actionPerformed(anActionEvent: AnActionEvent) { - dataEventCollector = DataEventCollector(anActionEvent) - - load(dataEventCollector!!) - - dataEventCollector!!.disableDataReach() - - super.actionPerformed(anActionEvent) - } - override fun afterBuildActionContext(event: AnActionEvent, builder: ActionContext.ActionContextBuilder) { super.afterBuildActionContext(event, builder) - val copyDataEventCollector = dataEventCollector - builder.bind(DataContext::class) { it.toInstance(copyDataEventCollector) } - builder.bind(ClassExporter::class) { it.with(CompositeClassExporter::class).singleton() } builder.bindInstance(ExportDoc::class, ExportDoc.of("request", "methodDoc")) @@ -52,25 +36,21 @@ class SuvExportAction : ApiExportAction("Export Api") { builder.bind(MethodFilter::class) { it.with(ConfigurableMethodFilter::class).singleton() } - builder.cache("DATA_EVENT_COLLECTOR", dataEventCollector) - - dataEventCollector = null - builder.bind(PostmanApiHelper::class) { it.with(PostmanCachedApiHelper::class).singleton() } - } - private fun load(dataContext: DataContext) { - dataContext.getData(CommonDataKeys.PROJECT) - dataContext.getData(CommonDataKeys.PSI_FILE) - dataContext.getData(CommonDataKeys.NAVIGATABLE_ARRAY) - dataContext.getData(CommonDataKeys.NAVIGATABLE) - dataContext.getData(CommonDataKeys.EDITOR) - dataContext.getData(CommonDataKeys.PSI_ELEMENT) + private fun DataContextProvider.preLoadData() { + getData(CommonDataKeys.PROJECT) + getData(CommonDataKeys.PSI_FILE) + getData(CommonDataKeys.NAVIGATABLE_ARRAY) + getData(CommonDataKeys.NAVIGATABLE) + getData(CommonDataKeys.EDITOR) + getData(CommonDataKeys.PSI_ELEMENT) } override fun actionPerformed(actionContext: ActionContext, project: Project?, anActionEvent: AnActionEvent) { super.actionPerformed(actionContext, project, anActionEvent) + actionContext.instance(DataContextProvider::class).preLoadData() val multipleApiExporter = actionContext.instance(SuvApiExporter::class) multipleApiExporter.showExportWindow() } diff --git a/idea-plugin/src/test/kotlin/com/itangcent/idea/plugin/DataEventCollectorTest.kt b/idea-plugin/src/test/kotlin/com/itangcent/idea/plugin/DataEventCollectorTest.kt deleted file mode 100644 index 7870bf445..000000000 --- a/idea-plugin/src/test/kotlin/com/itangcent/idea/plugin/DataEventCollectorTest.kt +++ /dev/null @@ -1,66 +0,0 @@ -package com.itangcent.idea.plugin - -import com.google.inject.Inject -import com.intellij.openapi.actionSystem.AnActionEvent -import com.intellij.openapi.actionSystem.CommonDataKeys -import com.intellij.openapi.actionSystem.DataKey -import com.itangcent.intellij.context.ActionContext -import com.itangcent.mock.BaseContextTest -import com.itangcent.test.mock -import org.junit.jupiter.api.Assertions -import org.junit.jupiter.api.Test -import org.mockito.Mockito - -/** - * Test case for [DataEventCollector] - * - * @author tangcent - */ -internal class DataEventCollectorTest : BaseContextTest() { - - @Inject - private lateinit var dataEventCollector: DataEventCollector - - override fun bind(builder: ActionContext.ActionContextBuilder) { - builder.mock(AnActionEvent::class) { - Mockito.`when`(it.getData(CommonDataKeys.PROJECT)) - .thenReturn(mockProject) - Mockito.`when`(it.getData(DataKey.create("hello"))) - .thenReturn("world") - } - } - - @Test - fun getData() { - Assertions.assertEquals(mockProject, dataEventCollector.getData(CommonDataKeys.PROJECT)) - //cached - Assertions.assertEquals(mockProject, dataEventCollector.getData(CommonDataKeys.PROJECT)) - - Assertions.assertEquals("world", dataEventCollector.getData("hello")) - //cached - Assertions.assertEquals("world", dataEventCollector.getData("hello")) - - //create DataEventCollector by constructor - DataEventCollector(actionContext.instance(AnActionEvent::class)).let { - Assertions.assertEquals(mockProject, it.getData(CommonDataKeys.PROJECT)) - Assertions.assertEquals("world", it.getData("hello")) - } - } - - @Test - fun getDataWithDisableDataReachBeforeAccess() { - dataEventCollector.disableDataReach() - Assertions.assertNull(dataEventCollector.getData(CommonDataKeys.PROJECT)) - Assertions.assertNull(dataEventCollector.getData("hello")) - } - - @Test - fun getDataWithDisableDataReachAfterAccess() { - Assertions.assertEquals(mockProject, dataEventCollector.getData(CommonDataKeys.PROJECT)) - Assertions.assertEquals("world", dataEventCollector.getData("hello")) - dataEventCollector.disableDataReach() - //cached - Assertions.assertEquals(mockProject, dataEventCollector.getData(CommonDataKeys.PROJECT)) - Assertions.assertEquals("world", dataEventCollector.getData("hello")) - } -} \ No newline at end of file diff --git a/idea-plugin/src/test/kotlin/com/itangcent/intellij/extend/ActionContextKitTest.kt b/idea-plugin/src/test/kotlin/com/itangcent/intellij/extend/ActionContextKitTest.kt index bcbc3187b..efe62856c 100644 --- a/idea-plugin/src/test/kotlin/com/itangcent/intellij/extend/ActionContextKitTest.kt +++ b/idea-plugin/src/test/kotlin/com/itangcent/intellij/extend/ActionContextKitTest.kt @@ -233,7 +233,7 @@ internal class PsiActionContextKitTest : PluginContextLightCodeInsightFixtureTes val psiElement = mock { on(it.context).thenReturn(it) } - actionContext.cache(CommonDataKeys.PSI_ELEMENT.name, psiElement) + actionContext.cache("dataContext:${CommonDataKeys.PSI_ELEMENT.name}", psiElement) kotlin.test.assertEquals(psiElement, actionContext.findCurrentMethod()) } } \ No newline at end of file