Skip to content

Commit

Permalink
合并拉取请求 #17
Browse files Browse the repository at this point in the history
1.6.0
  • Loading branch information
mdddj authored May 13, 2024
2 parents 3baed1f + 238d84f commit fc5ef05
Show file tree
Hide file tree
Showing 14 changed files with 339 additions and 16 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
# Changelog



## [1.6.0] - 2024-05-13

- 注意:下面的功能需要在前端项目中添加两个前端框架(<a href='https://tailwindcss.com/docs/installation/'>tailwindcss</a>和<a href='https://daisyui.com/'>daisy ui</a>)
- 添加生成react hook from的功能 (struct)
- 添加一个自用的ts模板文件窗口
- (如果要使用生成react hook from的功能,需要添加这两个模版文件才能使用,模板文件名:ts_input.tsx,ts_modal.tsx)
- <a href='https://mdddj.github.io/SalvoRsToolDocument/changelog%E6%9B%B4%E6%96%B0%E6%97%A5%E5%BF%97.html#1-6-0'>更新日志</a>

## [1.5.2] - 2024-05-10

- 修复struct属性为Option类型无法生成Typescript Interface字段的问题
- <p><b><a href="https://mdddj.github.io/SalvoRsToolDocument/changelog%E6%9B%B4%E6%96%B0%E6%97%A5%E5%BF%97.html#1-5-1">更新日志</a></b></p>
- <p><b><a href="https://mdddj.github.io/SalvoRsToolDocument/changelog%E6%9B%B4%E6%96%B0%E6%97%A5%E5%BF%97.html#1-5-2">更新日志</a></b></p>

## [1.5.1] - 2024-05-08

Expand Down
13 changes: 4 additions & 9 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@ plugins {
}

group = "shop.itbug"
version = "1.5.2"
version = "1.6.0"

repositories {
mavenCentral()
}

intellij {
version.set("LATEST-EAP-SNAPSHOT")
// localPath.set("/Users/ldd/Applications/RustRover.app/Contents")
// localSourcesPath.set("/Users/hlx/github/intellij-community")
type.set("RR")
plugins.set(listOf("com.jetbrains.rust","JavaScriptBase"))
plugins.set(listOf("com.jetbrains.rust", "JavaScriptBase"))
}

val pushToken: String? = System.getenv("PUBLISH_TOKEN")
Expand All @@ -29,9 +27,7 @@ tasks {
targetCompatibility = "17"
}



val myChangeLog = provider {
val myChangeLog = provider {
changelog.renderItem(
changelog
.getOrNull(project.version as String) ?: changelog.getUnreleased()
Expand All @@ -43,7 +39,6 @@ tasks {

val descText = projectDir.resolve("DESCRIPTION.md").readText()

println(descText)

patchPluginXml {
sinceBuild.set("232")
Expand All @@ -59,7 +54,7 @@ tasks {
}

publishPlugin {
if(pushToken != null) {
if (pushToken != null) {
token.set(pushToken)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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.project.Project
import com.intellij.openapi.ui.DialogWrapper
import com.intellij.ui.components.JBTabbedPane
import org.rust.lang.core.psi.impl.RsStructItemImpl
import shop.itbug.salvorstool.tool.myManager
import shop.itbug.salvorstool.tool.tryGetRsStructPsiElement
import shop.itbug.salvorstool.widget.TypeJavaScriptEditor
import javax.swing.JComponent

class GenerateHookFormAction : AnAction() {
override fun actionPerformed(e: AnActionEvent) {
Dialog(e.project!!,e.tryGetRsStructPsiElement()!!).show()
}

override fun update(e: AnActionEvent) {
e.presentation.isVisible = e.tryGetRsStructPsiElement() != null && e.project != null
e.presentation.text = "Generate React Hook Form"
super.update(e)
}

override fun getActionUpdateThread(): ActionUpdateThread {
return ActionUpdateThread.BGT
}

private inner class Dialog(project: Project,psi: RsStructItemImpl) : DialogWrapper(project) {
private val manager = psi.myManager
private val tab = JBTabbedPane()

init {
super.init()
tab.add("Component", TypeJavaScriptEditor(project,manager.getHookForm))
}

override fun createCenterPanel(): JComponent {
return tab
}
}
}

28 changes: 28 additions & 0 deletions src/main/kotlin/shop/itbug/salvorstool/tool/FilesUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package shop.itbug.salvorstool.tool

import org.apache.commons.io.IOUtils

object FilesUtil
{

val files = listOf("temps/ts_input.tsx","temps/ts_modal.tsx")

/**
* 加载模板文件列表
*/
fun loadTemplateFileList(path: String): String {
try {
println("path $path")
val resource = javaClass.getResource("/$path")
println(resource)
resource?.let {
return IOUtils.toString(it,"UTF-8")
}
} catch (e: Exception) {
e.printStackTrace()
return ""
}
return ""
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ class MyRsStructManager(private val psiElement: RsStructItemImpl) {
sb.appendLine("]")
return sb.toString()
}

val getHookForm: String get() {
val sb = StringBuilder()
jsModelList.forEach {
sb.appendLine(it.hookFormItem())
}
var temp = FilesUtil.loadTemplateFileList("temps/ts_dialog_warp.tsx")
temp = temp.replace("{model}",getTSInterface).replace("Model",structName?:"Model").replace("{fields}",sb.toString())
return temp
}
}


Expand Down Expand Up @@ -116,7 +126,6 @@ class MyFieldPsiElementManager(private val psiElement: RsNamedFieldDecl) {
///参数类型文本
val typeString: String?
get() {
println("${psiElement.text} is options? ${isOption}")
if (isOption) {
return extractTextBetweenBrackets(typePsiText?:"")
}
Expand Down Expand Up @@ -149,7 +158,6 @@ class MyFieldPsiElementManager(private val psiElement: RsNamedFieldDecl) {
//获取JavaScript类型
private val javaScriptType: JavascriptType
get() {
println("type string is $typeString")
if (typeString == null) {
return JavascriptType.Unknown
}
Expand Down Expand Up @@ -263,6 +271,43 @@ fun MyFieldPsiElementManager.JsModel.antdTableColumnItem(isLast: Boolean): Strin
}


/// react hook form
fun MyFieldPsiElementManager.JsModel.hookFormItem() : String {
val sb = StringBuilder()

val requiredString = if(this.isOption){
""
} else {
"rules={{ required: '请输入${this.comment}' }}"
}

when(this.type){
JavascriptType.Number -> {
sb.appendLine("""
<Controller render={function({ field, fieldState: { error } }) {
return <InputWrapper label={'${this.comment}'} bottomLeftLabel={error?.message}>
<input type={'number'} {...field} {...register("${this.fieldName}")} className={get_input_class(error?.message)} placeholder={'${this.comment}'} />
</InputWrapper>;
}} name={'name'} control={control} $requiredString />
""".trimIndent())
}
JavascriptType.String -> {
sb.appendLine("""
<Controller render={function({ field, fieldState: { error } }) {
return <InputWrapper label={'${this.comment}'} bottomLeftLabel={error?.message}>
<input type={'text'} {...field} {...register("${this.fieldName}")} className={get_input_class(error?.message)} placeholder={'${this.fieldName}'} />
</InputWrapper>;
}} name={'name'} control={control} $requiredString />
""".trimIndent())
}
JavascriptType.Bool -> {

}
JavascriptType.Unknown -> {}
}
return sb.toString()
}

class MyRsOuterAttrPsiElementManager(private val psiElement: RsOuterAttr) {


Expand Down
6 changes: 6 additions & 0 deletions src/main/kotlin/shop/itbug/salvorstool/tool/Tools.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package shop.itbug.salvorstool.tool

import com.intellij.lang.Language
import com.intellij.lang.javascript.dialects.TypeScriptJSXLanguageDialect
import com.intellij.openapi.application.runWriteAction
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.guessProjectDir
Expand All @@ -16,6 +17,11 @@ import java.io.File
object Tools {


val jsxLanguage: TypeScriptJSXLanguageDialect = Language.findInstance(
TypeScriptJSXLanguageDialect::class.java
)

val rustLanguage: RsLanguage = Language.findInstance(RsLanguage::class.java)
/**
* 获取dto目录
*/
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/shop/itbug/salvorstool/widget/RsEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import com.intellij.ui.EditorSettingsProvider
import com.intellij.ui.LanguageTextField
import com.intellij.util.ui.JBFont
import org.rust.lang.RsLanguage
import shop.itbug.salvorstool.tool.Tools
import java.awt.Dimension
import java.awt.Font
import javax.swing.BorderFactory
import javax.swing.border.Border

class RsEditor(projectP: Project, initText: String) : LanguageTextField(Language.findInstance(RsLanguage::class.java),projectP,initText,false) {
class RsEditor(projectP: Project, initText: String) : LanguageTextField(Tools.rustLanguage,projectP,initText,false) {

override fun createEditor(): EditorEx {
return myCreateEditor(super.createEditor())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import com.intellij.openapi.editor.colors.EditorFontType
import com.intellij.openapi.editor.ex.EditorEx
import com.intellij.openapi.project.Project
import com.intellij.ui.LanguageTextField
import shop.itbug.salvorstool.tool.Tools
import java.awt.Font
import javax.swing.BorderFactory
import javax.swing.border.Border

/// type script 编辑器
class TypeJavaScriptEditor(projectP: Project, initText: String) : LanguageTextField(
Language.findInstance(
TypeScriptJSXLanguageDialect::class.java
), projectP, initText, false
Tools.jsxLanguage, projectP, initText, false
) {
override fun createEditor(): EditorEx {
return myCreateEditor(super.createEditor())
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/shop/itbug/salvorstool/window/SalvoWindow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@ class SalvoWindow : ToolWindowFactory {
val apiWindow = ApiScanWindow(project,toolWindow)
val apiWindowContent = contentFactory.createContent(apiWindow,"Api",false)
toolWindow.contentManager.addContent(apiWindowContent)

val tempWindow = TempFilesWindow(project)
val tempWindowContent = contentFactory.createContent(tempWindow,"Temp",false)
toolWindow.contentManager.addContent(tempWindowContent)

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

import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project
import com.intellij.testFramework.LightVirtualFile
import com.intellij.ui.ColoredListCellRenderer
import com.intellij.ui.components.JBList
import com.intellij.ui.components.JBScrollPane
import com.intellij.util.ui.components.BorderLayoutPanel
import shop.itbug.salvorstool.tool.FilesUtil
import javax.swing.DefaultListModel
import javax.swing.JList
import javax.swing.event.ListSelectionEvent
import javax.swing.event.ListSelectionListener

class TempFilesWindow(val project: Project) : BorderLayoutPanel(), ListSelectionListener {

private val tempFiles: List<String> = FilesUtil.files
private val list = JBList<String>().apply {
cellRenderer = Layout()
model = Model(tempFiles)
addListSelectionListener(this@TempFilesWindow)
}

init {
addToCenter(JBScrollPane(list))
}

private inner class Model(data: List<String>): DefaultListModel<String>(){
init {
addAll(data)
}
}

private inner class Layout: ColoredListCellRenderer<String>() {
override fun customizeCellRenderer(
list: JList<out String>,
value: String?,
index: Int,
selected: Boolean,
hasFocus: Boolean
) {
value?.let { append(it) }
}
}

override fun valueChanged(e: ListSelectionEvent?) {
e?.let {
if(!e.valueIsAdjusting){
val text = FilesUtil.loadTemplateFileList(list.selectedValue)
println(text)
val lf = LightVirtualFile(list.selectedValue,text)
FileEditorManager.getInstance(project).openFile(lf)
}
}
}

}
3 changes: 3 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,8 @@
</group>


<action id="GenerateHookFormAction" class="shop.itbug.salvorstool.action.GenerateHookFormAction">
<add-to-group group-id="EditorPopupMenu" anchor="first"/>
</action>
</actions>
</idea-plugin>
28 changes: 28 additions & 0 deletions src/main/resources/temps/ts_dialog_warp.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React, { JSX } from 'react';
import {
Controller, useForm,
} from 'react-hook-form';

{model}

const MyDialog: React.FC<Prop> = (props) => {
const { register, handleSubmit, reset, control} = useForm<Model>();

//todo!提交数据
const onFinish = async (values: Model) => {

};

return (
<Dialog id={'my-dialog'} onClose={() => reset()}>
<DialogBody>
<DialogTitle title={'表单'} />
<form onSubmit={handleSubmit(onFinish)}>
{fields}
</form>
</DialogBody>
<DialogCloseBtn />
</Dialog>
);
};
export { MyDialog };
Loading

0 comments on commit fc5ef05

Please sign in to comment.