Home Download Buy Blog Forum Support

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

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

Postby iamntz on Wed Jun 08, 2011 4:51 pm

Hi guys. I'm migrating from E and I'm missing some features. One of that is toggling single/multi line for css properties.

Let's say this code:
Code: Select all
.test {
   color:#000;
   text-decoration:none;
   background:red;
}


After I press a key combination will become
Code: Select all
.test {color:#000; text-decoration:none; background:red; }


I know about ctrl+j command, but that will work only one way. Ok, so the code from E is this:
Code: Select all
#!/usr/bin/env ruby

if ENV['TM_SOFT_TABS'] == 'NO'
  tab = "\t"
else
  tab = " " * ENV['TM_TAB_SIZE'].to_i
end

print case str = STDIN.read
 
  # Expand
  when /\A\{(.*)\}\z/
    "{\n#{tab}" + $1.strip.gsub("; ", ";\n#{tab}") + "\n}"
 
    # Collapse
  when /\A\{(.*)\}\z/m
    ("{" + $1.gsub("\n", " ") + "}").gsub(/\s{2,}/, " ")
 
  # Default case
  else str
   
end


My ruby and py skills are zero, so I don't even know where to start. However, i did this:
Code: Select all
import sublime, sublime_plugin, re

class ToggleSingleLineCommand(sublime_plugin.TextCommand):
   def run(self, edit):

Which is... all I know how to do it :D

Can i get some hints/help/solution?

Thanks!
iamntz
 
Posts: 819
Joined: Fri Apr 29, 2011 8:52 am
Location: Romania

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

Postby jbjornson on Thu Jun 09, 2011 12:36 pm

For me, ctrl+j did exactly what you asked for (for the example you gave). What do you mean by "but that will work only one way"?
jbjornson
 
Posts: 335
Joined: Tue Oct 27, 2009 4:25 pm
Location: Zurich, Switzerland

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

Postby iamntz on Thu Jun 09, 2011 12:40 pm

I mean i would like to split single line rules into multiple lines rules :D

From :
Code: Select all
    .test {color:#000; text-decoration:none; background:red; }


to:
Code: Select all
    .test {
       color:#000;
       text-decoration:none;
       background:red;
    }
iamntz
 
Posts: 819
Joined: Fri Apr 29, 2011 8:52 am
Location: Romania

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

Postby jbjornson on Fri Jun 10, 2011 4:16 pm

Hi iamntz,

This seems to work for me (although it is definitely not well tested). It works with multiple selections and each selection should be a single css statement. It only works with css and might not work in all cases.

Happy styling :-)

Cheers,
Josh

Code: Select all
import sublime, sublime_plugin, re

# toggle a single-line or multi-line formatted css statement
class ToggleSingleLineCssCommand(sublime_plugin.TextCommand):
   def run(self,edit):
      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
                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:
                # collapse the css statement
                singleline = ' '.join([x.strip() for x in text.split('\n')])
                self.view.replace(edit, region, singleline)


...and make a shortcut with something like:

Code: Select all
{ "keys": ["ctrl+shift+j"], "command": "toggle_single_line_css" }
Last edited by jbjornson on Thu Jul 14, 2011 1:28 pm, edited 1 time in total.
jbjornson
 
Posts: 335
Joined: Tue Oct 27, 2009 4:25 pm
Location: Zurich, Switzerland

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

Postby iamntz on Sat Jun 11, 2011 8:16 am

Awesome! Thanks a lot!

I don't really care to work with other languages :D

(it took a little to me to got it that is working only if the text is selected, but works GREAT)
iamntz
 
Posts: 819
Joined: Fri Apr 29, 2011 8:52 am
Location: Romania

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

Postby tux. on Tue Jul 12, 2011 9:18 pm

I'm lost here. :)
Where to put that function? - Will I have to create a new module or is there a way to put common functions in a single script file somewhere?
Image
tux.
 
Posts: 96
Joined: Thu Apr 14, 2011 11:04 am

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

Postby natebeaty on Wed Jul 13, 2011 7:26 am

Thanks for that, Josh! It's a great start if nothing else. I've put off setting something like this up for ages, could have saved me thousands of manual keystrokes.

tux. wrote:I'm lost here. :)
Where to put that function? - Will I have to create a new module or is there a way to put common functions in a single script file somewhere?


See: http://sublimetext.info/docs/en/extensi ... rst-plugin
natebeaty
 
Posts: 119
Joined: Wed Apr 06, 2011 8:28 pm
Location: Chicago, IL

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

Postby tux. on Wed Jul 13, 2011 4:12 pm

Ah, well; thanks!
Image
tux.
 
Posts: 96
Joined: Thu Apr 14, 2011 11:04 am

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

Postby tux. on Wed Jul 13, 2011 4:17 pm

Hmm, I put the keymap into a new .sublime-keymap file there, created a new helper_functions.py, put the function in, restarted Sublime; and it doesn't do anything?
Image
tux.
 
Posts: 96
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 7:34 am

tux. wrote:Hmm, I put the keymap into a new .sublime-keymap file there, created a new helper_functions.py, put the function in, restarted Sublime; and it doesn't do anything?


How are you calling it? Maybe try selecting a block of css code that you want to expand/collapse, open the console (view \ show console) and type:
Code: Select all
view.run_command("toggle_single_line_css")


Anything?

If that doesn't work, try adding some print statements at the beginning of the run method so you get some visual feedback (in the console) that the command is actually being called.

One important note is that the plugin requires you to select the css you want formatted, and the selection should be a single css statement. You can have multiple selections, but each selection must be an individual css statement. I'm sure the code could be improved to work on multiple css statements in a single selection but I don't actually work with css and couldn't be bothered to spend more time on the plugin (it was simply a fun challenge for me to solve the original poster's problem).

I hope you are able to get it working.
jbjornson
 
Posts: 335
Joined: Tue Oct 27, 2009 4:25 pm
Location: Zurich, Switzerland

Next

Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 9 guests

cron