aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBlaž Hrastnik2021-09-21 15:51:49 +0000
committerBlaž Hrastnik2021-09-24 01:30:28 +0000
commit2e0803c8d9ec0028c0d018be251c7c2b781247b3 (patch)
tree342c9a4745c8becd490c1ecddaa38b11d24e6b9a
parent75dba1f9560c6ea579e79ff074e60ba2fb87ca63 (diff)
Implement 'remove_primary_selection' as Alt-,
This allows removing search matches from the selection Fixes #713
-rw-r--r--helix-core/src/selection.rs9
-rw-r--r--helix-term/src/commands.rs17
-rw-r--r--helix-term/src/keymap.rs1
3 files changed, 27 insertions, 0 deletions
diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs
index a3ea2ed4..755ee679 100644
--- a/helix-core/src/selection.rs
+++ b/helix-core/src/selection.rs
@@ -360,6 +360,15 @@ impl Selection {
self.normalize()
}
+ /// Adds a new range to the selection and makes it the primary range.
+ pub fn remove(mut self, index: usize) -> Self {
+ self.ranges.remove(index);
+ if index < self.primary_index || self.primary_index == self.ranges.len() {
+ self.primary_index -= 1;
+ }
+ self
+ }
+
/// Map selections over a set of changes. Useful for adjusting the selection position after
/// applying changes to a document.
pub fn map(self, changes: &ChangeSet) -> Self {
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs
index e3c351f6..025639a5 100644
--- a/helix-term/src/commands.rs
+++ b/helix-term/src/commands.rs
@@ -290,6 +290,7 @@ impl Command {
join_selections, "Join lines inside selection",
keep_selections, "Keep selections matching regex",
keep_primary_selection, "Keep primary selection",
+ remove_primary_selection, "Remove primary selection",
completion, "Invoke completion popup",
hover, "Show docs for item under cursor",
toggle_comments, "Comment/uncomment selections",
@@ -4016,11 +4017,27 @@ fn keep_selections(cx: &mut Context) {
fn keep_primary_selection(cx: &mut Context) {
let (view, doc) = current!(cx.editor);
+ // TODO: handle count
let range = doc.selection(view.id).primary();
doc.set_selection(view.id, Selection::single(range.anchor, range.head));
}
+fn remove_primary_selection(cx: &mut Context) {
+ let (view, doc) = current!(cx.editor);
+ // TODO: handle count
+
+ let selection = doc.selection(view.id);
+ if selection.len() == 1 {
+ cx.editor.set_error("no selections remaining".to_owned());
+ return;
+ }
+ let index = selection.primary_index();
+ let selection = selection.clone().remove(index);
+
+ doc.set_selection(view.id, selection);
+}
+
fn completion(cx: &mut Context) {
// trigger on trigger char, or if user calls it
// (or on word char typing??)
diff --git a/helix-term/src/keymap.rs b/helix-term/src/keymap.rs
index 4343a0b6..cd4d3a32 100644
--- a/helix-term/src/keymap.rs
+++ b/helix-term/src/keymap.rs
@@ -499,6 +499,7 @@ impl Default for Keymaps {
// TODO: and another method for inverse
"," => keep_primary_selection,
+ "A-," => remove_primary_selection,
// "q" => record_macro,
// "Q" => replay_macro,