diff --git a/src/documents/document/tests/point.rs b/src/documents/document/tests/point.rs index 0bc3089..4435eaf 100644 --- a/src/documents/document/tests/point.rs +++ b/src/documents/document/tests/point.rs @@ -100,6 +100,14 @@ fn a_press_past_a_crlf_line_types_before_its_end(cx: &mut TestAppContext) { assert_eq!(super::text(&document, cx), "oneX\r\ntwo\r\n"); } +#[gpui::test] +fn a_press_in_code_keeping_a_crlf_lands_on_its_character(cx: &mut TestAppContext) { + let source = "`one\r\ntwo`\n"; + let (document, cx) = open(source, MarkdownMode::Visual, cx); + press_on(0, 2, MouseButton::Left, cx); + assert_eq!(opened_caret(&document, cx), "`on".len()); +} + #[gpui::test] fn a_secondary_press_moves_the_caret_and_opens_nothing(cx: &mut TestAppContext) { let (document, cx) = open(TEXT, MarkdownMode::Visual, cx); diff --git a/src/documents/tree/sources.rs b/src/documents/tree/sources.rs index 7f8f6e9..2e62fc9 100644 --- a/src/documents/tree/sources.rs +++ b/src/documents/tree/sources.rs @@ -319,10 +319,16 @@ fn aligned(drawn: &str, written: &str, escapes: Escapes) -> Vec<(usize, usize)> let Some(s) = source[..j].last() else { return out; }; - // A line's end, drawn or joined; CRLF is one end. if d == s.ch || (d.is_whitespace() && s.ends_line()) { j -= 1; - if s.ch == '\n' && j > 0 && source[j - 1].ch == '\r' && source[j - 1].raw == s.raw { + // A CRLF drawn as one char is one end. + let collapsed = !drawn[..byte].ends_with('\r'); + if collapsed + && s.ch == '\n' + && j > 0 + && source[j - 1].ch == '\r' + && source[j - 1].raw == s.raw + { j -= 1; } break source[j].at; @@ -398,6 +404,13 @@ mod tests { assert_eq!(under(source, "two"), source.find("two").unwrap()); } + #[test] + fn a_kept_crlf_reads_back_char_for_char() { + let source = "`one\r\ntwo`\n"; + assert_eq!(under(source, "ne"), source.find("ne").unwrap()); + assert_eq!(under(source, "two"), source.find("two").unwrap()); + } + #[test] fn joins_escapes_and_indents_read_back() { let source = "a \\*star\\* x\n y";