forked from jdavisclark/CaseConversion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcase_conversion.py
More file actions
77 lines (49 loc) · 1.88 KB
/
Copy pathcase_conversion.py
File metadata and controls
77 lines (49 loc) · 1.88 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
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
67
68
69
70
71
72
73
74
75
76
77
import sublime_plugin
import re
def to_snake_case(text):
text = re.sub('[-. _]+', '_', text)
if text.isupper():
# Entirely uppercase; assume case is insignificant.
return text.lower()
return re.sub('(?<=[^_])([A-Z])', r'_\1', text).lower()
def strip_wrapping_underscores(text):
return re.sub("^(_*)(.*?)(_*)$", r'\2', text)
def to_pascal_case(text):
callback = lambda pat: pat.group(1).upper()
text = re.sub("_(\w)", callback, text)
if text[0].islower():
text = text[0].upper() + text[1:]
return text
def to_camel_case(text):
text = to_pascal_case(text)
return text[0].lower() + text[1:]
def to_dot_case(text):
return text.replace("_", ".")
def to_dash_case(text):
return text.replace("_", "-")
def to_separate_words(text):
return text.replace("_", " ")
def run_on_selections(view, edit, func):
for s in view.sel():
region = s if s else view.word(s)
text = to_snake_case(view.substr(region))
text = strip_wrapping_underscores(text)
view.replace(edit, region, func(text))
class ConvertToSnakeCommand(sublime_plugin.TextCommand):
def run(self, edit):
run_on_selections(self.view, edit, lambda text: text)
class ConvertToCamel(sublime_plugin.TextCommand):
def run(self, edit):
run_on_selections(self.view, edit, to_camel_case)
class ConvertToPascal(sublime_plugin.TextCommand):
def run(self, edit):
run_on_selections(self.view, edit, to_pascal_case)
class ConvertToDot(sublime_plugin.TextCommand):
def run(self, edit):
run_on_selections(self.view, edit, to_dot_case)
class ConvertToDash(sublime_plugin.TextCommand):
def run(self, edit):
run_on_selections(self.view, edit, to_dash_case)
class ConvertToSeparateWords(sublime_plugin.TextCommand):
def run(self, edit):
run_on_selections(self.view, edit, to_separate_words)