Sublime Forum

Toggle Quotes (for v2)

#1

Hi all,

I’ve recently discovered Sublime and as a long-time TextMate user I must say I’m really enjoying it!

I’ve been working on making my transition nice and smooth and have written a small plugin to switch the type of quotes (single vs. double).

It should work for most needs, it only relies on the scope starting string.quoted (double or single are assumed). I think I’ve quashed any bugs, the only thing I would like to change is where the cursor moves to, assume you are here in the string ‘testestest|’ and run the command, the cursor will end up here “testestest”|. I’m not even sure if I can move the cursor, or if it’s a major issue, but there you go!

You can grab the code here:

http://dom111.co.uk/files/sublime/plugins/toggle_quotes.py

Apologies if this is of no use, or already implemented somewhere and I just haven’t found it, in either case, please let me know!

Dom

EDIT: Updated the script as per advice from adzenith.

0 Likes

#2

You can move the cursor by modifying the RegionSet returned by View.sel().

0 Likes

#3

Thanks! I’ve just updated it to fix that problem, I expected it to be more complex than that…

Really enjoying the syntax!

0 Likes

#4

Thanks for this, very handy. For some reason I use this type of command a lot, and yours is so much nicer than the version I wrote for TextMate. (Basically a regex search/replace macro that I hadn’t bothered converting to Python.)

0 Likes

#5

Dom,

Thanks for this code. I implemented this feature in a previous editor but your code works much nicer than what I did before. I use this quite a bit but I found a few issues with the code that bothered me a little.

  1. The cursor moves to the left if code is run and it isn’t in a string. The code should have no effect.
  2. When starting on the opening quote, the cursor ends up moving to the right. The cursor should stay on the opening quote.
  3. The code doesn’t support multiple selects.

I made a few simple changes to resolve these issues. Below is a patch to add the fixes to your code.

--- toggle_quotes_original.py	Wed Jul 13 10:03:17 2011
+++ toggle_quotes.py	Mon Jul 11 09:38:24 2011
@@ -2,6 +2,7 @@
 
 class ToggleQuotesCommand(sublime_plugin.TextCommand):
   def run(self, edit):
+    sels = list(self.view.sel())
     for region in self.view.sel():
       scope = self.view.scope_name(region.begin())
 
@@ -63,15 +64,10 @@
               self.view.replace(edit, sublime.Region(start - 1, start), '')
               end -= 1
 
-    # fix for cursor moving outside string, thanks to adzenith
+    # fix for cursor moving outside string
     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.clear()
+    for region in sels:
       rs.add(region)
 
   def is_escaped(self, point):

Dan

0 Likes

#6

Hi,

Just switched from TextMate this morning and I miss this feature. I can’t make it work on dev build 2101.

I copied it in Packages/User, made a shortcut like so :

{ "keys": "ctrl+\""], "command": "ToggleQuotesCommand"} ]
Nothing appears in console when I trigger the shortcut.

Did i miss something ?

Thanks.

0 Likes

#7

You need to change your command from “ToggleQuotesCommand” to “toggle_quotes”. Sublime Text does a bit of name-mangling when calling plugins from key commands.

0 Likes

#8

Arg I was close I tried “toggleQuotes” :smile:

Thanks a lot it work just fine :smile:

0 Likes