on master help sign in
Add trim_selections command (#1092)
commit6fa76d9fe77d43ebc18cc78a6a1c1957d72cf59b authorath3 <45574139+ath3@users.noreply.github.com> dateSun 14 Nov 2021, 16:16 (5 years ago) parentedc976b
4 files changed+40−2
book/src/keymap.md+1 helix-core/src/movement.rs+1−1 helix-term/src/commands.rs+37 helix-term/src/keymap.rs+1−1
book/src/keymap.md+1
@@ -88,6 +88,7 @@
8888| `s` | Select all regex matches inside selections | `select_regex` |
8989| `S` | Split selection into subselections on regex matches | `split_selection` |
9090| `Alt-s` | Split selection on newlines | `split_selection_on_newline` |
91+| `_` | Trim whitespace from the selection | `trim_selections` |
9192| `;` | Collapse selection onto a single cursor | `collapse_selection` |
9293| `Alt-;` | Flip selection cursor and anchor | `flip_selections` |
9394| `,` | Keep only the primary selection | `keep_primary_selection` |
helix-core/src/movement.rs+1−1
@@ -168,7 +168,7 @@ pub fn backwards_skip_while<F>(slice: RopeSlice, pos: usize, fun: F) -> Option<u
168168where
169169 F: Fn(char) -> bool,
170170{
171− let mut chars_starting_from_next = slice.chars_at(pos + 1);
171+ let mut chars_starting_from_next = slice.chars_at(pos);
172172 let mut backwards = iter::from_fn(|| chars_starting_from_next.prev()).enumerate();
173173 backwards.find_map(|(i, c)| {
174174 if !fun(c) {
helix-term/src/commands.rs+37
@@ -272,6 +272,7 @@ impl Command {
272272 // TODO: different description ?
273273 goto_line_end_newline, "Goto line end",
274274 goto_first_nonwhitespace, "Goto first non-blank in line",
275+ trim_selections, "Trim whitespace from selections",
275276 extend_to_line_start, "Extend to line start",
276277 extend_to_line_end, "Extend to line end",
277278 extend_to_line_end_newline, "Extend to line end",
@@ -584,6 +585,42 @@ fn goto_first_nonwhitespace(cx: &mut Context) {
584585 doc.set_selection(view.id, selection);
585586}
586587
588+fn trim_selections(cx: &mut Context) {
589+ let (view, doc) = current!(cx.editor);
590+ let text = doc.text().slice(..);
591+
592+ let ranges: SmallVec<[Range; 1]> = doc
593+ .selection(view.id)
594+ .iter()
595+ .filter_map(|range| {
596+ if range.is_empty() || range.fragment(text).chars().all(|ch| ch.is_whitespace()) {
597+ return None;
598+ }
599+ let mut start = range.from();
600+ let mut end = range.to();
601+ start = movement::skip_while(text, start, |x| x.is_whitespace()).unwrap_or(start);
602+ end = movement::backwards_skip_while(text, end, |x| x.is_whitespace()).unwrap_or(end);
603+ if range.anchor < range.head {
604+ Some(Range::new(start, end))
605+ } else {
606+ Some(Range::new(end, start))
607+ }
608+ })
609+ .collect();
610+
611+ if !ranges.is_empty() {
612+ let primary = doc.selection(view.id).primary();
613+ let idx = ranges
614+ .iter()
615+ .position(|range| range.overlaps(&primary))
616+ .unwrap_or(ranges.len() - 1);
617+ doc.set_selection(view.id, Selection::new(ranges, idx));
618+ } else {
619+ collapse_selection(cx);
620+ keep_primary_selection(cx);
621+ };
622+}
623+
587624fn goto_window(cx: &mut Context, align: Align) {
588625 let (view, doc) = current!(cx.editor);
589626
helix-term/src/keymap.rs+1−1
@@ -603,7 +603,7 @@ impl Default for Keymaps {
603603 // "Q" => replay_macro,
604604
605605 // & align selections
606− // _ trim selections
606+ "_" => trim_selections,
607607
608608 "(" => rotate_selections_backward,
609609 ")" => rotate_selections_forward,