-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrap.py
More file actions
38 lines (30 loc) · 1.4 KB
/
wrap.py
File metadata and controls
38 lines (30 loc) · 1.4 KB
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
import sublime
import sublime_plugin
class WrapCommand(sublime_plugin.TextCommand):
def run(self, edit):
settings = sublime.load_settings("Wrap.sublime-settings")
file_name = self.view.file_name()
if file_name:
extension = self.view.file_name().split(".")[-1]
else: # In blank view without file open
extension = ""
# Determine bracket type
for c in settings.get("contexts"):
if self.view.match_selector(self.view.sel()[0].begin(), c.get("scope")) or extension in c.get("extensions"):
a, b = c.get("bracket_type", "()")
break
else:
a, b = settings.get("bracket_type", "()")
region = self.view.sel()[0]
# Cursor on empty line
if self.view.substr(self.view.word(region)).strip() == "":
self.view.run_command("insert_snippet", {"contents": "${1:abc}" + a + "$0" + b})
return
# No selection
if region.empty():
# Do nothing if selection has no alphanumeric character
if not any(char.isalnum() for char in self.view.substr(self.view.word(region))):
return
self.view.run_command("find_under_expand") # Select text under cursor
# Wrap selection with parentheses
self.view.run_command("insert_snippet", {"contents": "${1:abc}" + a + "${0:$SELECTION}" + b})