Skip to content

Commit

Permalink
only keep selections invariant under replacement
Browse files Browse the repository at this point in the history
Keeping selections unchanged if they are inside an exact replacement
is intuitive. However, for diagnostics this is not desirable as
helix would otherwise fail to remove diagnostics if replacing parts
of the document.
  • Loading branch information
pascalkuthe committed Jul 9, 2023
1 parent 93002f9 commit c892d2a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
24 changes: 12 additions & 12 deletions helix-core/src/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,16 @@ impl Range {

let positions_to_map = match self.anchor.cmp(&self.head) {
Ordering::Equal => [
(&mut self.anchor, Assoc::After),
(&mut self.head, Assoc::After),
(&mut self.anchor, Assoc::AfterSticky),
(&mut self.head, Assoc::AfterSticky),
],
Ordering::Less => [
(&mut self.anchor, Assoc::After),
(&mut self.head, Assoc::Before),
(&mut self.anchor, Assoc::AfterSticky),
(&mut self.head, Assoc::BeforeSticky),
],
Ordering::Greater => [
(&mut self.head, Assoc::After),
(&mut self.anchor, Assoc::Before),
(&mut self.head, Assoc::AfterSticky),
(&mut self.anchor, Assoc::BeforeSticky),
],
};
changes.update_positions(positions_to_map.into_iter());
Expand Down Expand Up @@ -467,16 +467,16 @@ impl Selection {
range.old_visual_position = None;
match range.anchor.cmp(&range.head) {
Ordering::Equal => [
(&mut range.anchor, Assoc::After),
(&mut range.head, Assoc::After),
(&mut range.anchor, Assoc::AfterSticky),
(&mut range.head, Assoc::AfterSticky),
],
Ordering::Less => [
(&mut range.anchor, Assoc::After),
(&mut range.head, Assoc::Before),
(&mut range.anchor, Assoc::AfterSticky),
(&mut range.head, Assoc::BeforeSticky),
],
Ordering::Greater => [
(&mut range.head, Assoc::After),
(&mut range.anchor, Assoc::Before),
(&mut range.head, Assoc::AfterSticky),
(&mut range.anchor, Assoc::BeforeSticky),
],
}
});
Expand Down
32 changes: 26 additions & 6 deletions helix-core/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ pub enum Operation {
pub enum Assoc {
Before,
After,
BeforeSticky,
AfterSticky,
}

impl Assoc {
pub fn before(self) -> bool {
matches!(self, Assoc::Before | Assoc::BeforeSticky)
}

pub fn after(self) -> bool {
matches!(self, Assoc::After | Assoc::AfterSticky)
}

pub fn sticky(self) -> bool {
matches!(self, Assoc::BeforeSticky | Assoc::AfterSticky)
}
}

#[derive(Debug, Default, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -420,14 +436,14 @@ impl ChangeSet {
old_end = old_pos + len;
// in range of replaced text
map!(
|pos, assoc| (old_end > pos).then(|| {
|pos, assoc: Assoc| (old_end > pos).then(|| {
// if the deleted and inserted text have the exact same size
// keep the relative offset into the new text
if *len == ins {
if *len == ins && assoc.sticky() {
return new_pos + (pos - old_pos);
}
// at point or tracking before
if pos == old_pos || assoc == Assoc::Before {
if pos == old_pos || assoc.before() {
new_pos
} else {
// place to end of insert
Expand All @@ -439,9 +455,9 @@ impl ChangeSet {
} else {
// at insert point
map!(
|pos, assoc| (old_pos == pos).then(|| {
|pos, assoc: Assoc| (old_pos == pos).then(|| {
// return position before inserted text
if assoc == Assoc::Before {
if assoc.before() {
new_pos
} else {
// after text
Expand All @@ -464,10 +480,14 @@ impl ChangeSet {

/// Map a position through the changes.
///
/// `assoc` indicates which side to associate the position with. `Before` will keep the
/// `assoc` indicates which size to associate the position with. `Before` will keep the
/// position close to the character before, and will place it before insertions over that
/// range, or at that point. `After` will move it forward, placing it at the end of such
/// insertions.
///
/// The "stick" variants of these two indicate that `pos` should remain
//// unchanged for exact replacements. This is desirable for cursors but
//// undesirable for other things like diagnostics
pub fn map_pos(&self, mut pos: usize, assoc: Assoc) -> usize {
self.update_positions(once((&mut pos, assoc)));
pos
Expand Down

0 comments on commit c892d2a

Please sign in to comment.