Skip to content

Commit

Permalink
Merge pull request #4 from mdddj/1.3.3
Browse files Browse the repository at this point in the history
1.3.3
  • Loading branch information
mdddj authored Apr 24, 2024
2 parents dc90fb4 + ea2f594 commit a00c4cc
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 32 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.3] - 2024-04-24

- 添加antd table column 快速生成
- 添加interface快速生成

## [1.3.0] - 2024-04-23

- Add antd from form generation function
Expand Down
19 changes: 14 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@

plugins {
id("org.jetbrains.kotlin.jvm") version "1.9.22"
id("org.jetbrains.intellij") version "1.17.2"
id("org.jetbrains.changelog") version "1.3.1"
id("org.jetbrains.changelog") version "2.2.0"
}

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

repositories {
mavenCentral()
Expand Down Expand Up @@ -49,8 +50,16 @@ tasks {
jvmArgs = listOf("-XX:+AllowEnhancedClassRedefinition")
}

dependencies {
implementation("com.alibaba.fastjson2:fastjson2-kotlin:2.0.49")
implementation("cn.hutool:hutool-extra:5.8.27")
test {
useJUnitPlatform()
}
}

dependencies {
testImplementation(kotlin("test"))
}

changelog {
unreleasedTerm.set("Unreleased")
groups.empty()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package shop.itbug.salvorstool.intention

import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import org.rust.lang.core.psi.impl.RsStructItemImpl
import shop.itbug.salvorstool.tool.antdTableColumnItem
import shop.itbug.salvorstool.tool.copy
import shop.itbug.salvorstool.tool.myManager

class CopyAntdTableColumnAction: PsiElementBaseIntentionAction(),IntentionAction {

override fun getFamilyName(): String {
return "SalvoRsTool: Copy Antd Table Column"
}

override fun getText(): String {
return familyName
}

override fun isAvailable(project: Project, editor: Editor?, element: PsiElement): Boolean {
return element.parent is RsStructItemImpl
}

override fun invoke(project: Project, editor: Editor?, element: PsiElement) {
val rs = element.parent as? RsStructItemImpl ?: return
val manager = rs.myManager
val sb = StringBuilder()
sb.appendLine("[")
manager.jsModelList.map {
sb.append("\n\t\t")
sb.append(it.antdTableColumnItem)
sb.append(",\n")
}
sb.appendLine("]")
sb.toString().copy()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package shop.itbug.salvorstool.intention

import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction
import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import org.rust.lang.core.psi.impl.RsStructItemImpl
import shop.itbug.salvorstool.tool.copy
import shop.itbug.salvorstool.tool.myManager

class CopyTSInterfaceAction: PsiElementBaseIntentionAction(),IntentionAction {
override fun getFamilyName(): String {
return "SalvoRsTool: Copy TS interface"
}

override fun getText(): String {
return familyName
}

override fun isAvailable(project: Project, editor: Editor?, element: PsiElement): Boolean {
return element.parent is RsStructItemImpl
}
override fun invoke(project: Project, editor: Editor?, element: PsiElement) {
val rs = element.parent as? RsStructItemImpl ?: return
rs.myManager.getTSInterface.copy()
}

override fun generatePreview(project: Project, editor: Editor, file: PsiFile): IntentionPreviewInfo {
var preview = IntentionPreviewInfo.Html("")
val psiElement = getElement(editor,file)?.parent as? RsStructItemImpl
psiElement?.let {
preview = IntentionPreviewInfo.Html(it.myManager.getTSInterface)
}
return preview
}

}
9 changes: 3 additions & 6 deletions src/main/kotlin/shop/itbug/salvorstool/tool/AntdFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ object AntdFactory {
///生成form
fun generateAntdForm(psiElement: RsStructItemImpl): String {
val sb = StringBuilder()
val jsModels = psiElement.myManager.jsModelList
val manager = psiElement.myManager
val jsModels = manager.jsModelList

//1.添加参数
sb.appendLine(
Expand All @@ -32,11 +33,7 @@ object AntdFactory {
)

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

//3.添加field
val fieldSb = StringBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ 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.openapi.ide.CopyPasteManager
import com.intellij.psi.PsiElement
import org.rust.lang.core.psi.RsNamedFieldDecl
import org.rust.lang.core.psi.RsOuterAttr
import org.rust.lang.core.psi.impl.RsStructItemImpl
import java.awt.datatransfer.StringSelection
import java.util.*

val PsiElement.myManager get() = MyRsPsiElementManager(this)
Expand Down Expand Up @@ -34,4 +36,8 @@ fun AnActionEvent.tryGetRsStructPsiElement(): RsStructItemImpl? {
return psiElement as? RsStructItemImpl
}
return null
}

fun String.copy(){
CopyPasteManager.getInstance().setContents(StringSelection(this))
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package shop.itbug.salvorstool.tool

import com.intellij.psi.PsiComment
import com.intellij.psi.PsiElement
import com.intellij.psi.impl.source.tree.PsiCommentImpl
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.util.firstLeaf
import com.intellij.psi.util.lastLeaf
import org.rust.lang.core.parser.RustDocTestInjectableParserDefinition
import org.rust.lang.core.psi.RsMetaItem
import org.rust.lang.core.psi.RsNamedFieldDecl
import org.rust.lang.core.psi.RsOuterAttr
Expand All @@ -19,12 +16,14 @@ import org.rust.lang.core.psi.impl.RsStructItemImpl
import org.rust.lang.doc.psi.RsDocTokenType
import org.rust.lang.doc.psi.impl.RsDocCommentImpl

val JavascriptType.typeScriptText get() = when (this) {
JavascriptType.Number -> "number"
JavascriptType.String -> "string"
JavascriptType.Bool -> "bool"
JavascriptType.Unknown -> "any"
}
val JavascriptType.typeScriptText
get() = when (this) {
JavascriptType.Number -> "number"
JavascriptType.String -> "string"
JavascriptType.Bool -> "bool"
JavascriptType.Unknown -> "any"
}

//rs属性类型对应的js类型
enum class JavascriptType {
Number, String, Bool, Unknown
Expand Down Expand Up @@ -60,9 +59,20 @@ class MyRsStructManager(private val psiElement: RsStructItemImpl) {
val primaryField = fieldList.find { it.myManager.isPrimaryKey }

///js 模型列表
val jsModelList: List<MyFieldPsiElementManager.JsModel> =
val jsModelList: List<MyFieldPsiElementManager.JsModel> =
fieldList.mapNotNull { it.myManager.getJsModel }

///获取ts模型
val getTSInterface: String get() {
val sb = StringBuilder()
sb.appendLine("interface PropInitValue {")
jsModelList.forEach {
sb.appendLine("\t\t${it.propTextString},")
}
sb.appendLine("}")
return sb.toString()
}

}


Expand All @@ -81,12 +91,16 @@ class MyFieldPsiElementManager(private val psiElement: RsNamedFieldDecl) {
//判断//
val comm = PsiTreeUtil.getChildOfType(psiElement, PsiCommentImpl::class.java)
if (comm != null) {
return comm.text.replace("//","")
return comm.text.replace("//", "")
}
val docPsi = PsiTreeUtil.getChildOfType(psiElement, RsDocCommentImpl::class.java)
//判断///

if (docPsi != null && docPsi.lastLeaf.elementType is RsDocTokenType) {
return docPsi.lastLeaf.text
val last = PsiTreeUtil.lastChild(docPsi)
if (last.elementType is RsDocTokenType) {
return last.text
}
}
return null
}
Expand Down Expand Up @@ -197,7 +211,7 @@ class MyFieldPsiElementManager(private val psiElement: RsNamedFieldDecl) {
}
}

data class JsModel(val type: JavascriptType, val fieldName: String, val comment: String?,val isOption: Boolean)
data class JsModel(val type: JavascriptType, val fieldName: String, val comment: String?, val isOption: Boolean)

val getJsModel: JsModel?
get() {
Expand All @@ -206,17 +220,31 @@ class MyFieldPsiElementManager(private val psiElement: RsNamedFieldDecl) {
} else if (name == null) {
return null
}
return JsModel(javaScriptType, name, comment = comment,isOption = isOption)
return JsModel(javaScriptType, name, comment = comment, isOption = isOption)
}
}


val MyFieldPsiElementManager.JsModel.propTextString : String get() {
if(this.isOption) {
return "${fieldName}: ${type.typeScriptText} | undefined"
/// interface 字段
val MyFieldPsiElementManager.JsModel.propTextString: String
get() {
if (this.isOption) {
return "${fieldName}: ${type.typeScriptText} | undefined"
}
return "${fieldName}: ${type.typeScriptText}"
}

/// antd 表格字段
val MyFieldPsiElementManager.JsModel.antdTableColumnItem: String
get() {
val sb = StringBuilder()
sb.appendLine("{")
sb.appendLine("\t\tdataIndex: '${fieldName}', ")
sb.appendLine("\t\ttitle: '${comment ?: fieldName}',")
sb.appendLine("\t\tkey: '${fieldName}'")
sb.appendLine("}")
return "$sb"
}
return "${fieldName}: ${type.typeScriptText}"
}


class MyRsOuterAttrPsiElementManager(private val psiElement: RsOuterAttr) {
Expand Down Expand Up @@ -270,4 +298,5 @@ class MyRsOuterAttrPsiElementManager(private val psiElement: RsOuterAttr) {
}
return null
}
}
}

9 changes: 8 additions & 1 deletion src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@
<depends>JavaScript</depends>
<resource-bundle>messages.pluginBundle</resource-bundle>
<extensions defaultExtensionNs="com.intellij">
<intentionAction>
<className>shop.itbug.salvorstool.intention.CopyAntdTableColumnAction</className>
<language>Rust</language>
</intentionAction>
<intentionAction>
<className>shop.itbug.salvorstool.intention.CopyTSInterfaceAction</className>
<language>Rust</language>
</intentionAction>
</extensions>

<actions>
<action id="GenerateDtoAction" class="shop.itbug.salvorstool.action.GenerateDtoAction">
<add-to-group group-id="EditorPopupMenu" anchor="first"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<html>
<body>
Write your description here.
Start the description with a verb in 3rd person singular, like reports, detects, highlights.
In the first sentence, briefly explain what exactly the inspection helps you detect.
Make sure the sentence is not very long and complicated.
<p>
The first sentence must be in a dedicated paragraph separated from the rest of the text. This will make the
description easier to read.
Make sure the description doesn’t just repeat the inspection title.
</p>
<p>
See https://jetbrains.design/intellij/text/inspections/#descriptions for more information.
</p>
<p>
Embed code snippets:
</p>
<pre><code>
// automatically highlighted according to inspection registration 'language' attribute
</code></pre>
<!-- tooltip end -->
<p>Text after this comment will only be shown in the settings of the inspection.</p>

<p>To open related settings directly from the description, add a link with `settings://$` optionally followed by `?$` to
pre-select a UI element.</p>
</body>
</html>

0 comments on commit a00c4cc

Please sign in to comment.