forked from bordaigorl/sublime-markdown-editing
-
Notifications
You must be signed in to change notification settings - Fork 1
/
indent_list_item.py
42 lines (31 loc) · 1.53 KB
/
indent_list_item.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
import sublime_plugin
import re
class IndentListItemCommand(sublime_plugin.TextCommand):
def run(self, edit, reverse = False):
for region in self.view.sel():
line = self.view.line(region)
line_content = self.view.substr(line)
bullet_pattern = "([*+\\-])"
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):
bullets = self.view.settings().get("mde.list_indent_bullets", ["*", "-", "+"])
for key, bullet in enumerate(bullets):
if bullet in new_line:
new_line = new_line.replace(bullet, bullets[(key + (1 if not reverse else -1)) % len(bullets)])
break
# 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 not reverse:
# Do the indentation
new_line = re.sub(bullet_pattern, tab_str + "\\1", new_line)
else:
# Do the unindentation
new_line = re.sub(tab_str + bullet_pattern, "\\1", new_line)
# Insert the new item
self.view.replace(edit, line, new_line)
def is_enabled(self):
return bool(self.view.score_selector(self.view.sel()[0].a, "text.html.markdown"))