Skip to content

Commit 080be6a

Browse files
committed
修复无法展开包名及目录问题
1 parent 6fd0f66 commit 080be6a

File tree

3 files changed

+95
-19
lines changed

3 files changed

+95
-19
lines changed

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

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ open class FileTreeNode() {
4848

4949
companion object {
5050

51+
var packageNameToDirs = true
5152
private val TAG = FileTreeNode::class.java.simpleName
5253
private val sPathSplitPattern = Pattern.compile("[./]")
5354
private val sPlaceholderPattern = Pattern.compile("\\$\\{([A-Za-z0-9_\\-]+)}")
@@ -76,12 +77,19 @@ open class FileTreeNode() {
7677
fun removeFromParent(): Boolean {
7778
if (parent != null) {
7879
parent!!.labeledChildren.remove(getLabel())
79-
parent!!.children.remove(this)
80+
parent!!.realChildren.remove(this)
81+
parent = null
8082
return true
8183
}
8284
return false
8385
}
8486

87+
/**
88+
* add child to this node
89+
*
90+
* @param child The child need be add
91+
* @param override Weather override the node with the same name and type
92+
*/
8593
fun addChild(child: FileTreeNode, override: Boolean = false): Boolean {
8694
if (hasChild(child.getLabel())) {
8795
if (override) {
@@ -92,13 +100,13 @@ open class FileTreeNode() {
92100
}
93101
}
94102
child.parent = this
95-
children.add(child)
103+
realChildren.add(child)
96104
labeledChildren[child.getLabel()] = child
97105
return true
98106
}
99107

100108
fun hasChild(name: String, isDir: Boolean): Boolean {
101-
children.forEach {
109+
realChildren.forEach {
102110
if (it.name == name && isDir == it.isDir) {
103111
return true
104112
}
@@ -122,7 +130,6 @@ open class FileTreeNode() {
122130
}
123131

124132
fun getRealName(fileName: String = this.name): String {
125-
126133
return fileName.replacePlaceholder(getPlaceholderInherit(), !isDir)
127134
}
128135

@@ -190,7 +197,7 @@ open class FileTreeNode() {
190197
fun traversal(block: (it: FileTreeNode, depth: Int) -> Unit, depth: Int = 0) {
191198
if (!isDir) return
192199

193-
children.forEach {
200+
realChildren.forEach {
194201
block(it, depth)
195202
it.traversal(block, depth + 1)
196203
}
@@ -238,43 +245,60 @@ open class FileTreeNode() {
238245
return this == parent || parent == null
239246
}
240247

248+
/**
249+
* build file tree
250+
* replace placeholder in name
251+
* if this is directory and name is a path or package name
252+
* the name will expand to several dirs
253+
*/
241254
fun build() {
242255

243-
name = getRealName()
244256
if (!isDir) {
245257
return
246258
}
259+
name = getRealName()
247260
if (!isRoot()) {
248-
val dir = name.split(sPathSplitPattern).toMutableList()
261+
val dir = if (packageNameToDirs) {
262+
name.split(sPathSplitPattern)
263+
} else {
264+
name.split("/")
265+
}.filter {
266+
it.isNotBlank()
267+
}.toMutableList()
268+
249269
expandDirs(dir)
250270
}
251-
children.forEach {
271+
realChildren.forEach {
252272
it.build()
253273
}
254274
}
255275

256-
private fun expandDirs(dirs: List<String>) {
276+
/**
277+
* expand the dir list
278+
* all the children of this will move the new dir
279+
*/
280+
private fun expandDirs(dirs: MutableList<String>) {
257281
if (dirs.isEmpty() || dirs.size == 1) {
258282
return
259283
}
260284
this.name = dirs.first()
261-
dirs.drop(0)
285+
dirs.removeAt(0)
262286

263-
var node: FileTreeNode? = null
264287
var preNode: FileTreeNode = this
265288
var newChild: FileTreeNode
289+
290+
val oldChildren = realChildren.toMutableList()
291+
oldChildren.forEach {
292+
it.removeFromParent()
293+
}
266294
dirs.forEach {
267295
newChild = FileTreeNode(preNode, it, true)
268-
if (node == null) {
269-
node = newChild
270-
}
271296
preNode.addChild(newChild)
272297
preNode = newChild
273298
}
274-
preNode.children = children
275-
children.clear()
276-
labeledChildren.clear()
277-
realChildren.clear()
299+
oldChildren.forEach {
300+
preNode.addChild(it)
301+
}
278302
}
279303

280304
fun getAllPlaceholderInTree(): List<String> {
@@ -297,6 +321,11 @@ open class FileTreeNode() {
297321
return result
298322
}
299323

324+
/**
325+
* get the tree graph of node inherit
326+
*
327+
* @return The tree graph of node
328+
*/
300329
fun getTreeGraph(): String {
301330
return getNodeGraph().toString()
302331
}

test/AucTemplateTest.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ class AucTemplateTest {
1313
}
1414
println(app.placeholders)
1515
println(app.getAllPlaceholderInTree())
16-
println(app.getTreeGraph())
1716

1817
app.build()
1918
println(app)

test/FileTreeDslTest.kt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,52 @@ class FileTreeDslTest {
5858
}
5959
println(tree.getTreeGraph())
6060
}
61+
62+
@Test
63+
fun expandDirectoriesTest() {
64+
val tree = FileTreeDsl {
65+
dir("src") {
66+
dir("com.dengzii.plugin") {
67+
dir("model")
68+
dir("template")
69+
}
70+
dir("test")
71+
}
72+
}
73+
tree.build()
74+
println(tree.getTreeGraph())
75+
76+
val tree2 = FileTreeDsl {
77+
dir("src") {
78+
dir("com.dengzii.plugin") {
79+
dir("dir1/dir2/") {
80+
dir("model")
81+
dir("template")
82+
}
83+
dir("model")
84+
dir("template")
85+
}
86+
dir("test")
87+
}
88+
}
89+
tree2.build()
90+
println(tree2.getTreeGraph())
91+
}
92+
93+
@Test
94+
fun expandDirectoriesInPlaceholderTest() {
95+
96+
val tree = FileTreeDsl {
97+
placeholder("PACKAGE_NAME", "com.dengzii.plugin")
98+
dir("src") {
99+
dir("\${PACKAGE_NAME}") {
100+
dir("model")
101+
dir("template")
102+
}
103+
dir("test")
104+
}
105+
}
106+
tree.build()
107+
println(tree.getTreeGraph())
108+
}
61109
}

0 commit comments

Comments
 (0)