Permalink
Browse files

added conversion to slash

useful for translating classnames to directory map (p.e. Zend-Framework coding style)
  • Loading branch information...
1 parent 4a3a18b commit b16a01a6cebbe6b01829e508fee71f605f6f6195 @fpuc fpuc committed Feb 27, 2013
Showing with 26 additions and 5 deletions.
  1. +2 −1 Default (Linux).sublime-keymap
  2. +2 −1 Default (OSX).sublime-keymap
  3. +2 −1 Default (Windows).sublime-keymap
  4. +20 −2 case_conversion.py
@@ -4,5 +4,6 @@
{ "keys": ["ctrl+alt+c", "ctrl+alt+p"], "command": "convert_to_pascal"},
{ "keys": ["ctrl+alt+c", "ctrl+alt+d"], "command": "convert_to_dot"},
{ "keys": ["ctrl+alt+c", "ctrl+alt+h"], "command": "convert_to_dash"},
- { "keys": ["ctrl+alt+c", "ctrl+alt+w"], "command": "convert_to_separate_words"}
+ { "keys": ["ctrl+alt+c", "ctrl+alt+w"], "command": "convert_to_separate_words"},
+ { "keys": ["ctrl+alt+c", "ctrl+alt+/"], "command": "convert_to_slash"}
]
@@ -4,5 +4,6 @@
{ "keys": ["ctrl+alt+c", "ctrl+alt+p"], "command": "convert_to_pascal"},
{ "keys": ["ctrl+alt+c", "ctrl+alt+d"], "command": "convert_to_dot"},
{ "keys": ["ctrl+alt+c", "ctrl+alt+h"], "command": "convert_to_dash"},
- { "keys": ["ctrl+alt+c", "ctrl+alt+w"], "command": "convert_to_separate_words"}
+ { "keys": ["ctrl+alt+c", "ctrl+alt+w"], "command": "convert_to_separate_words"},
+ { "keys": ["ctrl+alt+c", "ctrl+alt+/"], "command": "convert_to_slash"}
]
@@ -4,5 +4,6 @@
{ "keys": ["ctrl+alt+c", "ctrl+alt+p"], "command": "convert_to_pascal"},
{ "keys": ["ctrl+alt+c", "ctrl+alt+d"], "command": "convert_to_dot"},
{ "keys": ["ctrl+alt+c", "ctrl+alt+h"], "command": "convert_to_dash"},
- { "keys": ["ctrl+alt+c", "ctrl+alt+w"], "command": "convert_to_separate_words"}
+ { "keys": ["ctrl+alt+c", "ctrl+alt+w"], "command": "convert_to_separate_words"},
+ { "keys": ["ctrl+alt+c", "ctrl+alt+/"], "command": "convert_to_slash"}
]
View
@@ -9,6 +9,12 @@ def to_snake_case(text):
return text.lower()
return re.sub('(?<=[^_])([A-Z])', r'_\1', text).lower()
+def to_snake_case_graceful(text):
+ text = re.sub('[-. _]+', '_', text)
+ if text.isupper():
+ # Entirely uppercase; assume case is insignificant.
+ return text;
+ return re.sub('(?<=[^_])([A-Z])', r'_\1', text)
def strip_wrapping_underscores(text):
return re.sub("^(_*)(.*?)(_*)$", r'\2', text)
@@ -35,14 +41,21 @@ def to_dash_case(text):
return text.replace("_", "-")
+def to_slash(text):
+ return text.replace("_", "/")
+
+
def to_separate_words(text):
return text.replace("_", " ")
-def run_on_selections(view, edit, func):
+def run_on_selections(view, edit, func, no_lower=False):
for s in view.sel():
region = s if s else view.word(s)
- text = to_snake_case(view.substr(region))
+ if no_lower:
+ text = to_snake_case_graceful(view.substr(region))
+ else:
+ text = to_snake_case(view.substr(region))
text = strip_wrapping_underscores(text)
view.replace(edit, region, func(text))
@@ -75,3 +88,8 @@ def run(self, edit):
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, True )

0 comments on commit b16a01a

Please sign in to comment.