forked from SublimeText-Markdown/MarkdownEditing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindent_list_item.py
49 lines (36 loc) · 1.71 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
43
44
45
46
47
48
49
import sublime_plugin
import re
try:
from MarkdownEditing.mdeutils import *
except ImportError:
from mdeutils import *
class IndentListItemCommand(MDETextCommand):
def run(self, edit, reverse=False):
for region in self.view.sel():
line = self.view.line(region)
line_content = self.view.substr(line)
bullets = self.view.settings().get("mde.list_indent_bullets", ["*", "-", "+"])
bullet_pattern = '([' + ''.join(re.escape(i) for i in bullets) + '])'
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):
if bullet in new_line:
if reverse and new_line.startswith(bullet) and key is 0:
# In this case, do not switch bullets
continue
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)