Skip to content

Commit

Permalink
adjust getX5, add "Observer" wrap
Browse files Browse the repository at this point in the history
  • Loading branch information
xdd666t committed Aug 17, 2024
1 parent 598d9c1 commit c36a3af
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 81 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
4 changes: 4 additions & 0 deletions changeNotes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 3.5.x
- Adjust getX5
- Add "Observer" wrap

# 3.3.x
- Adjust StatefulWidget template
- Optimize wrap feature
Expand Down
127 changes: 57 additions & 70 deletions src/main/kotlin/action/NewGetXAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import helper.GetXName
import helper.TemplateInfo
import java.io.*
import java.util.*
import kotlin.collections.HashMap


class NewGetXAction : AnAction() {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
}
}

Expand All @@ -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"
)
}

Expand Down Expand Up @@ -166,6 +169,8 @@ class NewGetXAction : AnAction() {
}
}

private var replaceContentMap = HashMap<String, String>()

//content need deal
private fun dealContent(inputFileName: String): String {
//module name
Expand All @@ -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 {
Expand Down
21 changes: 14 additions & 7 deletions src/main/kotlin/action/NewGetXView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@ 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
*/
lateinit var disposeBox: JCheckBox
lateinit var lifecycleBox: JCheckBox
lateinit var lintNormBox: JCheckBox
lateinit var bindingBox: JCheckBox

/**
* select Template:Template Function
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/helper/DataService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class DataService : PersistentStateComponent<DataService> {
@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,
)
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/helper/GetXConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/helper/GetXName.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/intention_action/SnippetType.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package intention_action

enum class SnippetType {
Obx, GetBuilder, GetBuilderAutoDispose, GetX
Obx, GetBuilder, Observer, GetBuilderAutoDispose, GetX,
}
9 changes: 9 additions & 0 deletions src/main/kotlin/intention_action/Snippets.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> ""
Expand All @@ -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>(
Expand Down
6 changes: 6 additions & 0 deletions src/main/kotlin/intention_action/WrapWithGetAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
5 changes: 5 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
<className>intention_action.WrapWithGetBuilderAction</className>
<category>GetX</category>
</intentionAction>
<!--Observer Action-->
<intentionAction>
<className>intention_action.WrapWithObserverAction</className>
<category>GetX</category>
</intentionAction>
<!--GetBuilder(Auto Dispose) Action-->
<intentionAction>
<className>intention_action.WrapWithGetBuilderAutoDisposeAction</className>
Expand Down
10 changes: 10 additions & 0 deletions src/main/resources/templates/normal/binding_5.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:get/get.dart';

import 'logic.dart';

class @nameBinding extends Binding {
@override
List<Bind> dependencies() {
return [Bind.lazyPut(() => @nameLogic())];
}
}

0 comments on commit c36a3af

Please sign in to comment.