From 7179c6be204fa92c4b36c03cb9efaf6d06318bbe Mon Sep 17 00:00:00 2001 From: Paul-Vincent Roll Date: Fri, 9 Oct 2026 03:33:23 +0200 Subject: [PATCH 1/2] fix(markdown): wrap at Unicode line and grapheme boundaries --- Cargo.lock | 11 +- Cargo.toml | 7 +- src/tui/render/markdown.rs | 45 +++ src/tui/render/markdown/wrapping.rs | 504 +++++++++++++++++++++------- 4 files changed, 442 insertions(+), 125 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7866ef1f..6119b430 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2466,6 +2466,8 @@ dependencies = [ "tree-sitter-toml-ng", "tree-sitter-typescript", "tree-sitter-yaml", + "unicode-linebreak", + "unicode-segmentation", "unicode-width", "zeroize", ] @@ -2610,11 +2612,12 @@ dependencies = [ [[package]] name = "notema-textarea" version = "0.9.2" -source = "git+https://github.com/paviro/notema-textarea?rev=cbf1b84#cbf1b8448599c981a398975c8bc4581286f019e8" +source = "git+https://github.com/paviro/notema-textarea?rev=2449a7e8b91cc083a9d9bb91b1e1d33375fbcd9e#2449a7e8b91cc083a9d9bb91b1e1d33375fbcd9e" dependencies = [ "ratatui-core", "ratatui-crossterm", "ratatui-widgets", + "unicode-linebreak", "unicode-segmentation", "unicode-width", ] @@ -4830,6 +4833,12 @@ version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" +[[package]] +name = "unicode-linebreak" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b09c83c3c29d37506a3e260c08c03743a6bb66a9cd432c6934ab501a190571f" + [[package]] name = "unicode-segmentation" version = "1.13.3" diff --git a/Cargo.toml b/Cargo.toml index 909ddc5e..a48ba27a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,6 +52,7 @@ pkg-config = "0.3" rand = "0.10" ratatui = "0.30" ratatui-image = { version = "11", default-features = false, features = ["crossterm"] } +# Maintained on the notema-textarea branch; pinned to a tested commit. # Fork of ratatui-textarea 0.9.2 the editor depends on: public accessors # (scroll_offset, horizontal_scroll_offset, screen_line_count, cursor_at_screen) # for its scrollbar, scroll-aware mouse mapping, and native-cursor placement in @@ -59,7 +60,7 @@ ratatui-image = { version = "11", default-features = false, features = ["crosste # highlighter's base styling layer. Upstream keeps these pub(crate). Also # set_top_padding and layout_for, so the editor can frame the body like the viewer, # and set_glyph_substitutions for the search field's chips. -ratatui-textarea = { package = "notema-textarea", git = "https://github.com/paviro/notema-textarea", rev = "cbf1b84", features = ["crossterm"] } +ratatui-textarea = { package = "notema-textarea", git = "https://github.com/paviro/notema-textarea", rev = "2449a7e8b91cc083a9d9bb91b1e1d33375fbcd9e", features = ["crossterm"] } pulldown-cmark = { version = "0.13.4", default-features = false } rayon = "1" regex = "1" @@ -90,6 +91,8 @@ tree-sitter-toml-ng = "0.7" tree-sitter-typescript = "0.23" tree-sitter-yaml = "0.7" tzf-rs = { version = "1", default-features = false, features = ["bundled"] } +unicode-linebreak = "0.1.5" +unicode-segmentation = "1.13.3" unicode-width = "0.2" ureq = { version = "3", default-features = false } zbus = "5" @@ -181,6 +184,8 @@ tree-sitter-swift.workspace = true tree-sitter-toml-ng.workspace = true tree-sitter-typescript.workspace = true tree-sitter-yaml.workspace = true +unicode-linebreak.workspace = true +unicode-segmentation.workspace = true unicode-width.workspace = true zeroize.workspace = true terminal-colorsaurus.workspace = true diff --git a/src/tui/render/markdown.rs b/src/tui/render/markdown.rs index d5a4171a..9e8057ac 100644 --- a/src/tui/render/markdown.rs +++ b/src/tui/render/markdown.rs @@ -884,6 +884,51 @@ mod wrap_tests { .lines } + #[test] + fn unicode_links_keep_cell_ranges_after_wrapping_and_prefixes() { + let rendered = render_text_chunk( + &Theme::terminal_default(), + "> [πŸ‘©πŸ½β€πŸ’»πŸ‡©πŸ‡ͺ](https://example.com)", + 4, + false, + None, + false, + None, + ); + assert_eq!(rendered.links.len(), 2); + for hit in &rendered.links { + assert_eq!((hit.start, hit.end), (2, 4)); + assert_eq!(hit.group, rendered.links[0].group); + assert_eq!(rendered.lines[hit.line].width(), 4); + } + } + + #[test] + fn unicode_code_wraps_keep_graphemes_and_indentation() { + let rows = render_lines("```\n πŸ‘©πŸ½β€πŸ’»πŸ‡©πŸ‡ͺ\n```", 5); + let rail = Theme::terminal_default() + .glyphs() + .markdown + .code_rail + .clone(); + let body: Vec<_> = rows + .iter() + .map(text) + .filter(|row| row.starts_with(&rail)) + .collect(); + assert_eq!(body, [format!("{rail} πŸ‘©πŸ½β€πŸ’»"), format!("{rail}πŸ‡©πŸ‡ͺ")]); + } + + #[test] + fn unicode_table_columns_stay_aligned() { + let rows = render_lines("| Emoji | Value |\n| --- | ---: |\n| πŸ‘©πŸ½β€πŸ’»πŸ‡©πŸ‡ͺ | ❀️ |", 18); + let table: Vec<_> = rows.iter().filter(|row| !text(row).is_empty()).collect(); + let width = table[0].width(); + assert!(width <= 18); + assert!(table.iter().all(|row| row.width() == width)); + assert!(table.iter().any(|row| text(row).contains("πŸ‘©πŸ½β€πŸ’»"))); + } + #[test] fn wraps_at_words_instead_of_the_panel_edge() { let wrapped = wrap_line(Line::from("alpha beta"), 7); diff --git a/src/tui/render/markdown/wrapping.rs b/src/tui/render/markdown/wrapping.rs index 07b03e54..b4743343 100644 --- a/src/tui/render/markdown/wrapping.rs +++ b/src/tui/render/markdown/wrapping.rs @@ -1,8 +1,12 @@ +use std::ops::Range; + use ratatui::{ style::Style, text::{Line, Span}, }; -use unicode_width::UnicodeWidthChar; +use unicode_linebreak::{BreakClass, BreakOpportunity, break_property, linebreaks}; +use unicode_segmentation::UnicodeSegmentation; +use unicode_width::UnicodeWidthStr; use super::RichSpan; @@ -11,58 +15,68 @@ pub(super) struct WrappedLine { pub(super) links: Vec<(usize, usize, usize)>, } -#[derive(Clone, Copy)] -struct StyledCharacter { - character: char, +struct StyledGrapheme<'a> { + text: &'a str, + end: usize, width: usize, style: Style, link: Option, } -/// Wrap link-tagged runs to `width`, returning display lines that carry both the -/// collapsed spans and the link regions the wrap produced. `hard_wrap` breaks -/// anywhere (rules, code); otherwise it breaks at whitespace, splitting a lone -/// token only when it alone exceeds the width. +/// Wrap styled text without separating a grapheme, including across input spans. +/// Prose uses Unicode line breaks; code and rules use grapheme boundaries. pub(super) fn wrap_rich(spans: Vec, width: usize, hard_wrap: bool) -> Vec { - let mut characters = Vec::new(); - for span in spans { - for character in span.content.chars() { - characters.push(StyledCharacter { - character, - width: character.width().unwrap_or(0), - style: span.style, - link: span.link, - }); - } - } - let char_lines = if hard_wrap { - hard_wrap_chars(characters, width) + let text: String = spans.iter().map(|span| span.content.as_str()).collect(); + let mut runs = spans.iter(); + let mut run = runs.next(); + let mut run_end = run.map_or(0, |span| span.content.len()); + let graphemes: Vec<_> = text + .grapheme_indices(true) + .map(|(start, text)| { + while start >= run_end { + run = runs.next(); + run_end += run.map_or(0, |span| span.content.len()); + } + let run = run.expect("each grapheme belongs to a source span"); + StyledGrapheme { + text, + end: start + text.len(), + width: text.width(), + style: run.style, + link: run.link, + } + }) + .collect(); + let width = width.max(1); + let mut rows = Vec::new(); + if hard_wrap { + split_graphemes(&graphemes, 0..graphemes.len(), width, &mut rows); } else { - soft_wrap_chars(characters, width) - }; - char_lines.iter().map(|line| finalize_line(line)).collect() + rows = prose_rows(&text, &graphemes, width); + } + if rows.is_empty() { + rows.push(0..0); + } + rows.into_iter() + .map(|mut row| { + if !hard_wrap { + while row.end > row.start && is_break_space(graphemes[row.end - 1].text) { + row.end -= 1; + } + } + finalize_line(&graphemes[row]) + }) + .collect() } -/// Word-wrap the table path, which has no link semantics; the surviving caller of -/// the plain-`Line` shape. Preserves the line-level style/alignment tables set. +/// Wrap a table cell while retaining its line-level style and alignment. pub(super) fn wrap_line(line: Line<'static>, width: usize) -> Vec> { let style = line.style; let alignment = line.alignment; - let mut characters = Vec::new(); - for span in line.spans { - for character in span.content.chars() { - characters.push(StyledCharacter { - character, - width: character.width().unwrap_or(0), - style: span.style, - link: None, - }); - } - } - soft_wrap_chars(characters, width) - .iter() - .map(|chars| { - let mut line = Line::from(finalize_line(chars).spans); + wrap_rich(rich_from_line(line), width, false) + .into_iter() + .map(|wrapped| { + let mut line = Line::from(wrapped.spans); line.style = style; line.alignment = alignment; line @@ -70,120 +84,174 @@ pub(super) fn wrap_line(line: Line<'static>, width: usize) -> Vec> .collect() } -fn soft_wrap_chars(characters: Vec, width: usize) -> Vec> { - let mut wrapped: Vec> = vec![Vec::new()]; - let mut used = 0usize; - let mut pending_whitespace: Option<&[StyledCharacter]> = None; - let mut cursor = 0; +fn is_break_space(text: &str) -> bool { + text.chars().all(|c| { + c == '\t' + || matches!( + break_property(c as u32), + BreakClass::Space + | BreakClass::Mandatory + | BreakClass::CarriageReturn + | BreakClass::LineFeed + | BreakClass::NextLine + ) + }) +} + +fn cells(graphemes: &[StyledGrapheme<'_>]) -> usize { + graphemes.iter().map(|g| g.width).sum() +} - while cursor < characters.len() { - let whitespace = characters[cursor].character.is_whitespace(); - let end = characters[cursor..] - .iter() - .position(|character| character.character.is_whitespace() != whitespace) - .map_or(characters.len(), |offset| cursor + offset); - let token = &characters[cursor..end]; - - if whitespace { - pending_whitespace = Some(token); - cursor = end; +fn prose_rows(text: &str, graphemes: &[StyledGrapheme<'_>], width: usize) -> Vec> { + let mut rows = Vec::new(); + let mut cursor = 0; + let mut chunk_start = 0; + let mut row_start = 0; + let mut row_width = 0; + for (byte_end, opportunity) in linebreaks(text) { + while cursor < graphemes.len() && graphemes[cursor].end < byte_end { + cursor += 1; + } + if cursor == graphemes.len() || graphemes[cursor].end != byte_end { continue; } + let end = cursor + 1; + let mut content_end = end; + while content_end > chunk_start && is_break_space(graphemes[content_end - 1].text) { + content_end -= 1; + } + let content_width = cells(&graphemes[chunk_start..content_end]); + if row_width + content_width > width && chunk_start > row_start { + rows.push(row_start..chunk_start); + row_start = chunk_start; + row_width = 0; + } + if content_width > width { + split_word(graphemes, chunk_start..content_end, width, &mut rows); + let last = rows.pop().expect("an oversized segment produces a row"); + row_start = last.start; + row_width = cells(&graphemes[row_start..end]); + } else { + row_width += cells(&graphemes[chunk_start..end]); + } + chunk_start = end; + if opportunity == BreakOpportunity::Mandatory { + rows.push(row_start..end); + row_start = end; + row_width = 0; + } + } + if row_start < chunk_start { + rows.push(row_start..chunk_start); + } + rows +} - let whitespace_width = pending_whitespace.map_or(0, token_width); - let word_width = token_width(token); - if used > 0 - && used - .saturating_add(whitespace_width) - .saturating_add(word_width) - > width - { - wrapped.push(Vec::new()); - used = 0; - } else if let Some(whitespace) = pending_whitespace { - wrapped - .last_mut() - .expect("wrapped output always has a line") - .extend_from_slice(whitespace); - used = used.saturating_add(whitespace_width); - } - pending_whitespace = None; - - for character in token { - if used > 0 && used.saturating_add(character.width) > width { - wrapped.push(Vec::new()); - used = 0; +fn split_word( + graphemes: &[StyledGrapheme<'_>], + range: Range, + width: usize, + rows: &mut Vec>, +) { + let mut tail = range.end; + for index in range.clone().rev() { + let punctuation = graphemes[index].text.chars().next().is_some_and(|c| { + matches!( + break_property(c as u32), + BreakClass::ClosePunctuation + | BreakClass::CloseParenthesis + | BreakClass::Exclamation + | BreakClass::InfixSeparator + | BreakClass::Inseparable + | BreakClass::NonStarter + | BreakClass::Quotation + | BreakClass::Postfix + | BreakClass::Symbol + ) + }); + if punctuation { + tail = index; + } else { + if tail < range.end && !graphemes[index].text.chars().all(char::is_whitespace) { + tail = index; } - wrapped - .last_mut() - .expect("wrapped output always has a line") - .push(*character); - used = used.saturating_add(character.width); + break; } - cursor = end; } - - wrapped + if tail == range.end || cells(&graphemes[tail..range.end]) > width { + split_graphemes(graphemes, range, width, rows); + return; + } + let first = rows.len(); + split_graphemes(graphemes, range.start..tail, width, rows); + if rows.len() > first { + let last = rows.last_mut().expect("the prefix produced a row"); + if cells(&graphemes[last.start..range.end]) <= width { + last.end = range.end; + return; + } + } + rows.push(tail..range.end); } -fn hard_wrap_chars(characters: Vec, width: usize) -> Vec> { - let mut wrapped: Vec> = vec![Vec::new()]; - let mut used = 0usize; - for character in characters { - if used > 0 && used.saturating_add(character.width) > width { - wrapped.push(Vec::new()); +fn split_graphemes( + graphemes: &[StyledGrapheme<'_>], + range: Range, + width: usize, + rows: &mut Vec>, +) { + let mut start = range.start; + let mut used = 0; + for index in range.clone() { + if index > start && used + graphemes[index].width > width { + rows.push(start..index); + start = index; used = 0; } - wrapped - .last_mut() - .expect("wrapped output always has a line") - .push(character); - used = used.saturating_add(character.width); + used += graphemes[index].width; + } + if start < range.end { + rows.push(start..range.end); } - wrapped } -/// Collapse a wrapped display line's characters into style-merged spans while -/// recording each contiguous same-id link run's column span. -fn finalize_line(characters: &[StyledCharacter]) -> WrappedLine { +fn finalize_line(graphemes: &[StyledGrapheme<'_>]) -> WrappedLine { let mut spans: Vec> = Vec::new(); let mut links = Vec::new(); - let mut column = 0usize; - let mut run: Option<(usize, usize)> = None; - for character in characters { + let mut column = 0; + let mut run = None; + let mut previous_link = None; + for grapheme in graphemes { if let Some(span) = spans.last_mut() - && span.style == character.style + && span.style == grapheme.style + && previous_link == grapheme.link { - span.content.to_mut().push(character.character); + span.content.to_mut().push_str(grapheme.text); } else { - spans.push(Span::styled( - character.character.to_string(), - character.style, - )); + spans.push(Span::styled(grapheme.text.to_owned(), grapheme.style)); } - match (run, character.link) { + previous_link = grapheme.link; + match (run, grapheme.link) { (Some((id, _)), Some(current)) if id == current => {} (Some((id, start)), _) => { - links.push((start, column, id)); - run = character.link.map(|current| (current, column)); + if start < column { + links.push((start, column, id)); + } + run = grapheme.link.map(|current| (current, column)); } (None, Some(current)) => run = Some((current, column)), (None, None) => {} } - column = column.saturating_add(character.width); + column += grapheme.width; } - if let Some((id, start)) = run { + if let Some((id, start)) = run + && start < column + { links.push((start, column, id)); } WrappedLine { spans, links } } -fn token_width(token: &[StyledCharacter]) -> usize { - token.iter().fold(0usize, |width, character| { - width.saturating_add(character.width) - }) -} - /// Convert a plain `Line` (code, rules, stacked-table rows β€” none carry link /// semantics) into the renderer's tagged runs for emission. pub(super) fn rich_from_line(line: Line<'static>) -> Vec { @@ -196,3 +264,193 @@ pub(super) fn rich_from_line(line: Line<'static>) -> Vec { }) .collect() } + +#[cfg(test)] +mod tests { + use super::*; + use ratatui::{buffer::Buffer, layout::Rect, style::Modifier, widgets::Widget}; + + fn visible(text: &str, width: usize) -> Vec { + wrap_rich( + vec![RichSpan { + content: text.to_owned(), + style: Style::default(), + link: None, + }], + width, + false, + ) + .into_iter() + .map(|line| { + line.spans + .iter() + .map(|span| span.content.as_ref()) + .collect() + }) + .collect() + } + + #[test] + fn unicode_breaks_and_punctuation_match_editor_policy() { + for (text, width, expected) in [ + ("a (word) end", 7, vec!["a", "(word)", "end"]), + ("δ½ ε₯½οΌŒδΈ–η•Œγ€‚", 6, vec!["δ½ ε₯½οΌŒ", "δΈ–η•Œγ€‚"]), + ("δ½ ε₯½οΌŒδΈ–η•Œγ€‚", 4, vec!["δ½ ", "ε₯½οΌŒ", "δΈ–", "η•Œγ€‚"]), + ("hello,", 5, vec!["hell", "o,"]), + ("abcdef?!", 3, vec!["abc", "de", "f?!"]), + ("abcdef?! next", 6, vec!["abcde", "f?!", "next"]), + ("abcde\u{301}!", 5, vec!["abcd", "e\u{301}!"]), + ("hello,", 1, vec!["h", "e", "l", "l", "o", ","]), + ("abc?!", 2, vec!["ab", "c?", "!"]), + ("hello world", 5, vec!["hello", "world"]), + ("", 0, vec![""]), + (" ", 0, vec![""]), + ] { + assert_eq!(visible(text, width), expected, "{text:?} at {width}"); + } + for punctuation in [ + ",", ".", "!", "?", ":", ";", "…", "?!", "...", ")", "]", "}", "\"", "”", "’", "。」", + ] { + let word = format!("word{punctuation}"); + assert_eq!( + visible(&format!("a {word} next"), word.width().max(6)), + ["a".to_owned(), word, "next".to_owned()] + ); + } + } + + #[test] + fn nonbreaking_spaces_and_explicit_breaks_survive() { + for joined in ["ab\u{a0}cd", "ab\u{202f}cd", "ab\u{2060}cd"] { + assert_eq!(visible(&format!("x {joined}"), 5), ["x", joined]); + } + assert_eq!(visible("ab\u{200b}cd", 2), ["ab\u{200b}", "cd"]); + for separator in ["\n", "\r\n", "\u{85}", "\u{2028}", "\u{2029}"] { + assert_eq!( + visible(&format!("one{separator}two{separator}"), 80), + ["one", "two"] + ); + } + } + + #[test] + fn style_boundaries_cannot_split_a_grapheme_or_its_link() { + let style = Style::new().add_modifier(Modifier::BOLD); + let rows = wrap_rich( + vec![ + RichSpan { + content: "aπŸ‘©".into(), + style, + link: Some(0), + }, + RichSpan { + content: "πŸ½β€πŸ’»".into(), + style: Style::default(), + link: Some(1), + }, + RichSpan { + content: "b".into(), + style: Style::default(), + link: Some(1), + }, + ], + 3, + false, + ); + assert_eq!(rows.len(), 2); + assert_eq!(rows[0].links, [(0, 3, 0)]); + assert_eq!(rows[1].links, [(0, 1, 1)]); + let area = Rect::new(0, 0, 3, 1); + let mut buffer = Buffer::empty(area); + Line::from(rows[0].spans.clone()).render(area, &mut buffer); + assert_eq!(buffer[(1, 0)].symbol(), "πŸ‘©πŸ½β€πŸ’»"); + assert_eq!(buffer[(1, 0)].style().add_modifier, Modifier::BOLD); + } + + #[test] + fn same_style_different_links_keep_separate_spans() { + let rows = wrap_rich( + vec![ + RichSpan { + content: "πŸ‘©πŸ½β€πŸ’»".into(), + style: Style::default(), + link: Some(0), + }, + RichSpan { + content: "πŸ‡©πŸ‡ͺ".into(), + style: Style::default(), + link: Some(1), + }, + ], + 4, + false, + ); + assert_eq!(rows[0].links, [(0, 2, 0), (2, 4, 1)]); + assert_eq!(rows[0].spans.len(), 2); + } + + #[test] + fn hard_wrapping_preserves_text_and_all_wraps_preserve_clusters() { + for text in [ + "a e\u{301}! πŸ‘©πŸ½β€πŸ’» πŸ‡©πŸ‡ͺ end", + "❀\u{fe0f} 1️⃣!", + "δ½ ε₯½οΌŒδΈ–η•Œγ€‚", + " x ", + ] { + let boundaries: Vec<_> = text + .grapheme_indices(true) + .map(|(i, _)| i) + .chain([text.len()]) + .collect(); + for width in 0..=20 { + for hard in [false, true] { + let rows = wrap_rich( + vec![RichSpan { + content: text.into(), + style: Style::default(), + link: None, + }], + width, + hard, + ); + let mut offset = 0; + for row in rows { + let rendered: String = + row.spans.iter().map(|s| s.content.as_ref()).collect(); + if !hard { + // Soft wrapping may consume spaces after the preceding row. + while !text[offset..].starts_with(&rendered) + && text[offset..].starts_with(' ') + { + offset += 1; + } + } + assert!(text[offset..].starts_with(&rendered)); + assert!(boundaries.contains(&offset)); + offset += rendered.len(); + assert!(boundaries.contains(&offset)); + assert!( + rendered.width() <= width.max(1) + || rendered.graphemes(true).count() == 1 + ); + } + if hard { + assert_eq!(offset, text.len()); + } + } + } + } + } + + #[test] + fn table_cell_retains_alignment_and_grapheme_width() { + let style = Style::new().bold(); + let rows = wrap_line(Line::from("πŸ‘©πŸ½β€πŸ’»πŸ‡©πŸ‡ͺ").style(style).right_aligned(), 2); + assert_eq!(rows.len(), 2); + for row in rows { + assert_eq!(row.width(), 2); + assert_eq!(row.style, style); + assert_eq!(row.alignment, Some(ratatui::layout::Alignment::Right)); + } + } +} From 2958cb5a16959ab86d105327ede6d01ff59f10a7 Mon Sep 17 00:00:00 2001 From: Paul-Vincent Roll Date: Fri, 9 Oct 2026 03:56:18 +0200 Subject: [PATCH 2/2] refactor(tui): adopt upstream textarea viewport APIs --- Cargo.lock | 4 ++-- Cargo.toml | 14 ++++-------- src/tui/editor_state.rs | 41 ++++++++++++++++++++++++++++++--- src/tui/render/editor.rs | 4 ++-- src/tui/text_input.rs | 49 ++++++++++++++++++++++++++++++++-------- 5 files changed, 86 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6119b430..c66b3731 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2611,8 +2611,8 @@ dependencies = [ [[package]] name = "notema-textarea" -version = "0.9.2" -source = "git+https://github.com/paviro/notema-textarea?rev=2449a7e8b91cc083a9d9bb91b1e1d33375fbcd9e#2449a7e8b91cc083a9d9bb91b1e1d33375fbcd9e" +version = "0.9.3" +source = "git+https://github.com/paviro/notema-textarea?rev=6e7fa5139d1d306b3043880e926394351dfcd13b#6e7fa5139d1d306b3043880e926394351dfcd13b" dependencies = [ "ratatui-core", "ratatui-crossterm", diff --git a/Cargo.toml b/Cargo.toml index a48ba27a..13b80761 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,15 +52,11 @@ pkg-config = "0.3" rand = "0.10" ratatui = "0.30" ratatui-image = { version = "11", default-features = false, features = ["crossterm"] } -# Maintained on the notema-textarea branch; pinned to a tested commit. -# Fork of ratatui-textarea 0.9.2 the editor depends on: public accessors -# (scroll_offset, horizontal_scroll_offset, screen_line_count, cursor_at_screen) -# for its scrollbar, scroll-aware mouse mapping, and native-cursor placement in -# horizontally-scrolling single-line fields, plus set_syntax_spans for the markdown -# highlighter's base styling layer. Upstream keeps these pub(crate). Also -# set_top_padding and layout_for, so the editor can frame the body like the viewer, -# and set_glyph_substitutions for the search field's chips. -ratatui-textarea = { package = "notema-textarea", git = "https://github.com/paviro/notema-textarea", rev = "2449a7e8b91cc083a9d9bb91b1e1d33375fbcd9e", features = ["crossterm"] } +# Fork of ratatui-textarea 0.9.3, pinned to a tested commit. Upstream supplies +# scroll_offset and screen_to_data; the fork adds padding-aware mouse mapping, +# screen-line counts, pre-render layout, syntax spans, glyph substitutions, +# Unicode wrapping, and grapheme cursor geometry. +ratatui-textarea = { package = "notema-textarea", git = "https://github.com/paviro/notema-textarea", rev = "6e7fa5139d1d306b3043880e926394351dfcd13b", features = ["crossterm"] } pulldown-cmark = { version = "0.13.4", default-features = false } rayon = "1" regex = "1" diff --git a/src/tui/editor_state.rs b/src/tui/editor_state.rs index aac83f6f..e3799207 100644 --- a/src/tui/editor_state.rs +++ b/src/tui/editor_state.rs @@ -185,9 +185,9 @@ impl EntryEditor { { return None; } - let screen_row = self.textarea.scroll_offset() as usize + (row - rect.y) as usize; + let screen_row = self.textarea.scroll_offset().0 as usize + (row - rect.y) as usize; let screen_col = (col - rect.x) as usize; - let DataCursor(line, column) = self.textarea.cursor_at_screen(screen_row, screen_col); + let DataCursor(line, column) = self.textarea.screen_to_data(screen_row, screen_col); Some((line as u16, column as u16)) } @@ -197,7 +197,7 @@ impl EntryEditor { pub(crate) fn scroll_lines(&mut self, delta: i16) { let height = self.text_rect.height as usize; let max_top = self.textarea.screen_line_count().saturating_sub(height) as i64; - let cur = self.textarea.scroll_offset() as i64; + let cur = self.textarea.scroll_offset().0 as i64; let target = (cur + delta as i64).clamp(0, max_top); if target != cur { self.textarea.scroll(((target - cur) as i16, 0)); @@ -267,6 +267,41 @@ fn new_textarea(body: &str, placeholder: Option<&str>) -> TextArea<'static> { #[cfg(test)] mod tests { use super::*; + use ratatui::{buffer::Buffer, widgets::Widget as _}; + use ratatui_textarea::CursorMove; + + #[test] + fn padded_editor_clicks_follow_wrapping_scrolling_and_resize() { + let body = "e\u{301}ζ—₯πŸ‘©πŸ½β€πŸ’»\tend\nsecond line\nlast"; + let mut editor = EntryEditor::for_new("work".to_string()); + editor.textarea.insert_str(body); + editor.textarea.set_top_padding(2); + for rect in [Rect::new(4, 3, 12, 6), Rect::new(4, 3, 5, 3)] { + editor.text_rect = rect; + editor.textarea.layout_for(rect); + editor.textarea.move_cursor(CursorMove::Top); + editor.textarea.render(rect, &mut Buffer::empty(rect)); + assert_eq!(editor.text_pos_at(rect.x, rect.y), Some((0, 0))); + assert_eq!( + editor.text_pos_at(rect.x + 2, rect.y + editor.textarea.top_padding()), + Some((0, 2)) + ); + assert_eq!(editor.text_pos_at(rect.x - 1, rect.y), None); + assert_eq!(editor.text_pos_at(rect.x, rect.bottom()), None); + + editor.textarea.move_cursor(CursorMove::Bottom); + editor.textarea.move_cursor(CursorMove::End); + editor.textarea.render(rect, &mut Buffer::empty(rect)); + let screen = editor.textarea.screen_cursor(); + let y = rect.y + screen.row as u16 + editor.textarea.top_padding() + - editor.textarea.scroll_offset().0; + assert_eq!( + editor.text_pos_at(rect.x + screen.col as u16, y), + Some((2, 4)) + ); + assert_eq!(editor.text(), body); + } + } /// A false positive here means the editor keeps painting a stale highlight, /// so the in-place comparison has to agree with `lines.join("\n")` exactly. diff --git a/src/tui/render/editor.rs b/src/tui/render/editor.rs index a7f6457c..ff83ef10 100644 --- a/src/tui/render/editor.rs +++ b/src/tui/render/editor.rs @@ -95,7 +95,7 @@ pub(crate) fn draw_entry_editor( // maps directly. Valid only after render. if !selecting && matches!(editor.prompt, EditorPrompt::None) { let sc = editor.textarea.screen_cursor(); - let scroll = editor.textarea.scroll_offset() as usize; + let scroll = editor.textarea.scroll_offset().0 as usize; let row = sc.row + editor.textarea.top_padding() as usize; if let Some(rel) = row.checked_sub(scroll) { let x = text_rect.x + sc.col as u16; @@ -114,7 +114,7 @@ pub(crate) fn draw_entry_editor( area, editor.textarea.screen_line_count(), text_rect.height, - editor.textarea.scroll_offset() as usize, + editor.textarea.scroll_offset().0 as usize, // The editor is always the active surface while shown. true, ); diff --git a/src/tui/text_input.rs b/src/tui/text_input.rs index 91a200fd..f203cd8d 100644 --- a/src/tui/text_input.rs +++ b/src/tui/text_input.rs @@ -175,24 +175,24 @@ impl TextInput { /// stranding at the right edge as an overflowing query is edited down. pub(crate) fn visible_cursor_col(&self, width: u16) -> u16 { let col = (self.textarea.screen_cursor().col as u16) - .saturating_sub(self.textarea.horizontal_scroll_offset()); + .saturating_sub(self.textarea.scroll_offset().1); col.min(width.saturating_sub(1)) } /// The content column a click `col` cells into the field lands on. /// - /// `col` arrives relative to the field's rect, but `cursor_at_screen` takes an + /// `col` arrives relative to the field's rect, but `screen_to_data` takes an /// absolute content column β€” so the horizontal scroll has to be *added*, the /// inverse of what [`Self::visible_cursor_col`] subtracts on the way out. /// Without it every click on a query wider than the field lands on the wrong /// character. fn content_col(&self, col: u16) -> usize { - col.saturating_add(self.textarea.horizontal_scroll_offset()) as usize + col.saturating_add(self.textarea.scroll_offset().1) as usize } /// Jump the caret to a click `col` cells into the field. fn jump_to_col(&mut self, col: u16) { - let cursor = self.textarea.cursor_at_screen(0, self.content_col(col)); + let cursor = self.textarea.screen_to_data(0, self.content_col(col)); self.textarea .move_cursor(CursorMove::Jump(cursor.0 as u16, cursor.1 as u16)); } @@ -211,7 +211,7 @@ impl TextInput { /// the reversed-block caret sits on a selected char rather than highlighting /// the boundary cell after the word. pub(crate) fn select_word_at(&mut self, col: u16) { - let column = self.textarea.cursor_at_screen(0, self.content_col(col)).1; + let column = self.textarea.screen_to_data(0, self.content_col(col)).1; let Some((start, end)) = word_bounds(&self.textarea.lines()[0], column) else { self.jump_to_col(col); return; @@ -232,7 +232,7 @@ impl TextInput { /// case where you cannot see what you are selecting. pub(crate) fn drag_mouse_selection(&mut self, col: u16, overshoot: i16) { let target = (self.content_col(col) as isize + overshoot as isize).max(0) as usize; - let cursor = self.textarea.cursor_at_screen(0, target); + let cursor = self.textarea.screen_to_data(0, target); self.textarea .move_cursor(CursorMove::Jump(cursor.0 as u16, cursor.1 as u16)); } @@ -571,7 +571,7 @@ mod tests { terminal .draw(|frame| input.render_in(&theme, frame, rect, true, false)) .unwrap(); - let scroll = input.horizontal_scroll_offset(); + let scroll = input.scroll_offset().1; assert!(scroll > 0, "the field scrolled"); // Clicking the leftmost cell lands on the leftmost *visible* char. @@ -583,6 +583,35 @@ mod tests { assert_eq!(input.cursor().1, 0, "one alphanumeric run, selected whole"); } + #[test] + fn deleting_an_overflowing_field_restores_its_text_and_native_cursor() { + let theme = crate::tui::theme::Theme::terminal_default(); + let mut input = TextInput::from("abcdefghijklmnop"); + let rect = Rect::new(2, 1, 6, 1); + let backend = ratatui::backend::TestBackend::new(12, 3); + let mut terminal = ratatui::Terminal::new(backend).unwrap(); + terminal + .draw(|frame| input.render_in(&theme, frame, rect, true, false)) + .unwrap(); + assert!(input.scroll_offset().1 > 0); + for _ in 0..13 { + input.input(key(KeyCode::Backspace)); + terminal + .draw(|frame| input.render_in(&theme, frame, rect, true, false)) + .unwrap(); + } + assert_eq!(input.as_str(), "abc"); + assert_eq!(input.scroll_offset().1, 0); + assert_eq!(input.visible_cursor_col(rect.width), 3); + assert_eq!(terminal.backend().buffer()[(rect.x, rect.y)].symbol(), "a"); + assert_eq!( + terminal.get_cursor_position().unwrap(), + (rect.x + 3, rect.y).into() + ); + input.begin_mouse_selection(1); + assert_eq!(input.cursor().1, 1); + } + /// Dragging a selection past the field's edge has to keep going, or the one /// thing you cannot see is the text you are selecting. #[test] @@ -602,7 +631,7 @@ mod tests { // there is somewhere to the right to drag toward. input.set_cursor_byte(0); render(&mut input); - assert_eq!(input.horizontal_scroll_offset(), 0); + assert_eq!(input.scroll_offset().1, 0); // Select from the field's left edge, then drag off its right edge. input.begin_mouse_selection(0); @@ -618,9 +647,9 @@ mod tests { // …and the render that follows scrolls to keep the caret visible, so the // next drag at the same pointer position reaches further still. - let scrolled_from = input.horizontal_scroll_offset(); + let scrolled_from = input.scroll_offset().1; render(&mut input); - assert!(input.horizontal_scroll_offset() > scrolled_from, "followed"); + assert!(input.scroll_offset().1 > scrolled_from, "followed"); input.drag_mouse_selection(rect.width - 1, 3); assert!(input.cursor().1 > at_edge + 3);