-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.py
57 lines (45 loc) · 2.41 KB
/
plugin.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
import sublime
import sublime_plugin
class ConvertToTemplateString(sublime_plugin.TextCommand):
""" This command is guaranteed to executed when { is pressed """
def run(self, edit):
sel = self.view.sel()
if not sel:
return
for r in sel:
point = r.b
if not in_supported_file(self.view, point):
return None
regular_string_region = get_regular_string_region(self.view, point)
if not regular_string_region:
continue
scan_region = self.view.substr(regular_string_region)
# the user could typed $ or {
if "${" not in scan_region:
continue
first_quote = regular_string_region.begin()
last_quote = regular_string_region.end() - 1
are_quotes = is_regular_quote(self.view.substr(first_quote)) and is_regular_quote(self.view.substr(last_quote))
if not are_quotes:
continue # sanity check, just to 100% make sure we are replacing quotes
if is_jsx_attribute(self.view, point) and not is_jsx_attribute_wrapped_with_curly_brackets(self.view, point):
# insert surrounding curly brackets
self.view.replace(
edit, sublime.Region(last_quote, last_quote + 1), '`}')
self.view.replace(
edit, sublime.Region(first_quote, first_quote + 1), '{`')
continue
self.view.replace(
edit, sublime.Region(last_quote, last_quote + 1), '`')
self.view.replace(
edit, sublime.Region(first_quote, first_quote + 1), '`')
def is_jsx_attribute(view: sublime.View, point: int) -> bool:
return view.match_selector(point, "meta.jsx meta.tag.attributes")
def is_jsx_attribute_wrapped_with_curly_brackets(view: sublime.View, point: int) -> bool:
return view.match_selector(point, "meta.jsx meta.tag.attributes meta.interpolation meta.string string.quoted")
def is_regular_quote(char: str) -> bool:
return char in ["'", '"']
def in_supported_file(view: sublime.View, point: int) -> bool:
return view.match_selector(point, "source.js | source.jsx | source.ts | source.tsx | text.html.ngx | text.html.svelte | text.html.vue")
def get_regular_string_region(view: sublime.View, point: int):
return view.expand_to_scope(point, "string.quoted.single | string.quoted.double")