Permalink
Browse files

merged toggle case command from @tbasse #7

  • Loading branch information...
2 parents daec45c + 8b4ac56 commit d4340032f0958a30828b3f824cfcb9bd6865b706 @jdc0589 committed Feb 16, 2014
Showing with 60 additions and 24 deletions.
  1. +3 −2 Default (OSX).sublime-keymap
  2. +3 −2 Default (Windows).sublime-keymap
  3. +8 −3 Default.sublime-commands
  4. +12 −11 Main.sublime-menu
  5. +27 −0 case_conversion.py
  6. +7 −6 readme.md
@@ -5,5 +5,6 @@
{ "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+/"], "command": "convert_to_slash"}
-]
+ { "keys": ["ctrl+alt+c", "ctrl+alt+/"], "command": "convert_to_slash"},
+ { "keys": ["ctrl+shift+-"], "command": "toggle_snake_camel_pascal"}
+]
@@ -5,5 +5,6 @@
{ "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+/"], "command": "convert_to_slash"}
-]
+ { "keys": ["ctrl+alt+c", "ctrl+alt+/"], "command": "convert_to_slash"},
+ { "keys": ["ctrl+shift+-"], "command": "toggle_snake_camel_pascal"}
+]
View
@@ -24,7 +24,12 @@
"command": "convert_to_separate_words"
},
{
- "caption": "Convert Case: separate/with/slash",
- "command": "convert_to_slash"
+ "caption": "Convert Case: Toggle Case",
+ "command": "toggle_snake_camel_pascal"
+ },
+ {
+ "caption": "Convert Case: Toggle Case",
+ "command": "toggle_snake_camel_pascal"
+
}
-]
+]
View
@@ -1,21 +1,22 @@
-[
-{
- "id": "edit",
- "children":
- [
+[
+{
+ "id": "edit",
+ "children":
+ [
{
- "id": "convert_case",
+ "id": "convert_case",
"children":
[
{ "command": "convert_to_snake", "caption": "snake_case" },
- { "command": "convert_to_camel", "caption": "camelCase" },
+ { "command": "convert_to_camel", "caption": "camelCase" },
{ "command": "convert_to_pascal", "caption": "PascalCase" },
{ "command": "convert_to_dot", "caption": "dot.case" },
{ "command": "convert_to_dash", "caption": "dash-case" },
{ "command": "convert_to_separate_words", "caption": "separate␣words" },
- { "command": "convert_to_slash", "caption": "separate/with/slash" }
+ { "command": "convert_to_slash", "caption": "separate/with/slash" },
+ { "command": "toggle_snake_camel_pascal", "caption": "Toggle Case" }
]
}
- ]
-}
-]
+ ]
+}
+]
View
@@ -49,6 +49,20 @@ def to_separate_words(text):
return text.replace("_", " ")
+def toggle_case(word):
+ pascalcase = re.search('^[A-Z][a-z]+(?:[A-Z][a-z]+)*$', word)
+ snakecase = re.search('^[a-z]+(?:_[a-z]+)*$', word)
+ camelcase = re.search('^[a-z]+(?:[A-Z][a-z]+)*$', word)
+ if (pascalcase):
+ return to_snake_case(word)
+ elif (snakecase):
+ return to_camel_case(word)
+ elif (camelcase):
+ return to_pascal_case(word)
+ else:
+ return word
+
+
def run_on_selections(view, edit, func, no_lower=False):
for s in view.sel():
region = s if s else view.word(s)
@@ -60,6 +74,19 @@ def run_on_selections(view, edit, func, no_lower=False):
view.replace(edit, region, func(text))
+def run_on_selections_togglecase(view, edit, func):
+ for s in view.sel():
+ region = s if s else view.word(s)
+ text = view.substr(region)
+ text = strip_wrapping_underscores(text)
+ view.replace(edit, region, func(text))
+
+
+class ToggleSnakeCamelPascalCommand(sublime_plugin.TextCommand):
+ def run(self, edit):
+ run_on_selections_togglecase(self.view, edit, toggle_case)
+
+
class ConvertToSnakeCommand(sublime_plugin.TextCommand):
def run(self, edit):
run_on_selections(self.view, edit, lambda text: text)
View
@@ -3,13 +3,14 @@ Case conversion is a plugin for Sublime Text 2. It converts the current word/tok
camel, snake, dot, dash (hyphen) cases, and separated words.
## Keybindings
-- To snake_case: "ctrl+alt+c", "ctrl+alt+s"
-- to camelCase: "ctrl+alt+c", "ctrl+alt+c"
-- to PascalCase: "ctrl+alt+c", "ctrl+alt+p"
-- to dot.case: "ctrl+alt+c", "ctrl+alt+d"
-- to dash-case: "ctrl+alt+c", "ctrl+alt+h"
-- to separate words: "ctrl+alt+c", "ctrl+alt+w"
+- To snake_case: "ctrl+alt+c", "ctrl+alt+s"
+- To camelCase: "ctrl+alt+c", "ctrl+alt+c"
+- To PascalCase: "ctrl+alt+c", "ctrl+alt+p"
+- To dot.case: "ctrl+alt+c", "ctrl+alt+d"
+- To dash-case: "ctrl+alt+c", "ctrl+alt+h"
+- To separate words: "ctrl+alt+c", "ctrl+alt+w"
- to separate with slashes: "ctrl+alt+c", "ctrl+alt+/"
+- To toggle between snake_case, camelCase and PascalCase: "ctrl-shift-\_"
## Install
#### Git Clone

0 comments on commit d434003

Please sign in to comment.