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

Add shrink equivalent of extend_to_line_bounds #2450

Merged
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
1 change: 1 addition & 0 deletions book/src/keymap.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
| `%` | Select entire file | `select_all` |
| `x` | Select current line, if already selected, extend to next line | `extend_line` |
| `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` |
| `K` | Keep selections matching the regex | `keep_selections` |
| `Alt-K` | Remove selections matching the regex | `remove_selections` |
Expand Down
42 changes: 42 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ impl MappableCommand {
extend_line, "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 (line-wise selection)",
shrink_to_line_bounds, "Shrink selection to line bounds (line-wise selection)",
delete_selection, "Delete selection",
delete_selection_noyank, "Delete selection, without yanking",
change_selection, "Change selection (delete and enter insert mode)",
Expand Down Expand Up @@ -1937,6 +1938,47 @@ fn extend_to_line_bounds(cx: &mut Context) {
);
}

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

doc.set_selection(
view.id,
doc.selection(view.id).clone().transform(|range| {
let text = doc.text();

let (start_line, end_line) = range.line_range(text.slice(..));

// Do nothing if the selection is within one line to prevent
// conditional logic for the behavior of this command
if start_line == end_line {
return range;
}

let mut start = text.line_to_char(start_line);

// line_to_char gives us the start position of the line, so
// we need to get the start position of the next line. In
// the editor, this will correspond to the cursor being on
// the EOL whitespace charactor, which is what we want.
let mut end = text.line_to_char((end_line + 1).min(text.len_lines()));

if start != range.from() {
start = text.line_to_char((start_line + 1).min(text.len_lines()));
}

if end != range.to() {
end = text.line_to_char(end_line);
}

if range.anchor <= range.head {
Range::new(start, end)
} else {
Range::new(end, start)
}
}),
);
Comment on lines +1944 to +1979
Copy link
Contributor

@pickfire pickfire May 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to move this into selection variable to reduce one layer of nesting.

}

enum Operation {
Delete,
Change,
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 @@ -87,7 +87,7 @@ pub fn default() -> HashMap<Mode, Keymap> {
"%" => select_all,
"x" => extend_line,
"X" => extend_to_line_bounds,
// crop_to_whole_line
"A-x" => shrink_to_line_bounds,

"m" => { "Match"
"m" => match_brackets,
Expand Down