Permalink
Browse files

Toggle Quote Function

  • Loading branch information...
1 parent a2d6a0f commit 138b1fc99a5dfa25d761c9a3e9857e0c73cbec1a @zlogan zlogan committed Jul 6, 2011
Showing with 89 additions and 2 deletions.
  1. +2 −1 Base File.sublime-settings
  2. +2 −1 Default (OSX).sublime-keymap
  3. +85 −0 toggle_quotes.py
@@ -4,5 +4,6 @@
"translate_tabs_to_spaces": true,
"use_tab_stops": true,
"trim_trailing_white_space_on_save": true,
- "font_face": "Bitstream Vera Sans Mono"
+ "font_face": "Bitstream Vera Sans Mono",
+ "save_on_focus_lost": true
}
@@ -1,3 +1,4 @@
[
- { "keys": ["alt+tab"], "command": "focus_next_group" }
+ { "keys": ["alt+tab"], "command": "focus_next_group" },
+ { "keys": ["ctrl+shift+'"], "command": "toggle_quotes" }
]
View
@@ -0,0 +1,85 @@
+import sublime, sublime_plugin
+
+class ToggleQuotesCommand(sublime_plugin.TextCommand):
+ def run(self, edit):
+ for region in self.view.sel():
+ scope = self.view.scope_name(region.begin())
+
+ if scope.find('string.quoted.') > -1:
+ start = end = region.begin()
+
+ if scope.find('string.quoted.double') > -1:
+ l = '"'
+
+ else:
+ l = '\''
+
+ if self.view.substr(start) == l:
+ scope = self.view.scope_name(start - 1)
+
+ if scope.find('string.quoted.') > -1:
+ start = end = start - 1
+
+ while start >= 0:
+ c = self.view.substr(start)
+
+ if c == l and not self.is_escaped(start):
+ break
+
+ else:
+ start -= 1
+
+ end = start + 1
+
+ while (end - start) <= self.view.size():
+ c = self.view.substr(end)
+
+ if c == l and not self.is_escaped(end):
+ break
+
+ else:
+ end += 1
+
+ if start >= 0 and end <= self.view.size():
+ if l == '\'':
+ r = '"'
+
+ else:
+ r = '\''
+
+ self.view.replace(edit, sublime.Region(start, start + 1), r)
+ self.view.replace(edit, sublime.Region(end, end + 1), r)
+
+ while start < (end - 1):
+ start += 1
+
+ c = self.view.substr(start)
+
+ if c == r and not self.is_escaped(start):
+ self.view.insert(edit, start, '\\')
+ end += 1
+
+ elif c == l and self.is_escaped(start):
+ self.view.replace(edit, sublime.Region(start - 1, start), '')
+ end -= 1
+
+ # fix for cursor moving outside string, thanks to adzenith
+ rs = self.view.sel()
+ scope = self.view.scope_name(rs[0].begin())
+
+ if scope.find('string.quoted') == -1:
+ p = rs[0].begin() - 1
+ region = sublime.Region(p, p)
+
+ rs.clear()
+ rs.add(region)
+
+ def is_escaped(self, point):
+ point -= 1
+ i = 0
+
+ while self.view.substr(point) == '\\':
+ i += 1
+ point -= 1
+
+ return i % 2 == 1

0 comments on commit 138b1fc

Please sign in to comment.