1
1
package org.tabooproject.intellij.step
2
2
3
+ import ai.grazie.utils.capitalize
3
4
import com.intellij.ide.util.projectWizard.ModuleWizardStep
5
+ import com.intellij.ide.util.projectWizard.WizardContext
4
6
import com.intellij.ui.dsl.builder.columns
5
7
import com.intellij.ui.dsl.builder.panel
6
8
import org.tabooproject.intellij.component.AddDeleteModuleListPanel
@@ -10,6 +12,7 @@ import org.tabooproject.intellij.util.LOCAL_MODULES
10
12
import java.io.IOException
11
13
import java.util.concurrent.TimeUnit
12
14
import javax.swing.JComponent
15
+ import javax.swing.JTextField
13
16
14
17
private fun fetchAndParseModules (
15
18
url : String = "https : // raw.githubusercontent.com/TabooLib/taboolib-gradle-plugin/master/src/main/kotlin/io/izzel/taboolib/gradle/Standards.kt",
@@ -51,7 +54,7 @@ val TEMPLATE_DOWNLOAD_MIRROR = mapOf(
51
54
)
52
55
53
56
data class ConfigurationProperty (
54
- var name : String = " untitled " ,
57
+ var name : String? = null ,
55
58
var mainClass : String = " org.example.untitled.UntitledPlugin" ,
56
59
var version : String = " 1.0-SNAPSHOT" ,
57
60
var mirrorIndex : String = " github.com" ,
@@ -61,10 +64,13 @@ data class ConfigurationProperty(
61
64
},
62
65
)
63
66
64
- class ConfigurationPropertiesStep : ModuleWizardStep () {
67
+ class ConfigurationPropertiesStep ( val context : WizardContext ) : ModuleWizardStep() {
65
68
66
69
private val modulePanel = AddDeleteModuleListPanel (" Modules" , property.modules)
67
70
71
+ private var mainClassTextField: JTextField ? = null
72
+
73
+
68
74
companion object {
69
75
70
76
var property = ConfigurationProperty ()
@@ -82,15 +88,20 @@ class ConfigurationPropertiesStep : ModuleWizardStep() {
82
88
row(" Plugin name:" ) {
83
89
textField()
84
90
.apply {
91
+ property.mainClass = " org.example.${property.name?.lowercase()} .${property.name?.capitalize()} Plugin"
85
92
component.text = property.name
86
93
component.columns = 30
87
- }.onChanged { property.name = it.text }
94
+ }.onChanged {
95
+ autoChangeMainClass(it.text)
96
+ property.name = it.text
97
+ }
88
98
}
89
99
row(" Plugin main class:" ) {
90
100
textField()
91
101
.apply {
92
102
component.text = property.mainClass
93
103
component.columns = 30
104
+ mainClassTextField = this .component
94
105
}.onChanged { property.mainClass = it.text }
95
106
}
96
107
row(" Plugin version:" ) {
@@ -120,11 +131,56 @@ class ConfigurationPropertiesStep : ModuleWizardStep() {
120
131
}
121
132
}
122
133
134
+ override fun updateStep () {
135
+ if (property.name == null ){
136
+ property.name = context.projectName
137
+ }
138
+ }
139
+
123
140
override fun updateDataModel () {
124
141
// 针对控件数据 (AddDeleteListPanel) 无法直接绑定到数据模型的问题,手动导出数据
125
142
property.modules.apply {
126
143
clear()
127
144
addAll(modulePanel.export())
128
145
}
129
146
}
147
+
148
+ /* *
149
+ * 自动更改主类名。
150
+ * 此函数检查当前的主类名是否符合特定的插件命名模式,并根据匹配情况自动更新主类名。
151
+ * 如果输入的文本与插件名匹配,且现有插件名以该文本为前缀,则将插件名中的该文本替换为新文本。
152
+ *
153
+ * @param text 要替换到主类名中的新文本。
154
+ */
155
+ private fun autoChangeMainClass (text : String ) {
156
+ // 如果 mainClassTextField 未初始化,则直接返回
157
+ if (mainClassTextField == null ) return
158
+
159
+ // 提取重复的字符串操作,减少代码重复并提高性能
160
+ var baseClass = property.mainClass.substringBeforeLast(" ." )
161
+ val currentLastPart = property.mainClass.substringAfterLast(" ." )
162
+
163
+ val newLastPart = when {
164
+ currentLastPart == " Plugin" -> text + " Plugin"
165
+ currentLastPart.isEmpty() -> text.capitalize()
166
+ currentLastPart == property.name?.lowercase() -> text.capitalize()
167
+ currentLastPart.removeSuffix(" Plugin" ).lowercase() == property.name?.lowercase() -> text.capitalize() + " Plugin"
168
+ else -> currentLastPart
169
+ }
170
+
171
+
172
+ val lastGroup = baseClass.substringAfterLast(" ." ).let {
173
+ if (it.lowercase() == property.name?.lowercase()){
174
+ return @let text.lowercase()
175
+ }else {
176
+ it
177
+ }
178
+ }
179
+
180
+ baseClass = baseClass.substringBeforeLast(" ." )
181
+
182
+ // 更新 mainClassTextField 的文本
183
+ mainClassTextField!! .text = " $baseClass .$lastGroup .$newLastPart "
184
+
185
+ }
130
186
}
0 commit comments