Skip to content

Commit

Permalink
合并拉取请求 #55
Browse files Browse the repository at this point in the history
2.1.1
  • Loading branch information
mdddj authored Aug 26, 2024
2 parents 365e584 + 0062e9d commit 27f91e9
Show file tree
Hide file tree
Showing 25 changed files with 211 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .intellijPlatform/self-update.lock
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2024-08-17
2024-08-23
4 changes: 3 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ repositories {
//RR:
//rustRover("2024.1.5")
//bundledPlugins("JavaScriptBase","com.jetbrains.rust","org.toml.lang")

// IDEA
//intellijIdeaUltimate("2024.2")
//plugins("com.jetbrains.rust:242.20224.309")
//bundledPlugins("org.toml.lang","JavaScript")

dependencies {
intellijPlatform {
rustRover("2024.1.5")
// rustRover("2024.1.5")
local("/Users/ldd/Applications/RustRover.app")
bundledPlugins("JavaScriptBase","com.jetbrains.rust","org.toml.lang")
zipSigner()
instrumentationTools()
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
salvo.rs快速开发工具
JB RustRover Rust dev tool

文档: [https://mdddj.github.io/SalvoRsToolDocument](https://mdddj.github.io/SalvoRsToolDocument)
document: [https://mdddj.github.io/SalvoRsToolDocument](https://mdddj.github.io/SalvoRsToolDocument)

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.intellij.openapi.actionSystem.AnActionEvent
import shop.itbug.salvorstool.dialog.GenerateDtoDialog
import shop.itbug.salvorstool.i18n.MyI18n
import shop.itbug.salvorstool.tool.myManager
import shop.itbug.salvorstool.tool.structItemManager
import shop.itbug.salvorstool.tool.tryGetRsStructPsiElement

class GenerateDtoAction : AnAction() {
Expand All @@ -15,7 +16,8 @@ class GenerateDtoAction : AnAction() {
}
}
override fun update(e: AnActionEvent) {
e.presentation.isVisible = e.project != null && e.tryGetRsStructPsiElement()!=null
val struct = e.tryGetRsStructPsiElement()
e.presentation.isVisible = e.project != null && struct!=null && struct.structItemManager.getTableName != 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 @@ -6,6 +6,7 @@ import com.intellij.openapi.actionSystem.AnActionEvent
import shop.itbug.salvorstool.dialog.GenerateRouterDialog
import shop.itbug.salvorstool.i18n.MyI18n
import shop.itbug.salvorstool.tool.myManager
import shop.itbug.salvorstool.tool.structItemManager
import shop.itbug.salvorstool.tool.tryGetRsStructPsiElement

class GenerateRouterAction : AnAction() {
Expand All @@ -17,7 +18,8 @@ class GenerateRouterAction : AnAction() {
}

override fun update(e: AnActionEvent) {
e.presentation.isVisible = e.project != null && e.tryGetRsStructPsiElement() != null
val struct = e.tryGetRsStructPsiElement()
e.presentation.isVisible = e.project != null && struct != null && struct.structItemManager.getTableName != 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 @@ -6,6 +6,7 @@ import com.intellij.openapi.actionSystem.AnActionEvent
import shop.itbug.salvorstool.dialog.GenerateServiceDialog
import shop.itbug.salvorstool.i18n.MyI18n
import shop.itbug.salvorstool.tool.myManager
import shop.itbug.salvorstool.tool.structItemManager
import shop.itbug.salvorstool.tool.tryGetRsStructPsiElement

class GenerateServiceAction : AnAction() {
Expand All @@ -16,7 +17,8 @@ class GenerateServiceAction : AnAction() {
}

override fun update(e: AnActionEvent) {
e.presentation.isVisible = e.project != null && e.tryGetRsStructPsiElement() != null
val struct = e.tryGetRsStructPsiElement()
e.presentation.isVisible = e.project != null && struct != null && struct.structItemManager.getTableName != null
e.presentation.text = MyI18n.getMessage("g_service")
super.update(e)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class GenerateAntdFormDialog(project: Project, psiElement: RsStructItemImpl) : D
init {
super.init()
title = "Generate Antd Form"
tabview.add("预览", TypeJavaScriptEditor(project, AntdFactory.generateAntdForm(psiElement)))
tabview.add("Preview", TypeJavaScriptEditor(project, AntdFactory.generateAntdForm(psiElement)))
}

override fun createCenterPanel(): JComponent {
Expand Down
95 changes: 58 additions & 37 deletions src/main/kotlin/shop/itbug/salvorstool/dialog/GenerateDtoDialog.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
package shop.itbug.salvorstool.dialog

import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.guessProjectDir
import com.intellij.openapi.ui.DialogPanel
import com.intellij.openapi.ui.DialogWrapper
import com.intellij.openapi.ui.ValidationInfo
import com.intellij.ui.components.JBTabbedPane
import com.intellij.ui.dsl.builder.Align
import com.intellij.ui.dsl.builder.Panel
import com.intellij.ui.dsl.builder.bindText
import com.intellij.ui.dsl.builder.panel
import com.intellij.util.ui.components.BorderLayoutPanel
import org.rust.lang.core.psi.RsNamedFieldDecl
import org.rust.lang.core.psi.impl.RsStructItemImpl
import shop.itbug.salvorstool.dsl.SaveToBindModel
import shop.itbug.salvorstool.dsl.saveTo
import shop.itbug.salvorstool.i18n.MyI18n
import shop.itbug.salvorstool.tool.*
import shop.itbug.salvorstool.widget.RsEditor
import java.awt.Dimension
import java.io.File
import java.util.*
import javax.swing.JComponent
import javax.swing.SwingUtilities


data class GenerateDtoDialogParam(
Expand Down Expand Up @@ -52,60 +58,70 @@ fun GenerateDtoDialogResultEnum.getOuterAttr(): String {
}

///过滤字段
fun GenerateDtoDialogResultEnum.getFields(list: List<RsNamedFieldDecl>) : List<RsNamedFieldDecl> {
fun GenerateDtoDialogResultEnum.getFields(list: List<RsNamedFieldDecl>): List<RsNamedFieldDecl> {
return when (this) {
GenerateDtoDialogResultEnum.AddRequest -> list.filter { !it.namedFieldManager.isPrimaryKey }
GenerateDtoDialogResultEnum.UpdateRequest -> list
GenerateDtoDialogResultEnum.Response -> list
}
}

///预处理
fun GenerateDtoDialogResultEnum.preview(field: RsNamedFieldDecl) : String? {
fun GenerateDtoDialogResultEnum.preview(field: RsNamedFieldDecl): String? {
return when (this) {
GenerateDtoDialogResultEnum.UpdateRequest -> {
if(field.namedFieldManager.isPrimaryKey){
if (field.namedFieldManager.isPrimaryKey) {
return " #[salvo(extract(source(from = \"param\")))]"
}
return null
}

else -> null
}
}

///执行写入
fun GenerateDtoDialogParam.save(project: Project,psiElement: RsStructItemImpl) {
fun GenerateDtoDialogParam.save(project: Project, psiElement: RsStructItemImpl) {
val sb = StringBuilder()
val imports = Tools.getDtoImportPackagesText
sb.append(imports)

//添加add
sb.appendLine(MyRsPsiFactory.generateDto(GenerateDtoDialogResultEnum.AddRequest, psiElement))
//添加update
sb.appendLine(MyRsPsiFactory.generateDto(GenerateDtoDialogResultEnum.UpdateRequest,psiElement))
sb.appendLine(MyRsPsiFactory.generateDto(GenerateDtoDialogResultEnum.UpdateRequest, psiElement))
//添加response
sb.appendLine(MyRsPsiFactory.generateDto(GenerateDtoDialogResultEnum.Response,psiElement))
sb.appendLine(MyRsPsiFactory.generateDto(GenerateDtoDialogResultEnum.Response, psiElement))

val newPsiFile = Tools.createRsPsiFile(fileName,sb.toString(),project)
val newPsiFile = Tools.createRsPsiFile(fileName, sb.toString(), project)
Tools.saveTo(project, newPsiFile, saveTo)
}



///生成dto对象
class GenerateDtoDialog(private val project: Project, private 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),
addRequestName = GenerateDtoDialogResultEnum.AddRequest.getStructName(psiElement.structItemManager.getTableName ?: ""),
updateRequestText = MyRsPsiFactory.generateDto(GenerateDtoDialogResultEnum.UpdateRequest,psiElement),
updateRequestName = GenerateDtoDialogResultEnum.UpdateRequest.getStructName(psiElement.structItemManager.getTableName ?: ""),
responseText = MyRsPsiFactory.generateDto(GenerateDtoDialogResultEnum.Response,psiElement),
responseName = GenerateDtoDialogResultEnum.Response.getStructName(psiElement.structItemManager.getTableName ?: ""),
addRequestName = GenerateDtoDialogResultEnum.AddRequest.getStructName(
psiElement.structItemManager.getTableName ?: ""
),
updateRequestText = MyRsPsiFactory.generateDto(GenerateDtoDialogResultEnum.UpdateRequest, psiElement),
updateRequestName = GenerateDtoDialogResultEnum.UpdateRequest.getStructName(
psiElement.structItemManager.getTableName ?: ""
),
responseText = MyRsPsiFactory.generateDto(GenerateDtoDialogResultEnum.Response, psiElement),
responseName = GenerateDtoDialogResultEnum.Response.getStructName(
psiElement.structItemManager.getTableName ?: ""
),
saveTo = Tools.getDtoFolder(project)?.path ?: "",
fileName = (psiElement.structItemManager.getTableName?:"root")
fileName = (psiElement.structItemManager.getTableName ?: "root")
)

private val tabView = JBTabbedPane()
private lateinit var myPanel: DialogPanel
private lateinit var settingPanel: Panel

init {
super.init()
Expand Down Expand Up @@ -137,36 +153,41 @@ class GenerateDtoDialog(private val project: Project, private val psiElement: R
}

override fun createCenterPanel(): JComponent {

return panel {
row {
scrollCell(tabView).align(Align.FILL)
}
group(MyI18n.saveTo) {
row(MyI18n.selectDir) {
textFieldWithBrowseButton(
project = project,
fileChooserDescriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor().withRoots(
project.guessProjectDir()
)
).align(Align.FILL)
.bindText(model::saveTo)
}
row (MyI18n.getMessage("file_name")) {
textField().bindText(model::fileName)
}
}
myPanel = panel {
settingPanel = saveTo(
project, SaveToBindModel(
model::saveTo,
model::fileName
)
)
}
SwingUtilities.invokeLater {
myPanel.registerValidators(disposable)
}
return BorderLayoutPanel().apply {
addToCenter(tabView)
addToBottom(myPanel)
}
}


override fun doOKAction() {
model.save(project,psiElement)
model.save(project, psiElement)
super.doOKAction()
}

override fun getPreferredSize(): Dimension {
return Dimension(550, super.getPreferredSize().height)
}

override fun doValidate(): ValidationInfo? {
if (!project.fileIsExits(model.saveTo)) {
return ValidationInfo(MyI18n.folderIsNotFound)
} else if (project.fileIsExits(model.saveTo.removeSuffix(File.separator) + File.separator + model.fileName + ".rs")) {
return ValidationInfo(MyI18n.fileIsExist)
}
return super.doValidate()
}


}
53 changes: 53 additions & 0 deletions src/main/kotlin/shop/itbug/salvorstool/dsl/PanelDsl.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package shop.itbug.salvorstool.dsl

import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.guessProjectDir
import com.intellij.openapi.ui.DialogWrapper
import com.intellij.ui.dsl.builder.Align
import com.intellij.ui.dsl.builder.Panel
import com.intellij.ui.dsl.builder.bindText
import com.intellij.ui.layout.ValidationInfoBuilder
import shop.itbug.salvorstool.i18n.MyI18n
import shop.itbug.salvorstool.tool.fileIsExits
import java.io.File
import kotlin.reflect.KMutableProperty0

data class SaveToBindModel(var folder: KMutableProperty0<String>, var fileName: KMutableProperty0<String>)

/**
* 保存文件到目录的基本配置
*/
fun Panel.saveTo(project: Project,bind: SaveToBindModel) : Panel {
this.group(MyI18n.saveTo) {
row(MyI18n.selectDir) {
textFieldWithBrowseButton(
project = project,
fileChooserDescriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor().withRoots(
project.guessProjectDir()
)
).align(Align.FILL)
.bindText(bind.folder).validationOnInput {
val fullPath = it.text
if(!project.fileIsExits(fullPath)) {
return@validationOnInput ValidationInfoBuilder(it.textField).error(MyI18n.folderIsNotFound)
}
return@validationOnInput null
}
}
row (MyI18n.getMessage("file_name")) {
textField().bindText(bind.fileName).validationOnInput {
var folderPath = bind.folder.get()
if(!folderPath.endsWith(File.separatorChar)){
folderPath += File.separator
}
val fullPath = folderPath + it.text + ".rs"
if(project.fileIsExits(fullPath)){
return@validationOnInput ValidationInfoBuilder(it).error(MyI18n.fileIsExist)
}
return@validationOnInput null
}
}
}
return this
}
4 changes: 4 additions & 0 deletions src/main/kotlin/shop/itbug/salvorstool/i18n/MyPluginBundle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ object MyI18n: MyPluginBundle(){
val selectDir: String get() = getMessage("select_directory")
//数据验证失败
val fieldValidFailed: String get() = getMessage("valid_failed")
//文件已存在
val fileIsExist: String get() = getMessage("file_is_exits")
//目录不存在
val folderIsNotFound: String get() = getMessage("folder_is_not_found")
}

open class MyPluginBundle : AbstractBundle("messages.pluginBundle") {
Expand Down
21 changes: 15 additions & 6 deletions src/main/kotlin/shop/itbug/salvorstool/tool/ExtendedFuntions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.fileTypes.FileTypeManager
import com.intellij.openapi.ide.CopyPasteManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.LocalFileSystem
import com.intellij.pom.Navigatable
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
Expand Down Expand Up @@ -118,11 +120,18 @@ fun PsiFile.getUseAge(element: PsiElement) : List<PsiReference> {
* 尝试跳转到代码为止
*/
fun PsiElement.tryNavTo() {
ApplicationManager.getApplication().invokeLater {
if (navigationElement != null && navigationElement is Navigatable && (navigationElement as Navigatable).canNavigate()) {
(navigationElement as Navigatable).navigate(true)
}else{
FileEditorManager.getInstance(project).openFile(this.containingFile.virtualFile)
}
if (navigationElement != null && navigationElement is Navigatable && (navigationElement as Navigatable).canNavigate()) {
(navigationElement as Navigatable).navigate(true)
}else{
FileEditorManager.getInstance(project).openFile(this.containingFile.virtualFile)
}
}


/**
* 验证[path]是否存在,可以是目录或者文件
*/
fun Project.fileIsExits(path: String) : Boolean {
val vf = LocalFileSystem.getInstance().findFileByPath(path)
return vf != null
}
Loading

0 comments on commit 27f91e9

Please sign in to comment.