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 11, 2023
1 parent 546b92c commit 83ac2da
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 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::After, true);
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::After, true),
changes.map_pos(self.head, Assoc::After, true),
),
Ordering::Less => (
changes.map_pos(self.anchor, Assoc::After),
changes.map_pos(self.head, Assoc::Before),
changes.map_pos(self.anchor, Assoc::After, true),
changes.map_pos(self.head, Assoc::Before, true),
),
Ordering::Greater => (
changes.map_pos(self.anchor, Assoc::Before),
changes.map_pos(self.head, Assoc::After),
changes.map_pos(self.anchor, Assoc::Before, true),
changes.map_pos(self.head, Assoc::After, true),
),
};

Expand Down
27 changes: 15 additions & 12 deletions helix-core/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,10 @@ 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.
pub fn map_pos(&self, pos: usize, assoc: Assoc) -> usize {
///
/// `sticky` indicates 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, sticky: bool) -> usize {
use Operation::*;
let mut old_pos = 0;
let mut new_pos = 0;
Expand Down Expand Up @@ -368,7 +371,7 @@ 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 && sticky {
return new_pos + (pos - old_pos);
}
// at point or tracking before
Expand Down Expand Up @@ -659,21 +662,21 @@ mod test {
len_after: 10,
};

assert_eq!(cs.map_pos(0, Assoc::Before), 0); // before insert region
assert_eq!(cs.map_pos(4, Assoc::Before), 4); // at insert, track before
assert_eq!(cs.map_pos(4, Assoc::After), 6); // at insert, track after
assert_eq!(cs.map_pos(5, Assoc::Before), 7); // after insert region
assert_eq!(cs.map_pos(0, Assoc::Before, false), 0); // before insert region
assert_eq!(cs.map_pos(4, Assoc::Before, false), 4); // at insert, track before
assert_eq!(cs.map_pos(4, Assoc::After, false), 6); // at insert, track after
assert_eq!(cs.map_pos(5, Assoc::Before, false), 7); // after insert region

// maps deletes
let cs = ChangeSet {
changes: vec![Retain(4), Delete(4), Retain(4)],
len: 12,
len_after: 8,
};
assert_eq!(cs.map_pos(0, Assoc::Before), 0); // at start
assert_eq!(cs.map_pos(4, Assoc::Before), 4); // before a delete
assert_eq!(cs.map_pos(5, Assoc::Before), 4); // inside a delete
assert_eq!(cs.map_pos(5, Assoc::After), 4); // inside a delete
assert_eq!(cs.map_pos(0, Assoc::Before, false), 0); // at start
assert_eq!(cs.map_pos(4, Assoc::Before, false), 4); // before a delete
assert_eq!(cs.map_pos(5, Assoc::Before, false), 4); // inside a delete
assert_eq!(cs.map_pos(5, Assoc::After, false), 4); // inside a delete

// TODO: delete tracking

Expand All @@ -688,8 +691,8 @@ mod test {
len: 4,
len_after: 4,
};
assert_eq!(cs.map_pos(2, Assoc::Before), 2);
assert_eq!(cs.map_pos(2, Assoc::After), 2);
assert_eq!(cs.map_pos(2, Assoc::Before, false), 2);
assert_eq!(cs.map_pos(2, Assoc::After, false), 2);
}

#[test]
Expand Down
5 changes: 3 additions & 2 deletions helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -863,8 +863,9 @@ impl Document {
for diagnostic in &mut self.diagnostics {
use helix_core::Assoc;
let changes = transaction.changes();
diagnostic.range.start = changes.map_pos(diagnostic.range.start, Assoc::After);
diagnostic.range.end = changes.map_pos(diagnostic.range.end, Assoc::After);
diagnostic.range.start =
changes.map_pos(diagnostic.range.start, Assoc::After, false);
diagnostic.range.end = changes.map_pos(diagnostic.range.end, Assoc::After, false);
diagnostic.line = self.text.char_to_line(diagnostic.range.start);
}
self.diagnostics
Expand Down

0 comments on commit 83ac2da

Please sign in to comment.