Skip to content

Commit

Permalink
add option "rename macro" && insert code from caret position
Browse files Browse the repository at this point in the history
  • Loading branch information
1595901624 committed Mar 23, 2024
1 parent 229f80d commit c26688e
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 38 deletions.
6 changes: 4 additions & 2 deletions src/main/kotlin/com/rust/json/quick/rustjson/JsonParseUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class JsonParseUtil(
public = parseConfig.publicStruct,
clone = parseConfig.cloneDerive,
option = parseConfig.option,
debug = parseConfig.debugDerive
debug = parseConfig.debugDerive,
rename = parseConfig.renameMacro
)
if (jsonElement.isJsonObject) {
parseJsonObject(jsonElement, "Root", list)
Expand Down Expand Up @@ -58,7 +59,8 @@ class JsonParseUtil(
public = parseConfig.publicStruct,
clone = parseConfig.cloneDerive,
option = parseConfig.option,
debug = parseConfig.debugDerive
debug = parseConfig.debugDerive,
rename = parseConfig.renameMacro
)
// get json object
val jsonObject = jsonElement as JsonObject
Expand Down
56 changes: 33 additions & 23 deletions src/main/kotlin/com/rust/json/quick/rustjson/JsonToRustDialog.form
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<xy x="48" y="54" width="530" height="297"/>
<xy x="48" y="54" width="550" height="297"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="94766" layout-manager="GridLayoutManager" row-count="3" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="94766" layout-manager="GridLayoutManager" row-count="3" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
Expand All @@ -27,7 +27,7 @@
<grid id="9538f" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="2" column="2" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="2" column="3" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
Expand All @@ -50,66 +50,76 @@
</component>
</children>
</grid>
<component id="554c7" class="javax.swing.JButton" binding="buttonFormat">
<component id="60110" class="javax.swing.JCheckBox" binding="checkBoxFieldPublic">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="1" column="4" 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="Format"/>
<selected value="true"/>
<text value="Public Field"/>
</properties>
</component>
<component id="f462" class="javax.swing.JCheckBox" binding="checkBoxDebug">
<component id="c29a" class="javax.swing.JCheckBox" binding="checkBoxClone">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="0" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<selected value="true"/>
<text value="Debug Derive"/>
<selected value="false"/>
<text value="Clone Derive"/>
</properties>
</component>
<component id="c29a" class="javax.swing.JCheckBox" binding="checkBoxClone">
<component id="f462" class="javax.swing.JCheckBox" binding="checkBoxDebug">
<constraints>
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<selected value="false"/>
<text value="Clone Derive"/>
<selected value="true"/>
<text value="Debug Derive"/>
</properties>
</component>
<component id="22c00" class="javax.swing.JCheckBox" binding="checkBoxSerde">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<selected value="true"/>
<text value="Serde Derive"/>
</properties>
</component>
<component id="1810" class="javax.swing.JCheckBox" binding="checkBoxOption">
<component id="a435e" class="javax.swing.JCheckBox" binding="checkBoxStructPublic">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<selected value="true"/>
<text value="Add Option"/>
<text value="Public Struct"/>
</properties>
</component>
<component id="a435e" class="javax.swing.JCheckBox" binding="checkBoxStructPublic">
<component id="1810" class="javax.swing.JCheckBox" binding="checkBoxOption">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<selected value="true"/>
<text value="Public Struct"/>
<text value="Add Option&lt;T&gt;"/>
</properties>
</component>
<component id="60110" class="javax.swing.JCheckBox" binding="checkBoxFieldPublic">
<component id="ff17d" class="javax.swing.JCheckBox" binding="checkBoxRename">
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<selected value="true"/>
<text value="Public Field"/>
<actionCommand value="Add Rename Macro"/>
<selected value="false"/>
<text value="Add Rename Macro"/>
</properties>
</component>
<component id="554c7" class="javax.swing.JButton" binding="buttonFormat">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Format"/>
</properties>
</component>
</children>
Expand Down
22 changes: 12 additions & 10 deletions src/main/kotlin/com/rust/json/quick/rustjson/JsonToRustDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.fileTypes.PlainTextLanguage
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiFileFactory
import java.awt.Dimension
import java.awt.Toolkit
import java.awt.event.KeyEvent
Expand All @@ -32,6 +30,7 @@ class JsonToRustDialog(
private var checkBoxFieldPublic: JCheckBox? = null
private var checkBoxOption: JCheckBox? = null
private var checkBoxClone: JCheckBox? = null
private var checkBoxRename: JCheckBox? = null

init {
// set dialog title
Expand Down Expand Up @@ -94,7 +93,8 @@ class JsonToRustDialog(
cloneDerive = checkBoxClone!!.isSelected,
publicStruct = checkBoxStructPublic!!.isSelected,
publicField = checkBoxFieldPublic!!.isSelected,
option = checkBoxOption!!.isSelected
option = checkBoxOption!!.isSelected,
renameMacro = checkBoxRename!!.isSelected
)
val list = JsonParseUtil(parseConfig).parse(jsonString)
// generate rust code
Expand All @@ -110,18 +110,20 @@ class JsonToRustDialog(
}

// insert code to file
val newFile = PsiFileFactory.getInstance(event.project)
.createFileFromText(
PlainTextLanguage.INSTANCE,
importUseCode + file.text + "\n" + codeStringBuilder.toString()
)
// val newFile = PsiFileFactory.getInstance(event.project)
// .createFileFromText(
// PlainTextLanguage.INSTANCE,
// importUseCode + file.text + "\n" + codeStringBuilder.toString()
// )
// get workspace current file editor
val currentEditor = event.getData<Editor>(CommonDataKeys.EDITOR)
?: return@Runnable
// refresh workspace editor
currentEditor.document.setText(newFile.text)
// currentEditor.document.setText(newFile.text)
currentEditor.document.insertString(0, importUseCode)
currentEditor.document.insertString(currentEditor.caretModel.offset, codeStringBuilder.toString())
currentEditor.selectionModel.removeSelection()
currentEditor.caretModel.moveToOffset(0)
// currentEditor.caretModel.moveToOffset(0)
}

dispose()
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/com/rust/json/quick/rustjson/ParseConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ data class ParseConfig(
val cloneDerive: Boolean = true,
val publicStruct: Boolean = true,
val publicField: Boolean = true,
val option: Boolean = true
val option: Boolean = true,
val renameMacro: Boolean = false
)
9 changes: 7 additions & 2 deletions src/main/kotlin/com/rust/json/quick/rustjson/RustData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ data class RustStruct(
* clone derive
*/
val clone: Boolean = false,

/**
* rename macro
*/
val rename: Boolean = false,
) {

/**
Expand Down Expand Up @@ -69,8 +74,8 @@ data class RustStruct(
// add fields
fields.forEachIndexed { index, it ->
stringBuilder.append("\t")
// add serde
if (serde) {
// add rename
if (rename) {
stringBuilder.append("#[serde(rename = \"${it.name}\")]\n")
stringBuilder.append("\t")
}
Expand Down

0 comments on commit c26688e

Please sign in to comment.