WIP new UI elem

This commit is contained in:
minjaesong
2018-12-08 23:10:48 +09:00
committed by Minjae Song
parent 4b04bf3781
commit 6f0a923df7
9 changed files with 294 additions and 94 deletions

View File

@@ -0,0 +1,118 @@
package net.torvald.terrarum
import java.util.*
/**
* Simplified version of YAML, only for the representation of a text tree.
*
* Example code:
* ```
* - File
* - New : Ctrl-N
* - Open : Ctrl-O
* - Open Recent
* - yaml_example.yaml
* - Yaml.kt
* - Close : Ctrl-W
* - Settings
* - Line Separators
* - CRLF
* - CR
* - LF
* - Edit
* - Undo : Ctrl-Z
* - Redo : Shift-Ctrl-Z
* - Cut : Ctrl-X
* - Copy : Ctrl-C
* - Paste : Ctrl-V
* - Find
* - Find : Ctrl-F
* - Replace : Shift-Ctrl-F
* - Convert Indents
* - To Spaces
* - Set Project Indentation
* - To Tabs
* - Refactor
* - Refactor This
* - Rename : Shift-Ctrl-R
* - Extract
* - Variable
* - Property
* - Function
* ```
*
* - All lines are indented with one space
* - All entries are preceded by '- ' (dash and a space)
* - All propery are separated by ' : ' (space colon space)
*
* Any deviation to the above rule will cause a parse failure, because it's simple and dumb as that.
*
* Created by minjaesong on 2018-12-08.
*/
inline class Yaml(val text: String) {
companion object {
val SEPARATOR = Regex(" : ")
}
fun parse(): QNDTreeNode<String> {
var currentIndentLevel = -1
val root = QNDTreeNode<String>()
var currentNode = root
val nodesStack = Stack<QNDTreeNode<String>>()
nodesStack.push(currentNode)
text.split('\n') .forEach {
val indentLevel = it.countSpaces()
val it = it.trimIndent()
if (it.startsWith("- ")) {
val nodeName = it.drop(2)
if (indentLevel == currentIndentLevel) {
val sibling = QNDTreeNode(nodeName, currentNode.parent)
currentNode.parent!!.children.add(sibling)
currentNode = sibling
}
else if (indentLevel > currentIndentLevel) {
val childNode = QNDTreeNode(nodeName, currentNode)
currentNode.children.add(childNode)
nodesStack.push(currentNode)
currentNode = childNode
currentIndentLevel = indentLevel
}
else {
repeat(currentIndentLevel - indentLevel) { currentNode = nodesStack.pop() }
currentIndentLevel = indentLevel
val sibling = QNDTreeNode(nodeName, currentNode.parent)
currentNode.parent!!.children.add(sibling)
currentNode = sibling
}
}
}
// test traverse resulting tree
/*root.traversePreorder { node, depth ->
repeat(depth + 1) { print("-") }
println("${node.data} -> ${node.parent}")
}*/
return root
}
private fun String.countSpaces(): Int {
var c = 0
while (c <= this.length) {
if (this[c] == ' ')
c++
else
break
}
return c
}
}