forked from ziglang/sublime-zig-language
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ContextCommands.py
62 lines (46 loc) · 1.79 KB
/
ContextCommands.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
import sublime
import sublime_plugin
import os
import shutil
import threading
import re
to_snake_case_pattern = re.compile(r'(?!^)(?<!_)([A-Z])')
def to_snake_case(txt):
return to_snake_case_pattern.sub(r'_\1', txt).lower()
class InsertImportCommand(sublime_plugin.TextCommand):
def run(self, edit, package):
region = self.view.find_by_class(0, True, sublime.CLASS_EMPTY_LINE)
self.view.insert(edit, region + 1, 'import "' + package + '"\n')
class SnakeCaseSelectionCommand(sublime_plugin.TextCommand):
def run(self, edit):
sel = self.view.sel()
regions_by_line = self.view.split_by_newlines(sel[0])
word_match_pattern = re.compile(r'^\s*(\w+)') # extracts the first word
replacements = {}
for region in regions_by_line:
txt = self.view.substr(region)
matches = word_match_pattern.findall(txt)
if len(matches) == 0:
continue
txt = txt.replace(matches[0], to_snake_case(matches[0]))
replacements[matches[0]] = to_snake_case(matches[0])
full_region_txt = self.view.substr(sel[0])
for orig_word in replacements:
full_region_txt = full_region_txt.replace(orig_word, replacements[orig_word])
self.view.replace(edit, self.view.full_line(sel[0]), full_region_txt)
class ToUpperSelectionCommand(sublime_plugin.TextCommand):
def run(self, edit):
sel = self.view.sel()
for selection in sel:
regions_by_line = self.view.split_by_newlines(selection)
for region in regions_by_line:
txt = self.view.substr(region)
self.view.replace(edit, region, txt.upper())
class ToLowerSelectionCommand(sublime_plugin.TextCommand):
def run(self, edit):
sel = self.view.sel()
for selection in sel:
regions_by_line = self.view.split_by_newlines(selection)
for region in regions_by_line:
txt = self.view.substr(region)
self.view.replace(edit, region, txt.lower())