-
Notifications
You must be signed in to change notification settings - Fork 47
/
case_conversion.py
143 lines (97 loc) · 4.32 KB
/
case_conversion.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import sublime
import sublime_plugin
import re
import sys
PYTHON = sys.version_info[0]
if 3 == PYTHON:
# Python 3 and ST3
from . import case_parse
else:
# Python 2 and ST2
import case_parse
SETTINGS_FILE = "CaseConversion.sublime-settings"
def to_snake_case(text, detectAcronyms, acronyms):
words, case, sep = case_parse.parseVariable(text, detectAcronyms, acronyms)
return '_'.join([w.lower() for w in words])
def to_screaming_snake_case(text, detectAcronyms, acronyms):
words, case, sep = case_parse.parseVariable(text, detectAcronyms, acronyms)
return '_'.join([w.upper() for w in words])
def to_pascal_case(text, detectAcronyms, acronyms):
words, case, sep = case_parse.parseVariable(text, detectAcronyms, acronyms)
return ''.join(words)
def to_camel_case(text, detectAcronyms, acronyms):
words, case, sep = case_parse.parseVariable(text, detectAcronyms, acronyms)
words[0] = words[0].lower()
return ''.join(words)
def to_dot_case(text, detectAcronyms, acronyms):
words, case, sep = case_parse.parseVariable(text, detectAcronyms, acronyms)
return '.'.join([w.lower() for w in words])
def to_dash_case(text, detectAcronyms, acronyms):
words, case, sep = case_parse.parseVariable(text, detectAcronyms, acronyms)
return '-'.join([w.lower() for w in words])
def to_slash(text, detectAcronyms, acronyms):
words, case, sep = case_parse.parseVariable(text, detectAcronyms, acronyms, True)
return '/'.join(words)
def to_backslash(text, detectAcronyms, acronyms):
words, case, sep = case_parse.parseVariable(text, detectAcronyms, acronyms, True)
return '\\'.join(words)
def to_separate_words(text, detectAcronyms, acronyms):
words, case, sep = case_parse.parseVariable(text, detectAcronyms, acronyms, True)
return ' '.join(words)
def toggle_case(text, detectAcronyms, acronyms):
words, case, sep = case_parse.parseVariable(text, detectAcronyms, acronyms)
if case == 'pascal' and not sep:
return to_snake_case(text, detectAcronyms, acronyms)
elif case == 'lower' and sep == '_':
return to_camel_case(text, detectAcronyms, acronyms)
elif case == 'camel' and not sep:
return to_pascal_case(text, detectAcronyms, acronyms)
else:
return text
def run_on_selections(view, edit, func):
settings = sublime.load_settings(SETTINGS_FILE)
detectAcronyms = settings.get("detect_acronyms", True)
useList = settings.get("use_acronyms_list", True)
if useList:
acronyms = settings.get("acronyms", [])
else:
acronyms = False
for s in view.sel():
region = s if s else view.word(s)
text = view.substr(region)
# Preserve leading and trailing whitespace
leading = text[:len(text)-len(text.lstrip())]
trailing = text[len(text.rstrip()):]
new_text = leading + func(text.strip(), detectAcronyms, acronyms) + trailing
if new_text != text:
view.replace(edit, region, new_text)
class ToggleSnakeCamelPascalCommand(sublime_plugin.TextCommand):
def run(self, edit):
run_on_selections(self.view, edit, toggle_case)
class ConvertToSnakeCommand(sublime_plugin.TextCommand):
def run(self, edit):
run_on_selections(self.view, edit, to_snake_case)
class ConvertToScreamingSnakeCommand(sublime_plugin.TextCommand):
def run(self, edit):
run_on_selections(self.view, edit, to_screaming_snake_case)
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)
class ConvertToSlash(sublime_plugin.TextCommand):
def run(self, edit):
run_on_selections(self.view, edit, to_slash )
class ConvertToBackSlash(sublime_plugin.TextCommand):
def run(self, edit):
run_on_selections(self.view, edit, to_backslash )