forked from SublimeText-Markdown/MarkdownEditing
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindent_list_multiitem.py
66 lines (56 loc) · 2.81 KB
/
indent_list_multiitem.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
import sublime_plugin
import re
try:
from MarkdownEditing.mdeutils import *
except ImportError:
from mdeutils import *
class IndentListMultiitemCommand(MDETextCommand):
def run(self, edit, reverse=False):
todo = []
for region in self.view.sel():
lines = self.view.line(region)
lines = self.view.split_by_newlines(lines)
for line in lines:
line_content = self.view.substr(line)
if len(line_content) == 0:
continue
# Determine how to indent (tab or spaces)
if self.view.settings().get("translate_tabs_to_spaces"):
tab_str = self.view.settings().get("tab_size", 4) * " "
else:
tab_str = "\t"
if re.match("^\\s*(>\\s*)?[*+\\-]\\s+(.*)$", line_content):
bullets = self.view.settings().get("mde.list_indent_bullets", ["*", "-", "+"])
bullet_pattern = '([' + ''.join(re.escape(i) for i in bullets) + '])'
bullet_pattern_a = "^\\s*(?:>\\s*)?("
bullet_pattern_b = ")\\s+"
new_line = line_content
# Transform the bullet to the next/previous bullet type
if self.view.settings().get("mde.list_indent_auto_switch_bullet", True):
for key, bullet in enumerate(bullets):
re_bullet = re.escape(bullet)
search_pattern = bullet_pattern_a + \
re_bullet + bullet_pattern_b
if re.search(search_pattern, line_content):
if reverse and new_line.startswith(bullet) and key is 0:
# In this case, do not switch bullets
continue
new_bullet = bullets[(key + (1 if not reverse else -1)) % len(bullets)]
new_line = re.sub(re_bullet, new_bullet, new_line, 1)
break
if not reverse:
# Do the indentation
new_line = re.sub(bullet_pattern, tab_str + "\\1", new_line, 1)
else:
# Do the unindentation
new_line = re.sub(tab_str + bullet_pattern, "\\1", new_line, 1)
else:
if not reverse:
new_line = tab_str + line_content
else:
new_line = re.sub(tab_str, "", line_content, 1)
# Insert the new item
todo.append([line, new_line])
while len(todo) > 0:
j = todo.pop()
self.view.replace(edit, j[0], j[1])