-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathurtext_folding.py
81 lines (73 loc) · 2.86 KB
/
urtext_folding.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
import sublime, sublime_plugin
folding_margins = {
'frame.urtext' : [2,2],
'entity.name.struct.datestamp.urtext' : [1,1],
'metadata_entry.urtext' : [0,0],
'inline_node_5.urtext' : [1,1],
'inline_node_4.urtext' : [1,1],
'inline_node_3.urtext' : [1,1],
'inline_node_2.urtext' : [1,1],
'inline_node_1.urtext' : [1,1],
'inline_node_as_metadata_1.urtext' : [3,1],
'compact_node.urtext' : [1,0], }
scopes_to_fold = [
'frame.urtext',
'entity.name.struct.datestamp.urtext',
'metadata_entry.urtext',
'compact_node.urtext',
'inline_node_5.urtext',
'inline_node_4.urtext',
'inline_node_3.urtext',
'inline_node_2.urtext',
'inline_node_as_metadata_1.urtext',
'inline_node_1.urtext',
]
class ToggleFoldSingleCommand(sublime_plugin.TextCommand):
def run(self, edit):
region = self.view.extract_scope(self.view.sel()[0].a)
scope_name = self.view.scope_name(self.view.sel()[0].a).split(' ')
scope = get_scope_for_folding(scope_name)
if scope and region:
region = expand_scope_for_folding(scope_name, region, self.view)
if region:
if self.view.is_folded(region):
self.view.unfold(region)
else:
self.view.fold(region)
class ToggleFoldAllCommand(sublime_plugin.TextCommand):
def run(self, edit):
region = self.view.extract_scope(self.view.sel()[0].a)
scope_name = self.view.scope_name(self.view.sel()[0].a).split(' ')
scope = get_scope_for_folding(scope_name)
if scope:
region = expand_scope_for_folding(scope_name, region, self.view)
if region:
all_regions = self.view.find_by_selector(scope)
if self.view.is_folded(region):
action = self.view.unfold
else:
action = self.view.fold
for r in all_regions:
r = expand_scope_for_folding(scope, r, self.view)
action(r)
def get_scope_for_folding(scope_name):
for s in scopes_to_fold:
if s in scope_name:
if s == 'compact_node.urtext':
for t in scopes_to_fold:
if t in scope_name and t != 'compact_node.urtext':
return t
return s
def expand_scope_for_folding(scope_name, region, view):
for s in scopes_to_fold:
if s in scope_name:
if s == 'compact_node.urtext':
for t in scopes_to_fold:
if t != 'compact_node.urtext' and t in scope_name:
s = t
region = view.expand_to_scope(region.a, s)
if region:
return sublime.Region(
region.a + folding_margins[s][0],
region.b - folding_margins[s][1])
return region