-
Notifications
You must be signed in to change notification settings - Fork 13
/
ExpandSelectionToQuotes.py
41 lines (30 loc) · 1.56 KB
/
ExpandSelectionToQuotes.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
import sublime, sublime_plugin
class ExpandSelectionToQuotesCommand(sublime_plugin.TextCommand):
def run(self, edit):
double_quotes = list(map(lambda x: x.begin(), self.view.find_all('"')))
single_quotes = list(map(lambda x: x.begin(), self.view.find_all("'")))
backtick_quotes = list(map(lambda x: x.begin(), self.view.find_all("`")))
def search_for_quotes(q_type, quotes):
q_size, before, after = False, False, False
if len(quotes) - self.view.substr(sel).count('"') >= 2:
all_before = list(filter(lambda x: x < sel.begin(), quotes))
all_after = list(filter(lambda x: x >= sel.end(), quotes))
if all_before: before = all_before[-1]
if all_after: after = all_after[0]
if all_before and all_after: q_size = after - before
return q_size, before, after
def replace_region(start, end):
if sel.size() < end-start-2:
start += 1; end -= 1
self.view.sel().subtract(sel)
self.view.sel().add(sublime.Region(start, end))
for sel in self.view.sel():
d_size, d_before, d_after = search_for_quotes('"', double_quotes)
s_size, s_before, s_after = search_for_quotes("'", single_quotes)
b_size, b_before, b_after = search_for_quotes("`", backtick_quotes)
if d_size and (not s_size or d_size < s_size) and (not b_size or d_size < b_size):
replace_region(d_before, d_after+1)
elif s_size and (not d_size or s_size < d_size) and (not b_size or s_size < b_size):
replace_region(s_before, s_after+1)
elif b_size and (not d_size or b_size < d_size) and (not s_size or b_size < s_size):
replace_region(b_before, b_after+1)