Home Download Buy Blog Forum Support

Key-Value Pair Align

Key-Value Pair Align

Postby marksteve on Tue Nov 02, 2010 2:11 pm

Turn this:
Code: Select all
key: "value",
longKey: 12345,
"key with spaces": CONSTANT

Into this:
Code: Select all
key               : "value",
longKey           : 12345,
"key with spaces" : CONSTANT


It's kind of dumb right now. You have to select the pairs only or it won't work.
It also can't handle selections with multiple instances of your separator. e.g. (key: "value:value=>" // this will melt your computer)
Well I just wrote it down because I noticed I've been doing it a lot in PHP and I'm also trying to learn Python.
You will notice my excitement in writing list comprehensions. :P

It works with multiple regions though :D

Code: Select all
import sublime, sublimeplugin, re

SEPARATOR = ':|=>'

class KeyValueAlignCommand(sublimeplugin.TextCommand):
   def run(self, view, args):
      maxKeyLen = max([max([len(k) for k in dict([[s.rstrip() for s in re.split(SEPARATOR, line, 1)] for line in view.substr(region).split("\n")]).keys()]) for region in view.sel()])
      for region in view.sel():
         lines = view.substr(region).split("\n")
         padded = []
         for line in lines:
            key = re.split(SEPARATOR, line, 1)[0]
            padded.append(line.replace(key, key.rstrip().ljust(maxKeyLen + 1)))
         view.replace(region, "\n".join(padded))
marksteve
 
Posts: 43
Joined: Thu Sep 30, 2010 6:44 am

Re: Key-Value Pair Align

Postby marksteve on Wed Nov 03, 2010 3:35 pm

sublimator wrote:Sublime python API has scoping capabilities


Pardon my ignorance but what is scoping?
marksteve
 
Posts: 43
Joined: Thu Sep 30, 2010 6:44 am

Re: Key-Value Pair Align

Postby darren_n on Tue Jul 19, 2011 1:30 pm

Would this work in ST2?
darren_n
 
Posts: 3
Joined: Tue Jul 19, 2011 1:27 pm

Re: Key-Value Pair Align

Postby jbjornson on Tue Jul 19, 2011 2:53 pm

darren_n wrote:Would this work in ST2?


Below is an updated version that works in ST2. I basically updated the old code to use the new ST2 api and also fixed a bug that occurred if there was an empty line in your selection (failed when calculating the maxKeyLen).

Please note that I didn't write the original code and can't really speak for the completeness of this solution (I just updated it for fun and to get some more practice coding in python). For sure it does not consider your settings for tab_size or translate_tabs_to_spaces; it just blindly aligns the key-values using spaces. For a potentially more complete implementation, you might want to check out this: http://www.sublimetext.com/forum/viewtopic.php?f=5&t=2205&start=0.

Code: Select all
import sublime, sublime_plugin, re

SEPARATOR = ':|=>'

class KeyValueAlignCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        # calculate the max length of a key
        maxKeyLen = 0
        for region in self.view.sel():
            for line in self.view.substr(region).split("\n"):
                key = re.split(SEPARATOR, line, 1)[0]
                keyLength = len(key)
                maxKeyLen = max(maxKeyLen, keyLength)

        # now pad all of the keys to the max length
        for region in self.view.sel():
            lines = self.view.substr(region).split("\n")
            padded = []
            for line in lines:
                key = re.split(SEPARATOR, line, 1)[0]
                line = line.replace(key, key.rstrip().ljust(maxKeyLen + 1)) if len(line.strip()) else ''
                padded.append(line)
            self.view.replace(edit, region, "\n".join(padded))
jbjornson
 
Posts: 345
Joined: Tue Oct 27, 2009 4:25 pm
Location: Zurich, Switzerland

Re: Key-Value Pair Align

Postby jbjornson on Tue Jul 19, 2011 2:59 pm

jbjornson wrote:For a potentially more complete implementation, you might want to check out this: http://www.sublimetext.com/forum/viewtopic.php?f=5&t=2205&start=0
.

Funny enough, senzo just posted a fix (for the alternative code I linked to above) to support aligning with colons (rather than with equals): http://www.sublimetext.com/forum/viewtopic.php?f=5&t=2205&start=10#p11309
jbjornson
 
Posts: 345
Joined: Tue Oct 27, 2009 4:25 pm
Location: Zurich, Switzerland

Re: Key-Value Pair Align

Postby lexybam on Thu Dec 08, 2011 2:49 pm

I beg your pardon, can this work with any model of system? I need to know before I melt down my computer.
lexybam
 
Posts: 1
Joined: Fri Dec 02, 2011 9:38 am


Return to Plugin Announcements

Who is online

Users browsing this forum: No registered users and 21 guests