Skip to content

Commit 1d49b32

Browse files
committed
fix: file template doesn't work.
1 parent 58f560d commit 1d49b32

8 files changed

Lines changed: 57 additions & 52 deletions

File tree

src/com/dengzii/plugin/template/model/FileTreeDsl.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ class FileTreeDsl() : FileTreeNode() {
3535

3636
fun FileTreeNode.file(name: String) {
3737
if (!isDir) return
38-
if (name.getPlaceholder().isNotEmpty()) {
39-
allPlaceholder.addAll(name.getPlaceholder())
38+
name.getPlaceholder().forEach {
39+
if (getPlaceholderInherit()?.containsKey(it) == false) {
40+
placeholder(it, "")
41+
}
4042
}
4143
addChild(FileTreeNode(this, name, false))
4244
}

src/com/dengzii/plugin/template/model/FileTreeNode.kt

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ open class FileTreeNode() {
2929

3030
var placeholders: MutableMap<String, String>? = null
3131

32-
// all placeholder in tree node name
33-
val allPlaceholder = mutableListOf<String>()
34-
3532
// template for node, higher priority than fileTemplates
3633
private var template: String? = null
3734

@@ -70,9 +67,6 @@ open class FileTreeNode() {
7067
this.name = name
7168
this.parent = parent
7269
this.isDir = isDir
73-
if (name.getPlaceholder().isNotEmpty()) {
74-
allPlaceholder.addAll(name.getPlaceholder())
75-
}
7670
}
7771

7872
fun getTreeNodeCount(): Int {
@@ -146,6 +140,21 @@ open class FileTreeNode() {
146140
}
147141
}
148142

143+
fun removeAllTemplateInTree() {
144+
fileTemplates?.clear()
145+
template = null
146+
traversal({ it, _ ->
147+
it.removeAllTemplateInTree()
148+
})
149+
}
150+
151+
fun removeAllPlaceHolderInTree() {
152+
placeholders?.clear()
153+
traversal({ it, _ ->
154+
it.removeAllPlaceHolderInTree()
155+
})
156+
}
157+
149158
/**
150159
* Return all file template in tree node
151160
*/
@@ -169,11 +178,19 @@ open class FileTreeNode() {
169178
}
170179

171180
fun getFileTemplateInherit(): MutableMap<String, String>? {
172-
return fileTemplates ?: parent?.getFileTemplateInherit()
181+
return if (fileTemplates.isNullOrEmpty()) {
182+
return parent?.getFileTemplateInherit()
183+
} else {
184+
fileTemplates
185+
}
173186
}
174187

175188
fun getPlaceholderInherit(): MutableMap<String, String>? {
176-
return placeholders ?: parent?.getPlaceholderInherit()
189+
return if (placeholders.isNullOrEmpty()) {
190+
return parent?.getPlaceholderInherit()
191+
} else {
192+
placeholders
193+
}
177194
}
178195

179196
fun getTemplateFile(): String? {
@@ -351,17 +368,6 @@ open class FileTreeNode() {
351368
}
352369
}
353370

354-
/**
355-
* Return the placeholder key list in node tree.
356-
*/
357-
fun getAllPlaceholderInTree(): List<String> {
358-
val result = allPlaceholder.toMutableList()
359-
traversal({ fileTreeNode: FileTreeNode, _: Int ->
360-
result.addAll(fileTreeNode.allPlaceholder)
361-
})
362-
return result.toList()
363-
}
364-
365371
/**
366372
* Return the placeholder key/replacement map of the node tree.
367373
*/

src/com/dengzii/plugin/template/template/Template.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,12 @@ object Template {
3333
}
3434

3535
val EMPTY = FileTreeDsl {
36-
file("src")
36+
3737
}
3838

3939
val ANDROID_MVP = FileTreeDsl {
4040
placeholder("MVP_NAME", "Example")
4141

42-
// fileTemplate("", "")
43-
4442
file("\${MVP_NAME}Contract.java")
4543
file("\${MVP_NAME}View.java")
4644
file("\${MVP_NAME}Presenter.java")

src/com/dengzii/plugin/template/ui/EditableTable.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class EditableTable(header: Array<String>, colEditable: Array<Boolean> = emptyAr
6868
for (i in 0 until table.rowCount) {
6969
val key = table.getValueAt(i, 0).toString()
7070
val value = table.getValueAt(i, 1).toString()
71-
if (key.isBlank() || value.isBlank()) {
71+
if (key.isBlank()) {
7272
continue
7373
}
7474
result[key] = value

src/com/dengzii/plugin/template/ui/PreviewPanel.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import com.dengzii.plugin.template.tools.ui.PopMenuUtils
66
import com.dengzii.plugin.template.tools.ui.onRightMouseButtonClicked
77
import com.dengzii.plugin.template.utils.Logger
88
import com.intellij.icons.AllIcons
9-
import com.intellij.openapi.ui.JBMenuItem
109
import com.intellij.packageDependencies.ui.TreeModel
1110
import com.intellij.ui.ColoredTreeCellRenderer
1211
import com.intellij.ui.components.JBScrollPane
@@ -17,7 +16,6 @@ import java.awt.event.*
1716
import java.util.*
1817
import java.util.function.Consumer
1918
import javax.swing.Icon
20-
import javax.swing.JDialog
2119
import javax.swing.JPanel
2220
import javax.swing.JTree
2321
import javax.swing.tree.DefaultMutableTreeNode
@@ -28,7 +26,7 @@ class PreviewPanel : JPanel() {
2826

2927
private var fileTree: Tree = Tree()
3028

31-
private lateinit var module:Module
29+
private lateinit var module: Module
3230
private val fileIconMap: MutableMap<String, Icon> = HashMap()
3331
private var replacePlaceholder = true
3432
private var onTreeUpdateListener: (() -> Unit)? = null

src/com/dengzii/plugin/template/ui/RealConfigurePanel.kt

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,12 @@ class RealConfigurePanel : ConfigurePanel() {
4848

4949
fun cacheConfig() {
5050
currentConfig ?: return
51-
currentConfig!!.template.fileTemplates = tableFileTemp.getPairResult()
52-
currentConfig!!.template.placeholders = tablePlaceholder.getPairResult()
51+
currentConfig!!.template.run {
52+
removeAllPlaceHolderInTree()
53+
removeAllTemplateInTree()
54+
fileTemplates = tableFileTemp.getPairResult()
55+
placeholders = tablePlaceholder.getPairResult()
56+
}
5357
}
5458

5559
fun saveConfig() {
@@ -68,7 +72,6 @@ class RealConfigurePanel : ConfigurePanel() {
6872
panelFileTemp.add(tableFileTemp, BorderLayout.CENTER)
6973
}
7074

71-
7275
private fun initData() {
7376
actionbar.onAdd { e ->
7477
if (e != null) {
@@ -93,9 +96,12 @@ class RealConfigurePanel : ConfigurePanel() {
9396
tabbedPane.addChangeListener {
9497
currentConfig ?: return@addChangeListener
9598
when (tabbedPane.selectedIndex) {
96-
0 -> currentConfig!!.template.placeholders = tablePlaceholder.getPairResult()
97-
1 -> updateTableFileTemplate()
98-
2 -> updateTablePlaceholder()
99+
0 -> {
100+
currentConfig!!.template.placeholders = tablePlaceholder.getPairResult()
101+
currentConfig!!.template.fileTemplates = tableFileTemp.getPairResult()
102+
}
103+
1 -> tableFileTemp.setPairData(currentConfig!!.template.getAllTemplateMap())
104+
2 -> tablePlaceholder.setPairData(currentConfig!!.template.getAllPlaceholdersMap().toMutableMap())
99105
}
100106
panelPreview.setModuleConfig(currentConfig!!)
101107
}
@@ -115,22 +121,6 @@ class RealConfigurePanel : ConfigurePanel() {
115121
}
116122
}
117123

118-
private fun updateTableFileTemplate() {
119-
val fileTemplates = currentConfig!!.template.getAllTemplateMap()
120-
tableFileTemp.setPairData(fileTemplates)
121-
}
122-
123-
private fun updateTablePlaceholder() {
124-
val mergedPlaceholder = currentConfig!!.template.getAllPlaceholdersMap().toMutableMap()
125-
val allPlaceholders = currentConfig!!.template.getAllPlaceholderInTree()
126-
allPlaceholders.forEach { s: String ->
127-
if (s !in mergedPlaceholder) {
128-
mergedPlaceholder[s] = ""
129-
}
130-
}
131-
tablePlaceholder.setPairData(mergedPlaceholder)
132-
}
133-
134124
private fun addModuleTemplate(module: Module) {
135125
configs!!.add(module)
136126
templateListModel!!.addElement(module.templateName)
@@ -146,6 +136,17 @@ class RealConfigurePanel : ConfigurePanel() {
146136
val selectedIndex = getSelectedConfigIndex()
147137
configs!!.removeAt(selectedIndex)
148138
templateListModel!!.remove(selectedIndex)
139+
if (!listTemplate.isEmpty) {
140+
panelPreview.isEnabled = true
141+
listTemplate.selectedIndex = 0
142+
onConfigListSelected()
143+
} else {
144+
currentConfig = null
145+
tablePlaceholder.setPairData(mapOf())
146+
tableFileTemp.setPairData(mapOf())
147+
panelPreview.setModuleConfig(getEmpty())
148+
panelPreview.isEnabled = false
149+
}
149150
listTemplate.doLayout()
150151
}
151152

test/AucTemplateTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class AucTemplateTest {
1212
placeholder("PACKAGE_NAME", "com.dengzii.plugin")
1313
}
1414
println(app.placeholders)
15-
println(app.getAllPlaceholderInTree())
15+
// println(app.getAllPlaceholderInTree())
1616

1717
app.build()
1818
println(app)

test/FileTreeDslTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,6 @@ class FileTreeDslTest {
122122
dir("test")
123123
}
124124
}
125-
println(tree.getAllPlaceholderInTree())
125+
// println(tree.getAllPlaceholderInTree())
126126
}
127127
}

0 commit comments

Comments
 (0)