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 Feb 12, 2023
1 parent 546b92c commit d76e361
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion helix-core/src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl History {
let pos = current_revision
.transaction
.changes()
.map_pos(to, Assoc::After);
.map_pos(to, Assoc::AfterSticky);
Some(pos)
}

Expand Down
12 changes: 6 additions & 6 deletions helix-core/src/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,16 @@ impl Range {
use std::cmp::Ordering;
let (anchor, head) = match self.anchor.cmp(&self.head) {
Ordering::Equal => (
changes.map_pos(self.anchor, Assoc::After),
changes.map_pos(self.head, Assoc::After),
changes.map_pos(self.anchor, Assoc::AfterSticky),
changes.map_pos(self.head, Assoc::AfterSticky),
),
Ordering::Less => (
changes.map_pos(self.anchor, Assoc::After),
changes.map_pos(self.head, Assoc::Before),
changes.map_pos(self.anchor, Assoc::AfterSticky),
changes.map_pos(self.head, Assoc::BeforeSticky),
),
Ordering::Greater => (
changes.map_pos(self.anchor, Assoc::Before),
changes.map_pos(self.head, Assoc::After),
changes.map_pos(self.anchor, Assoc::BeforeSticky),
changes.map_pos(self.head, Assoc::AfterSticky),
),
};

Expand Down
25 changes: 22 additions & 3 deletions helix-core/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,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 @@ -329,6 +345,9 @@ impl ChangeSet {
/// 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 unchaged for exact replacements. This is
//// desirable for cursors but undesirable for other things like diagnostics
pub fn map_pos(&self, pos: usize, assoc: Assoc) -> usize {
use Operation::*;
let mut old_pos = 0;
Expand Down Expand Up @@ -368,11 +387,11 @@ impl ChangeSet {
if old_end > pos {
// 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() {
return new_pos;
} else {
// place to end of insert
Expand All @@ -383,7 +402,7 @@ impl ChangeSet {
// at insert point
if old_pos == pos {
// return position before inserted text
if assoc == Assoc::Before {
if assoc.before() {
return new_pos;
} else {
// after text
Expand Down

0 comments on commit d76e361

Please sign in to comment.