-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathIndentRespectfulSort.py
99 lines (88 loc) · 3.87 KB
/
IndentRespectfulSort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import sublime, sublime_plugin, sys
class IndentRespectfulSortCommand(sublime_plugin.TextCommand):
def run(self, edit, **args):
#self.view.insert(edit, 0, "Hello, World!")
region = sublime.Region(0, self.view.size())
#lines = self.view.lines(region)
content = self.view.substr(sublime.Region(0, self.view.size()))
lines = content.split("\n")
onlyDepth = args.get("onlyDepth", None)
if onlyDepth is not None:
onlyDepth = int(onlyDepth) - 1
rootNode = RootNode(lines, args.get("indent","\t"), 0, int(args.get("maxDepth", sys.maxsize)) , onlyDepth)
self.view.erase(edit, region)
self.view.insert(edit, 0, str(rootNode))
class RootNode:
def __init__(self, content, indent, level, maxDepth, onlyDepth):
self.content = content
self.indent = indent
self.level = level
self.maxDepth = maxDepth
self.onlyDepth = onlyDepth
self.children = self.getChildren()
def getChildren(self):
children = []
indices = []
for idx, val in enumerate(self.content):
import re
if re.search("^\S.*", val):
indices.append(idx)
iterate = True
startIdx = 0
while iterate:
if len(indices) > startIdx + 1:
endIdx = indices[startIdx + 1]
children.append(Node(str(self.content[indices[startIdx]]), self.level + 1, self.indent , list(self.content[indices[startIdx] + 1: endIdx]), self.maxDepth, self.onlyDepth))
startIdx += 1
elif len(indices) == startIdx + 1:
children.append(Node(str(self.content[indices[startIdx]]), self.level + 1, self.indent , list(self.content[indices[startIdx]+ 1:]), self.maxDepth, self.onlyDepth))
startIdx += 1
iterate = False
else:
iterate = False
return children
def __str__ (self):
childrenString = ""
if (self.onlyDepth is None) or (self.onlyDepth is not None and self.level == self.onlyDepth):
self.children.sort(key=lambda node: node.value)
for child in self.children:
childrenString += str(child)
return childrenString
class Node:
def __init__(self, value, level, indent, content, maxDepth, onlyDepth):
self.value = value
self.indent = indent
self.content = content
self.level = level
self.maxDepth = maxDepth
self.onlyDepth = onlyDepth
self.children = self.getChildren()
def getChildren(self):
children = []
indices = []
for idx, val in enumerate(self.content):
import re
if re.search("^" + self.indent*self.level +"\S.*", val):
indices.append(idx)
iterate = True
startIdx = 0
while iterate:
if len(indices) > startIdx + 1:
endIdx = indices[startIdx + 1]
children.append(Node(str(self.content[indices[startIdx]]), self.level + 1, self.indent, self.content[indices[startIdx] + 1: endIdx],self.maxDepth, self.onlyDepth))
startIdx += 1
elif len(indices) == startIdx + 1:
children.append(Node(str(self.content[indices[startIdx]]), self.level + 1, self.indent, self.content[indices[startIdx] + 1:], self.maxDepth, self.onlyDepth))
startIdx += 1
iterate = False
else:
iterate = False
return children
def __str__(self):
childrenString = ""
if self.level < self.maxDepth:
if (self.onlyDepth is None) or (self.onlyDepth is not None and self.level == self.onlyDepth):
self.children.sort(key=lambda node: node.value)
for child in self.children:
childrenString += str(child)
return self.value + "\n" + childrenString