Skip to content

Commit

Permalink
Implement X as extend selection to line bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
archseer committed Jul 5, 2021
1 parent d02bbb7 commit a4e28c6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions book/src/keymap.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
| `Alt-;` | Flip selection cursor and anchor |
| `%` | Select entire file |
| `x` | Select current line, if already selected, extend to next line |
| `X` | Extend selection to line bounds (line-wise selection) |
| | Expand selection to parent syntax node TODO: pick a key |
| `J` | join lines inside selection |
| `K` | keep selections matching the regex TODO: overlapped by hover help |
Expand Down
21 changes: 21 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ impl Command {
extend_search_next,
search_selection,
extend_line,
extend_to_line_bounds,
delete_selection,
change_selection,
collapse_selection,
Expand Down Expand Up @@ -1021,6 +1022,26 @@ fn extend_line(cx: &mut Context) {
doc.set_selection(view.id, Selection::single(start, end));
}

fn extend_to_line_bounds(cx: &mut Context) {
let (view, doc) = current!(cx.editor);

let text = doc.text();
let selection = doc.selection(view.id).transform(|range| {
let start = text.line_to_char(text.char_to_line(range.from()));
let end = text
.line_to_char(text.char_to_line(range.to()) + 1)
.saturating_sub(1);

if range.anchor < range.head {
Range::new(start, end)
} else {
Range::new(end, start)
}
});

doc.set_selection(view.id, selection);
}

fn delete_selection_impl(reg: &mut Register, doc: &mut Document, view_id: ViewId) {
// first yank the selection
let values: Vec<String> = doc
Expand Down
4 changes: 3 additions & 1 deletion helix-term/src/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ impl Default for Keymaps {
alt!(';') => Command::flip_selections,
key!('%') => Command::select_all,
key!('x') => Command::extend_line,
// extend_to_whole_line, crop_to_whole_line
key!('x') => Command::extend_line,
key!('X') => Command::extend_to_line_bounds,
// crop_to_whole_line


key!('m') => Command::match_mode,
Expand Down

0 comments on commit a4e28c6

Please sign in to comment.