Created
August 26, 2012 12:13
-
-
Save vitaLee/3478311 to your computer and use it in GitHub Desktop.
Sublime Text - Extended Add Line command, allowing column selection even on shorter lines.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ "keys": ["ctrl+alt+up"], "command": "smart_select_lines", "args": {"forward": false} }, | |
{ "keys": ["ctrl+alt+down"], "command": "smart_select_lines", "args": {"forward": true} } | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sublime_plugin | |
from sublime import Region | |
class SmartSelectLinesCommand(sublime_plugin.TextCommand): | |
""" | |
Upgraded version of default Add Previous/Next Line commands | |
Pads new lines with empty spaces to allow column editing | |
as if there are virtual spaces. | |
""" | |
def run(self, edit, forward): | |
region = self.view.sel()[-1 if forward else 0] | |
caret_position = region.b | |
row_position, col_position = self.view.rowcol(caret_position) | |
total_rows = self.view.rowcol(self.view.size())[0] | |
skip_on_row = [0, total_rows] | |
if(row_position == skip_on_row[forward]): | |
return | |
curr_line_region = self.view.line(caret_position) | |
if forward: | |
next_line_offset = (curr_line_region.size() - col_position + 1) | |
else: | |
next_line_offset = -(col_position + 1) | |
next_line_region = self.view.line(caret_position + next_line_offset) | |
diff = col_position - next_line_region.size() | |
if diff > 0: | |
fill = diff * ' ' | |
self.view.insert(edit, next_line_region.end(), fill) | |
new_caret_position = next_line_region.begin() + col_position | |
new_region = Region(new_caret_position, new_caret_position) | |
self.view.sel().add(new_region) |
Thanks for letting me know.
I had something in mind right there but probably didn't get the api documentation right.
Anyway, what i wanted to address there was extending the selection even if the first region was not empty.
I was falling back to the default 'select_lines' command for region placement, which is not working correctly in the case i wanted to address.
Here comes the update.
Works perfectly in Sublime Text 3 :o)
Thank you for this neat piece of software :o)
You can enhance this to extend selections up and down as well as just the caret:
change:
caret_position = region.b
to:
caret_position = region.end()
caret_offset = region.b - caret_position
caret_size = region.a - region.b
and also change:
new_region = Region(new_caret_position, new_caret_position)
to:
new_region = Region( new_caret_position+caret_offset+caret_size, new_caret_position+caret_offset )
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
caret_position = region.begin() if region.empty() else (region.a if region.a < region.b else region.b)
is actually
caret_position = region.begin()