From c36a3afad3f9980d541fe342ac47a112a029451b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=91=86=E5=91=86666?= Date: Sat, 17 Aug 2024 14:00:28 +0800 Subject: [PATCH] adjust getX5, add "Observer" wrap --- build.gradle.kts | 4 +- changeNotes.md | 4 + src/main/kotlin/action/NewGetXAction.kt | 127 ++++++++---------- src/main/kotlin/action/NewGetXView.kt | 21 ++- src/main/kotlin/helper/DataService.kt | 2 +- src/main/kotlin/helper/GetXConfig.kt | 2 + src/main/kotlin/helper/GetXName.kt | 1 + .../kotlin/intention_action/SnippetType.kt | 2 +- src/main/kotlin/intention_action/Snippets.kt | 9 ++ .../intention_action/WrapWithGetAction.kt | 6 + src/main/resources/META-INF/plugin.xml | 5 + .../normal/{binding.dart => binding_4.dart} | 0 .../resources/templates/normal/binding_5.dart | 10 ++ 13 files changed, 112 insertions(+), 81 deletions(-) rename src/main/resources/templates/normal/{binding.dart => binding_4.dart} (100%) create mode 100644 src/main/resources/templates/normal/binding_5.dart diff --git a/build.gradle.kts b/build.gradle.kts index 3ccbe9b..cb8287f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -49,11 +49,11 @@ tasks { } } -version = "3.3.2" +version = "3.5.0" tasks.patchPluginXml { sinceBuild.set("200") untilBuild.set("") - pluginId.set("com.xdd.flutter_getx") + pluginId.set("com.tao.getx") pluginDescription.set(markdownToHTML(File(rootDir, "pluginDescription.md").readText())) changeNotes.set(markdownToHTML(File(rootDir, "changeNotes.md").readText())) } diff --git a/changeNotes.md b/changeNotes.md index 457a7d0..e349c96 100644 --- a/changeNotes.md +++ b/changeNotes.md @@ -1,3 +1,7 @@ +# 3.5.x +- Adjust getX5 +- Add "Observer" wrap + # 3.3.x - Adjust StatefulWidget template - Optimize wrap feature diff --git a/src/main/kotlin/action/NewGetXAction.kt b/src/main/kotlin/action/NewGetXAction.kt index a6b786e..b8d181d 100644 --- a/src/main/kotlin/action/NewGetXAction.kt +++ b/src/main/kotlin/action/NewGetXAction.kt @@ -12,6 +12,7 @@ import helper.GetXName import helper.TemplateInfo import java.io.* import java.util.* +import kotlin.collections.HashMap class NewGetXAction : AnAction() { @@ -48,6 +49,7 @@ class NewGetXAction : AnAction() { data.modeEasy = (GetXName.ModeEasy == modelType) //function area + data.function.useGetX5 = view.getX5Box.isSelected data.function.useFolder = view.folderBox.isSelected data.function.usePrefix = view.prefixBox.isSelected data.function.isPageView = view.pageViewBox.isSelected @@ -119,7 +121,12 @@ class NewGetXAction : AnAction() { //add binding file if (data.function.addBinding) { - generateFile("binding.dart", path, "${prefixName}binding.dart") + val inputFileName = if (data.function.useGetX5) { + "binding_5.dart" + } else { + "binding_4.dart" + } + generateFile(inputFileName, path, "${prefixName}binding.dart") } } @@ -131,14 +138,10 @@ class NewGetXAction : AnAction() { private fun generateEasy(path: String, prefixName: String) { generateFile( - "easy/logic.dart", - path, - "${prefixName}${data.module.logicName.lowercase(Locale.getDefault())}.dart" + "easy/logic.dart", path, "${prefixName}${data.module.logicName.lowercase(Locale.getDefault())}.dart" ) generateFile( - "easy/view.dart", - path, - "${prefixName}${data.module.viewFileName.lowercase(Locale.getDefault())}.dart" + "easy/view.dart", path, "${prefixName}${data.module.viewFileName.lowercase(Locale.getDefault())}.dart" ) } @@ -166,6 +169,8 @@ class NewGetXAction : AnAction() { } } + private var replaceContentMap = HashMap() + //content need deal private fun dealContent(inputFileName: String): String { //module name @@ -178,114 +183,96 @@ class NewGetXAction : AnAction() { //select suitable file, return suitable content var content = getSuitableContent(inputFileName) + replaceContentMap.clear() //replace view file - content = replaceView(content, inputFileName, prefixName) + replaceView(inputFileName, prefixName) //replace logic file - content = replaceLogic(content, inputFileName, prefixName) + replaceLogic(inputFileName, prefixName) //replace binding file - content = replaceBinding(content, inputFileName, prefixName) + replaceBinding(inputFileName, prefixName) //replace state file - content = replaceState(content, inputFileName) + replaceState(inputFileName) - content = content.replace("@name".toRegex(), name) + replaceContentMap["@name"] = name + + replaceContentMap.forEach { (key, value) -> + content = content.replace(key.toRegex(), value) + } return content } - private fun replaceLogic(content: String, inputFileName: String, prefixName: String): String { - var tempContent = content + private fun replaceLogic(inputFileName: String, prefixName: String) { if (!inputFileName.contains("logic.dart")) { - return tempContent + return } - tempContent = tempContent.replace( - "state.dart".toRegex(), - "$prefixName${data.module.stateName.lowercase(Locale.getDefault())}.dart" - ) - tempContent = tempContent.replace("Logic".toRegex(), data.module.logicName) - tempContent = tempContent.replace("State".toRegex(), data.module.stateName) - tempContent = tempContent.replace("state".toRegex(), data.module.stateName.lowercase(Locale.getDefault())) - - return tempContent + replaceContentMap["state.dart"] = "$prefixName${data.module.stateName.lowercase(Locale.getDefault())}.dart" + replaceContentMap["Logic"] = data.module.logicName + replaceContentMap["State"] = data.module.stateName + replaceContentMap["state"] = data.module.stateName.lowercase(Locale.getDefault()) } - private fun replaceView(content: String, inputFileName: String, prefixName: String): String { - var tempContent = content + private fun replaceView(inputFileName: String, prefixName: String) { if (!inputFileName.contains("view.dart")) { - return tempContent + return + } + + // deal getX5 + if (data.function.useGetX5) { + replaceContentMap["Get.find"] = "Bind.find" + replaceContentMap["Get.delete"] = "Bind.delete" } //deal binding function if (data.function.addBinding) { - tempContent = tempContent.replace("Get.put\\(@nameLogic\\(\\)\\)".toRegex(), "Get.find<@nameLogic>()") + replaceContentMap["Get.put\\(@nameLogic\\(\\)\\)"] = "Get.find<@nameLogic>()" } //remove lint if (!data.function.lintNorm || (!data.setting.lint && data.function.lintNorm)) { - tempContent = tempContent.replace("\\s*\nimport 'state.dart';".toRegex(), "") - tempContent = tempContent.replace("final @nameLogic".toRegex(), "final") - tempContent = tempContent.replace("final @nameState".toRegex(), "final") + replaceContentMap["\\s*\nimport 'state.dart';"] = "" + replaceContentMap["final @nameLogic"] = "final" + replaceContentMap["final @nameState"] = "final" } //remove flutter_lints if (!data.function.lintNorm || (!data.setting.flutterLints && data.function.lintNorm)) { - tempContent = tempContent.replace( - "const @namePage\\(\\{Key\\? key}\\) : super\\(key: key\\);\\s*\n\\s\\s".toRegex(), - "" - ) - tempContent = tempContent.replace( - "@namePage\\(\\{Key\\? key}\\) : super\\(key: key\\);\\s*\n\\s\\s".toRegex(), - "" - ) + replaceContentMap["const @namePage\\(\\{Key\\? key}\\) : super\\(key: key\\);\\s*\n\\s\\s"] = "" + replaceContentMap["@namePage\\(\\{Key\\? key}\\) : super\\(key: key\\);\\s*\n\\s\\s"] = "" } //deal suffix of custom module name - tempContent = tempContent.replace( - "logic.dart".toRegex(), - "$prefixName${data.module.logicName.lowercase(Locale.getDefault())}.dart" - ) - tempContent = tempContent.replace( - "state.dart".toRegex(), - "$prefixName${data.module.stateName.lowercase(Locale.getDefault())}.dart" - ) - tempContent = tempContent.replace("Page".toRegex(), data.module.viewName) - tempContent = tempContent.replace("Logic".toRegex(), data.module.logicName) - tempContent = tempContent.replace("logic".toRegex(), data.module.logicName.lowercase(Locale.getDefault())) - tempContent = tempContent.replace("@nameState".toRegex(), "@name${data.module.stateName}") - tempContent = tempContent.replace("state".toRegex(), data.module.stateName.lowercase(Locale.getDefault())) - - return tempContent + replaceContentMap["logic.dart"] = "$prefixName${data.module.logicName.lowercase(Locale.getDefault())}.dart" + replaceContentMap["state.dart"] = "$prefixName${data.module.stateName.lowercase(Locale.getDefault())}.dart" + + replaceContentMap["Page"] = data.module.viewName + replaceContentMap["Logic"] = data.module.logicName + replaceContentMap["logic"] = data.module.logicName.lowercase(Locale.getDefault()) + replaceContentMap["@nameState"] = "@name${data.module.stateName}" + replaceContentMap["state"] = data.module.stateName.lowercase(Locale.getDefault()) } - private fun replaceState(content: String, inputFileName: String): String { - var tempContent = content + private fun replaceState(inputFileName: String) { if (!inputFileName.contains("state.dart")) { - return tempContent + return } - tempContent = tempContent.replace("State".toRegex(), data.module.stateName) - - return tempContent + replaceContentMap["State"] = data.module.stateName } - private fun replaceBinding(content: String, inputFileName: String, prefixName: String): String { - var tempContent = content + private fun replaceBinding(inputFileName: String, prefixName: String) { if (!inputFileName.contains("binding.dart")) { - return tempContent + return } if (data.function.addBinding) { - tempContent = tempContent.replace("Logic".toRegex(), data.module.logicName) - tempContent = tempContent.replace( - "logic.dart".toRegex(), - "$prefixName${data.module.logicName.lowercase(Locale.getDefault())}.dart" - ) + replaceContentMap["Logic"] = data.module.logicName + replaceContentMap["logic.dart"] = "$prefixName${data.module.logicName.lowercase(Locale.getDefault())}.dart" } - - return tempContent } private fun getSuitableContent(inputFileName: String): String { diff --git a/src/main/kotlin/action/NewGetXView.kt b/src/main/kotlin/action/NewGetXView.kt index 6bb9a7f..5664308 100644 --- a/src/main/kotlin/action/NewGetXView.kt +++ b/src/main/kotlin/action/NewGetXView.kt @@ -26,10 +26,10 @@ open class NewGetXView(private val getXListener: GetXListener) { /** * select Function:main Function */ + lateinit var getX5Box: JCheckBox lateinit var folderBox: JCheckBox lateinit var prefixBox: JCheckBox lateinit var pageViewBox: JCheckBox - lateinit var bindingBox: JCheckBox /** * select Function:minor Function @@ -37,6 +37,7 @@ open class NewGetXView(private val getXListener: GetXListener) { lateinit var disposeBox: JCheckBox lateinit var lifecycleBox: JCheckBox lateinit var lintNormBox: JCheckBox + lateinit var bindingBox: JCheckBox /** * select Template:Template Function @@ -129,6 +130,12 @@ open class NewGetXView(private val getXListener: GetXListener) { val main = JPanel() main.layout = GridLayout(2, 2) + //use getX5 + getX5Box = JCheckBox(GetXName.mainUseGetX5, data.function.useGetX5) + getX5Box.addActionListener(actionChangeListener) + setMargin(getX5Box) + main.add(getX5Box) + //use folder folderBox = JCheckBox(GetXName.mainUseFolder, data.function.useFolder) folderBox.addActionListener(actionChangeListener) @@ -147,12 +154,6 @@ open class NewGetXView(private val getXListener: GetXListener) { setBottomMargin(pageViewBox) main.add(pageViewBox) - //add binding - bindingBox = JCheckBox(GetXName.mainAddBinding, data.function.addBinding) - bindingBox.addActionListener(actionChangeListener) - setBottomMargin(bindingBox) - main.add(bindingBox) - return main } @@ -162,6 +163,12 @@ open class NewGetXView(private val getXListener: GetXListener) { val minor = JPanel() minor.layout = GridLayout(2, 2) + //add binding + bindingBox = JCheckBox(GetXName.mainAddBinding, data.function.addBinding) + bindingBox.addActionListener(actionChangeListener) + setBottomMargin(bindingBox) + minor.add(bindingBox) + //add lifecycle lifecycleBox = JCheckBox(GetXName.minorAddLifecycle, data.function.addLifecycle) lifecycleBox.addActionListener(actionChangeListener) diff --git a/src/main/kotlin/helper/DataService.kt b/src/main/kotlin/helper/DataService.kt index 669a545..133d340 100644 --- a/src/main/kotlin/helper/DataService.kt +++ b/src/main/kotlin/helper/DataService.kt @@ -32,7 +32,7 @@ class DataService : PersistentStateComponent { @JvmField @OptionTag(converter = FunctionInfoConverter::class) var function = FunctionInfo( - useFolder = true, usePrefix = false, isPageView = false, + useGetX5 = true, useFolder = true, usePrefix = false, isPageView = false, addBinding = false, addLifecycle = false, autoDispose = false, lintNorm = true, funTabIndex = 0, ) diff --git a/src/main/kotlin/helper/GetXConfig.kt b/src/main/kotlin/helper/GetXConfig.kt index ce112f7..58a3b25 100644 --- a/src/main/kotlin/helper/GetXConfig.kt +++ b/src/main/kotlin/helper/GetXConfig.kt @@ -13,6 +13,8 @@ data class TemplateInfo( ///select function data class FunctionInfo( + //default true + var useGetX5: Boolean = false, //default true var useFolder: Boolean = true, //default false diff --git a/src/main/kotlin/helper/GetXName.kt b/src/main/kotlin/helper/GetXName.kt index 6fd2a68..2f56f51 100644 --- a/src/main/kotlin/helper/GetXName.kt +++ b/src/main/kotlin/helper/GetXName.kt @@ -6,6 +6,7 @@ object GetXName { const val ModeEasy = "Easy" //main function + const val mainUseGetX5 = "useGetX5" const val mainUseFolder = "useFolder" const val mainUsePrefix = "usePrefix" const val mainIsPageView = "isPageView" diff --git a/src/main/kotlin/intention_action/SnippetType.kt b/src/main/kotlin/intention_action/SnippetType.kt index 8d8059b..68a1bce 100644 --- a/src/main/kotlin/intention_action/SnippetType.kt +++ b/src/main/kotlin/intention_action/SnippetType.kt @@ -1,5 +1,5 @@ package intention_action enum class SnippetType { - Obx, GetBuilder, GetBuilderAutoDispose, GetX + Obx, GetBuilder, Observer, GetBuilderAutoDispose, GetX, } \ No newline at end of file diff --git a/src/main/kotlin/intention_action/Snippets.kt b/src/main/kotlin/intention_action/Snippets.kt index 1ed898b..23c8644 100644 --- a/src/main/kotlin/intention_action/Snippets.kt +++ b/src/main/kotlin/intention_action/Snippets.kt @@ -13,6 +13,7 @@ object Snippets { return when (snippetType) { SnippetType.Obx -> snippetObx(widget) SnippetType.GetBuilder -> snippetGetBuilder(widget) + SnippetType.Observer -> snippetObserver(widget) SnippetType.GetBuilderAutoDispose -> snippetGetBuilderAutoDispose(widget) SnippetType.GetX -> snippetGetX(widget) else -> "" @@ -35,6 +36,14 @@ object Snippets { ) } + private fun snippetObserver(widget: String): String { + return String.format( + """Observer(builder: (BuildContext context) { + return %1${"$"}s; +})""", widget + ) + } + private fun snippetGetBuilderAutoDispose(widget: String): String { return String.format( """GetBuilder<%1${"$"}s>( diff --git a/src/main/kotlin/intention_action/WrapWithGetAction.kt b/src/main/kotlin/intention_action/WrapWithGetAction.kt index 86556dc..bed44ac 100644 --- a/src/main/kotlin/intention_action/WrapWithGetAction.kt +++ b/src/main/kotlin/intention_action/WrapWithGetAction.kt @@ -23,3 +23,9 @@ class WrapWithObxAction : WrapWithAction(SnippetType.Obx) { return "Wrap with Obx" } } + +class WrapWithObserverAction : WrapWithAction(SnippetType.Observer) { + override fun getText(): String { + return "Wrap with Observer" + } +} \ No newline at end of file diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index becb508..dffa694 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -45,6 +45,11 @@ intention_action.WrapWithGetBuilderAction GetX + + + intention_action.WrapWithObserverAction + GetX + intention_action.WrapWithGetBuilderAutoDisposeAction diff --git a/src/main/resources/templates/normal/binding.dart b/src/main/resources/templates/normal/binding_4.dart similarity index 100% rename from src/main/resources/templates/normal/binding.dart rename to src/main/resources/templates/normal/binding_4.dart diff --git a/src/main/resources/templates/normal/binding_5.dart b/src/main/resources/templates/normal/binding_5.dart new file mode 100644 index 0000000..1e6a3f4 --- /dev/null +++ b/src/main/resources/templates/normal/binding_5.dart @@ -0,0 +1,10 @@ +import 'package:get/get.dart'; + +import 'logic.dart'; + +class @nameBinding extends Binding { + @override + List dependencies() { + return [Bind.lazyPut(() => @nameLogic())]; + } +} \ No newline at end of file