Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vertical Selection #462

Merged
merged 3 commits into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions book/src/keymap.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
| `Alt-s` | Split selection on newlines |
| `;` | Collapse selection onto a single cursor |
| `Alt-;` | Flip selection cursor and anchor |
| `C` | Copy selection onto the next line |
| `Alt-C` | Copy selection onto the previous line |
| `%` | Select entire file |
| `x` | Select current line, if already selected, extend to next line |
| `X` | Extend selection to line bounds (line-wise selection) |
Expand Down
72 changes: 72 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ impl Command {
extend_char_right, "Extend right",
extend_line_up, "Extend up",
extend_line_down, "Extend down",
copy_selection_on_next_line, "Copy selection on next line",
copy_selection_on_prev_line, "Copy selection on previous line",
move_next_word_start, "Move to beginning of next word",
move_prev_word_start, "Move to beginning of previous word",
move_next_word_end, "Move to end of next word",
Expand Down Expand Up @@ -958,6 +960,76 @@ fn extend_char_right(cx: &mut Context) {
doc.set_selection(view.id, selection);
}

fn copy_selection_on_line(cx: &mut Context, direction: Direction) {
let count = cx.count();
let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);
let selection = doc.selection(view.id);
let mut ranges = SmallVec::with_capacity(selection.ranges().len() * (count + 1));
ranges.extend_from_slice(selection.ranges());
let mut primary_index = 0;
for range in selection.iter() {
let is_primary = *range == selection.primary();
let head_pos = coords_at_pos(text, range.head);
let anchor_pos = coords_at_pos(text, range.anchor);
let height = std::cmp::max(head_pos.row, anchor_pos.row)
- std::cmp::min(head_pos.row, anchor_pos.row)
+ 1;

if is_primary {
primary_index = ranges.len();
}
ranges.push(*range);

let mut sels = 0;
let mut i = 0;
while sels < count {
let offset = (i + 1) * height;

let anchor_row = match direction {
Direction::Forward => anchor_pos.row + offset,
Direction::Backward => anchor_pos.row.saturating_sub(offset),
};

let head_row = match direction {
Direction::Forward => head_pos.row + offset,
Direction::Backward => head_pos.row.saturating_sub(offset),
};

if anchor_row >= text.len_lines() || head_row >= text.len_lines() {
break;
}

let anchor = pos_at_coords(text, Position::new(anchor_row, anchor_pos.col), true);
let head = pos_at_coords(text, Position::new(head_row, head_pos.col), true);

// skip lines that are too short
if coords_at_pos(text, anchor).col == anchor_pos.col
&& coords_at_pos(text, head).col == head_pos.col
{
if is_primary {
primary_index = ranges.len();
}
ranges.push(Range::new(anchor, head));
sels += 1;
}

i += 1;
}
}

let selection = Selection::new(ranges, primary_index);
doc.set_selection(view.id, selection);
}

fn copy_selection_on_prev_line(cx: &mut Context) {
copy_selection_on_line(cx, Direction::Backward)
}

fn copy_selection_on_next_line(cx: &mut Context) {
copy_selection_on_line(cx, Direction::Forward)
}

fn extend_line_up(cx: &mut Context) {
let count = cx.count();
let (view, doc) = current!(cx.editor);
Expand Down
4 changes: 4 additions & 0 deletions helix-term/src/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,10 @@ impl Default for Keymaps {
"c" => change_selection,
// TODO: also change delete without yanking

"C" => copy_selection_on_next_line,
"A-C" => copy_selection_on_prev_line,


"s" => select_regex,
"A-s" => split_selection_on_newline,
"S" => split_selection,
Expand Down