Add trim_selections command (#1092)

imgbot
ath3 3 years ago committed by GitHub
parent edc976b6bb
commit 6fa76d9fe7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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` |

@ -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) {

@ -272,6 +272,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",
@ -584,6 +585,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);

@ -603,7 +603,7 @@ impl Default for Keymaps {
// "Q" => replay_macro,
// & align selections
// _ trim selections
"_" => trim_selections,
"(" => rotate_selections_backward,
")" => rotate_selections_forward,

Loading…
Cancel
Save