-
Notifications
You must be signed in to change notification settings - Fork 71
/
note_headings.py
212 lines (177 loc) · 8.18 KB
/
note_headings.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# -*- coding: utf-8 -*-
"""
This is borrowed from [SmartMarkdown plugin](https://github.com/demon386/SmartMarkdown)
Smart folding is a feature borrowed from [Org-mode](http://org-mode.org).
It enables folding / unfolding the headlines by simply pressing TAB on headlines.
Global headline folding / unfolding is recommended to be trigged by Shift + TAB,
at anywhere.
"""
# Author: Muchenxuan Tong <[email protected]>
import re
import sys, os
import sublime
import sublime_plugin
sys.path.append(os.path.join(os.path.dirname(__file__), 'lib'))
import headline
from utilities import is_region_void
HEADLINE_PATTERN = re.compile(r'^(#+)\s.*')
class NoteSmartNewLineCommand(sublime_plugin.TextCommand):
"""Changes behavior of default 'insert line after'
Puts new line after folding mark if any.
"""
def run(self, edit):
points = []
for s in self.view.sel():
r = self.view.full_line(s)
if headline._is_region_folded(r.b + 1, self.view):
i = headline.region_of_content_of_headline_at_point(self.view, s.b)
else:
i = sublime.Region(r.a, r.b - 1)
points.append(i)
self.view.insert(edit, i.b, '\n')
self.view.sel().clear()
for p in points:
self.view.sel().add(p.b + 1)
class NoteSmartFoldingCommand(sublime_plugin.TextCommand):
"""Smart folding is used to fold / unfold headline at the point.
It's designed to bind to TAB key, and if the current line is not
a headline, a \t would be inserted.
"""
def run(self, edit):
ever_matched = False
for region in self.view.sel():
matched = self.fold_or_unfold_headline_at_point(region.a)
if matched:
ever_matched = True
if not ever_matched:
for r in self.view.sel():
self.view.insert(edit, r.a, '\t')
self.view.show(r)
def fold_or_unfold_headline_at_point(self, from_point):
"""Smart folding of the current headline.
Unfold only when it's totally folded. Otherwise fold it.
"""
_, level = headline.headline_and_level_at_point(self.view,
from_point)
# Not a headline, cancel
if level is None or not headline.is_scope_headline(self.view, from_point):
return False
content_region = headline.region_of_content_of_headline_at_point(self.view,
from_point)
# If the content is empty, Nothing needs to be done.
if content_region is None:
# Return True because there is a headline anyway.
return True
# Check if content region is folded to decide the action.
if self.is_region_totally_folded(content_region):
self.unfold_yet_fold_subheads(content_region, level)
else:
self.view.fold(sublime.Region(content_region.a - 1, content_region.b))
return True
def is_region_totally_folded(self, region):
"""Decide if the region is folded. Treat empty region as folded."""
if (region is None) or (region.a == region.b):
return True
for i in self.view.folded_regions():
if i.contains(region):
return True
return False
def unfold_yet_fold_subheads(self, region, level):
"""Unfold the region while keeping the subheadlines folded."""
## First unfold all
self.view.unfold(region)
## Fold subheads
child_headline_region, _ = headline.find_headline(self.view, region.a, level, True, \
headline.MATCH_CHILD)
while (not is_region_void(child_headline_region) and child_headline_region.b <= region.b):
child_content_region = headline.region_of_content_of_headline_at_point(self.view,
child_headline_region.a)
if child_content_region is not None:
self.view.fold(sublime.Region(child_content_region.a - 1, child_content_region.b))
search_start_point = child_content_region.b
else:
search_start_point = child_headline_region.b
child_headline_region, _ = headline.find_headline(self.view, \
search_start_point, level, True, \
headline.MATCH_CHILD,
skip_headline_at_point=True)
class NoteGlobalFoldingCommand(NoteSmartFoldingCommand):
"""Global folding / unfolding headlines at any point.
Unfold only when top-level headlines are totally folded.
Otherwise fold.
"""
def run(self, edit):
if self.is_global_folded():
# Unfold all
self.unfold_all()
else:
self.fold_all()
def is_global_folded(self):
"""Check if all headlines are folded.
"""
region, level = headline.find_headline(self.view, 0, \
headline.ANY_LEVEL, True)
# Treating no heeadline as folded, since unfolded all makes
# no harm in this situation.
if is_region_void(region):
return True
point = region.a
# point can be zero
while (point is not None and region):
region = headline.region_of_content_of_headline_at_point(self.view, \
point)
if not is_region_void(region):
point = region.b
if not self.is_region_totally_folded(region):
return False
else:
region, level = headline.find_headline(self.view, point, \
headline.ANY_LEVEL, \
True,
skip_headline_at_point=True)
if not is_region_void(region):
point = region.a
return True
def unfold_all(self):
self.view.unfold(sublime.Region(0, self.view.size()))
self.view.show(self.view.sel()[0])
def fold_all(self):
region, level = headline.find_headline(self.view, \
0, \
headline.ANY_LEVEL, \
True)
# At this point, headline region is sure to exist, otherwise it would be
# treated as gobal folded. (self.is_global_folded() would return True)
point = region.a
# point can be zero
while (point is not None and region):
region = headline.region_of_content_of_headline_at_point(self.view, \
point)
if not is_region_void(region):
point = region.b
self.view.fold(sublime.Region(region.a - 1, region.b))
region, level = headline.find_headline(self.view, point, \
headline.ANY_LEVEL,
True, \
skip_headline_at_point=True)
if not is_region_void(region):
point = region.a
self.adjust_cursors_and_view()
def adjust_cursors_and_view(self):
"""After folder, adjust cursors and view.
If the current point is inside the folded region, move it move
otherwise it's easy to perform some unintentional editing.
"""
folded_regions = self.view.folded_regions()
new_sel = []
for r in self.view.sel():
for folded in folded_regions:
if folded.contains(r):
new_sel.append(sublime.Region(folded.b, folded.b))
break
else:
new_sel.append(r)
self.view.sel().clear()
for r in new_sel:
self.view.sel().add(r)
self.view.show(r)