public
Last active

Reworked version of Sublime Text 2 default Add Line command. Allows expanding selection at the same column even on shorter lines (as if there is virtual space). Works with multi selections and solves problem Sublime has with expanding non-empty selection regions.

  • Download Gist
Default (OSX).sublime-keymap
JSON
1 2 3 4
[
{"keys":["ctrl+shift+up"], "command":"super_add_line", "args":{ "forward": false } },
{"keys":["ctrl+shift+down"], "command":"super_add_line", "args":{ "forward": true } }
]
super_add_line_command.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
import sublime
import sublime_plugin
 
 
class SuperAddLineCommand(sublime_plugin.TextCommand):
 
def run(self, edit, forward):
old_regions = self.save_regions()
new_regions = self.new_regions(forward)
 
# clear all present sel regions since they might have been displaced
# when ensure_line_min_width was run
self.view.sel().clear()
# restore all sel regions by row, col locations
self.restore_regions(old_regions + new_regions)
 
def save_regions(self):
# [( a ) ( b )]
# [(row, col), (row, col)]
view = self.view
return map(lambda sel: [view.rowcol(sel.a), view.rowcol(sel.b)], view.sel())
 
def restore_regions(self, regions_locs):
for region_bounds in regions_locs:
a, b = [self.view.text_point(r[0], r[1]) for r in region_bounds]
region = sublime.Region(a, b)
self.view.sel().add(region)
 
def new_regions(self, forward):
chain_tip = [0, -1][forward]
row_offset = [-1, 1][forward]
new_regions = []
 
sel_columns = self.selection_columns(forward)
for col, row_chains in sel_columns.items():
for chain in row_chains:
row = chain[chain_tip] + row_offset
r = self.new_region(row, col)
new_regions.append(r)
return new_regions
 
def new_region(self, row, col):
self.ensure_line_min_width(row, col)
return [[row, col]] * 2
 
def ensure_line_min_width(self, row, min_width):
line_region = self.view.line(self.view.text_point(row, 0))
 
diff = min_width - line_region.size()
if diff > 0:
fill = diff * ' '
edit = self.view.begin_edit()
self.view.insert(edit, line_region.end(), fill)
self.view.end_edit(edit)
 
def selection_columns(self, forward):
columns = {}
 
for sel in self.view.sel():
row, col = self.view.rowcol(sel.b)
if not col in columns:
columns[col] = []
cols_chains = columns[col]
 
# if either first sel column or new sel not part of last column
if not len(cols_chains) or cols_chains[-1][-1] != row - 1:
cols_chains.append([])
 
cols_chains[-1].append(row)
 
# remove chains that has nowhere to expand
total_rows = self.view.rowcol(self.view.size())[0]
check_against = [0, total_rows][forward]
check_tip = [0, -1][forward]
 
for col, chains in columns.items():
columns[col] = filter(lambda list: list[check_tip] != check_against, chains)
 
return columns

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.