Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add at_or_next and at_or_prev to Cursor #1019

Merged
merged 1 commit into from Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 2 additions & 9 deletions rust/core-lib/src/linewrap.rs
Expand Up @@ -321,12 +321,7 @@ fn compute_rewrap_width(
let mut builder = BreakBuilder::new();
let mut pos = start;
let mut break_cursor = Cursor::new(&breaks, end);
// TODO: factor this into `at_or_next` method on cursor.
let mut next_break = if break_cursor.is_boundary::<BreaksBaseMetric>() {
Some(end)
} else {
break_cursor.next::<BreaksBaseMetric>()
};
let mut next_break = break_cursor.at_or_next::<BreaksBaseMetric>();
loop {
// iterate newly computed breaks and existing breaks until they converge
if let Some(new_next) = ctx.wrap_one_line(pos) {
Expand Down Expand Up @@ -379,9 +374,7 @@ pub fn rewrap_width(
// Find a point earlier than any possible breaks change. For simplicity, this is the
// beginning of the paragraph, but going back two breaks would be better.
let mut cursor = Cursor::new(&text, start);
if !cursor.is_boundary::<LinesMetric>() {
start = cursor.prev::<LinesMetric>().unwrap_or(0);
}
start = cursor.at_or_prev::<LinesMetric>().unwrap_or(0);

let new_breaks = compute_rewrap_width(
text,
Expand Down
35 changes: 35 additions & 0 deletions rust/rope/src/tree.rs
Expand Up @@ -670,6 +670,26 @@ impl<'a, N: NodeInfo> Cursor<'a, N> {
}
}

/// Returns the current position if it is a boundary in this [`Metric`],
/// else behaves like [`next`](#method.next).
pub fn at_or_next<M: Metric<N>>(&mut self) -> Option<usize> {
if self.is_boundary::<M>() {
Some(self.pos())
} else {
self.next::<M>()
}
}

/// Returns the current position if it is a boundary in this [`Metric`],
/// else behaves like [`prev`](#method.prev).
pub fn at_or_prev<M: Metric<N>>(&mut self) -> Option<usize> {
if self.is_boundary::<M>() {
Some(self.pos())
} else {
self.prev::<M>()
}
}

/// Returns an iterator with this cursor over the given [`Metric`].
///
/// # Examples:
Expand Down Expand Up @@ -948,4 +968,19 @@ mod test {
}
}

#[test]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No test for at_or_prev?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basic functionality for both is included in this test case, didn't feel this was complicated enough to warrant serious energy.

fn at_or_next() {
let text: Rope = "this\nis\nalil\nstring".into();
let mut cursor = Cursor::new(&text, 0);
assert_eq!(cursor.at_or_next::<LinesMetric>(), Some(0));
assert_eq!(cursor.at_or_next::<LinesMetric>(), Some(0));
cursor.set(1);
assert_eq!(cursor.at_or_next::<LinesMetric>(), Some(5));
assert_eq!(cursor.at_or_prev::<LinesMetric>(), Some(5));
cursor.set(6);
assert_eq!(cursor.at_or_prev::<LinesMetric>(), Some(5));
cursor.set(6);
assert_eq!(cursor.at_or_next::<LinesMetric>(), Some(8));
assert_eq!(cursor.at_or_next::<LinesMetric>(), Some(8));
}
}