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

@ -27,7 +27,6 @@ use cosmic_files::{
};
use cosmic_text::{Cursor, Edit, Family, Selection, SwashCache, SyntaxSystem, ViMode};
use serde::{Deserialize, Serialize};
use unicode_segmentation::UnicodeSegmentation;
use std::{
any::TypeId,
collections::HashMap,
@ -37,6 +36,7 @@ use std::{
sync::{Mutex, OnceLock},
};
use tokio::time;
use unicode_segmentation::UnicodeSegmentation;
use config::{AppTheme, CONFIG_VERSION, Config, ConfigState};
mod config;
@ -871,7 +871,7 @@ impl App {
for line in buffer.lines.iter() {
let text = line.text();
let mut last_whitespace = true;
// Count graphemes instead of Unicode scalar values for accurate character count
for grapheme in text.graphemes(true) {
character_count += 1;

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);