aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOmnikar2021-11-12 00:34:08 +0000
committerGitHub2021-11-12 00:34:08 +0000
commitbf95a9ed043242d95e431412e45e218d40a5695a (patch)
treeb3b141542fd05f088fabadeb67681dcd560bdbe2
parent9d591427be900b7a43fc7e13dd86f31199e8c00e (diff)
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>
-rw-r--r--book/src/keymap.md1
-rw-r--r--helix-core/src/selection.rs5
-rw-r--r--helix-term/src/commands.rs19
-rw-r--r--helix-term/src/keymap.rs2
4 files changed, 20 insertions, 7 deletions
diff --git a/book/src/keymap.md b/book/src/keymap.md
index 901c8471..7a9a4378 100644
--- a/book/src/keymap.md
+++ b/book/src/keymap.md
@@ -101,6 +101,7 @@
| | Expand selection to parent syntax node TODO: pick a key | `expand_selection` |
| `J` | Join lines inside selection | `join_selections` |
| `K` | Keep selections matching the regex | `keep_selections` |
+| `Alt-K` | Remove selections matching the regex | `remove_selections` |
| `$` | Pipe each selection into shell command, keep selections where command returned 0 | `shell_keep_pipe` |
| `Ctrl-c` | Comment/uncomment the selections | `toggle_comments` |
diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs
index f3b5d2c8..f7c7dbcb 100644
--- a/helix-core/src/selection.rs
+++ b/helix-core/src/selection.rs
@@ -528,14 +528,15 @@ impl<'a> IntoIterator for &'a Selection {
// TODO: checkSelection -> check if valid for doc length && sorted
-pub fn keep_matches(
+pub fn keep_or_remove_matches(
text: RopeSlice,
selection: &Selection,
regex: &crate::regex::Regex,
+ remove: bool,
) -> Option<Selection> {
let result: SmallVec<_> = selection
.iter()
- .filter(|range| regex.is_match(&range.fragment(text)))
+ .filter(|range| regex.is_match(&range.fragment(text)) ^ remove)
.copied()
.collect();
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs
index 738621b0..4352ee66 100644
--- a/helix-term/src/commands.rs
+++ b/helix-term/src/commands.rs
@@ -302,6 +302,7 @@ impl Command {
format_selections, "Format selection",
join_selections, "Join lines inside selection",
keep_selections, "Keep selections matching regex",
+ remove_selections, "Remove selections matching regex",
keep_primary_selection, "Keep primary selection",
remove_primary_selection, "Remove primary selection",
completion, "Invoke completion popup",
@@ -4320,12 +4321,12 @@ fn join_selections(cx: &mut Context) {
doc.append_changes_to_history(view.id);
}
-fn keep_selections(cx: &mut Context) {
- // keep selections matching regex
+fn keep_or_remove_selections_impl(cx: &mut Context, remove: bool) {
+ // keep or remove selections matching regex
let reg = cx.register.unwrap_or('/');
let prompt = ui::regex_prompt(
cx,
- "keep:".into(),
+ if !remove { "keep:" } else { "remove:" }.into(),
Some(reg),
|_input: &str| Vec::new(),
move |view, doc, regex, event| {
@@ -4334,7 +4335,9 @@ fn keep_selections(cx: &mut Context) {
}
let text = doc.text().slice(..);
- if let Some(selection) = selection::keep_matches(text, doc.selection(view.id), &regex) {
+ if let Some(selection) =
+ selection::keep_or_remove_matches(text, doc.selection(view.id), &regex, remove)
+ {
doc.set_selection(view.id, selection);
}
},
@@ -4343,6 +4346,14 @@ fn keep_selections(cx: &mut Context) {
cx.push_layer(Box::new(prompt));
}
+fn keep_selections(cx: &mut Context) {
+ keep_or_remove_selections_impl(cx, false)
+}
+
+fn remove_selections(cx: &mut Context) {
+ keep_or_remove_selections_impl(cx, true)
+}
+
fn keep_primary_selection(cx: &mut Context) {
let (view, doc) = current!(cx.editor);
// TODO: handle count
diff --git a/helix-term/src/keymap.rs b/helix-term/src/keymap.rs
index b2b865e4..f79978fb 100644
--- a/helix-term/src/keymap.rs
+++ b/helix-term/src/keymap.rs
@@ -577,7 +577,7 @@ impl Default for Keymaps {
"=" => format_selections,
"J" => join_selections,
"K" => keep_selections,
- // TODO: and another method for inverse
+ "A-K" => remove_selections,
"," => keep_primary_selection,
"A-," => remove_primary_selection,