Home Download Buy Blog Forum Support

Toggle single/multi line: from E-text to Sublime

Re: Toggle single/multi line: from E-text to Sublime

Postby jbjornson on Thu Jul 14, 2011 7:48 am

...just found some good advice from sublimator on how to debug plugins:
sublimator wrote:Open a new window (File -> New Window ( ctrl+shift+n))
Resize it to take up some space in the top right hand corner of your screen
Use your window manager to make it `always on top`
Open the python console ( ctrl+`)
>>> sublime.log_commands(True)

It should now output the command name and any paramaters whenever you run a command via mouse || keybinding
jbjornson
 
Posts: 348
Joined: Tue Oct 27, 2009 4:25 pm
Location: Zurich, Switzerland

Re: Toggle single/multi line: from E-text to Sublime

Postby tux. on Thu Jul 14, 2011 1:16 pm

I see ...

Traceback (most recent call last):
File ".\sublime_plugin.py", line 255, in run_
File ".\helper_functions.py", line 10, in run
NameError: global name 're' is not defined
Image
tux.
 
Posts: 112
Joined: Thu Apr 14, 2011 11:04 am

Re: Toggle single/multi line: from E-text to Sublime

Postby jbjornson on Thu Jul 14, 2011 1:28 pm

tux. wrote:NameError: global name 're' is not defined


...oops, looks like there are some imports missing at the top. Add this line to the top of your plugin file:
Code: Select all
import sublime, sublime_plugin, re
jbjornson
 
Posts: 348
Joined: Tue Oct 27, 2009 4:25 pm
Location: Zurich, Switzerland

Re: Toggle single/multi line: from E-text to Sublime

Postby tux. on Thu Jul 14, 2011 7:19 pm

Great. The error is gone.
Now the C-S-J command still doesn't do anything...
Image
tux.
 
Posts: 112
Joined: Thu Apr 14, 2011 11:04 am

Re: Toggle single/multi line: from E-text to Sublime

Postby jbjornson on Fri Jul 15, 2011 7:14 am

Ok. Try using this version with some logging included. It should print out some messages in the console that will hopefully help to understand what is going wrong. If you don't see anything being printed into the console then I guess the shortcut isn't set up properly.

Code: Select all
import sublime, sublime_plugin, re

DEBUG_ENABLED = True
PRINT_CONTEXT = True

# toggle a single-line or multi-line formatted css statement
# { "keys": ["ctrl+shift+j"], "command": "toggle_single_line_css" }
class ToggleSingleLineCssCommand(sublime_plugin.TextCommand):
    def run(self,edit):
        debug('Invoked "toggle_single_line_css" with %d region(s)' % (len(self.view.sel())))
        for region in reversed(self.view.sel()):
            text = self.view.substr(region)

            # check if the css statement needs to be expanded or collapsed
            if re.match('^.*\{.*}\s*$', text):
                 # expand the css statement
                 debug('The css statement needs to be expanded', text)
                 m = re.search('^(?P<key>.*)\{(?P<params>.*)\;\s*}$', text)
                 multiline = '%s{\n\t%s;\n}' % (m.group('key'), m.group('params').strip().replace('; ', ';\n\t'))
                 self.view.replace(edit, region, multiline)
            else:
                 debug('The css statement needs to be collapsed', text)
                 # collapse the css statement
                 singleline = ' '.join([x.strip() for x in text.split('\n')])
                 self.view.replace(edit, region, singleline)


def debug(text, context=""):
    if DEBUG_ENABLED:
        print '[toggle_single_line_css]: ' + text
    if PRINT_CONTEXT and context != "":
        print '>>> ' + context
jbjornson
 
Posts: 348
Joined: Tue Oct 27, 2009 4:25 pm
Location: Zurich, Switzerland

Re: Toggle single/multi line: from E-text to Sublime

Postby tux. on Sat Jul 16, 2011 1:29 am

Well:

[toggle_single_line_css]: Invoked "toggle_single_line_css" with 1 region(s)
[toggle_single_line_css]: The css statement needs to be collapsed


But it doesn't collapse.
Image
tux.
 
Posts: 112
Joined: Thu Apr 14, 2011 11:04 am

Re: Toggle single/multi line: from E-text to Sublime

Postby jbjornson on Mon Jul 18, 2011 7:11 am

Is it possible for you to send me your css code?

Are you sure you have the whole css statement selected?
jbjornson
 
Posts: 348
Joined: Tue Oct 27, 2009 4:25 pm
Location: Zurich, Switzerland

Re: Toggle single/multi line: from E-text to Sublime

Postby tux. on Tue Jul 19, 2011 3:08 pm

I actually was!

Anyway, reloading already helped. Whew. Sorry for confusing you - works!
Image
tux.
 
Posts: 112
Joined: Thu Apr 14, 2011 11:04 am

Previous

Return to Plugin Development

Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests