nomad / crop

🌾 A pretty fast text rope
https://crates.io/crates/crop
MIT License
261 stars 13 forks source link

Make trailing line breaks count as lines #21

Open QnnOkabayashi opened 1 month ago

QnnOkabayashi commented 1 month ago

Related: https://github.com/nomad/crop/issues/20

This makes the concept of "lines" and line counts align with what we would expect when displaying text in a text editor, e.g. the empty string is 1 line, the string "foo\n" is 2 lines, "foo\nbar" is 2 lines, and so on. The main difference is that trailing newlines now count as another line.

noib3 commented 1 month ago

LGTM (once Clippy passes), but I wouldn't merge it without the corresponding changes to the Iterator/DoubleEndedIterator impls of RawLines and Lines.

For example, this test should pass:

#[test]
fn lines_iterator_len_mismatch() {
    let r = Rope::from("foo\n");
    assert_eq!(r.lines().len(), 2);

    let mut lines = r.lines();
    assert_eq!(lines.next().unwrap(), "foo");
    assert_eq!(lines.next().unwrap(), "");
    assert_eq!(lines.next(), None);

    let mut lines = r.lines();
    assert_eq!(lines.next_back().unwrap(), "");
    assert_eq!(lines.next_back().unwrap(), "foo");
    assert_eq!(lines.next_back(), None);

}