documents: a caret inside CRLF reads as its line's end

The code editor splits lines on LF only, so End and Right can leave the
caret between CR and LF. line_start and line_end now read that spot as
the end of its line, so insert_table keeps CRLF around a new table.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
ZacharyZhang-NYandClaude Opus 5.5 committed 2026-10-14 15:44:59 -04:00
1 parent 07fee552c3
commit ce015e7b7b
2 files changed
+26

No files matched your search

+8
View File
@@ -251,11 +251,19 @@ fn path<'a>(text: &str, root: &'a Node, at: usize) -> Vec<&'a Node> {
path
}
/// `at`, before the CR when it splits a CRLF.
fn settled(text: &str, at: usize) -> usize {
let split = text[..at].ends_with('\r') && text[at..].starts_with('\n');
if split { at - 1 } else { at }
}
pub(super) fn line_start(text: &str, at: usize) -> usize {
let at = settled(text, at);
text[..at].rfind(['\n', '\r']).map_or(0, |at| at + 1)
}
pub(super) fn line_end(text: &str, at: usize) -> usize {
let at = settled(text, at);
at + text[at..].find(['\n', '\r']).unwrap_or(text.len() - at)
}
+18
View File
@@ -69,6 +69,24 @@ fn a_new_table_goes_after_the_caret_line(cx: &mut TestAppContext) {
);
}
#[gpui::test]
fn a_new_table_after_end_keeps_crlf(cx: &mut TestAppContext) {
let (document, cx) = open("Intro\r\nNext\r\n", MarkdownMode::Source, cx);
let editor = document.read_with(cx, |document, _| document.editor().clone());
let focus = editor.read_with(cx, gpui::Focusable::focus_handle);
cx.update(|window, cx| window.focus(&focus, cx));
cx.simulate_keystrokes("end");
let caret = editor.read_with(cx, |editor, _| editor.primary().range().start);
assert_eq!(caret, 6, "End lands between CR and LF");
document.update(cx, |document, cx| {
document.insert_table(cx).expect("inserted")
});
assert_eq!(
text(&document, cx),
"Intro\r\n\r\n| | | |\r\n| ---- | ---- | ---- |\r\n| | | |\r\n| | | |\r\n\r\nNext\r\n"
);
}
#[gpui::test]
fn a_match_in_visual_opens_its_block_selected(cx: &mut TestAppContext) {
let (document, cx) = open("# A\n\nfind me here\n", MarkdownMode::Visual, cx);