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 trim_selections command #1092

Merged
merged 1 commit into from
Nov 14, 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
1 change: 1 addition & 0 deletions book/src/keymap.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
| `s` | Select all regex matches inside selections | `select_regex` |
| `S` | Split selection into subselections on regex matches | `split_selection` |
| `Alt-s` | Split selection on newlines | `split_selection_on_newline` |
| `_` | Trim whitespace from the selection | `trim_selections` |
| `;` | Collapse selection onto a single cursor | `collapse_selection` |
| `Alt-;` | Flip selection cursor and anchor | `flip_selections` |
| `,` | Keep only the primary selection | `keep_primary_selection` |
Expand Down
2 changes: 1 addition & 1 deletion helix-core/src/movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub fn backwards_skip_while<F>(slice: RopeSlice, pos: usize, fun: F) -> Option<u
where
F: Fn(char) -> bool,
{
let mut chars_starting_from_next = slice.chars_at(pos + 1);
let mut chars_starting_from_next = slice.chars_at(pos);
let mut backwards = iter::from_fn(|| chars_starting_from_next.prev()).enumerate();
backwards.find_map(|(i, c)| {
if !fun(c) {
Expand Down
37 changes: 37 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ impl Command {
// TODO: different description ?
goto_line_end_newline, "Goto line end",
goto_first_nonwhitespace, "Goto first non-blank in line",
trim_selections, "Trim whitespace from selections",
extend_to_line_start, "Extend to line start",
extend_to_line_end, "Extend to line end",
extend_to_line_end_newline, "Extend to line end",
Expand Down Expand Up @@ -582,6 +583,42 @@ fn goto_first_nonwhitespace(cx: &mut Context) {
doc.set_selection(view.id, selection);
}

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

let ranges: SmallVec<[Range; 1]> = doc
.selection(view.id)
.iter()
.filter_map(|range| {
if range.is_empty() || range.fragment(text).chars().all(|ch| ch.is_whitespace()) {
return None;
}
let mut start = range.from();
let mut end = range.to();
start = movement::skip_while(text, start, |x| x.is_whitespace()).unwrap_or(start);
end = movement::backwards_skip_while(text, end, |x| x.is_whitespace()).unwrap_or(end);
if range.anchor < range.head {
Some(Range::new(start, end))
} else {
Some(Range::new(end, start))
}
})
.collect();

if !ranges.is_empty() {
let primary = doc.selection(view.id).primary();
let idx = ranges
.iter()
.position(|range| range.overlaps(&primary))
.unwrap_or(ranges.len() - 1);
doc.set_selection(view.id, Selection::new(ranges, idx));
} else {
collapse_selection(cx);
keep_primary_selection(cx);
};
}

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

Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ impl Default for Keymaps {
// "Q" => replay_macro,

// & align selections
// _ trim selections
"_" => trim_selections,

"(" => rotate_selections_backward,
")" => rotate_selections_forward,
Expand Down