Skip to content

Commit

Permalink
feat: support new setting 'export selected method only' (#783)
Browse files Browse the repository at this point in the history
  • Loading branch information
tangcent authored Jun 9, 2022
1 parent d79f020 commit 31d4865
Show file tree
Hide file tree
Showing 22 changed files with 183 additions and 58 deletions.
2 changes: 1 addition & 1 deletion common-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dependencies {

implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

implementation('com.itangcent:commons:1.2.4') {
implementation('com.itangcent:commons:1.2.5') {
exclude group: 'com.google.inject'
exclude group: 'com.google.code.gson'
}
Expand Down
8 changes: 4 additions & 4 deletions idea-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,17 @@ dependencies {
// implementation fileTree(dir: 'libs', include: ['*.jar'])


implementation('com.itangcent:intellij-idea:1.2.4') {
implementation('com.itangcent:intellij-idea:1.2.5') {
exclude group: 'com.google.inject'
exclude group: 'com.google.code.gson'
}

implementation('com.itangcent:intellij-kotlin-support:1.2.4') {
implementation('com.itangcent:intellij-kotlin-support:1.2.5') {
exclude group: 'com.google.inject'
exclude group: 'com.google.code.gson'
}

// implementation('com.itangcent:intellij-scala-support:1.2.4') {
// implementation('com.itangcent:intellij-scala-support:1.2.5') {
// exclude group: 'com.google.inject'
// exclude group: 'com.google.code.gson'
// }
Expand Down Expand Up @@ -122,7 +122,7 @@ dependencies {
// https://mvnrepository.com/artifact/org.mockito/mockito-inline
testImplementation group: 'org.mockito', name: 'mockito-inline', version: '3.11.0'

testImplementation('com.itangcent:intellij-idea-test:1.2.4') {
testImplementation('com.itangcent:intellij-idea-test:1.2.5') {
exclude group: 'com.nhaarman.mockitokotlin2', module: 'mockito-kotlin'
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ abstract class AnnotatedCondition<T : Annotation> : Condition, ConditionSupporte
throw IllegalArgumentException("failed get condition class of ${this::class}")
}

abstract protected fun matches(actionContext: ActionContext, annotation: T): Boolean
protected abstract fun matches(actionContext: ActionContext, annotation: T): Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@ import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.project.Project
import com.itangcent.idea.plugin.api.cache.CachedRequestClassExporter
import com.itangcent.idea.plugin.api.call.ApiCaller
import com.itangcent.idea.plugin.api.export.ExportChannel
import com.itangcent.idea.plugin.api.export.ExportDoc
import com.itangcent.idea.plugin.api.export.condition.ConditionOnSimple
import com.itangcent.idea.plugin.api.export.core.ClassExporter
import com.itangcent.idea.plugin.api.export.core.CompositeClassExporter
import com.itangcent.idea.plugin.api.export.core.EasyApiConfigReader
import com.itangcent.idea.plugin.api.export.generic.GenericRequestClassExporter
import com.itangcent.idea.plugin.api.export.spring.SpringRequestClassExporter
import com.itangcent.idea.plugin.config.RecommendConfigReader
import com.itangcent.intellij.config.ConfigReader
import com.itangcent.intellij.context.ActionContext
Expand All @@ -35,7 +31,7 @@ class ApiCallAction : ApiExportAction("Call Api") {
}

builder.bindInstance(ExportDoc::class, ExportDoc.of("request"))

builder.bind(ClassExporter::class) { it.with(CachedRequestClassExporter::class).singleton() }

builder.bind(ConfigReader::class, "delegate_config_reader") { it.with(EasyApiConfigReader::class).singleton() }
Expand All @@ -44,7 +40,6 @@ class ApiCallAction : ApiExportAction("Call Api") {
builder.bind(HttpClientProvider::class) { it.with(ConfigurableHttpClientProvider::class).singleton() }

builder.bind(ApiCaller::class) { it.singleton() }

}

override fun actionPerformed(actionContext: ActionContext, project: Project?, anActionEvent: AnActionEvent) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.itangcent.idea.plugin.actions

import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.project.Project
import com.itangcent.idea.plugin.api.cache.DefaultFileApiCacheRepository
import com.itangcent.idea.plugin.api.cache.FileApiCacheRepository
import com.itangcent.idea.plugin.api.cache.ProjectCacheRepository
Expand All @@ -12,10 +13,12 @@ import com.itangcent.idea.utils.RuleComputeListenerRegistry
import com.itangcent.intellij.config.rule.RuleComputeListener
import com.itangcent.intellij.config.rule.RuleParser
import com.itangcent.intellij.context.ActionContext
import com.itangcent.intellij.extend.findCurrentMethod
import com.itangcent.intellij.extend.guice.singleton
import com.itangcent.intellij.extend.guice.with
import com.itangcent.intellij.file.LocalFileRepository
import com.itangcent.intellij.jvm.PsiClassHelper
import com.itangcent.intellij.util.ActionUtils

abstract class ApiExportAction(text: String) : BasicAnAction(text) {

Expand All @@ -33,4 +36,9 @@ abstract class ApiExportAction(text: String) : BasicAnAction(text) {
it.with(ProjectCacheRepository::class).singleton()
}
}

override fun actionPerformed(actionContext: ActionContext, project: Project?, anActionEvent: AnActionEvent) {
super.actionPerformed(actionContext, project, anActionEvent)
actionContext.findCurrentMethod()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import com.intellij.openapi.project.Project
import com.itangcent.idea.plugin.api.export.ExportChannel
import com.itangcent.idea.plugin.api.export.ExportDoc
import com.itangcent.idea.plugin.api.export.condition.ConditionOnSimple
import com.itangcent.idea.plugin.api.export.core.ClassExporter
import com.itangcent.idea.plugin.api.export.core.CompositeClassExporter
import com.itangcent.idea.plugin.api.export.core.EasyApiConfigReader
import com.itangcent.idea.plugin.api.export.core.*
import com.itangcent.idea.plugin.api.export.generic.GenericMethodDocClassExporter
import com.itangcent.idea.plugin.api.export.generic.GenericRequestClassExporter
import com.itangcent.idea.plugin.api.export.markdown.MarkdownApiExporter
Expand All @@ -34,7 +32,9 @@ class MarkdownExportAction : ApiExportAction("Export Markdown") {

builder.bindInstance(ExportChannel::class, ExportChannel.of("markdown"))
builder.bindInstance(ExportDoc::class, ExportDoc.of("request", "methodDoc"))


builder.bind(MethodFilter::class) { it.with(ConfigurableMethodFilter::class).singleton() }

//always not read api from cache
builder.bindInstance("class.exporter.read.cache", false)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class PostmanExportAction : ApiExportAction("Export Postman") {
builder.bindInstance(ExportDoc::class, ExportDoc.of("request"))

builder.bind(RequestBuilderListener::class) { it.with(CompositeRequestBuilderListener::class).singleton() }

builder.bind(MethodFilter::class) { it.with(ConfigurableMethodFilter::class).singleton() }
//always not read api from cache
builder.bindInstance("class.exporter.read.cache", false)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ 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
import com.itangcent.idea.plugin.api.export.core.CompositeClassExporter
import com.itangcent.idea.plugin.api.export.core.EasyApiConfigReader
import com.itangcent.idea.plugin.api.export.core.*
import com.itangcent.idea.plugin.api.export.postman.PostmanApiHelper
import com.itangcent.idea.plugin.api.export.postman.PostmanCachedApiHelper
import com.itangcent.idea.plugin.api.export.suv.SuvApiExporter
Expand Down Expand Up @@ -54,6 +52,8 @@ class SuvExportAction : ApiExportAction("Export Api") {

builder.bind(SuvApiExporter::class) { it.singleton() }

builder.bind(MethodFilter::class) { it.with(ConfigurableMethodFilter::class).singleton() }

builder.cache("DATA_EVENT_COLLECTOR", dataEventCollector)

dataEventCollector = null
Expand All @@ -68,6 +68,7 @@ class SuvExportAction : ApiExportAction("Export Api") {
dataContext.getData(CommonDataKeys.NAVIGATABLE_ARRAY)
dataContext.getData(CommonDataKeys.NAVIGATABLE)
dataContext.getData(CommonDataKeys.EDITOR)
dataContext.getData(CommonDataKeys.PSI_ELEMENT)
}

override fun actionPerformed(actionContext: ActionContext, project: Project?, anActionEvent: AnActionEvent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class YapiExportAction : ApiExportAction("Export Yapi") {
builder.bind(RequestBuilderListener::class) { it.with(CompositeRequestBuilderListener::class).singleton() }
builder.bind(MethodDocBuilderListener::class) { it.with(CompositeMethodDocBuilderListener::class).singleton() }

builder.bind(MethodFilter::class) { it.with(ConfigurableMethodFilter::class).singleton() }

builder.bindInstance("file.save.default", "yapi.json")
builder.bindInstance("file.save.last.location.key", "com.itangcent.yapi.export.path")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.itangcent.idea.plugin.api.export.core

import com.google.inject.ImplementedBy
import com.google.inject.Inject
import com.google.inject.Singleton
import com.intellij.psi.PsiMethod
import com.itangcent.idea.plugin.settings.helper.IntelligentSettingsHelper
import com.itangcent.intellij.context.ActionContext
import com.itangcent.intellij.extend.findCurrentMethod

@ImplementedBy(EmptyMethodFilter::class)
interface MethodFilter {
Expand All @@ -14,5 +18,22 @@ class EmptyMethodFilter : MethodFilter {
override fun checkMethod(method: PsiMethod): Boolean {
return true
}
}

@Singleton
class ConfigurableMethodFilter : MethodFilter {

@Inject
private lateinit var intelligentSettingsHelper: IntelligentSettingsHelper

private val selectedMethod by lazy {
return@lazy ActionContext.getContext()?.findCurrentMethod()
}

override fun checkMethod(method: PsiMethod): Boolean {
if (intelligentSettingsHelper.selectedOnly()) {
return selectedMethod == null || selectedMethod == method
}
return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.google.inject.Inject
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiMethod
import com.itangcent.common.logger.traceError
import com.itangcent.common.model.Doc
Expand All @@ -30,10 +31,7 @@ import com.itangcent.idea.plugin.rule.SuvRuleParser
import com.itangcent.idea.plugin.script.GroovyActionExtLoader
import com.itangcent.idea.plugin.script.LoggerBuffer
import com.itangcent.idea.plugin.settings.SettingBinder
import com.itangcent.idea.plugin.settings.helper.MarkdownSettingsHelper
import com.itangcent.idea.plugin.settings.helper.PostmanSettingsHelper
import com.itangcent.idea.plugin.settings.helper.YapiSettingsHelper
import com.itangcent.idea.plugin.settings.helper.YapiTokenChecker
import com.itangcent.idea.plugin.settings.helper.*
import com.itangcent.idea.psi.PsiResource
import com.itangcent.idea.swing.MessagesHelper
import com.itangcent.idea.utils.CustomizedPsiClassHelper
Expand All @@ -45,6 +43,7 @@ import com.itangcent.intellij.config.rule.RuleComputeListener
import com.itangcent.intellij.config.rule.RuleParser
import com.itangcent.intellij.constant.EventKey
import com.itangcent.intellij.context.ActionContext
import com.itangcent.intellij.extend.findCurrentMethod
import com.itangcent.intellij.extend.guice.singleton
import com.itangcent.intellij.extend.guice.with
import com.itangcent.intellij.extend.withBoundary
Expand Down Expand Up @@ -73,10 +72,10 @@ open class SuvApiExporter {
private lateinit var actionContext: ActionContext

@Inject
private val classExporter: ClassExporter? = null
private lateinit var classApiExporterHelper: ClassApiExporterHelper

@Inject
private lateinit var classApiExporterHelper: ClassApiExporterHelper
private lateinit var intelligentSettingsHelper: IntelligentSettingsHelper

@Suppress("UNCHECKED_CAST")
fun showExportWindow() {
Expand All @@ -98,11 +97,11 @@ open class SuvApiExporter {

multipleApiExportDialog.setOnChannelChanged { channel ->
if (channel == null) {
multipleApiExportDialog.updateRequestList(docs)
multipleApiExportDialog.updateRequestListToUI(docs)
return@setOnChannelChanged
}
val apiExporterAdapter = channel as ApiExporterWrapper
multipleApiExportDialog.updateRequestList(docs
multipleApiExportDialog.updateRequestListToUI(docs
.filter { apiExporterAdapter.support(it.docType) }
.toList())
}
Expand All @@ -118,6 +117,21 @@ open class SuvApiExporter {
}
}

private fun SuvApiExportDialog.updateRequestListToUI(docs: List<DocWrapper>) {
this.updateRequestList(docs)
if (intelligentSettingsHelper.selectedOnly()) {
val currentMethod = actionContext.findCurrentMethod()
if (currentMethod != null) {
docs.firstOrNull { it.resourceMethod() == currentMethod }
?.let {
this.selectMethod(it)
return
}
}
}
this.selectAll()
}

private var customActionExtLoader: ((String, ActionContext.ActionContextBuilder) -> Unit)? = null

fun setCustomActionExtLoader(customActionExtLoader: (String, ActionContext.ActionContextBuilder) -> Unit) {
Expand Down Expand Up @@ -151,6 +165,10 @@ open class SuvApiExporter {
}
}

fun DocWrapper.resourceMethod(): PsiMethod? {
return (this.resource as? PsiResource)?.resource() as? PsiMethod
}

abstract class ApiExporterAdapter {

@Inject(optional = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@
</grid>
</constraints>
<properties>
<text value="max deep:"/>
<text value="max infer deep:"/>
</properties>
</component>
<component id="9bab4" class="javax.swing.JTextField" binding="maxDeepTextField">
Expand Down Expand Up @@ -1277,7 +1277,16 @@
</constraints>
<properties/>
<border type="none"/>
<children/>
<children>
<component id="dbb01" class="javax.swing.JCheckBox" binding="selectedOnlyCheckBox">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="export current method only"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ class EasyApiSettingGUI : AbstractEasyApiSettingGUI() {

private var inferEnableCheckBox: JCheckBox? = null

private var selectedOnlyCheckBox: JCheckBox? = null

private var maxDeepTextField: JTextField? = null

private var readGetterCheckBox: JCheckBox? = null
Expand Down Expand Up @@ -268,6 +270,7 @@ class EasyApiSettingGUI : AbstractEasyApiSettingGUI() {
this.feignEnableCheckBox!!.isSelected = settings.feignEnable
this.jaxrsEnableCheckBox!!.isSelected = settings.jaxrsEnable
this.inferEnableCheckBox!!.isSelected = settings.inferEnable
this.selectedOnlyCheckBox!!.isSelected = settings.selectedOnly
this.readGetterCheckBox!!.isSelected = settings.readGetter
this.readSetterCheckBox!!.isSelected = settings.readSetter
this.formExpandedCheckBox!!.isSelected = settings.formExpanded
Expand Down Expand Up @@ -581,6 +584,7 @@ class EasyApiSettingGUI : AbstractEasyApiSettingGUI() {
settings.readGetter = readGetterCheckBox!!.isSelected
settings.readSetter = readSetterCheckBox!!.isSelected
settings.inferEnable = inferEnableCheckBox!!.isSelected
settings.selectedOnly = selectedOnlyCheckBox!!.isSelected
settings.inferMaxDeep = maxDeepTextField!!.text.toIntOrNull() ?: Settings.DEFAULT_INFER_MAX_DEEP
settings.yapiServer = yapiServerTextField!!.text
settings.yapiTokens = yapiTokenTextArea!!.text
Expand Down
Loading

0 comments on commit 31d4865

Please sign in to comment.