Skip to content

Commit 4d64de5

Browse files
authored
Merge pull request #34 from lordfuoco/master
Added conversion between Camel Case and Dash
2 parents 2926133 + 9535eba commit 4d64de5

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

Context.sublime-menu

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
{ "command": "convert_html_to_chars", "caption" : "Convert HTML Entities to Chars" },
1414
{ "caption": "-" },
1515
{ "command": "convert_camel_underscores", "caption" : "Convert camelCase <-> Underscores" },
16+
{ "command": "convert_camel_dash", "caption" : "Convert camelCase <-> Dash" },
1617
{ "command": "convert_pascal_underscores", "caption" : "Convert PascalCase <-> Underscores" },
1718
{ "caption": "-" },
1819
{ "command": "convert_single_quotes_to_double", "caption" : "Convert Single Quotes To Double" },

Default.sublime-commands

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
"caption": "String Utilities: Convert camelCase <-> Underscores",
2424
"command": "convert_camel_underscores"
2525
},
26+
{
27+
"caption": "String Utilities: Convert camelCase <-> Dash",
28+
"command": "convert_camel_dash"
29+
},
2630
{
2731
"caption": "String Utilities: Convert PascalCase <-> Underscores",
2832
"command": "convert_pascal_underscores"

stringutilities.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,22 @@ def toUnderscores(self, name):
9999
def toCamelCase(self, name):
100100
return ''.join(ch.capitalize() if i > 0 else ch for i, ch in enumerate(name.split('_')))
101101

102+
class ConvertCamelDashCommand(sublime_plugin.TextCommand):
103+
#Convert camelCase to dash and vice versa
104+
def run(self, edit):
105+
for region in self.view.sel():
106+
if not region.empty():
107+
text = self.view.substr(region)
108+
text = self.toCamelCase(text) if '-' in text and text[0].islower() else (text[0].islower() and self.toDash(text))
109+
self.view.replace(edit, region, text)
110+
111+
def toDash(self, name):
112+
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1-\2', name)
113+
return re.sub('([a-z0-9])([A-Z])', r'\1-\2', s1).lower()
114+
115+
def toCamelCase(self, name):
116+
return ''.join(ch.capitalize() if i > 0 else ch for i, ch in enumerate(name.split('-')))
117+
102118

103119
class ConvertPascalUnderscoresCommand(sublime_plugin.TextCommand):
104120
#Convert PascalCase to under_scores and vice versa

0 commit comments

Comments
 (0)