Preserve newlines and get closest cursor on reload, fixes #390

This commit is contained in:
Jeremy Soller 2025-10-09 12:28:39 -06:00
parent 801c7fa68c
commit 117f00337d
No known key found for this signature in database
GPG key ID: 670FDFB5428E05CA
3 changed files with 157 additions and 134 deletions

View file

@ -152,7 +152,30 @@ impl EditorTab {
// Replace everything in the buffer with the content from disk
editor.delete_range(cursor_start, cursor_end);
editor.insert_at(cursor_start, &file_content, None);
editor.set_cursor(cursor_start);
// Adjust cursor to closest position
let mut cursor = editor.cursor();
editor.with_buffer(|buffer| {
cursor.line = cursor.line.min(buffer.lines.len().saturating_sub(1));
cursor.index = if let Some(line) = buffer.lines.get(cursor.line) {
let mut closest = line.text().len();
for (i, _) in line.text().char_indices().rev() {
if i >= cursor.index {
closest = i;
} else {
// i < cursor.index
if cursor.index - i < closest - cursor.index {
closest = i;
}
break;
}
}
closest
} else {
0
}
});
editor.set_cursor(cursor);
editor.finish_change();
editor.set_changed(false);