-
Notifications
You must be signed in to change notification settings - Fork 21
/
commands.py
71 lines (51 loc) · 2.27 KB
/
commands.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
from sublime_plugin import TextCommand
import re
class ReplaceFollowingCharacterCommand(TextCommand):
""" Replace the following character after the cursor with the replacement. """
def run(self, edit, replacement=' '):
self.view.run_command('right_delete')
self.view.run_command('insert', {'characters': replacement})
class AsciidocIndentListItemCommand(TextCommand):
""" (Un)indent an item or selected items of ordered and unordered list. """
def run(self, edit, reverse=False):
view = self.view
indent_str = self._indent_str()
# \1: single indentation (optional)
# \2: remaining indentation followed by markers (except the last one)
# \3: the last marker
pattern = re.compile(r'^((?:%s)?)(\s*[*.-]*)([*.-])' % indent_str)
def indent_line(line_region):
if line_region.empty(): return ''
replacement = r'\2' if reverse else indent_str + r'\1\2\3\3'
return re.sub(pattern, replacement, view.substr(line_region))
changes = [
(region, indent_line(region))
for regions in view.sel()
for region in view.split_by_newlines(view.line(regions))]
for item in reversed(changes):
view.replace(edit, *item)
def _indent_str(self):
""" Get indentation string. """
setting = self.view.settings().get
if not setting('indent_lists', True):
return ''
elif setting('translate_tabs_to_spaces'):
return setting('tab_size', 2) * ' '
else:
return '\t'
class AsciidocExtendCalloutsListCommand(TextCommand):
def run(self, edit):
view = self.view
for selection in view.sel():
line = view.substr(view.line(selection))
indent, num = re.findall(r'^(\s*)<(\d+)>', line)[0]
new_line = "\n%s<%d> " % (indent, int(num) + 1)
view.insert(edit, selection.begin(), new_line)
class AsciidocRunCommandsCommand(TextCommand):
""" Run multiple commands in chain. """
def run(self, edit, commands):
for command in commands:
if isinstance(command, str):
self.view.run_command(command)
else:
self.view.run_command(command[0], *command[1:])