Sublime Forum

Key-Value Pair Align

#1

Turn this:

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

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. :stuck_out_tongue:

It works with multiple regions though :smiley:

[code]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))[/code]

0 Likes

#2

Pardon my ignorance but what is scoping?

0 Likes

#3

Would this work in ST2?

0 Likes

#4

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.

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))
0 Likes

#5

.

Funny enough, senzo just posted a fix (for the alternative code I linked to above) to support aligning with colons (rather than with equals): https://forum.sublimetext.com/t/file-change-detection/21/1#p11309

0 Likes

#6

I beg your pardon, can this work with any model of system? I need to know before I melt down my computer.

0 Likes