aboutsummaryrefslogtreecommitdiff
path: root/helix-core
diff options
context:
space:
mode:
authorNathan Vegdahl2021-07-26 15:40:30 +0000
committerNathan Vegdahl2021-07-26 15:40:30 +0000
commit0883b4fae03343978e61fc377775d7ba93f86b40 (patch)
tree1a3d6fe100b39a2cae88842017e441aa3e96707a /helix-core
parentf96b8b769b3c7457935b5c02db870af97036f7b6 (diff)
Collect some common patterns into methods on `Range`.
Diffstat (limited to 'helix-core')
-rw-r--r--helix-core/src/movement.rs50
-rw-r--r--helix-core/src/selection.rs80
-rw-r--r--helix-core/src/textobject.rs15
3 files changed, 51 insertions, 94 deletions
diff --git a/helix-core/src/movement.rs b/helix-core/src/movement.rs
index 80d19501..4af82a1d 100644
--- a/helix-core/src/movement.rs
+++ b/helix-core/src/movement.rs
@@ -31,15 +31,7 @@ pub fn move_horizontally(
count: usize,
behaviour: Movement,
) -> Range {
- use Movement::Extend;
-
- // Shift back one grapheme if needed, to account for
- // the cursor being visually 1-width.
- let pos = if range.head > range.anchor {
- prev_grapheme_boundary(slice, range.head)
- } else {
- range.head
- };
+ let pos = range.cursor(slice);
// Compute the new position.
let new_pos = if dir == Direction::Backward {
@@ -49,11 +41,7 @@ pub fn move_horizontally(
};
// Compute the final new range.
- if behaviour == Extend {
- range.move_head(slice, new_pos, true)
- } else {
- Range::point(new_pos)
- }
+ range.put_cursor(slice, new_pos, behaviour == Movement::Extend)
}
pub fn move_vertically(
@@ -63,13 +51,7 @@ pub fn move_vertically(
count: usize,
behaviour: Movement,
) -> Range {
- // Shift back one grapheme if needed, to account for
- // the cursor being visually 1-width.
- let pos = if range.head > range.anchor {
- prev_grapheme_boundary(slice, range.head)
- } else {
- range.head
- };
+ let pos = range.cursor(slice);
// Compute the current position's 2d coordinates.
let Position { row, col } = coords_at_pos(slice, pos);
@@ -89,24 +71,14 @@ pub fn move_vertically(
)
};
- // Compute the new range according to the type of movement.
- match behaviour {
- Movement::Move => Range {
- anchor: new_pos,
- head: new_pos,
- horiz: Some(horiz),
- },
-
- Movement::Extend => {
- if slice.line(new_row).len_chars() > 0 {
- let mut new_range = range.move_head(slice, new_pos, true);
- new_range.horiz = Some(horiz);
- new_range
- } else {
- range
- }
- }
+ // Special-case to avoid moving to the end of the last non-empty line.
+ if behaviour == Movement::Extend && slice.line(new_row).len_chars() == 0 {
+ return range;
}
+
+ let mut new_range = range.put_cursor(slice, new_pos, behaviour == Movement::Extend);
+ new_range.horiz = Some(horiz);
+ new_range
}
pub fn move_next_word_start(slice: RopeSlice, range: Range, count: usize) -> Range {
@@ -153,7 +125,7 @@ fn word_move(slice: RopeSlice, range: Range, count: usize, target: WordMotionTar
// Prepare the range appropriately based on the target movement
// direction. This is addressing two things at once:
//
- // 1. 1-width range sementics.
+ // 1. Block-cursor semantics.
// 2. The anchor position being irrelevant to the output result.
#[allow(clippy::collapsible_else_if)] // Makes the structure clearer in this case.
let start_range = if is_prev {
diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs
index cef68212..247a69fe 100644
--- a/helix-core/src/selection.rs
+++ b/helix-core/src/selection.rs
@@ -73,16 +73,11 @@ impl Range {
std::cmp::max(self.anchor, self.head)
}
- /// The line number that the head is on (using 1-width semantics).
+ /// The line number that the block-cursor is on.
#[inline]
#[must_use]
- pub fn head_line(&self, text: RopeSlice) -> usize {
- let head = if self.anchor < self.head {
- prev_grapheme_boundary(text, self.head)
- } else {
- self.head
- };
- text.char_to_line(head)
+ pub fn cursor_line(&self, text: RopeSlice) -> usize {
+ text.char_to_line(self.cursor(text))
}
/// The (inclusive) range of lines that the range overlaps.
@@ -248,29 +243,44 @@ impl Range {
}
}
- /// Moves the head of the `Range` to `char_idx`, adjusting the anchor
- /// as needed to preserve 1-width range semantics.
+ /// Gets the left-side position of the block cursor.
+ #[must_use]
+ #[inline]
+ pub fn cursor(self, text: RopeSlice) -> usize {
+ if self.head > self.anchor {
+ prev_grapheme_boundary(text, self.head)
+ } else {
+ self.head
+ }
+ }
+
+ /// Puts the left side of the block cursor at `char_idx`, optionally extending.
///
- /// `block_cursor` specifies whether it should treat `char_idx` as a block
- /// cursor position or as a range-end position.
+ /// This follows "1-width" semantics, and therefore does a combination of anchor
+ /// and head moves to behave as if both the front and back of the range are 1-width
+ /// blocks
///
/// This method assumes that the range and `char_idx` are already properly
/// grapheme-aligned.
#[must_use]
#[inline]
- pub fn move_head(self, text: RopeSlice, char_idx: usize, block_cursor: bool) -> Range {
- let anchor = if self.head >= self.anchor && char_idx < self.anchor {
- next_grapheme_boundary(text, self.anchor)
- } else if self.head < self.anchor && char_idx >= self.anchor {
- prev_grapheme_boundary(text, self.anchor)
- } else {
- self.anchor
- };
+ pub fn put_cursor(self, text: RopeSlice, char_idx: usize, extend: bool) -> Range {
+ if extend {
+ let anchor = if self.head >= self.anchor && char_idx < self.anchor {
+ next_grapheme_boundary(text, self.anchor)
+ } else if self.head < self.anchor && char_idx >= self.anchor {
+ prev_grapheme_boundary(text, self.anchor)
+ } else {
+ self.anchor
+ };
- if block_cursor && anchor <= char_idx {
- Range::new(anchor, next_grapheme_boundary(text, char_idx))
+ if anchor <= char_idx {
+ Range::new(anchor, next_grapheme_boundary(text, char_idx))
+ } else {
+ Range::new(anchor, char_idx)
+ }
} else {
- Range::new(anchor, char_idx)
+ Range::point(char_idx)
}
}
@@ -309,18 +319,6 @@ impl Selection {
self.ranges[self.primary_index]
}
- #[must_use]
- pub fn cursor(&self, text: RopeSlice) -> usize {
- let range = self.primary();
-
- // For 1-width cursor semantics.
- if range.anchor < range.head {
- prev_grapheme_boundary(text, range.head).max(range.anchor)
- } else {
- range.head
- }
- }
-
/// Ensure selection containing only the primary selection.
pub fn into_single(self) -> Self {
if self.ranges.len() == 1 {
@@ -452,17 +450,9 @@ impl Selection {
}
/// Transforms the selection into all of the left-side head positions,
- /// using 1-width semantics.
+ /// using block-cursor semantics.
pub fn cursors(self, text: RopeSlice) -> Self {
- self.transform(|range| {
- // For 1-width cursor semantics.
- let pos = if range.anchor < range.head {
- prev_grapheme_boundary(text, range.head).max(range.anchor)
- } else {
- range.head
- };
- Range::new(pos, pos)
- })
+ self.transform(|range| Range::point(range.cursor(text)))
}
pub fn fragments<'a>(&'a self, text: RopeSlice<'a>) -> impl Iterator<Item = Cow<str>> + 'a {
diff --git a/helix-core/src/textobject.rs b/helix-core/src/textobject.rs
index ae18d7cf..e011c912 100644
--- a/helix-core/src/textobject.rs
+++ b/helix-core/src/textobject.rs
@@ -59,17 +59,12 @@ pub fn textobject_word(
textobject: TextObject,
_count: usize,
) -> Range {
- // For 1-width cursor semantics.
- let head = if range.head > range.anchor {
- prev_grapheme_boundary(slice, range.head)
- } else {
- range.head
- };
+ let pos = range.cursor(slice);
- let word_start = find_word_boundary(slice, head, Direction::Backward);
- let word_end = match slice.get_char(head).map(categorize_char) {
- None | Some(CharCategory::Whitespace | CharCategory::Eol) => head,
- _ => find_word_boundary(slice, head + 1, Direction::Forward),
+ let word_start = find_word_boundary(slice, pos, Direction::Backward);
+ let word_end = match slice.get_char(pos).map(categorize_char) {
+ None | Some(CharCategory::Whitespace | CharCategory::Eol) => pos,
+ _ => find_word_boundary(slice, pos + 1, Direction::Forward),
};
// Special case.