-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdir_structure.py
147 lines (111 loc) · 3.36 KB
/
dir_structure.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import sublime
import sublime_plugin
import re
class FileNode:
def __init__(self, level, isLastChild, nodeStr, parent):
"""
FileNode constructor
Args:
level: level of the node
isLastChild: is last node?
nodeStr: full content string of the node
"""
self.level = level
self.isLastChild = isLastChild
self.text = ''
self.comment = ''
self.nodeStr = nodeStr
self.hasChild = False
self.children = self.getChildren()
self.parent = parent
if len(self.children):
self.hasChild = True
self.setText(nodeStr)
def splitChild(self, childrenStr):
"""
Split nodes with ','
Args:
childrenStr: content string of children
Returns:
children array
"""
result = []
temp = []
count = 0
arr = childrenStr.split(',')
for i,str in enumerate(arr):
count+=str.count('(')
count-=str.count(')')
temp.append(str)
if count == 0:
result.append(','.join(temp))
temp = []
return result
def setText(self, nodeStr):
"""
Set text and comment for the node
Args:
nodeStr: full content string of the node
"""
pattern = re.compile(r'^([\w\#\-\.\s]+)')
matched = pattern.match(nodeStr)
if matched and matched.group(1):
arr = matched.group(1).split('#')
self.text = arr[0]
if len(arr) == 2:
self.comment = arr[1]
def getChildren(self):
"""
Get children of the node
Returns:
children array
"""
pattern = re.compile(r'^.+?\((.*)\)$')
matched = pattern.match(self.nodeStr)
result = []
if matched:
s = matched.group(1)
children = self.splitChild(s)
for i,child in enumerate(children):
child = FileNode(self.level + 1, bool(i == len(children) - 1), child, self)
result.append(child)
return result
class DirStructureCommand(sublime_plugin.TextCommand):
def buildNodeList(self, node):
self.nodeList.append(node)
if node.hasChild:
for i,a in enumerate(node.children):
self.buildNodeList(node.children[i])
def prefix(self, node):
"""
Prefix node
Args:
node: node
Return:
string prefix of a node
"""
if node.parent == 'root':
return ''
else:
return self.prefix(node.parent) + (' ' if node.parent.isLastChild else '│ ')
def run(self, edit):
self.nodeList = []
for region in self.view.sel():
if not region.empty():
s = self.view.substr(region)
arr = s.split('\n')
for i,a in enumerate(arr):
node = FileNode(0, bool(i == len(arr) - 1), arr[i], 'root')
self.buildNodeList(node)
result = ['.']
max = 0
for j,node in enumerate(self.nodeList):
line = self.prefix(node) + ('└── ' if node.isLastChild else '├── ') + node.text
max = len(line) if max < len(line) else max
node.line = line
for k,node in enumerate(self.nodeList):
padding = (max + 4 - len(node.line)) * '·'
if node.comment:
node.line = ' '.join([node.line, padding, node.comment])
result.append(node.line)
self.view.replace(edit, region, ('\n').join(result))