Home Download Buy Blog Forum Support

Toggle Quotes (for v2)

Toggle Quotes (for v2)

Postby dom111 on Thu Jun 09, 2011 2:59 pm

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.
Last edited by dom111 on Fri Jun 10, 2011 8:55 am, edited 1 time in total.
dom111
 
Posts: 12
Joined: Wed Jun 08, 2011 9:13 am
Location: UK

Re: Toggle Quotes (for v2)

Postby adzenith on Thu Jun 09, 2011 9:36 pm

You can move the cursor by modifying the RegionSet returned by View.sel().
adzenith
 
Posts: 1217
Joined: Mon Oct 19, 2009 9:12 pm

Re: Toggle Quotes (for v2)

Postby dom111 on Fri Jun 10, 2011 8:55 am

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

Really enjoying the syntax!
dom111
 
Posts: 12
Joined: Wed Jun 08, 2011 9:13 am
Location: UK

Re: Toggle Quotes (for v2)

Postby natebeaty on Tue Jun 14, 2011 10:42 pm

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.)
natebeaty
 
Posts: 121
Joined: Wed Apr 06, 2011 8:28 pm
Location: Chicago, IL

Re: Toggle Quotes (for v2)

Postby ldhughes on Wed Jul 13, 2011 2:15 pm

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.

Code: Select all
--- 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
ldhughes
 
Posts: 6
Joined: Thu May 19, 2011 3:00 pm

Re: Toggle Quotes (for v2)

Postby Cal on Sun Aug 14, 2011 4:39 pm

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 :
Code: Select all
[
   { "keys": ["ctrl+\""], "command": "ToggleQuotesCommand"}
]

Nothing appears in console when I trigger the shortcut.

Did i miss something ?

Thanks.
Last edited by Cal on Mon Aug 15, 2011 9:07 am, edited 1 time in total.
Cal
 
Posts: 6
Joined: Sun Aug 14, 2011 4:29 pm

Re: Toggle Quotes (for v2)

Postby adzenith on Mon Aug 15, 2011 1:29 am

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.
adzenith
 
Posts: 1217
Joined: Mon Oct 19, 2009 9:12 pm

Re: Toggle Quotes (for v2)

Postby Cal on Mon Aug 15, 2011 9:05 am

Arg I was close I tried "toggleQuotes" :)

Thanks a lot it work just fine :)
Cal
 
Posts: 6
Joined: Sun Aug 14, 2011 4:29 pm


Return to Plugin Announcements

Who is online

Users browsing this forum: No registered users and 9 guests

cron