on master help sign in
Implement 'remove_primary_selection' as Alt-,
2e0803cBlaž Hrastnik
helix-core/src/selection.rs+9
@@ -360,6 +360,15 @@ impl Selection {
360 self.normalize()
360 self.normalize()
361 }
361 }
362
362
363+ /// Adds a new range to the selection and makes it the primary range.
364+ pub fn remove(mut self, index: usize) -> Self {
365+ self.ranges.remove(index);
366+ if index < self.primary_index || self.primary_index == self.ranges.len() {
367+ self.primary_index -= 1;
368+ }
369+ self
370+ }
371+
363 /// Map selections over a set of changes. Useful for adjusting the selection position after
372 /// Map selections over a set of changes. Useful for adjusting the selection position after
364 /// applying changes to a document.
373 /// applying changes to a document.
365 pub fn map(self, changes: &ChangeSet) -> Self {
374 pub fn map(self, changes: &ChangeSet) -> Self {
helix-term/src/commands.rs+17
@@ -290,6 +290,7 @@ impl Command {
290 join_selections, "Join lines inside selection",
290 join_selections, "Join lines inside selection",
291 keep_selections, "Keep selections matching regex",
291 keep_selections, "Keep selections matching regex",
292 keep_primary_selection, "Keep primary selection",
292 keep_primary_selection, "Keep primary selection",
293+ remove_primary_selection, "Remove primary selection",
293 completion, "Invoke completion popup",
294 completion, "Invoke completion popup",
294 hover, "Show docs for item under cursor",
295 hover, "Show docs for item under cursor",
295 toggle_comments, "Comment/uncomment selections",
296 toggle_comments, "Comment/uncomment selections",
@@ -4016,11 +4017,27 @@ fn keep_selections(cx: &mut Context) {
4016
4017
4017fn keep_primary_selection(cx: &mut Context) {
4018fn keep_primary_selection(cx: &mut Context) {
4018 let (view, doc) = current!(cx.editor);
4019 let (view, doc) = current!(cx.editor);
4020+ // TODO: handle count
4019
4021
4020 let range = doc.selection(view.id).primary();
4022 let range = doc.selection(view.id).primary();
4021 doc.set_selection(view.id, Selection::single(range.anchor, range.head));
4023 doc.set_selection(view.id, Selection::single(range.anchor, range.head));
4022}
4024}
4023
4025
4026+fn remove_primary_selection(cx: &mut Context) {
4027+ let (view, doc) = current!(cx.editor);
4028+ // TODO: handle count
4029+
4030+ let selection = doc.selection(view.id);
4031+ if selection.len() == 1 {
4032+ cx.editor.set_error("no selections remaining".to_owned());
4033+ return;
4034+ }
4035+ let index = selection.primary_index();
4036+ let selection = selection.clone().remove(index);
4037+
4038+ doc.set_selection(view.id, selection);
4039+}
4040+
4024fn completion(cx: &mut Context) {
4041fn completion(cx: &mut Context) {
4025 // trigger on trigger char, or if user calls it
4042 // trigger on trigger char, or if user calls it
4026 // (or on word char typing??)
4043 // (or on word char typing??)
helix-term/src/keymap.rs+1
@@ -499,6 +499,7 @@ impl Default for Keymaps {
499 // TODO: and another method for inverse
499 // TODO: and another method for inverse
500
500
501 "," => keep_primary_selection,
501 "," => keep_primary_selection,
502+ "A-," => remove_primary_selection,
502
503
503 // "q" => record_macro,
504 // "q" => record_macro,
504 // "Q" => replay_macro,
505 // "Q" => replay_macro,