-
Notifications
You must be signed in to change notification settings - Fork 53
/
simpleformat.py
42 lines (31 loc) · 1.26 KB
/
simpleformat.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
import sublime_plugin
class SurroundCommand(sublime_plugin.TextCommand):
"""
Base class to surround the selection with text.
"""
surround = ''
def run(self, edit):
for sel in self.view.sel():
len_surround = len(self.surround)
sel_str = self.view.substr(sel)
rsel = sublime.Region(sel.begin() - len_surround, sel.end() + len_surround)
rsel_str = self.view.substr(rsel)
if sel_str[:len_surround] == self.surround and sel_str[-len_surround:] == self.surround:
replacement = sel_str[len_surround:-len_surround]
else:
replacement = "%s%s%s" % (self.surround, sel_str, self.surround)
if rsel_str == replacement:
self.view.sel().subtract(sel)
self.view.replace(edit, rsel, sel_str)
self.view.sel().add(sublime.Region(rsel.begin(), rsel.begin() + len(sel_str)))
else:
self.view.replace(edit, sel, replacement)
class StrongemphasisCommand(SurroundCommand):
surround = "**"
class EmphasisCommand(SurroundCommand):
surround = "*"
class LiteralCommand(SurroundCommand):
surround = "``"
class SubstitutionCommand(SurroundCommand):
surround = "|"