Skip to content

Commit e30d9bd

Browse files
committed
Add trim_selections command
1 parent b824e09 commit e30d9bd

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

book/src/keymap.md

+1
Original file line numberDiff line numberDiff line change
@@ -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
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ pub fn backwards_skip_while<F>(slice: RopeSlice, pos: usize, fun: F) -> Option<u
168168
where
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
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ impl Command {
270270
// TODO: different description ?
271271
goto_line_end_newline, "Goto line end",
272272
goto_first_nonwhitespace, "Goto first non-blank in line",
273+
trim_selections, "Trim whitespace from selections",
273274
extend_to_line_start, "Extend to line start",
274275
extend_to_line_end, "Extend to line end",
275276
extend_to_line_end_newline, "Extend to line end",
@@ -582,6 +583,42 @@ fn goto_first_nonwhitespace(cx: &mut Context) {
582583
doc.set_selection(view.id, selection);
583584
}
584585

586+
fn trim_selections(cx: &mut Context) {
587+
let (view, doc) = current!(cx.editor);
588+
let text = doc.text().slice(..);
589+
590+
let ranges: SmallVec<[Range; 1]> = doc
591+
.selection(view.id)
592+
.iter()
593+
.filter_map(|range| {
594+
if range.is_empty() || range.fragment(text).chars().all(|ch| ch.is_whitespace()) {
595+
return None;
596+
}
597+
let mut start = range.from();
598+
let mut end = range.to();
599+
start = movement::skip_while(text, start, |x| x.is_whitespace()).unwrap_or(start);
600+
end = movement::backwards_skip_while(text, end, |x| x.is_whitespace()).unwrap_or(end);
601+
if range.anchor < range.head {
602+
Some(Range::new(start, end))
603+
} else {
604+
Some(Range::new(end, start))
605+
}
606+
})
607+
.collect();
608+
609+
if !ranges.is_empty() {
610+
let primary = doc.selection(view.id).primary();
611+
let idx = ranges
612+
.iter()
613+
.position(|range| range.overlaps(&primary))
614+
.unwrap_or(ranges.len() - 1);
615+
doc.set_selection(view.id, Selection::new(ranges, idx));
616+
} else {
617+
collapse_selection(cx);
618+
keep_primary_selection(cx);
619+
};
620+
}
621+
585622
fn goto_window(cx: &mut Context, align: Align) {
586623
let (view, doc) = current!(cx.editor);
587624

helix-term/src/keymap.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ impl Default for Keymaps {
602602
// "Q" => replay_macro,
603603

604604
// & align selections
605-
// _ trim selections
605+
"_" => trim_selections,
606606

607607
"(" => rotate_selections_backward,
608608
")" => rotate_selections_forward,

0 commit comments

Comments
 (0)