Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,11 @@ pkg-config = "0.3"
rand = "0.10"
ratatui = "0.30"
ratatui-image = { version = "11", default-features = false, features = ["crossterm"] }
# 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 = "cbf1b84", 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"
Expand Down Expand Up @@ -90,6 +87,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"
Expand Down Expand Up @@ -181,6 +180,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
Expand Down
41 changes: 38 additions & 3 deletions src/tui/editor_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand All @@ -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));
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/tui/render/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
);
Expand Down
45 changes: 45 additions & 0 deletions src/tui/render/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading
Loading