mirror of
https://github.com/helix-editor/helix.git
synced 2025-01-19 13:37:06 +04:00
Implement Selection::replace to replace a single range
Fixes #985 Co-authored-by: Daniel S Poulin <crimsonmage+github@gmail.com>
This commit is contained in:
parent
911b9b3276
commit
6431b26a6a
@ -362,6 +362,11 @@ pub fn push(mut self, range: Range) -> Self {
|
|||||||
|
|
||||||
/// Adds a new range to the selection and makes it the primary range.
|
/// Adds a new range to the selection and makes it the primary range.
|
||||||
pub fn remove(mut self, index: usize) -> Self {
|
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);
|
self.ranges.remove(index);
|
||||||
if index < self.primary_index || self.primary_index == self.ranges.len() {
|
if index < self.primary_index || self.primary_index == self.ranges.len() {
|
||||||
self.primary_index -= 1;
|
self.primary_index -= 1;
|
||||||
@ -369,6 +374,12 @@ pub fn remove(mut self, index: usize) -> Self {
|
|||||||
self
|
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
|
/// Map selections over a set of changes. Useful for adjusting the selection position after
|
||||||
/// applying changes to a document.
|
/// applying changes to a document.
|
||||||
pub fn map(self, changes: &ChangeSet) -> Self {
|
pub fn map(self, changes: &ChangeSet) -> Self {
|
||||||
|
@ -1227,8 +1227,7 @@ fn search_impl(
|
|||||||
Movement::Extend => selection.clone().push(Range::new(start, end)),
|
Movement::Extend => selection.clone().push(Range::new(start, end)),
|
||||||
Movement::Move => selection
|
Movement::Move => selection
|
||||||
.clone()
|
.clone()
|
||||||
.remove(selection.primary_index())
|
.replace(selection.primary_index(), Range::new(start, end)),
|
||||||
.push(Range::new(start, end)),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
doc.set_selection(view.id, selection);
|
doc.set_selection(view.id, selection);
|
||||||
|
Loading…
Reference in New Issue
Block a user