diff options
author | Nathan Vegdahl | 2021-07-28 22:57:00 +0000 |
---|---|---|
committer | Nathan Vegdahl | 2021-07-28 22:57:00 +0000 |
commit | cd7302ffd35c4972e7b0414a31a08f4e3c672df5 (patch) | |
tree | f4e8cbcacd3c6b54d2261eae457ec41be54fc2ce /helix-core | |
parent | a873e719d5dd1816434c16bb02d74f1e00ca847a (diff) |
Enforce cursor/selection invariants in one place.
Rather than per-command like before.
Diffstat (limited to 'helix-core')
-rw-r--r-- | helix-core/src/selection.rs | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs index 9016462c..14c54295 100644 --- a/helix-core/src/selection.rs +++ b/helix-core/src/selection.rs @@ -455,13 +455,18 @@ impl Selection { for range in self.ranges.iter_mut() { *range = f(*range) } - self.normalize() } - /// A convenience short-cut for `transform(|r| r.min_width_1(text))`. - pub fn min_width_1(self, text: RopeSlice) -> Self { - self.transform(|r| r.min_width_1(text)) + // Ensures the selection adheres to the following invariants: + // 1. All ranges are grapheme aligned. + // 2. All ranges are at least 1 character wide, unless at the + // very end of the document. + // 3. Ranges are non-overlapping. + // 4. Ranges are sorted by their position in the text. + pub fn ensure_invariants(self, text: RopeSlice) -> Self { + self.transform(|r| r.min_width_1(text).grapheme_aligned(text)) + .normalize() } /// Transforms the selection into all of the left-side head positions, |