- 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.
It works with multiple regions though
- 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))