on master help sign in
Add `remove_selections` command (#1065)

* Add `remove_selections` command * Document `remove_selections` * Update helix-term/src/keymap.rs Co-authored-by: Blaž Hrastnik <blaz@mxxn.io>

commitbf95a9ed043242d95e431412e45e218d40a5695a authorOmnikar <omnikar5@gmail.com> dateThu 11 Nov 2021, 19:34 (5 years ago) parent9d59142
4 files changed+20−7
book/src/keymap.md+1 helix-core/src/selection.rs+3−2 helix-term/src/commands.rs+15−4 helix-term/src/keymap.rs+1−1
book/src/keymap.md+1
@@ -101,6 +101,7 @@
101101| | Expand selection to parent syntax node TODO: pick a key | `expand_selection` |
102102| `J` | Join lines inside selection | `join_selections` |
103103| `K` | Keep selections matching the regex | `keep_selections` |
104+| `Alt-K` | Remove selections matching the regex | `remove_selections` |
104105| `$` | Pipe each selection into shell command, keep selections where command returned 0 | `shell_keep_pipe` |
105106| `Ctrl-c` | Comment/uncomment the selections | `toggle_comments` |
106107
helix-core/src/selection.rs+3−2
@@ -528,14 +528,15 @@ impl<'a> IntoIterator for &'a Selection {
528528
529529// TODO: checkSelection -> check if valid for doc length && sorted
530530
531−pub fn keep_matches(
531+pub fn keep_or_remove_matches(
532532 text: RopeSlice,
533533 selection: &Selection,
534534 regex: &crate::regex::Regex,
535+ remove: bool,
535536) -> Option<Selection> {
536537 let result: SmallVec<_> = selection
537538 .iter()
538− .filter(|range| regex.is_match(&range.fragment(text)))
539+ .filter(|range| regex.is_match(&range.fragment(text)) ^ remove)
539540 .copied()
540541 .collect();
541542
helix-term/src/commands.rs+15−4
@@ -302,6 +302,7 @@ impl Command {
302302 format_selections, "Format selection",
303303 join_selections, "Join lines inside selection",
304304 keep_selections, "Keep selections matching regex",
305+ remove_selections, "Remove selections matching regex",
305306 keep_primary_selection, "Keep primary selection",
306307 remove_primary_selection, "Remove primary selection",
307308 completion, "Invoke completion popup",
@@ -4320,12 +4321,12 @@ fn join_selections(cx: &mut Context) {
43204321 doc.append_changes_to_history(view.id);
43214322}
43224323
4323−fn keep_selections(cx: &mut Context) {
4324− // keep selections matching regex
4324+fn keep_or_remove_selections_impl(cx: &mut Context, remove: bool) {
4325+ // keep or remove selections matching regex
43254326 let reg = cx.register.unwrap_or('/');
43264327 let prompt = ui::regex_prompt(
43274328 cx,
4328− "keep:".into(),
4329+ if !remove { "keep:" } else { "remove:" }.into(),
43294330 Some(reg),
43304331 |_input: &str| Vec::new(),
43314332 move |view, doc, regex, event| {
@@ -4334,7 +4335,9 @@ fn keep_selections(cx: &mut Context) {
43344335 }
43354336 let text = doc.text().slice(..);
43364337
4337− if let Some(selection) = selection::keep_matches(text, doc.selection(view.id), &regex) {
4338+ if let Some(selection) =
4339+ selection::keep_or_remove_matches(text, doc.selection(view.id), &regex, remove)
4340+ {
43384341 doc.set_selection(view.id, selection);
43394342 }
43404343 },
@@ -4343,6 +4346,14 @@ fn keep_selections(cx: &mut Context) {
43434346 cx.push_layer(Box::new(prompt));
43444347}
43454348
4349+fn keep_selections(cx: &mut Context) {
4350+ keep_or_remove_selections_impl(cx, false)
4351+}
4352+
4353+fn remove_selections(cx: &mut Context) {
4354+ keep_or_remove_selections_impl(cx, true)
4355+}
4356+
43464357fn keep_primary_selection(cx: &mut Context) {
43474358 let (view, doc) = current!(cx.editor);
43484359 // TODO: handle count
helix-term/src/keymap.rs+1−1
@@ -577,7 +577,7 @@ impl Default for Keymaps {
577577 "=" => format_selections,
578578 "J" => join_selections,
579579 "K" => keep_selections,
580− // TODO: and another method for inverse
580+ "A-K" => remove_selections,
581581
582582 "," => keep_primary_selection,
583583 "A-," => remove_primary_selection,