Skip to content

Instantly share code, notes, and snippets.

Created December 19, 2012 18:14
Show Gist options
  • Save vitaLee/4338959 to your computer and use it in GitHub Desktop.
Save vitaLee/4338959 to your computer and use it in GitHub Desktop.
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.
{"keys":["ctrl+shift+up"], "command":"super_add_line", "args":{ "forward": false } },
{"keys":["ctrl+shift+down"], "command":"super_add_line", "args":{ "forward": true } }
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
# 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)
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)
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)
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:
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment