Skip to content

Commit 1ca2722

Browse files
committed
add auto change main class function
Signed-off-by: dkinging <[email protected]>
1 parent 20c2f16 commit 1ca2722

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

src/main/kotlin/org/tabooproject/intellij/FunctionTemplate.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ object FunctionTemplate {
99
return mutableMapOf<String, Any>().apply {
1010
val configProperty = ConfigurationPropertiesStep.property
1111
// 插件名
12-
put("name", configProperty.name)
12+
put("name", configProperty.name!!)
1313
// 主类
1414
put("group", configProperty.mainClass
1515
// 截去插件名

src/main/kotlin/org/tabooproject/intellij/ProjectBuilder.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class ProjectBuilder : AbstractNewProjectWizardBuilder() {
4848
}
4949

5050
override fun createWizardSteps(wizardContext: WizardContext, modulesProvider: ModulesProvider): Array<ModuleWizardStep> {
51-
return arrayOf(ConfigurationPropertiesStep(), OptionalPropertiesStep())
51+
return arrayOf(ConfigurationPropertiesStep(wizardContext), OptionalPropertiesStep())
5252
}
5353

5454
override fun cleanup() {

src/main/kotlin/org/tabooproject/intellij/step/ConfigurationPropertiesStep.kt

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.tabooproject.intellij.step
22

3+
import ai.grazie.utils.capitalize
34
import com.intellij.ide.util.projectWizard.ModuleWizardStep
5+
import com.intellij.ide.util.projectWizard.WizardContext
46
import com.intellij.ui.dsl.builder.columns
57
import com.intellij.ui.dsl.builder.panel
68
import org.tabooproject.intellij.component.AddDeleteModuleListPanel
@@ -10,6 +12,7 @@ import org.tabooproject.intellij.util.LOCAL_MODULES
1012
import java.io.IOException
1113
import java.util.concurrent.TimeUnit
1214
import javax.swing.JComponent
15+
import javax.swing.JTextField
1316

1417
private fun fetchAndParseModules(
1518
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(
5154
)
5255

5356
data class ConfigurationProperty(
54-
var name: String = "untitled",
57+
var name: String? = null,
5558
var mainClass: String = "org.example.untitled.UntitledPlugin",
5659
var version: String = "1.0-SNAPSHOT",
5760
var mirrorIndex: String = "github.com",
@@ -61,10 +64,13 @@ data class ConfigurationProperty(
6164
},
6265
)
6366

64-
class ConfigurationPropertiesStep : ModuleWizardStep() {
67+
class ConfigurationPropertiesStep(val context: WizardContext) : ModuleWizardStep() {
6568

6669
private val modulePanel = AddDeleteModuleListPanel("Modules", property.modules)
6770

71+
private var mainClassTextField: JTextField? = null
72+
73+
6874
companion object {
6975

7076
var property = ConfigurationProperty()
@@ -82,15 +88,20 @@ class ConfigurationPropertiesStep : ModuleWizardStep() {
8288
row("Plugin name:") {
8389
textField()
8490
.apply {
91+
property.mainClass = "org.example.${property.name?.lowercase()}.${property.name?.capitalize()}Plugin"
8592
component.text = property.name
8693
component.columns = 30
87-
}.onChanged { property.name = it.text }
94+
}.onChanged {
95+
autoChangeMainClass(it.text)
96+
property.name = it.text
97+
}
8898
}
8999
row("Plugin main class:") {
90100
textField()
91101
.apply {
92102
component.text = property.mainClass
93103
component.columns = 30
104+
mainClassTextField = this.component
94105
}.onChanged { property.mainClass = it.text }
95106
}
96107
row("Plugin version:") {
@@ -120,11 +131,56 @@ class ConfigurationPropertiesStep : ModuleWizardStep() {
120131
}
121132
}
122133

134+
override fun updateStep() {
135+
if (property.name == null){
136+
property.name = context.projectName
137+
}
138+
}
139+
123140
override fun updateDataModel() {
124141
// 针对控件数据 (AddDeleteListPanel) 无法直接绑定到数据模型的问题,手动导出数据
125142
property.modules.apply {
126143
clear()
127144
addAll(modulePanel.export())
128145
}
129146
}
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+
}
130186
}

0 commit comments

Comments
 (0)