-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtoggle_symbol.py
66 lines (50 loc) · 1.54 KB
/
toggle_symbol.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
import string
from sublime import Region
re_quotes = re.compile("^(['\"])(.*)\\1$")
re_symbol = re.compile("^(\:)(.*)$")
class ToggleSymbolCommand(sublime_plugin.TextCommand):
def matcher(self,text):
res = re_quotes.match(text)
if not res:
res = re_symbol.match(text)
return res
def replacer(self, v, edit, sel, text, res):
old_quotes = res.group(1)
text = res.group(2)
if old_quotes == ':':
text = "'" + text + "'"
elif old_quotes == "'":
text = '"' + text + '"'
else :
text = ':' + text
v.replace(edit, sel, text)
def run(self, edit):
v = self.view
if v.sel()[0].size() == 0:
v.run_command("expand_selection", {"to": "word"})
cur_start = v.sel()[0].begin()
cur_end = v.sel()[0].end()
for sel in v.sel():
text = v.substr(sel)
res = self.matcher(text)
if not res:
#first check one character to the left to see if its a symbol
sel = Region(sel.begin() - 1, sel.end())
text = v.substr(sel)
res = self.matcher(text)
if not res:
#now expand selection one character to the right to see if its a string
sel = Region(sel.begin(), sel.end() + 1)
text = v.substr(sel)
res = self.matcher(text)
if not res:
#this is a mute point
continue
self.replacer(v, edit, sel, text, res)
v.sel().clear()
v.sel().add(Region(cur_start, cur_end))
# 'dude'
# "dude"
# :dude