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

let extend-line respect range direction #3046

Merged
merged 5 commits into from
Aug 29, 2022
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: 1 addition & 1 deletion book/src/keymap.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
| `Alt-(` | Rotate selection contents backward | `rotate_selection_contents_backward` |
| `Alt-)` | Rotate selection contents forward | `rotate_selection_contents_forward` |
| `%` | Select entire file | `select_all` |
| `x` | Select current line, if already selected, extend to next line | `extend_line` |
| `x` | Select current line, if already selected, extend to next line | `extend_line_below` |
| `X` | Extend selection to line bounds (line-wise selection) | `extend_to_line_bounds` |
| `Alt-x` | Shrink selection to line bounds (line-wise selection) | `shrink_to_line_bounds` |
| `J` | Join lines inside selection | `join_selections` |
Expand Down
32 changes: 27 additions & 5 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ impl MappableCommand {
extend_search_prev, "Add previous search match to selection",
search_selection, "Use current selection as search pattern",
global_search, "Global search in workspace folder",
extend_line, "Select current line, if already selected, extend to next line",
extend_line, "Select current line, if already selected, extend to another line based on the anchor",
extend_line_below, "Select current line, if already selected, extend to next line",
extend_line_above, "Select current line, if already selected, extend to previous line",
extend_to_line_bounds, "Extend selection to line bounds",
shrink_to_line_bounds, "Shrink selection to line bounds",
Expand Down Expand Up @@ -1944,6 +1945,15 @@ enum Extend {
}

fn extend_line(cx: &mut Context) {
let (view, doc) = current_ref!(cx.editor);
let extend = match doc.selection(view.id).primary().direction() {
Direction::Forward => Extend::Below,
Direction::Backward => Extend::Above,
};
pickfire marked this conversation as resolved.
Show resolved Hide resolved
extend_line_impl(cx, extend);
}

fn extend_line_below(cx: &mut Context) {
extend_line_impl(cx, Extend::Below);
}

Expand All @@ -1959,20 +1969,32 @@ fn extend_line_impl(cx: &mut Context, extend: Extend) {
let selection = doc.selection(view.id).clone().transform(|range| {
let (start_line, end_line) = range.line_range(text.slice(..));

let start = text.line_to_char(start_line);
let end = text.line_to_char((end_line + count).min(text.len_lines()));
let start = text.line_to_char(match extend {
Extend::Above => start_line.saturating_sub(count),
Extend::Below => start_line,
});
let end = text.line_to_char(
match extend {
Extend::Above => end_line + 1, // the start of next line
Extend::Below => (end_line + count),
}
.min(text.len_lines()),
);

// extend to previous/next line if current line is selected
let (anchor, head) = if range.from() == start && range.to() == end {
match extend {
Extend::Above => (end, text.line_to_char(start_line.saturating_sub(1))),
Extend::Above => (end, text.line_to_char(start_line.saturating_sub(count + 1))),
Extend::Below => (
start,
text.line_to_char((end_line + count + 1).min(text.len_lines())),
),
}
} else {
(start, end)
match extend {
Extend::Above => (end, start),
Extend::Below => (start, end),
}
};

Range::new(anchor, head)
Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/keymap/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub fn default() -> HashMap<Mode, Keymap> {
"A-n" | "A-right" => select_next_sibling,

"%" => select_all,
"x" => extend_line,
"x" => extend_line_below,
"X" => extend_to_line_bounds,
"A-x" => shrink_to_line_bounds,

Expand Down