aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBlaž Hrastnik2021-11-06 08:37:45 +0000
committerBlaž Hrastnik2021-11-06 08:37:45 +0000
commit6431b26a6a5fa4be5b91008f21537721d2ff4ba2 (patch)
treeaf48079d3ce0855c5158ceba9b74f7795abf9253
parent911b9b3276cb155eab023b24f1a6f336f4054087 (diff)
Implement Selection::replace to replace a single range
Fixes #985 Co-authored-by: Daniel S Poulin <crimsonmage+github@gmail.com>
-rw-r--r--helix-core/src/selection.rs11
-rw-r--r--helix-term/src/commands.rs3
2 files changed, 12 insertions, 2 deletions
diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs
index 18af4d08..f3b5d2c8 100644
--- a/helix-core/src/selection.rs
+++ b/helix-core/src/selection.rs
@@ -362,6 +362,11 @@ impl Selection {
/// Adds a new range to the selection and makes it the primary range.
pub fn remove(mut self, index: usize) -> Self {
+ assert!(
+ self.ranges.len() > 1,
+ "can't remove the last range from a selection!"
+ );
+
self.ranges.remove(index);
if index < self.primary_index || self.primary_index == self.ranges.len() {
self.primary_index -= 1;
@@ -369,6 +374,12 @@ impl Selection {
self
}
+ /// Replace a range in the selection with a new range.
+ pub fn replace(mut self, index: usize, range: Range) -> Self {
+ self.ranges[index] = range;
+ self.normalize()
+ }
+
/// 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 c8f64531..e3ebd128 100644
--- a/helix-term/src/commands.rs
+++ b/helix-term/src/commands.rs
@@ -1227,8 +1227,7 @@ fn search_impl(
Movement::Extend => selection.clone().push(Range::new(start, end)),
Movement::Move => selection
.clone()
- .remove(selection.primary_index())
- .push(Range::new(start, end)),
+ .replace(selection.primary_index(), Range::new(start, end)),
};
doc.set_selection(view.id, selection);