Skip to content

Commit

Permalink
Merge pull request #2 from mdddj/1.3.0
Browse files Browse the repository at this point in the history
添加antd 表单生成功能
  • Loading branch information
mdddj authored Apr 23, 2024
2 parents 528a650 + 83e9ba6 commit dc90fb4
Show file tree
Hide file tree
Showing 14 changed files with 373 additions and 57 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
## [1.1.2]
**Full Changelog**: https://github.com/mdddj/SalvoRsTool/compare/1.1.1.7...1.1.2

## [1.3.0] - 2024-04-23

- Add antd from form generation function
- 添加生成antd pro from表单功能

## [1.1.2] - 2024-04-19
- upload test

Expand Down
19 changes: 6 additions & 13 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ plugins {
}

group = "shop.itbug"
version = "1.1.2"
version = "1.3.0"

repositories {
mavenCentral()
}

//
intellij {
version.set("LATEST-EAP-SNAPSHOT")
type.set("RR")
plugins.set(listOf("com.jetbrains.rust"))
plugins.set(listOf("com.jetbrains.rust","JavaScript"))
}

val pushToken: String? = System.getenv("PUBLISH_TOKEN")
Expand All @@ -32,19 +31,9 @@ tasks {
patchPluginXml {
sinceBuild.set("232")
untilBuild.set("242.*")
changeNotes.set("""
<div>
<h1>1.1.0</h1>
<p>
Optimize the presentation of underlined database table names, which will be changed to camel case naming method
</p>
</div>
""".trimIndent())
}

signPlugin {
// certificateChainFile.set(file("chain.crt"))
// privateKeyFile.set(file("private.key"))
certificateChain.set(System.getenv("CERTIFICATE_CHAIN"))
privateKey.set(System.getenv("PRIVATE_KEY"))
password.set(System.getenv("PRIVATE_KEY_PASSWORD"))
Expand All @@ -60,4 +49,8 @@ tasks {
jvmArgs = listOf("-XX:+AllowEnhancedClassRedefinition")
}

dependencies {
implementation("com.alibaba.fastjson2:fastjson2-kotlin:2.0.49")
implementation("cn.hutool:hutool-extra:5.8.27")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package shop.itbug.salvorstool.action

import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import shop.itbug.salvorstool.dialog.GenerateAntdFormDialog
import shop.itbug.salvorstool.tool.tryGetRsStructPsiElement


///生成antd表单
class GenerateAntdFormAction : AnAction() {
override fun actionPerformed(e: AnActionEvent) {
e.tryGetRsStructPsiElement()?.let {
GenerateAntdFormDialog(e.project!!, it).show()
}
}

override fun update(e: AnActionEvent) {
e.presentation.isVisible = e.tryGetRsStructPsiElement() != null
super.update(e)
}

override fun getActionUpdateThread(): ActionUpdateThread {
return ActionUpdateThread.BGT
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,18 @@ package shop.itbug.salvorstool.action
import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import org.rust.lang.core.psi.impl.RsStructItemImpl
import shop.itbug.salvorstool.dialog.GenerateDtoDialog
import shop.itbug.salvorstool.i18n.MyI18n
import shop.itbug.salvorstool.tool.myManager
import shop.itbug.salvorstool.tool.tryGetRsStructPsiElement

class GenerateDtoAction : AnAction() {
override fun actionPerformed(e: AnActionEvent) {
val psiElement = e.getData(CommonDataKeys.PSI_ELEMENT)
GenerateDtoDialog(e.project!!,psiElement as RsStructItemImpl).show()
e.tryGetRsStructPsiElement()?.let {
GenerateDtoDialog(e.project!!,it).show()
}
}

override fun update(e: AnActionEvent) {
val psiElement = e.getData(CommonDataKeys.PSI_ELEMENT)
e.presentation.isVisible = e.project != null && psiElement != null && psiElement.myManager.isStruct
e.presentation.isVisible = e.project != null && e.tryGetRsStructPsiElement()!=null
e.presentation.text = MyI18n.getMessage("g_dto")
super.update(e)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ package shop.itbug.salvorstool.action
import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import org.rust.lang.core.psi.impl.RsStructItemImpl
import shop.itbug.salvorstool.dialog.GenerateRouterDialog
import shop.itbug.salvorstool.i18n.MyI18n
import shop.itbug.salvorstool.tool.myManager
import shop.itbug.salvorstool.tool.tryGetRsStructPsiElement

class GenerateRouterAction : AnAction() {
override fun actionPerformed(e: AnActionEvent) {
GenerateRouterDialog(e.project!!,e.getData(CommonDataKeys.PSI_ELEMENT) as RsStructItemImpl).show()
e.tryGetRsStructPsiElement()?.let {
GenerateRouterDialog(e.project!!,it).show()
}

}

override fun update(e: AnActionEvent) {
val psiElement = e.getData(CommonDataKeys.PSI_ELEMENT)
e.presentation.isVisible = e.project != null && psiElement != null && psiElement.myManager.isStruct
e.presentation.isVisible = e.project != null && e.tryGetRsStructPsiElement() != null
e.presentation.text = MyI18n.getMessage("g_router")
super.update(e)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@ package shop.itbug.salvorstool.action
import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import org.rust.lang.core.psi.impl.RsStructItemImpl
import shop.itbug.salvorstool.dialog.GenerateServiceDialog
import shop.itbug.salvorstool.i18n.MyI18n
import shop.itbug.salvorstool.tool.myManager
import shop.itbug.salvorstool.tool.tryGetRsStructPsiElement

class GenerateServiceAction : AnAction() {
override fun actionPerformed(e: AnActionEvent) {
GenerateServiceDialog(e.project!!,e.getData(CommonDataKeys.PSI_ELEMENT) as RsStructItemImpl).show()
e.tryGetRsStructPsiElement()?.let {
GenerateServiceDialog(e.project!!, it).show()
}
}


override fun update(e: AnActionEvent) {
val psiElement = e.getData(CommonDataKeys.PSI_ELEMENT)
e.presentation.isVisible = e.project != null && psiElement != null && psiElement.myManager.isStruct
e.presentation.isVisible = e.project != null && e.tryGetRsStructPsiElement() != null
e.presentation.text = MyI18n.getMessage("g_service")
super.update(e)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package shop.itbug.salvorstool.dialog

import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.DialogWrapper
import com.intellij.ui.components.JBTabbedPane
import com.intellij.ui.dsl.builder.panel
import org.rust.lang.core.psi.impl.RsStructItemImpl
import shop.itbug.salvorstool.tool.AntdFactory
import shop.itbug.salvorstool.tool.MyFieldPsiElementManager
import shop.itbug.salvorstool.tool.myManager
import shop.itbug.salvorstool.widget.TypeJavaScriptEditor
import java.awt.Dimension
import javax.swing.JComponent

///生成antd表单弹窗
class GenerateAntdFormDialog(project: Project, psiElement: RsStructItemImpl) : DialogWrapper(project, true) {


private val tabview = JBTabbedPane()

init {
super.init()
title = "Generate Antd Form"

tabview.add("预览", TypeJavaScriptEditor(project, AntdFactory.generateAntdForm(psiElement)))
}

override fun createCenterPanel(): JComponent {
return tabview
}

override fun getPreferredSize(): Dimension {
return Dimension(500, super.getPreferredSize().height)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ fun GenerateDtoDialogParam.save(project: Project,psiElement: RsStructItemImpl) {


///生成dto对象
class GenerateDtoDialog(private val project: Project,val psiElement: RsStructItemImpl) : DialogWrapper(project) {
class GenerateDtoDialog(private val project: Project, private val psiElement: RsStructItemImpl) : DialogWrapper(project) {

private val model = GenerateDtoDialogParam(
addRequestText = MyRsPsiFactory.generateDto(GenerateDtoDialogResultEnum.AddRequest, psiElement),
Expand Down
112 changes: 112 additions & 0 deletions src/main/kotlin/shop/itbug/salvorstool/tool/AntdFactory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package shop.itbug.salvorstool.tool

import org.rust.lang.core.psi.impl.RsStructItemImpl


sealed class Rule
data class AntdRequiredRule(val message: String, val required: Boolean) : Rule()

private fun Rule.generateString(): String {
return when (this) {
is AntdRequiredRule -> "{ message: '$message', required: ${if (required) "true" else "false"}}"
}
}


object AntdFactory {


///生成form
fun generateAntdForm(psiElement: RsStructItemImpl): String {
val sb = StringBuilder()
val jsModels = psiElement.myManager.jsModelList

//1.添加参数
sb.appendLine(
"""
type Prop = {
trigger?: JSX.Element | undefined,
initValues?: PropInitValue | undefined
}
""".trimIndent()
)

//2.添加模型
sb.appendLine("interface PropInitValue {")
jsModels.forEach {
sb.appendLine("\t\t${it.propTextString},")
}
sb.appendLine("}")

//3.添加field
val fieldSb = StringBuilder()

jsModels.forEach {
fieldSb.appendLine("\t\t" + generateFormItem(it))
}

//4.添加最外层的包装
sb.appendLine(getTemp(fieldSb.toString()))

return sb.toString()
}

///生成form item
fun generateFormItem(model: MyFieldPsiElementManager.JsModel): String {
val type = when (model.type) {
JavascriptType.Number -> "ProFormDigit"
JavascriptType.String -> "ProFormText"
JavascriptType.Bool -> "ProFormSwitch"
JavascriptType.Unknown -> ""
}
if (type.isEmpty()) {
return ""
}

val rules = mutableListOf<Rule>()
//添加rule
if (!model.isOption) {
rules.add(AntdRequiredRule(message = "请输入${model.comment ?: "内容"}", required = true))
}

return "<$type name='${model.fieldName}' label='${model.comment ?: ""}' ${generateRulesText(rules)} \t\t/>"
}

///生成规则
private fun generateRulesText(rules: List<Rule>): String {
if (rules.isEmpty()) {
return ""
}
val sb = StringBuilder()
sb.appendLine("rules={[")
rules.forEach {
sb.appendLine("\t\t\t\t\t" + it.generateString())
}
sb.appendLine("\t\t\t]}")
return sb.toString()
}

}


///模板引擎
private fun getTemp(field: String): String = """
const AddOrUpdateForm: React.FC<Prop> = (props) => {
let isUpdate = props.initValues !== undefined
//提交数据
const onFinish = async (values: PropInitValue) => {
if(isUpdate) {
// Todo! 修改
}else{
// ToDo! 新增
}
return true
}
return (
<ModalForm<PropInitValue> trigger={props.trigger} initialValues={props.initValues} onFinish={onFinish}>
$field
</ModalForm>
);
};
export {AddOrUpdateForm}
""".trimIndent()
12 changes: 12 additions & 0 deletions src/main/kotlin/shop/itbug/salvorstool/tool/ExtendedFuntions.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package shop.itbug.salvorstool.tool

import com.google.common.base.CaseFormat
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.psi.PsiElement
import org.rust.lang.core.psi.RsNamedFieldDecl
import org.rust.lang.core.psi.RsOuterAttr
Expand All @@ -22,4 +24,14 @@ val String.underlineToCamel: String get() = underlineToCamel(this)
///将驼峰变成下划线
fun underlineToCamel(underlineString: String): String {
return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, underlineString)
}



fun AnActionEvent.tryGetRsStructPsiElement(): RsStructItemImpl? {
val psiElement = this.getData(CommonDataKeys.PSI_ELEMENT)
if ( psiElement != null && psiElement.myManager.isStruct) {
return psiElement as? RsStructItemImpl
}
return null
}
Loading

0 comments on commit dc90fb4

Please sign in to comment.