diff --git a/src/completion.rs b/src/completion.rs index 70bc4ab..950b354 100644 --- a/src/completion.rs +++ b/src/completion.rs @@ -684,7 +684,7 @@ struct PopupRect { /// the viewport or nothing fits. fn resolve_popup_rect( buf: &Buffer, - viewport: Viewport, + viewport: Viewport<'_>, anchor: Position, rows: &[PopupCandidate], ) -> Option { @@ -700,10 +700,12 @@ fn resolve_popup_rect( let line_offsets = crate::diag::compute_line_offsets(&source); let start_line = crate::diag::line_at_offset(&line_offsets, viewport.buffer_start as u32); let anchor_line = crate::diag::line_at_offset(&line_offsets, anchor); - if anchor_line < start_line { - return None; // anchor scrolled above the viewport - } - let anchor_row = anchor_line - start_line; + // Arc 6 Stage 2: the popup anchors on the anchor byte's VISIBLE row, + // so a completion below a collapsed region lands on the right row; + // an anchor inside a collapse has no row and paints nothing. + let Some(anchor_row) = viewport.row_offset_of(start_line as usize, anchor_line as usize) else { + return None; // anchor scrolled above the viewport, or collapsed + }; let max_rows = viewport.cell_size.rows; let max_cols = viewport.cell_size.cols; if anchor_row >= max_rows || max_cols == 0 { @@ -820,7 +822,7 @@ impl View for CompletionView { "completion-popup" } - fn render(&mut self, buf: &Buffer, viewport: Viewport, cells: &mut CellGrid<'_>) { + fn render(&mut self, buf: &Buffer, viewport: Viewport<'_>, cells: &mut CellGrid<'_>) { // Snapshot under the lock, then drop it before touching the rope. let (anchor, rows_data, selected_in_window): (Position, Vec, usize) = { let guard = self.popup.lock().expect("completion popup poisoned"); diff --git a/src/daemon.rs b/src/daemon.rs index fe28112..8a3c727 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -1545,7 +1545,16 @@ fn handle_session_established( // Register the frontend's view (M10.8 Day 3: fresh scratch // buffer view; future milestones may clone LOCAL's view or // take an explicit initial-buffer argument). - let scratch_view = build_fresh_frontend_view(editor); + // + // Arc 6 Stage 2 (Q#FD21): the projection is decided in this same + // attach transaction and from the same bit that selects a grid + // `RenderState` vs a `SemanticRenderState` below — a grid session + // collapses folds, a semantic one keeps raw-line reckoning until + // Stage 3. + let scratch_view = build_fresh_frontend_view( + editor, + !session_state.negotiated_capabilities.semantic_render, + ); editor .core .borrow_mut() @@ -2622,7 +2631,13 @@ fn align_semantic_window_to_buffer( } } -fn build_fresh_frontend_view(editor: &mut EditorState) -> crate::window::FrontendView { +fn build_fresh_frontend_view( + editor: &mut EditorState, + // Arc 6 Stage 2 (Q#FD21, Bet B8): whether this session's display + // collapses folds. Passed explicitly from the negotiated + // selected-render bit at the call site — never inferred here. + fold_projection: bool, +) -> crate::window::FrontendView { use crate::text_view::TextView; use crate::window::{FrontendView, Layout, Window, WindowId}; let mut core = editor.core.borrow_mut(); @@ -2651,6 +2666,7 @@ fn build_fresh_frontend_view(editor: &mut EditorState) -> crate::window::Fronten FrontendView { layout: Layout::single(id), active: id, + fold_projection, } } @@ -3442,6 +3458,7 @@ mod tests { FrontendView { layout: Layout::single(wid), active: wid, + fold_projection: true, }, ); } diff --git a/src/desktop.rs b/src/desktop.rs index 23ff5ac..7f44524 100644 --- a/src/desktop.rs +++ b/src/desktop.rs @@ -435,6 +435,8 @@ pub fn restore_into( FrontendView { layout: Layout { root }, active, + // Desktop restore rebuilds LOCAL's grid view (Q#FD21). + fold_projection: true, }, ); active diff --git a/src/diag.rs b/src/diag.rs index cd2909e..88aa6cc 100644 --- a/src/diag.rs +++ b/src/diag.rs @@ -493,7 +493,7 @@ impl View for DiagnosticView { "diagnostic" } - fn render(&mut self, buf: &Buffer, viewport: Viewport, cells: &mut CellGrid<'_>) { + fn render(&mut self, buf: &Buffer, viewport: Viewport<'_>, cells: &mut CellGrid<'_>) { // Snapshot the diagnostics under the lock and drop it // immediately so we don't hold the lock through rendering // (rendering touches the rope, which could in principle @@ -557,20 +557,23 @@ impl View for DiagnosticView { if line >= total_lines { break; } - if line < start_line_buf { + let row_offset = viewport.row_offset_of(start_line_buf as usize, line as usize); + let Some(sign_row) = sign_row_for(viewport, start_line_buf, line) else { continue; - } - let row_offset = line - start_line_buf; - if row_offset >= max_rows { + }; + if sign_row >= max_rows { break; } // Record the line marker before any byte-range work: // zero-width ranges (`byte_end <= byte_start` below) // skip the underline but still mark the line. line_markers - .entry(row_offset) + .entry(sign_row) .and_modify(|s| *s = (*s).min(diag.severity)) .or_insert(diag.severity); + let Some(row_offset) = row_offset else { + continue; + }; let line_start = line_offsets[line as usize]; let line_end = line_offsets .get(line as usize + 1) @@ -625,6 +628,25 @@ impl View for DiagnosticView { } } +/// The grid row a diagnostic on source `line` marks (Arc 6 Stage 2, +/// Q#FD15). +/// +/// Its own row normally; when the line is collapsed away, the fold's +/// **outermost visible head** row — a clamp, not a drop, so "there is a +/// problem inside this collapsed region" survives the collapse (the +/// most-severe-per-row merge then makes the head show the worst severity +/// among itself and every line its fold hides). `None` when neither has +/// a row in this viewport. Only the *sign* clamps: a squiggle needs a +/// real row, so a hidden line contributes no underline. +fn sign_row_for(viewport: Viewport<'_>, start_line: u32, line: u32) -> Option { + viewport + .row_offset_of(start_line as usize, line as usize) + .or_else(|| { + let map = viewport.folds?; + viewport.row_offset_of(start_line as usize, map.visible_head_of(line as usize)) + }) +} + /// Paint one severity marker per diagnostic line (UX gutter sub-arc 2). /// /// When the window reserves a gutter (`gutter_w > 0`), draw the severity @@ -1046,6 +1068,7 @@ mod tests { cell_origin: CellCoord::new(0, 0), cell_size: CellSize::new(1, 10), gutter_w: 0, + folds: None, }, &mut grid, ); @@ -1110,6 +1133,7 @@ mod tests { cell_origin: CellCoord::new(0, 0), cell_size: CellSize::new(3, 10), gutter_w: 0, + folds: None, }, &mut grid, ); @@ -1185,6 +1209,7 @@ mod tests { cell_origin: CellCoord::new(0, 2), cell_size: CellSize::new(3, 8), gutter_w: 2, + folds: None, }, &mut grid, ); @@ -1253,6 +1278,7 @@ mod tests { cell_origin: CellCoord::new(0, 0), cell_size: CellSize::new(2, 10), gutter_w: 0, + folds: None, }, &mut grid, ); diff --git a/src/editor.rs b/src/editor.rs index 00f0fd4..3cf59e6 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -41,7 +41,7 @@ use crate::protocol::{ use crate::terminal::TerminalSnapshot; use crate::terminal::view::TerminalViewKey; use crate::view::{View, Viewport}; -use crate::window::{Rect, WindowId}; +use crate::window::{LineNumberMode, Rect, WindowId}; /// Ephemeral authenticated origin for one interactive command invocation. /// @@ -2230,8 +2230,20 @@ impl EditorState { core.set_active_window_id(win_id); let view_top = core.windows[&win_id].view_top; let buffer_id = core.windows[&win_id].buffer_id; - let display_row = view_top.saturating_add(local_row as usize); - let target = crate::view::DisplayCoord::new(display_row as u32, local_col); + // Arc 6 Stage 2 (Q#FD16/FD21): grid row `k` in this window shows + // its `k`-th VISIBLE line, so the inverse must walk the same way + // — a click can then never land on a collapsed line. The map is + // the CLICKED window's (round-3 F1), not the previously active + // one's. + let folds = core.fold_map_for_window(win_id); + let display_row = match folds.as_ref() { + Some(map) => map.nth_visible_from(view_top, local_row as usize), + None => view_top.saturating_add(local_row as usize), + }; + let Ok(display_row) = u32::try_from(display_row) else { + return; + }; + let target = crate::view::DisplayCoord::new(display_row, local_col); let pos = { let registry = core.registry.clone(); let reg = registry.borrow(); @@ -2263,23 +2275,33 @@ impl EditorState { /// `mouse-wheel-mode` and every modern editor's wheel behaviour. fn scroll_window(&mut self, win_id: WindowId, delta: i32) { let mut core = self.core.borrow_mut(); + // Arc 6 Stage 2 (Q#FD18/FD21, round-3 F1): a wheel event names + // the pane under the pointer and does NOT activate it, so the map + // must come from `win_id` — deriving the active window's would + // project a folded buffer onto an unfolded neighbour. The + // projection *policy* still comes from the acting frontend. + let folds = core.fold_map_for_window(win_id); let line_count = core.windows[&win_id].text_view.line_count(); let max_top = line_count.saturating_sub(1); let old_top = core.windows[&win_id].view_top; let scroll_up = delta < 0; let magnitude = delta.unsigned_abs() as usize; - let new_top = if scroll_up { - old_top.saturating_sub(magnitude) - } else { - old_top.saturating_add(magnitude).min(max_top) + let new_top = match folds.as_ref() { + Some(map) if scroll_up => map.nth_visible_back(old_top, magnitude), + Some(map) => map + .nth_visible_from(old_top, magnitude) + .min(map.visible_head_of(max_top)), + None if scroll_up => old_top.saturating_sub(magnitude), + None => old_top.saturating_add(magnitude).min(max_top), }; // Effective view delta — buffer-boundary clamping may shrink // the requested move, so the cursor only follows by however - // many lines the view actually shifted. - let view_shift = if scroll_up { - old_top.saturating_sub(new_top) - } else { - new_top.saturating_sub(old_top) + // many lines the view actually shifted (counted in VISIBLE + // lines once this window folds). + let view_shift = match folds.as_ref() { + Some(map) => map.visible_distance(old_top, new_top), + None if scroll_up => old_top.saturating_sub(new_top), + None => new_top.saturating_sub(old_top), }; let buffer_id = core.windows[&win_id].buffer_id; let new_cursor = { @@ -2289,10 +2311,13 @@ impl EditorState { let aw = &core.windows[&win_id]; let cur = aw.text_view.pos_to_display(buf, aw.cursor)?; let cur_row = cur.row as usize; - let target_row_usize = if scroll_up { - cur_row.saturating_sub(view_shift) - } else { - cur_row.saturating_add(view_shift).min(max_top) + let target_row_usize = match folds.as_ref() { + Some(map) if scroll_up => map.nth_visible_back(cur_row, view_shift), + Some(map) => map + .nth_visible_from(cur_row, view_shift) + .min(map.visible_head_of(max_top)), + None if scroll_up => cur_row.saturating_sub(view_shift), + None => cur_row.saturating_add(view_shift).min(max_top), }; let target_row = u32::try_from(target_row_usize).ok()?; aw.text_view @@ -2316,6 +2341,12 @@ impl EditorState { /// readline / Emacs default and is what most terminal users expect. const SCROLL_LINES: i32 = 3; +/// Gutter marker drawn on a collapsed region's head row (Arc 6 Stage 2, +/// Q#FD20). Occupies the gutter's leading pad cell — the same cell the +/// diagnostic sign uses — so it adds no column and changes no width; it +/// therefore only appears when a line-number mode reserves a gutter. +const FOLD_GUTTER_GLYPH: char = '▸'; + /// Shared outer/content geometry consumed by terminal paint and PTY resize. #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub(crate) struct WindowPlacement { @@ -2860,6 +2891,13 @@ pub fn paint_frame( && let Some(buf_id) = buf_id && let Ok(buf) = reg.get(buf_id) { + // Arc 6 Stage 2 (Q#FD18): the auto-scroll clamp reckons in + // VISIBLE lines. Built from the active window itself, before + // the mutable borrow below. + let folds = core + .windows + .get(&active) + .and_then(|w| crate::fold_view::map_for_window(&state.fold_registry, w)); let aw = core.windows.get_mut(&active).expect( "invariant: active_window_id always references a live window in core.windows", ); @@ -2867,10 +2905,31 @@ pub fn paint_frame( .text_view .pos_to_display(buf, aw.cursor) .map_or(0, |d| d.row as usize); - if cursor_row < aw.view_top { - aw.view_top = cursor_row; - } else if inner_rows > 0 && cursor_row >= aw.view_top + inner_rows as usize { - aw.view_top = cursor_row + 1 - inner_rows as usize; + match folds.as_ref() { + // The logical cursor may sit on a hidden line (a shared + // fold, or goto-line into one); the row that actually + // renders — and so the row to scroll to — is its visible + // head (Q#FD16/FD18, framing acceptance 8). + Some(map) => { + let anchor = map.visible_head_of(cursor_row); + let top = map.clamp_view_top(aw.view_top); + aw.view_top = if anchor < top { + anchor + } else if inner_rows > 0 + && map.visible_rows_between(top, anchor) >= inner_rows as usize + { + map.nth_visible_back(anchor, inner_rows as usize - 1) + } else { + top + }; + } + None => { + if cursor_row < aw.view_top { + aw.view_top = cursor_row; + } else if inner_rows > 0 && cursor_row >= aw.view_top + inner_rows as usize { + aw.view_top = cursor_row + 1 - inner_rows as usize; + } + } } } } @@ -2923,6 +2982,19 @@ pub fn paint_frame( let Ok(buf) = reg.get(window.buffer_id) else { continue; }; + // Arc 6 Stage 2 (Q#FD12, round-2 F2): ONE visible-line map per + // rendered document window, keyed on that window's own buffer and + // line offsets. A split may show different buffers with only one + // folded, so a per-frame singleton would leak one pane's folds + // into the other. `None` when this buffer has no folds — the + // unfolded path then paints exactly as before. + let folds = crate::fold_view::map_for_window(&state.fold_registry, window); + // `view_top` stays a source-line index (Bet B5) but must never + // rest on a hidden line: clamp BACKWARD so a fold at the top of + // the viewport shows its head (Q#FD18, acceptance 8). + if let Some(map) = folds.as_ref() { + window.view_top = map.clamp_view_top(window.view_top); + } let viewport_buffer_start = window.text_view.line_offset(window.view_top).unwrap_or(0); // UX gutter (Q#UX2): reserve a left strip for line numbers and // shrink+shift the text area into the remainder, so every @@ -2939,6 +3011,7 @@ pub fn paint_frame( cell_origin: CellCoord::new(rect.origin.row, rect.origin.col + gutter_w), cell_size: crate::cell::CellSize::new(inner_rows, rect.size.cols - gutter_w), gutter_w, + folds: folds.as_ref(), }; // Composition (T M2.9): base text_view paints first, then the // gutter numbers — before the overlays, so a diagnostic overlay @@ -2947,24 +3020,52 @@ pub fn paint_frame( // overlay in attach order. See [`crate::view::View`]. window.text_view.render(buf, viewport, grid); if gutter_w > 0 { - paint_line_number_gutter(grid, window, &rect, inner_rows, gutter_w, &theme); + paint_line_number_gutter( + grid, + window, + &rect, + inner_rows, + gutter_w, + folds.as_ref(), + &theme, + ); } for overlay in &mut window.overlays { overlay.render(buf, viewport, grid); } - paint_local_selection(grid, buf, window, &rect, inner_rows, gutter_w, &theme); + paint_local_selection( + grid, + buf, + window, + &rect, + inner_rows, + gutter_w, + folds.as_ref(), + &theme, + ); // Mode line for this window. Painted last so the line // itself is always visible regardless of overlay activity. let coord = window .text_view .pos_to_display(buf, window.cursor) .unwrap_or_default(); - let scroll = format_scroll_indicator( - window.view_top, - inner_rows as usize, - window.text_view.line_count(), - coord.row as usize, - ); + // Arc 6 Stage 2 (Q#FD18): All/Top/Bot/% are reckoned in + // VISIBLE-line space — a buffer whose remainder is collapsed + // reads "All", not "Top". The cursor's ordinal anchors on its + // visible head, since that is the row it renders on. + let (ind_top, ind_total, ind_cursor) = match folds.as_ref() { + Some(map) => ( + map.visible_rows_between(0, window.view_top), + map.visible_line_count(window.text_view.line_count()), + map.visible_rows_between(0, map.visible_head_of(coord.row as usize)), + ), + None => ( + window.view_top, + window.text_view.line_count(), + coord.row as usize, + ), + }; + let scroll = format_scroll_indicator(ind_top, inner_rows as usize, ind_total, ind_cursor); // Lock scoped to the summary computation only: the overlay // renders above include `DiagnosticView`, which takes this // same mutex — holding the guard across the loop deadlocked @@ -3030,9 +3131,31 @@ pub fn paint_frame( let aw = &core.windows[&active]; let inner_rows = inner_rows(&active_rect); let buf = reg.get(aw.buffer_id).ok()?; - let disp = aw.text_view.pos_to_display(buf, aw.cursor)?; - if (disp.row as usize) < aw.view_top || (disp.row as usize) >= aw.view_top + inner_rows as usize - { + // Arc 6 Stage 2 (Q#FD16, round-2 F3): a logical cursor on a hidden + // line renders at its hidden component's head POSITION — the visible + // head row *and* that head's end-of-content column, i.e. exactly + // where Stage 1 moves point on a fold-at-cursor. Row-only clamping + // would leave the column unspecified; resolving through the merged + // component (rather than the innermost containing fold) also keeps a + // crossing overlap from landing on another hidden position. + let folds = crate::fold_view::map_for_window(&state.fold_registry, aw); + let cursor = match folds.as_ref() { + Some(map) => map.visible_position(aw.text_view.line_at_offset(aw.cursor), aw.cursor), + None => aw.cursor, + }; + let disp = aw.text_view.pos_to_display(buf, cursor)?; + let row_offset = match folds.as_ref() { + Some(map) => { + let top = map.clamp_view_top(aw.view_top); + let row = disp.row as usize; + if row < top { + return None; + } + map.visible_rows_between(top, row) + } + None => (disp.row as usize).checked_sub(aw.view_top)?, + }; + if row_offset >= inner_rows as usize { return None; } // UX gutter: the terminal caret sits in the text area, past the @@ -3041,7 +3164,7 @@ pub fn paint_frame( let w = aw.gutter_width(); if w >= active_rect.size.cols { 0 } else { w } }; - let grid_row = active_rect.origin.row + (disp.row - aw.view_top as u32); + let grid_row = active_rect.origin.row + u32::try_from(row_offset).ok()?; let max_col = active_rect.origin.col + active_rect.size.cols.saturating_sub(1); let grid_col = (active_rect.origin.col + gutter_w + disp.col).min(max_col); Some(CellCoord::new(grid_row, grid_col)) @@ -3180,13 +3303,23 @@ fn paint_line_number_gutter( rect: &crate::window::Rect, inner_rows: u32, gutter_w: u32, + // Arc 6 Stage 2: this window's collapsed regions, or `None` when it + // has no folds (then every line below is the pre-folding walk). + folds: Option<&crate::fold_view::VisibleLineMap>, theme: &crate::highlight::Theme, ) { let line_count = window.text_view.line_count(); // Relative/Hybrid measure distance from the cursor's buffer line; // Absolute ignores it. Computed once per frame (the gutter repaints on // cursor motion, so this stays current). + // + // Arc 6 Stage 2 (Q#FD14): the anchor is the cursor's **visible head** + // — a shared fold (or goto-line) can leave the logical cursor on a + // hidden line, and the distance must be measured from the row the + // caret actually renders on. With no folds this is `cursor_line` + // verbatim, so the unfolded gutter is unchanged. let cursor_line = window.text_view.line_at_offset(window.cursor); + let anchor = folds.map_or(cursor_line, |map| map.visible_head_of(cursor_line)); // Themes Q#TH5: a set `ui.gutter` face owns the strip within its // {fg} mask; unset keeps the dim Indexed(8). let style = theme.face("ui.gutter").map_or( @@ -3202,6 +3335,9 @@ fn paint_line_number_gutter( // The number's rightmost digit sits at `field - 1`; the last gutter // cell (`gutter_w - 1`) is a trailing pad separating it from the code. let field = gutter_w.saturating_sub(1); + // Row `r` shows the `r`-th VISIBLE line at or after `view_top`, the + // same walk `TextView::render` performs (Q#FD13/FD14). + let mut buffer_line = folds.map_or(window.view_top, |map| map.visible_head_of(window.view_top)); for r in 0..inner_rows { let grid_row = rect.origin.row + r; // Blank + style the whole strip first, so a number that shrank a @@ -3212,16 +3348,43 @@ fn paint_line_number_gutter( cell.style = style; cell.attachment = None; } - let buffer_line = window.view_top + r as usize; if buffer_line >= line_count { continue; // past end-of-buffer: blank gutter } + let this_line = buffer_line; + buffer_line = folds.map_or(this_line + 1, |map| map.next_visible(this_line)); + + // Fold marker (Q#FD20, round-1 F3): the col-0 sign cell only + // exists when a gutter does, so the glyph is conditional on it — + // with line numbers off the content-area ellipsis is the sole + // indicator. Painted here, before the overlays: `DiagnosticView` + // writes the same cell later in the frame, so a diagnostic + // clamped onto this head wins (an error inside the collapsed + // region is higher-signal than "this is collapsed"). + if folds.is_some_and(|map| map.is_head(this_line)) { + grid.at(CellCoord::new(grid_row, rect.origin.col)).glyph = + crate::cell::Glyph::Char(FOLD_GUTTER_GLYPH); + } + // The mode picks the number: absolute (`line+1`), relative // distance, or hybrid (absolute on the cursor line, else relative). // Written right-aligned, rightmost digit first, alloc-free. // `field >= digits(line_count)` by construction, so the leftmost // digit always leaves at least a leading pad cell. - let Some(mut val) = window.line_numbers.number_for(buffer_line, cursor_line) else { + // + // Arc 6 Stage 2 (Q#FD14): with folds present, Relative/Hybrid + // distance is counted in VISIBLE lines across the collapse; + // Absolute keeps the raw `line + 1` (hidden numbers simply do not + // appear, so the column jumps from the head's number to the first + // post-fold number). Without folds this is `number_for` verbatim. + let number = match (folds, window.line_numbers) { + (Some(map), LineNumberMode::Relative) => Some(map.visible_distance(anchor, this_line)), + (Some(map), LineNumberMode::Hybrid) if this_line != anchor => { + Some(map.visible_distance(anchor, this_line)) + } + _ => window.line_numbers.number_for(this_line, anchor), + }; + let Some(mut val) = number else { continue; }; let mut col = field; @@ -3238,6 +3401,10 @@ fn paint_line_number_gutter( } } +#[allow( + clippy::too_many_arguments, + reason = "one window's already-resolved paint geometry; mirrors paint_selection_in_window" +)] fn paint_local_selection( grid: &mut crate::cell::CellGrid<'_>, buf: &crate::buffer::Buffer, @@ -3248,11 +3415,24 @@ fn paint_local_selection( // text-relative display column shifted right by this (Q#UX2). 0 when // the gutter is off, so this is a no-op then. gutter_w: u32, + // Arc 6 Stage 2: this window's collapsed regions, or `None`. + folds: Option<&crate::fold_view::VisibleLineMap>, theme: &crate::highlight::Theme, ) { let Some((sel_start, sel_end)) = window.region() else { return; }; + // Arc 6 Stage 2 (Q#FD16): each ENDPOINT on a hidden line projects to + // its component's head position; hidden interior cells simply have no + // row and drop. The visible portion then paints on the visible head + // row and the visible tail rows, contiguous on screen. + let (sel_start, sel_end) = match folds { + Some(map) => ( + map.visible_position(window.text_view.line_at_offset(sel_start), sel_start), + map.visible_position(window.text_view.line_at_offset(sel_end), sel_end), + ), + None => (sel_start, sel_end), + }; // Themes Q#TH5: the selection is a wash — a set `ui.selection` // face replaces the default overlay wholesale within its {bg} // mask (an all-default face disables the wash; out-of-mask @@ -3272,9 +3452,11 @@ fn paint_local_selection( } let text_cols = rect.size.cols.saturating_sub(gutter_w); - let first_row = window.view_top; - let last_row = first_row.saturating_add(inner_rows as usize); - for display_row in first_row..last_row { + // Row `r` shows the `r`-th VISIBLE line at or after `view_top`. + let mut next_line = folds.map_or(window.view_top, |map| map.visible_head_of(window.view_top)); + for row_offset in 0..inner_rows { + let display_row = next_line; + next_line = folds.map_or(display_row + 1, |map| map.next_visible(display_row)); let Some(line_start) = window.text_view.line_offset(display_row) else { continue; }; @@ -3298,7 +3480,6 @@ fn paint_local_selection( continue; } - let row_offset = display_row.saturating_sub(first_row) as u32; let start_col = start_coord.col.min(text_cols); let end_col = end_coord.col.min(text_cols); if start_col >= end_col { @@ -4028,6 +4209,7 @@ mod tests { &rect, rows, 4, + None, &crate::highlight::Theme::empty(), ); @@ -6672,6 +6854,7 @@ mod tests { cell_origin: rect.origin, cell_size: CellSize::new(rect.size.rows, rect.size.cols), gutter_w: 0, + folds: None, }; let mut grid = CellGrid { cells: &mut backing, @@ -6806,6 +6989,7 @@ mod tests { cell_origin: CellCoord::new(0, 0), cell_size: CellSize::new(24, 80), gutter_w: 0, + folds: None, }; // Two no-op overlays: probe the dispatch cost only. diff --git a/src/editor_core.rs b/src/editor_core.rs index 5afbe90..2a15e32 100644 --- a/src/editor_core.rs +++ b/src/editor_core.rs @@ -386,6 +386,8 @@ impl EditorCore { FrontendView { layout: Layout::single(id), active: id, + // LOCAL is the in-process grid editor (Q#FD21). + fold_projection: true, }, ); Self { @@ -534,6 +536,51 @@ impl EditorCore { self.windows.get_mut(&win_id) } + /// Whether the **acting** frontend's display collapses folds (Arc 6 + /// Stage 2, Q#FD21). + /// + /// The gate on every command/event-time visible-line reckoning — + /// motion, paging, wheel, the click inverse, the auto-scroll clamp. + /// A `semantic_render` (GPU) session still displays every source + /// line until Stage 3, so with this `false` those sites keep their + /// raw-line behavior and its cursor never skips a line it is showing + /// (even while a grid session folds the same shared buffer). + #[must_use] + pub fn fold_projection_active(&self) -> bool { + self.active_view().fold_projection + } + + /// The visible-line map for `win_id`'s buffer, or `None` when the + /// acting frontend does not project folds, `win_id` is unknown, or + /// that buffer has no folds (Q#FD12). + /// + /// The two axes are deliberately separate (round-3 F1): the **acting + /// frontend** supplies the projection policy, while the + /// **operation's target window** supplies the buffer and line + /// offsets. Motion, paging, and auto-scroll target the active + /// window; the click inverse and wheel scrolling name an explicit + /// `win_id` — a wheel event over an inactive pane does not activate + /// it, so deriving the active window's map there would project one + /// buffer's folds onto another. + #[must_use] + pub fn fold_map_for_window( + &self, + win_id: WindowId, + ) -> Option { + if !self.fold_projection_active() { + return None; + } + let window = self.windows.get(&win_id)?; + crate::fold_view::map_for_window(&self.fold_registry, window) + } + + /// [`Self::fold_map_for_window`] for the active window — the target + /// of motion, paging, and the auto-scroll clamp. + #[must_use] + pub fn fold_map_active(&self) -> Option { + self.fold_map_for_window(self.active_window_id()) + } + /// T M10.8 — register a `FrontendView` for `fid`. Called by the /// daemon on attach (Day 3 dispatcher work). Day 2's fallback /// path makes this optional; Day 3 makes it required. @@ -1264,6 +1311,16 @@ impl EditorCore { /// /// Returns a stringified error on buffer or view failure. pub fn apply_active_edit(&mut self, op: EditOp<'_>) -> Result { + // Arc 6 Stage 2 (Q#FD19): ONE pre-edit unfold funnel for every + // local point-anchored edit. This subsumes the six `dispatch_key` + // primitives' individual calls (retired) and widens the behavior + // to **yank** and **query-replace**, which reach the buffer + // through here rather than through those primitives — both place + // point at the edit site first, so keying on the active point + // covers them exactly. `apply_active_edit` is never the + // remote-apply path, so this funnel is inherently local: a remote + // peer's edit inside my fold must not unfold it (Stage 3). + self.unfold_before_point_edit(); let buffer_id = self.active_buffer_id(); // Scope the registry borrow: the origin translation below needs // `&mut self` after the views have been notified. @@ -1541,27 +1598,42 @@ impl EditorCore { } /// Move the cursor up one line, preserving display column. + /// + /// Arc 6 Stage 2 (Q#FD17, ruled: include): on a fold-projecting + /// frontend this steps to the previous **visible** line, so a + /// collapsed region is one motion step and the cursor never comes to + /// rest hidden. Motion that *begins* from a hidden logical cursor (a + /// shared fold, or goto-line into one) first normalizes to the + /// visible head. Scoped by Q#FD21 — a semantic frontend keeps + /// raw-line motion until Stage 3. pub fn move_up(&mut self) { + let folds = self.fold_map_active(); let id = self.active_buffer_id(); let cursor = self.active_window().cursor; let goal_col = self.active_window().goal_col; let result = { let reg = self.registry.borrow(); let Ok(buffer) = reg.get(id) else { return }; - let coord = self - .active_window() + let aw = self.active_window(); + let coord = aw .text_view .pos_to_display(buffer, cursor) .unwrap_or_default(); - if coord.row == 0 { + let from_row = folds.as_ref().map_or(coord.row as usize, |map| { + map.visible_head_of(coord.row as usize) + }); + if from_row == 0 { return; } + let target_row = folds + .as_ref() + .map_or(from_row - 1, |map| map.prev_visible(from_row)); let goal = goal_col.unwrap_or(coord.col); - let target = DisplayCoord::new(coord.row - 1, goal); - let new_pos = self - .active_window() - .text_view - .display_to_pos(buffer, target); + let Ok(target_row) = u32::try_from(target_row) else { + return; + }; + let target = DisplayCoord::new(target_row, goal); + let new_pos = aw.text_view.display_to_pos(buffer, target); (goal, new_pos) }; let (goal, new_pos) = result; @@ -1573,28 +1645,35 @@ impl EditorCore { } /// Move the cursor down one line, preserving display column. + /// Visible-line stepping mirrors [`Self::move_up`] (Q#FD17/FD21). pub fn move_down(&mut self) { + let folds = self.fold_map_active(); let id = self.active_buffer_id(); let cursor = self.active_window().cursor; let goal_col = self.active_window().goal_col; let result = { let reg = self.registry.borrow(); let Ok(buffer) = reg.get(id) else { return }; - let coord = self - .active_window() + let aw = self.active_window(); + let coord = aw .text_view .pos_to_display(buffer, cursor) .unwrap_or_default(); - let next_row = coord.row + 1; - if (next_row as usize) >= self.active_window().text_view.line_count() { + let from_row = folds.as_ref().map_or(coord.row as usize, |map| { + map.visible_head_of(coord.row as usize) + }); + let next_row = folds + .as_ref() + .map_or(from_row + 1, |map| map.next_visible(from_row)); + if next_row >= aw.text_view.line_count() { return; } let goal = goal_col.unwrap_or(coord.col); + let Ok(next_row) = u32::try_from(next_row) else { + return; + }; let target = DisplayCoord::new(next_row, goal); - let new_pos = self - .active_window() - .text_view - .display_to_pos(buffer, target); + let new_pos = aw.text_view.display_to_pos(buffer, target); (goal, new_pos) }; let (goal, new_pos) = result; @@ -1743,6 +1822,9 @@ impl EditorCore { /// of context); falls back to a sane default before the first /// frame has rendered. pub fn move_page_down(&mut self) { + // Arc 6 Stage 2 (Q#FD18/FD21): a screenful is a screenful of + // VISIBLE lines, and `view_top` never lands hidden. + let folds = self.fold_map_active(); let step = self.page_step(); let cursor = self.active_window().cursor; let view_top = self.active_window().view_top; @@ -1755,12 +1837,25 @@ impl EditorCore { .text_view .pos_to_display(buffer, cursor) .unwrap_or_default(); - let max_line = aw.text_view.line_count().saturating_sub(1) as u32; + let max_line = aw.text_view.line_count().saturating_sub(1); let goal_col = aw.goal_col.unwrap_or(coord.col); - let target_row = (coord.row + step).min(max_line); + let (target_row, new_top) = match folds.as_ref() { + Some(map) => ( + map.nth_visible_from(coord.row as usize, step as usize) + .min(map.visible_head_of(max_line)), + map.nth_visible_from(view_top, step as usize), + ), + None => ( + (coord.row as usize + step as usize).min(max_line), + view_top.saturating_add(step as usize), + ), + }; + let Ok(target_row) = u32::try_from(target_row) else { + return; + }; let target = DisplayCoord::new(target_row, goal_col); let new_pos = aw.text_view.display_to_pos(buffer, target); - (goal_col, new_pos, view_top.saturating_add(step as usize)) + (goal_col, new_pos, new_top) }; let (goal, new_pos, new_top) = result; let aw = self.active_window_mut(); @@ -1771,12 +1866,16 @@ impl EditorCore { // Also nudge view_top; render's scroll-into-view will clamp // and align further if needed. let max_top = aw.text_view.line_count().saturating_sub(1); - aw.view_top = new_top.min(max_top); + let clamped = new_top.min(max_top); + aw.view_top = folds + .as_ref() + .map_or(clamped, |map| map.clamp_view_top(clamped)); } /// Move the cursor up by approximately one screenful. Mirror of /// [`Self::move_page_down`]. pub fn move_page_up(&mut self) { + let folds = self.fold_map_active(); let step = self.page_step(); let cursor = self.active_window().cursor; let view_top = self.active_window().view_top; @@ -1790,10 +1889,22 @@ impl EditorCore { .pos_to_display(buffer, cursor) .unwrap_or_default(); let goal_col = aw.goal_col.unwrap_or(coord.col); - let target_row = coord.row.saturating_sub(step); + let (target_row, new_top) = match folds.as_ref() { + Some(map) => ( + map.nth_visible_back(coord.row as usize, step as usize), + map.nth_visible_back(view_top, step as usize), + ), + None => ( + (coord.row as usize).saturating_sub(step as usize), + view_top.saturating_sub(step as usize), + ), + }; + let Ok(target_row) = u32::try_from(target_row) else { + return; + }; let target = DisplayCoord::new(target_row, goal_col); let new_pos = aw.text_view.display_to_pos(buffer, target); - (goal_col, new_pos, view_top.saturating_sub(step as usize)) + (goal_col, new_pos, new_top) }; let (goal, new_pos, new_top) = result; let aw = self.active_window_mut(); @@ -1835,15 +1946,21 @@ impl EditorCore { aw.goal_col = None; } - /// Dispatch-layer pre-edit unfold (Arc 6, Q#FD5). Before a - /// command-path point-anchored edit (the six primitives below), - /// unfold every fold containing the active point so a self-insert or - /// delete inside a collapsed region reveals it rather than landing - /// invisibly. Keyed on the authenticated source frontend's active - /// point (this is `active_window().cursor`), not the transport. A - /// no-op when the buffer has no folds. Interactive Lua-command edits - /// (yank/query-replace/comment) reach the buffer through a different - /// path and are a named Stage 2 widening; CRDT-origin is Stage 3. + /// Pre-edit unfold (Arc 6, Q#FD5 / Stage 2 Q#FD19). Before a local + /// point-anchored edit, unfold every fold containing the active point + /// so an edit inside a collapsed region reveals it rather than + /// landing invisibly. Keyed on the authenticated source frontend's + /// active point (`active_window().cursor`), not the transport. A + /// no-op when the buffer has no folds. + /// + /// **Stage 2 widening:** Stage 1 called this from each of the six + /// `dispatch_key` edit primitives. It now runs once at the top of + /// [`Self::apply_active_edit`] — the single funnel those primitives + /// (and yank, and query-replace) all pass through. Interactive + /// Lua-mutator edits (comment-toggle, yank-pop) take a *different* + /// path and are hooked at `run_buffer_edit` in the Lua bindings; the + /// remote/optimistic-CRDT apply path is deliberately excluded + /// (Stage 3), as is undo/redo (deferred). fn unfold_before_point_edit(&self) { let id = self.active_buffer_id(); let point = self.active_window().cursor; @@ -1855,7 +1972,6 @@ impl EditorCore { /// line and returns `false`, and callers must not mutate dependent /// state (e.g. selection anchors) on a failed insert (Q#AI9). pub fn insert_char(&mut self, ch: char) -> bool { - self.unfold_before_point_edit(); self.active_window_mut().goal_col = None; let mut buf = [0u8; 4]; let s = ch.encode_utf8(&mut buf); @@ -1891,7 +2007,6 @@ impl EditorCore { /// delegates to [`Self::insert_char`] (a plain insert). The cursor /// lands just past the inserted bytes and any selection is cleared. pub fn insert_char_over_region(&mut self, ch: char) { - self.unfold_before_point_edit(); let Some((lo, hi)) = self.active_region() else { // Q#AI9: an empty selection (anchor == cursor) reports no // region yet stays armed — the insert moves the cursor off @@ -1933,7 +2048,6 @@ impl EditorCore { /// Delete the codepoint immediately before the cursor. pub fn backspace(&mut self) { - self.unfold_before_point_edit(); self.active_window_mut().goal_col = None; let cursor = self.active_window().cursor; if cursor == 0 { @@ -1955,7 +2069,6 @@ impl EditorCore { /// Delete the codepoint at the cursor (forward delete). pub fn delete_forward(&mut self) { - self.unfold_before_point_edit(); self.active_window_mut().goal_col = None; let cursor = self.active_window().cursor; let id = self.active_buffer_id(); @@ -1979,7 +2092,6 @@ impl EditorCore { /// between the cursor and where [`Self::move_word_left`] would /// land. pub fn delete_word_backward(&mut self) { - self.unfold_before_point_edit(); self.active_window_mut().goal_col = None; let cursor = self.active_window().cursor; if cursor == 0 { @@ -2007,7 +2119,6 @@ impl EditorCore { /// [`Self::delete_forward`] over the gap from the cursor to where /// [`Self::move_word_right`] would land. pub fn delete_word_forward(&mut self) { - self.unfold_before_point_edit(); self.active_window_mut().goal_col = None; let cursor = self.active_window().cursor; let id = self.active_buffer_id(); @@ -3345,6 +3456,7 @@ mod tests { FrontendView { layout: Layout::single(win_id), active: win_id, + fold_projection: true, }, ); win_id diff --git a/src/fold_view.rs b/src/fold_view.rs new file mode 100644 index 0000000..bebaff6 --- /dev/null +++ b/src/fold_view.rs @@ -0,0 +1,466 @@ +// fold_view.rs --- The visible-line map (Arc 6, Stage 2). + +//! The source-line ↔ display-row projection that folding introduces. +//! +//! Before folding, every grid consumer assumed `display_row = +//! source_line − view_top` — an identity map baked into the text walk, +//! the gutter, every overlay, the caret, both selection painters, the +//! mode-line indicator, and the click/scroll/motion inverses. +//! [`DisplayCoord`](crate::view::DisplayCoord) anticipated a +//! non-identity map "once virtual lines, wrapping, and inline +//! expansions appear"; **folding is the first**. +//! +//! This module is that map — **one derivation/query primitive** +//! (`docs/folding-stage2-framing.md` Q#FD12), derived from +//! [`crate::fold::FoldRegistry::folds`] plus the buffer's line offsets +//! and **never stored**: the byte-range store in [`crate::fold`] stays +//! the single source of truth. Instances are short-lived and built +//! **per rendered window** and **per command/event operation**, never +//! once per frame — a frame paints several windows that may show +//! different buffers, so a per-frame singleton would leak one pane's +//! folds into another's (framing round-2 F2). +//! +//! # Hidden components, not folds +//! +//! The unit here is not a fold. [`crate::fold::FoldStore::insert`] +//! accepts any normalized range, so folds may nest, share a head line, +//! or **cross**: with fold `A` hiding lines 1–3 and fold `B` headed on +//! line 2 hiding lines 3–5, a point on line 5 is directly inside only +//! `B` — yet `B`'s own head is hidden by `A`, so projecting to `B`'s +//! `range.start` would land on another *hidden* position (round-3 F2). +//! +//! The derivation therefore unions overlapping **or adjacent** hidden +//! line intervals into sorted, non-overlapping **hidden components**. +//! Adjacent intervals merge because the later fold's head is hidden by +//! the earlier one, so it can never render. Each component keeps the one +//! visible line immediately before it (`head_line`) and that line's exact +//! end-of-content byte (`head_position` — the fold `range.start` Stage 1 +//! already moves point to). Resolving through the component is +//! equivalent to repeatedly projecting a hidden fold head until it is +//! visible, and so covers nesting, shared heads, and crossing overlap +//! alike. + +use pmacs_protocol::ByteRange; + +use crate::fold::FoldRegistry; +use crate::rope::Position; +use crate::window::Window; + +/// The visible-line map for one **window's** buffer, or `None` when that +/// buffer has no folds. +/// +/// The single construction rule shared by the render path and +/// `EditorCore` (Q#FD12): keyed on *this* window's `buffer_id` and its +/// own [`TextView`](crate::text_view::TextView) line offsets — never the +/// active buffer's — so a split showing two buffers gets two independent +/// maps and neither leaks into the other (round-2 F2). Returning `None` +/// rather than an empty map keeps the unfolded path byte-identical. +#[must_use] +pub fn map_for_window(registry: &FoldRegistry, window: &Window) -> Option { + let folds = registry.folds(window.buffer_id); + if folds.is_empty() { + return None; + } + Some(VisibleLineMap::build(&folds, |off| { + window.text_view.line_at_offset(off) + })) +} + +/// A maximal run of consecutive hidden source lines, plus the one +/// visible line that heads it. +/// +/// `first_hidden >= 1` always: a component's `head_line` is +/// `first_hidden - 1`, and a fold's head line is the line *above* its +/// first hidden line, so line 0 can never be hidden. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +struct HiddenComponent { + /// First hidden source line (inclusive). + first_hidden: usize, + /// Last hidden source line (inclusive). + last_hidden: usize, + /// End-of-content byte of `head_line` — the `ByteRange::start` of + /// the earliest fold participating in this component, which is + /// exactly where Stage 1 moves point on a fold-at-cursor. + head_position: Position, +} + +impl HiddenComponent { + /// The one visible line immediately above this component. + const fn head_line(&self) -> usize { + self.first_hidden - 1 + } +} + +/// A buffer's collapsed regions, projected into line space. +/// +/// Derived from a fold list and a byte→line lookup; cheap enough to +/// rebuild per window per frame (**Bet B4**: `O(folds)` with one binary +/// search into the caller's existing line-offset table per fold, and +/// folds are `O(top-level blocks)`). +/// +/// An empty map (`is_identity`) means "no folds" — callers pass `None` +/// rather than an empty map so the unfolded path stays byte-identical. +#[derive(Clone, Debug, Default, Eq, PartialEq)] +pub struct VisibleLineMap { + /// Sorted by `first_hidden`, non-overlapping, and separated by at + /// least one visible line (adjacency is merged away at build time). + components: Vec, +} + +impl VisibleLineMap { + /// Derive the map from a buffer's folds. + /// + /// `line_at_offset` is the caller's own line-offset lookup (the + /// rendering window's [`TextView`](crate::text_view::TextView), the + /// only table guaranteed to agree with the rows being painted). A + /// fold's stored range is `[end of head line, end of last hidden + /// line]`, so `head_line = line_at_offset(start)` and `last_hidden = + /// line_at_offset(end)`; a fold that no longer spans a whole line + /// (mid-edit drift) contributes nothing. + #[must_use] + pub fn build(folds: &[ByteRange], line_at_offset: F) -> Self + where + F: Fn(Position) -> usize, + { + let mut raw: Vec = folds + .iter() + .filter_map(|f| { + let head_line = line_at_offset(f.start); + let last_hidden = line_at_offset(f.end); + (last_hidden > head_line).then_some(HiddenComponent { + first_hidden: head_line + 1, + last_hidden, + head_position: f.start, + }) + }) + .collect(); + raw.sort_by(|a, b| { + a.first_hidden + .cmp(&b.first_hidden) + .then(a.last_hidden.cmp(&b.last_hidden)) + }); + let mut components: Vec = Vec::with_capacity(raw.len()); + for c in raw { + match components.last_mut() { + // Overlapping OR adjacent: `c`'s head line is itself + // hidden by `prev`, so it can never render — the merged + // component keeps `prev`'s (visible) head. + Some(prev) if c.first_hidden <= prev.last_hidden + 1 => { + prev.last_hidden = prev.last_hidden.max(c.last_hidden); + } + _ => components.push(c), + } + } + Self { components } + } + + /// Whether this map hides nothing — the identity projection. + #[must_use] + pub fn is_identity(&self) -> bool { + self.components.is_empty() + } + + /// The component hiding `line`, if any. + fn component_of(&self, line: usize) -> Option<&HiddenComponent> { + let after = self.components.partition_point(|c| c.first_hidden <= line); + let c = self.components.get(after.checked_sub(1)?)?; + (line <= c.last_hidden).then_some(c) + } + + /// Whether `line` is collapsed away and renders no row. + #[must_use] + pub fn is_hidden(&self, line: usize) -> bool { + self.component_of(line).is_some() + } + + /// Whether `line` is the visible head of a collapsed region — the + /// row that carries the ellipsis and the gutter fold glyph. + #[must_use] + pub fn is_head(&self, line: usize) -> bool { + self.components + .binary_search_by(|c| c.first_hidden.cmp(&(line + 1))) + .is_ok() + } + + /// The **outermost visible head** of `line`: for a hidden line, its + /// component's head line; for a visible line, itself. + /// + /// The **row-only** clamp — diagnostic signs, the relative-number + /// cursor anchor, and the backward `view_top` clamp. Positions that + /// carry a column use [`Self::visible_position`] instead. + #[must_use] + pub fn visible_head_of(&self, line: usize) -> usize { + self.component_of(line) + .map_or(line, HiddenComponent::head_line) + } + + /// The **position** projection of a byte on `line`: for a hidden + /// line, its component's `head_position` (the head line's + /// end-of-content byte); for a visible line, `pos` unchanged. + /// + /// Used wherever a clamp carries a column — the local caret, peer + /// cursors, and selection endpoints — so a hidden point lands at the + /// head's end of content rather than at an arbitrary column on the + /// head (round-2 F3) or at a still-hidden crossing fold's start + /// (round-3 F2). + #[must_use] + pub fn visible_position(&self, line: usize, pos: Position) -> Position { + self.component_of(line).map_or(pos, |c| c.head_position) + } + + /// Clamp a candidate `view_top` **backward** to a visible line, so a + /// fold at the top of the viewport shows its head rather than being + /// skipped past (framing acceptance 8). + #[must_use] + pub fn clamp_view_top(&self, line: usize) -> usize { + self.visible_head_of(line) + } + + /// The next visible line strictly after `line`, skipping whole + /// collapsed regions. May exceed the buffer's line count; callers + /// bound it themselves. + #[must_use] + pub fn next_visible(&self, line: usize) -> usize { + let next = line + 1; + self.component_of(next) + .map_or(next, |c| c.last_hidden.saturating_add(1)) + } + + /// The previous visible line strictly before `line`, or `0` when + /// `line` is already the first line. + #[must_use] + pub fn prev_visible(&self, line: usize) -> usize { + match line.checked_sub(1) { + Some(prev) => self.visible_head_of(prev), + None => 0, + } + } + + /// Number of visible lines in the half-open range `[from, to)`; + /// `0` when `to <= from`. + /// + /// This is the framing's `visible_between` — exposed unsigned and + /// half-open (plus the symmetric [`Self::visible_distance`]) because + /// no consumer reads the sign: row offsets always measure forward + /// from `view_top`, and relative line numbers want a magnitude. + #[must_use] + pub fn visible_rows_between(&self, from: usize, to: usize) -> usize { + if to <= from { + return 0; + } + (to - from) - self.hidden_in(from, to) + } + + /// Visible-line distance between `a` and `b`, either order — the + /// relative/hybrid gutter number measured across collapses. + #[must_use] + pub fn visible_distance(&self, a: usize, b: usize) -> usize { + if a <= b { + self.visible_rows_between(a, b) + } else { + self.visible_rows_between(b, a) + } + } + + /// Hidden lines within the half-open range `[from, to)`. + fn hidden_in(&self, from: usize, to: usize) -> usize { + self.components + .iter() + .filter(|c| c.first_hidden < to && c.last_hidden >= from) + .map(|c| { + // `lo <= hi` holds under the filter, so this cannot + // underflow. + let lo = c.first_hidden.max(from); + let hi = c.last_hidden.min(to - 1); + hi + 1 - lo + }) + .sum() + } + + /// Total visible lines in a buffer of `total_lines` source lines — + /// the denominator the mode-line scroll indicator reckons in. + #[must_use] + pub fn visible_line_count(&self, total_lines: usize) -> usize { + total_lines - self.hidden_in(0, total_lines).min(total_lines) + } + + /// The line `n` visible steps forward from `from` (which is first + /// normalized to its visible head). `n == 0` yields that head. + #[must_use] + pub fn nth_visible_from(&self, from: usize, n: usize) -> usize { + let mut line = self.visible_head_of(from); + for _ in 0..n { + line = self.next_visible(line); + } + line + } + + /// The line `n` visible steps back from `from` (first normalized to + /// its visible head), saturating at line 0. + #[must_use] + pub fn nth_visible_back(&self, from: usize, n: usize) -> usize { + let mut line = self.visible_head_of(from); + for _ in 0..n { + if line == 0 { + break; + } + line = self.prev_visible(line); + } + line + } +} + +// --------------------------------------------------------------------------- +// Tests +// --------------------------------------------------------------------------- + +#[cfg(test)] +mod tests { + use super::*; + + /// A 40-line buffer of `"L\n"`-ish rows, 8 bytes each, so line + /// `n` starts at `8n` and its content ends at `8n + 7`. + fn line_of(offset: Position) -> usize { + (offset / 8) as usize + } + + /// The fold that hides lines `first..=last` in that fixture. + fn fold(head: usize, last_hidden: usize) -> ByteRange { + ByteRange { + start: (head as u64) * 8 + 7, + end: (last_hidden as u64) * 8 + 7, + } + } + + fn map(folds: &[ByteRange]) -> VisibleLineMap { + VisibleLineMap::build(folds, line_of) + } + + #[test] + fn empty_map_is_identity() { + let m = map(&[]); + assert!(m.is_identity()); + assert!(!m.is_hidden(5)); + assert_eq!(m.visible_head_of(5), 5); + assert_eq!(m.next_visible(5), 6); + assert_eq!(m.visible_rows_between(0, 10), 10); + } + + #[test] + fn single_fold_hides_its_interior_only() { + // head 2, hidden 3..=6. + let m = map(&[fold(2, 6)]); + assert!(!m.is_hidden(2)); + assert!(m.is_head(2)); + for line in 3..=6 { + assert!(m.is_hidden(line), "line {line} should be hidden"); + assert_eq!(m.visible_head_of(line), 2); + } + assert!(!m.is_hidden(7)); + assert_eq!(m.next_visible(2), 7); + assert_eq!(m.prev_visible(7), 2); + // 0,1,2,7,8,9 visible in [0,10). + assert_eq!(m.visible_rows_between(0, 10), 6); + assert_eq!(m.visible_line_count(10), 6); + } + + #[test] + fn nested_folds_resolve_to_the_outermost_visible_head() { + // Outer: head 0, hidden 1..=9. Inner: head 3, hidden 4..=6. + let m = map(&[fold(0, 9), fold(3, 6)]); + assert_eq!(m.visible_head_of(5), 0, "inner head 3 is itself hidden"); + assert_eq!(m.visible_head_of(3), 0); + assert!(m.is_head(0)); + assert!(!m.is_head(3), "a hidden head renders no row"); + assert_eq!(m.next_visible(0), 10); + } + + #[test] + fn shared_head_folds_merge_to_the_longer_reach() { + // Two folds on head 4: one hides 5..=6, the other 5..=9. + let m = map(&[fold(4, 6), fold(4, 9)]); + assert_eq!(m.visible_head_of(9), 4); + assert_eq!(m.next_visible(4), 10); + assert_eq!(m.visible_position(9, 9 * 8 + 3), fold(4, 6).start); + } + + #[test] + fn crossing_folds_project_to_the_first_visible_head() { + // Round-3 F2: A hides 1..=3 (head 0); B is headed on line 2 and + // hides 3..=5. A point on line 5 is directly inside only B, but + // B's head is hidden by A — it must resolve to A's head. + let m = map(&[fold(0, 3), fold(2, 5)]); + assert!(m.is_hidden(5)); + assert_eq!(m.visible_head_of(5), 0); + assert_eq!( + m.visible_position(5, 5 * 8 + 4), + fold(0, 3).start, + "never B's still-hidden range.start" + ); + assert_eq!(m.next_visible(0), 6); + assert!(!m.is_head(2), "B's head is hidden, so it heads nothing"); + } + + #[test] + fn adjacent_folds_merge_because_the_later_head_is_hidden() { + // A hides 1..=3 (head 0); B is headed on line 3 (hidden by A) + // and hides 4..=5. Lines 1..=5 collapse under head 0. + let m = map(&[fold(0, 3), fold(3, 5)]); + for line in 1..=5 { + assert_eq!(m.visible_head_of(line), 0, "line {line}"); + } + assert_eq!(m.next_visible(0), 6); + } + + #[test] + fn a_visible_line_between_two_folds_keeps_them_separate() { + // A hides 1..=3 (head 0); B hides 5..=6 (head 4). Line 4 stays + // visible, so the components do not merge. + let m = map(&[fold(0, 3), fold(4, 6)]); + assert!(!m.is_hidden(4)); + assert_eq!(m.visible_head_of(3), 0); + assert_eq!(m.visible_head_of(6), 4); + assert_eq!(m.next_visible(0), 4); + assert_eq!(m.next_visible(4), 7); + } + + #[test] + fn visible_position_leaves_a_visible_byte_alone() { + let m = map(&[fold(2, 6)]); + assert_eq!(m.visible_position(7, 7 * 8 + 2), 7 * 8 + 2); + } + + #[test] + fn clamp_view_top_goes_backward_to_the_head() { + let m = map(&[fold(2, 6)]); + assert_eq!(m.clamp_view_top(5), 2); + assert_eq!(m.clamp_view_top(2), 2); + assert_eq!(m.clamp_view_top(7), 7); + } + + #[test] + fn visible_distance_is_symmetric_and_skips_folds() { + let m = map(&[fold(2, 6)]); + // Visible order: 0,1,2,7,8 — line 8 is 4 visible steps from 0. + assert_eq!(m.visible_distance(0, 8), 4); + assert_eq!(m.visible_distance(8, 0), 4); + assert_eq!(m.visible_distance(2, 7), 1); + } + + #[test] + fn nth_visible_walks_forward_and_back_over_folds() { + let m = map(&[fold(2, 6)]); + assert_eq!(m.nth_visible_from(0, 3), 7); + assert_eq!(m.nth_visible_back(8, 4), 0); + // A hidden origin normalizes to its head first. + assert_eq!(m.nth_visible_from(5, 1), 7); + assert_eq!(m.nth_visible_back(5, 1), 1); + } + + #[test] + fn build_drops_a_fold_that_no_longer_spans_a_line() { + // start and end inside one line: nothing to hide. + let degenerate = ByteRange { start: 10, end: 12 }; + assert!(map(&[degenerate]).is_identity()); + } +} diff --git a/src/highlight.rs b/src/highlight.rs index 8a8914d..e7e5a98 100644 --- a/src/highlight.rs +++ b/src/highlight.rs @@ -413,7 +413,7 @@ impl View for SyntaxHighlightView { "syntax-highlight" } - fn render(&mut self, _buf: &Buffer, viewport: Viewport, cells: &mut CellGrid<'_>) { + fn render(&mut self, _buf: &Buffer, viewport: Viewport<'_>, cells: &mut CellGrid<'_>) { self.refresh_cache_if_stale(); let Some(bundle) = self.cache.bundle.clone() else { return; @@ -436,7 +436,11 @@ impl View for SyntaxHighlightView { // `compute_highlight_spans_for`) lets narrower captures override. for layer in &self.cache.layers { for row_offset in 0..max_rows { - let line_idx = start_line + row_offset; + // Arc 6 Stage 2: row `r` shows the `r`-th VISIBLE line, + // matching `TextView::render`'s walk (Q#FD13). + let line_idx = + u32::try_from(viewport.line_at_row_offset(start_line as usize, row_offset)) + .unwrap_or(u32::MAX); if line_idx >= total_lines { break; } @@ -593,7 +597,7 @@ impl View for LspStyleView { "lsp-style" } - fn render(&mut self, buf: &Buffer, viewport: Viewport, cells: &mut CellGrid<'_>) { + fn render(&mut self, buf: &Buffer, viewport: Viewport<'_>, cells: &mut CellGrid<'_>) { let Some(path) = buf.file_path() else { return; // No path ⇒ no URI ⇒ nothing to look up. }; @@ -644,7 +648,10 @@ impl View for LspStyleView { let total_lines = line_offsets.len() as u32; for row_offset in 0..max_rows { - let line_idx = start_line + row_offset; + // Arc 6 Stage 2: row `r` shows the `r`-th VISIBLE line. + let line_idx = + u32::try_from(viewport.line_at_row_offset(start_line as usize, row_offset)) + .unwrap_or(u32::MAX); if line_idx >= total_lines { break; } @@ -1058,6 +1065,7 @@ mod tests { cell_origin: CellCoord::new(0, 0), cell_size: CellSize::new(1, 20), gutter_w: 0, + folds: None, }; let registry = state.core.borrow().registry.clone(); let reg = registry.borrow(); @@ -1155,6 +1163,7 @@ mod tests { cell_origin: CellCoord::new(0, 0), cell_size: CellSize::new(1, 20), gutter_w: 0, + folds: None, }; let registry = state.core.borrow().registry.clone(); let reg = registry.borrow(); @@ -1274,6 +1283,7 @@ mod tests { cell_origin: CellCoord::new(0, 0), cell_size: CellSize::new(1, 20), gutter_w: 0, + folds: None, }; let registry = state.core.borrow().registry.clone(); let reg = registry.borrow(); @@ -1333,6 +1343,7 @@ mod tests { cell_origin: CellCoord::new(0, 0), cell_size: CellSize::new(rows as u32, cols as u32), gutter_w: 0, + folds: None, }; let registry = buf; // keep buf alive hv.render(®istry, viewport, &mut grid); @@ -1389,6 +1400,7 @@ mod tests { cell_origin: CellCoord::new(0, 0), cell_size: CellSize::new(rows as u32, cols as u32), gutter_w: 0, + folds: None, }; let registry = buf; // keep buf alive hv.render(®istry, viewport, &mut grid); diff --git a/src/hover.rs b/src/hover.rs index 6de7660..d32a86b 100644 --- a/src/hover.rs +++ b/src/hover.rs @@ -208,7 +208,7 @@ impl HoverView { } impl View for HoverView { - fn render(&mut self, _buf: &Buffer, viewport: Viewport, cells: &mut CellGrid<'_>) { + fn render(&mut self, _buf: &Buffer, viewport: Viewport<'_>, cells: &mut CellGrid<'_>) { let lines: Vec = { let guard = self.store.lock().expect("hover store poisoned"); guard diff --git a/src/lib.rs b/src/lib.rs index 392f7b7..e3971a3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -76,6 +76,7 @@ pub mod editor; pub mod editor_core; pub mod file_io; pub mod fold; +pub mod fold_view; pub mod font_pref; pub mod formatting; pub mod frontend; diff --git a/src/lua_bindings/mod.rs b/src/lua_bindings/mod.rs index 7e84899..4747a7c 100644 --- a/src/lua_bindings/mod.rs +++ b/src/lua_bindings/mod.rs @@ -1308,6 +1308,11 @@ fn run_buffer_edit( op: EditOp<'_>, bypass_intercept: bool, ) -> mlua::Result { + // Arc 6 Stage 2 (Q#FD19): the interactive-Lua-command unfold seam. + // Hooked HERE — above both `run_managed_edit` and `run_bypass_edit` — + // so an interactive command that passes `bypass_intercept` does not + // escape the widening. + unfold_before_interactive_lua_edit(lua, id); if bypass_intercept { run_bypass_edit(lua, id, op) } else { @@ -1315,6 +1320,50 @@ fn run_buffer_edit( } } +/// Unfold at the point before an **interactive** Lua-mutator edit +/// (comment-toggle, yank-pop, and any other `pmacs.buffer.X` mutation a +/// command body performs on the buffer the user is looking at). +/// +/// Unfolds only when **all** of: +/// +/// 1. [`InteractiveCommandOrigin::current`] is `Some(f)` — an +/// interactive command is running, and `f` is the authenticated +/// frontend that invoked it. This is the scoped authority that +/// distinguishes a user command's edit from a plugin's or the data +/// API's programmatic one; no command-history inference is involved, +/// and the guard clears even when the Lua command errors. +/// 2. The edited buffer **is `f`'s active-window buffer**. An explicit +/// mutation of some *other*, inactive buffer stays programmatic — it +/// is not an edit at the user's point. +/// 3. A fold actually contains that point (handled by +/// [`crate::fold::FoldRegistry::unfold_containing`], which is a no-op +/// otherwise). +/// +/// Matches Stage 1's data-API exemption: `pmacs.buffer.insert` called +/// from a plugin, a hook, or a bare Lua chunk unfolds nothing. +fn unfold_before_interactive_lua_edit(lua: &Lua, id: BufferId) { + let Some(origin) = lua + .app_data_ref::() + .and_then(|origin| origin.current()) + else { + return; // programmatic: no interactive command in scope + }; + let Some(folds) = lua.app_data_ref::() else { + return; + }; + let Some(core) = lua.app_data_ref::() else { + return; + }; + let core = core.borrow(); + let Some(window) = core.active_window_for(origin) else { + return; // the invoking frontend has no view (nothing to anchor on) + }; + if window.buffer_id != id { + return; // an inactive-buffer mutation stays programmatic + } + folds.unfold_containing(id, window.cursor); +} + fn run_bypass_edit(lua: &Lua, id: BufferId, op: EditOp<'_>) -> mlua::Result { with_registry_mut(lua, |r| { let buf = resolve_mut(r, id)?; diff --git a/src/menu.rs b/src/menu.rs index 5ce07a5..7de7bf7 100644 --- a/src/menu.rs +++ b/src/menu.rs @@ -378,7 +378,7 @@ impl View for MenuView { "context-menu" } - fn render(&mut self, _buf: &Buffer, viewport: Viewport, cells: &mut CellGrid<'_>) { + fn render(&mut self, _buf: &Buffer, viewport: Viewport<'_>, cells: &mut CellGrid<'_>) { let guard = self.menu.lock().expect("menu mutex poisoned"); let Some(menu) = guard.as_ref() else { return; diff --git a/src/overlay.rs b/src/overlay.rs index 5d41e28..cdbce35 100644 --- a/src/overlay.rs +++ b/src/overlay.rs @@ -101,7 +101,7 @@ impl StyleSpanOverlay { } impl View for StyleSpanOverlay { - fn render(&mut self, _buf: &Buffer, viewport: Viewport, cells: &mut CellGrid<'_>) { + fn render(&mut self, _buf: &Buffer, viewport: Viewport<'_>, cells: &mut CellGrid<'_>) { for span in &self.spans { if span.row >= viewport.cell_size.rows { continue; @@ -312,7 +312,7 @@ impl View for BufferStyleOverlay { Some(Box::new(self.clone())) } - fn render(&mut self, buf: &Buffer, viewport: Viewport, cells: &mut CellGrid<'_>) { + fn render(&mut self, buf: &Buffer, viewport: Viewport<'_>, cells: &mut CellGrid<'_>) { let spans = self .spans .lock() @@ -369,7 +369,7 @@ fn render_buffer_style_span( buf: &Buffer, line_offsets: &[u64], start_line: usize, - viewport: Viewport, + viewport: Viewport<'_>, cells: &mut CellGrid<'_>, span: BufferStyleSpan, ) { @@ -379,10 +379,11 @@ fn render_buffer_style_span( let first_line = line_at_offset(line_offsets, span.start); let last_line = line_at_offset(line_offsets, span.end.saturating_sub(1)); for line in first_line..=last_line { - if line < start_line { + // Arc 6 Stage 2: a span on a collapsed line paints nothing (that + // line has no row); one below a fold lands on its shifted-up row. + let Some(row_offset) = viewport.row_offset_of(start_line, line) else { continue; - } - let row_offset = (line - start_line) as u32; + }; if row_offset >= viewport.cell_size.rows { break; } @@ -457,7 +458,7 @@ impl VirtualCellOverlay { } impl View for VirtualCellOverlay { - fn render(&mut self, _buf: &Buffer, viewport: Viewport, cells: &mut CellGrid<'_>) { + fn render(&mut self, _buf: &Buffer, viewport: Viewport<'_>, cells: &mut CellGrid<'_>) { for vc in &self.cells { if vc.row >= viewport.cell_size.rows || vc.col >= viewport.cell_size.cols { continue; @@ -487,13 +488,14 @@ mod tests { vec![Cell::default(); (rows * cols) as usize] } - fn viewport(rows: u32, cols: u32) -> Viewport { + fn viewport(rows: u32, cols: u32) -> Viewport<'static> { Viewport { buffer_start: 0, buffer_end: u64::MAX, cell_origin: CellCoord::new(0, 0), cell_size: CellSize::new(rows, cols), gutter_w: 0, + folds: None, } } diff --git a/src/overlay_paint.rs b/src/overlay_paint.rs index af0ee7f..b16daae 100644 --- a/src/overlay_paint.rs +++ b/src/overlay_paint.rs @@ -144,21 +144,47 @@ pub fn paint_other_frontend_overlays( if g >= rect.size.cols { 0 } else { g } }; let text_cols = rect.size.cols.saturating_sub(gutter_w); + // Arc 6 Stage 2 (round-2 F2): the fold map belongs to THIS + // RECIPIENT WINDOW — built from its own buffer and line + // offsets, exactly like its `paint_frame` pass. A split + // showing two buffers derives two maps, and a peer in the + // unfolded pane is never projected through the folded one's. + let folds = crate::fold_view::map_for_window(&state.fold_registry, window); // Source's byte position → display coords via THIS // recipient window's text_view (the recipient's view - // of the buffer). - let Some(disp) = window - .text_view - .pos_to_display(buf, presence.snapshot.cursor) - else { + // of the buffer). Q#FD16: a peer cursor on a line this + // recipient has collapsed clamps to the head POSITION. + let peer_cursor = match folds.as_ref() { + Some(map) => map.visible_position( + window.text_view.line_at_offset(presence.snapshot.cursor), + presence.snapshot.cursor, + ), + None => presence.snapshot.cursor, + }; + let Some(disp) = window.text_view.pos_to_display(buf, peer_cursor) else { continue; }; // Filter to viewport visible range. `view_top` is the // top visible buffer-line; cells below it are in-frame - // until `view_top + inner_rows`. - let row_in_window = match (disp.row as usize).checked_sub(window.view_top) { - Some(r) if r < inner_rows as usize => r, - _ => continue, + // until `view_top + inner_rows` — counted in VISIBLE rows + // once this window has folds. + let row_in_window = match folds.as_ref() { + Some(map) => { + let top = map.clamp_view_top(window.view_top); + let row = disp.row as usize; + if row < top { + continue; + } + let r = map.visible_rows_between(top, row); + if r >= inner_rows as usize { + continue; + } + r + } + None => match (disp.row as usize).checked_sub(window.view_top) { + Some(r) if r < inner_rows as usize => r, + _ => continue, + }, }; // Column bounds: disp.col is the buffer column; window // doesn't horizontally scroll in v1.0, so cells past @@ -181,7 +207,16 @@ pub fn paint_other_frontend_overlays( (sel.active, sel.anchor) }; paint_selection_in_window( - grid, buf, window, rect, inner_rows, gutter_w, lo, hi, color, + grid, + buf, + window, + rect, + inner_rows, + gutter_w, + folds.as_ref(), + lo, + hi, + color, ); } } @@ -235,10 +270,21 @@ fn paint_selection_in_window( rect: Rect, inner_rows: u32, gutter_w: u32, + folds: Option<&crate::fold_view::VisibleLineMap>, lo: crate::rope::Position, hi: crate::rope::Position, color: Color, ) { + // Arc 6 Stage 2 (Q#FD16): endpoints on a collapsed line project to + // their component's head position; hidden interior bytes have no row + // and drop below. + let (lo, hi) = match folds { + Some(map) => ( + map.visible_position(window.text_view.line_at_offset(lo), lo), + map.visible_position(window.text_view.line_at_offset(hi), hi), + ), + None => (lo, hi), + }; if lo >= hi { return; } @@ -261,7 +307,15 @@ fn paint_selection_in_window( let Some(disp) = window.text_view.pos_to_display(buf, pos) else { break; }; - match (disp.row as usize).checked_sub(window.view_top) { + let row_in_window = match folds { + Some(map) if map.is_hidden(disp.row as usize) => None, + Some(map) => { + let top = map.clamp_view_top(window.view_top); + (disp.row as usize >= top).then(|| map.visible_rows_between(top, disp.row as usize)) + } + None => (disp.row as usize).checked_sub(window.view_top), + }; + match row_in_window { Some(r) if r < inner_rows as usize && disp.col < text_cols => { let grid_row = rect.origin.row + r as u32; let grid_col = rect.origin.col + gutter_w + disp.col; diff --git a/src/search.rs b/src/search.rs index 0ff1211..b77d231 100644 --- a/src/search.rs +++ b/src/search.rs @@ -411,7 +411,7 @@ impl View for SearchView { "search" } - fn render(&mut self, buf: &Buffer, viewport: Viewport, cells: &mut CellGrid<'_>) { + fn render(&mut self, buf: &Buffer, viewport: Viewport<'_>, cells: &mut CellGrid<'_>) { let buffer_id = buf.id(); // Snapshot the matches under the lock, release immediately // (same discipline as DiagnosticView). @@ -439,6 +439,9 @@ impl View for SearchView { let line_offsets = crate::diag::compute_line_offsets(&source); let start_line_buf = crate::diag::line_at_offset(&line_offsets, viewport.buffer_start as u32); + // Arc 6 Stage 2: source line → this viewport's row, `None` when + // the line is collapsed away (the wash then paints nothing). + let row_of = |line: u32| viewport.row_offset_of(start_line_buf as usize, line as usize); let max_rows = viewport.cell_size.rows; let max_cols = viewport.cell_size.cols; let cell_origin = viewport.cell_origin; @@ -479,17 +482,19 @@ impl View for SearchView { // Single-line matches (every literal match) touch one row. let first_line = crate::diag::line_at_offset(&line_offsets, m.start as u32); // Matches are sorted ascending, so once one starts below the - // viewport every later one does too — stop. - if first_line >= start_line_buf.saturating_add(max_rows) { + // viewport every later one does too — stop. Arc 6 Stage 2: + // measured in VISIBLE rows, since a collapse puts a far-away + // raw line back on screen; a hidden start yields no offset + // and falls through to the per-line skip below. + if row_of(first_line).is_some_and(|row| row >= max_rows) { break; } let last_byte = m.end.saturating_sub(1).max(m.start) as u32; let last_line = crate::diag::line_at_offset(&line_offsets, last_byte); for line in first_line..=last_line { - if line < start_line_buf { + let Some(row_offset) = row_of(line) else { continue; - } - let row_offset = line - start_line_buf; + }; if row_offset >= max_rows { break; } @@ -753,6 +758,7 @@ mod tests { cell_origin: CellCoord::new(0, 0), cell_size: CellSize::new(1, 10), gutter_w: 0, + folds: None, }, &mut grid, ); @@ -781,6 +787,7 @@ mod tests { cell_origin: CellCoord::new(0, 0), cell_size: CellSize::new(1, 10), gutter_w: 0, + folds: None, }, &mut grid2, ); @@ -822,6 +829,7 @@ mod tests { cell_origin: CellCoord::new(0, 0), cell_size: CellSize::new(rows, cols), gutter_w: 0, + folds: None, }, &mut grid, ); diff --git a/src/signature.rs b/src/signature.rs index 495e8bf..6121895 100644 --- a/src/signature.rs +++ b/src/signature.rs @@ -318,7 +318,7 @@ impl SignatureView { } impl View for SignatureView { - fn render(&mut self, _buf: &Buffer, viewport: Viewport, cells: &mut CellGrid<'_>) { + fn render(&mut self, _buf: &Buffer, viewport: Viewport<'_>, cells: &mut CellGrid<'_>) { let snap = self.snapshot(); let max_rows = viewport.cell_size.rows; let max_cols = viewport.cell_size.cols; diff --git a/src/text_view.rs b/src/text_view.rs index b94e03d..aae8c06 100644 --- a/src/text_view.rs +++ b/src/text_view.rs @@ -33,6 +33,10 @@ use crate::view::{DisplayCoord, View, Viewport}; /// [`TextView::pos_to_display`]; longer prefixes fall back to a heap buffer. const STACK_CAP: usize = 256; +/// Trailing marker painted after a collapsed region's head line (Arc 6 +/// Stage 2, Q#FD13). One column wide, so it never disturbs layout. +pub const FOLD_ELLIPSIS: char = '…'; + // --------------------------------------------------------------------------- // TextView // --------------------------------------------------------------------------- @@ -204,14 +208,26 @@ impl View for TextView { Some(line_start + walked_bytes as u64) } - fn render(&mut self, buf: &Buffer, viewport: Viewport, cells: &mut CellGrid<'_>) { - let start_line = self.line_at_offset(viewport.buffer_start); + fn render(&mut self, buf: &Buffer, viewport: Viewport<'_>, cells: &mut CellGrid<'_>) { + // Arc 6 Stage 2 (Q#FD13): row `r` shows the `r`-th VISIBLE source + // line at or after `view_top`; a collapsed region's lines are + // skipped entirely and the rows below shift up. Without a fold + // map this walk is the pre-folding `start_line + row_offset` + // identity. Folding is deliberately not an overlay — overlays + // repaint cells, they cannot delete rows. + let folds = viewport.folds.filter(|m| !m.is_identity()); + // `view_top` is clamped backward before the frame, but a caller + // that hands us a hidden start still gets its head. + let start_line = { + let raw = self.line_at_offset(viewport.buffer_start); + folds.map_or(raw, |m| m.visible_head_of(raw)) + }; let max_rows = viewport.cell_size.rows; let max_cols = viewport.cell_size.cols; let origin = viewport.cell_origin; + let mut line = start_line; for row_offset in 0..max_rows { - let line = start_line + row_offset as usize; let cell_row = origin.row + row_offset; // Always clear the visible row first so previous content does @@ -222,8 +238,10 @@ impl View for TextView { if line >= self.line_count() { continue; } + let this_line = line; + line = folds.map_or(this_line + 1, |m| m.next_visible(this_line)); - let line_bytes = self.read_line_bytes(buf, line); + let line_bytes = self.read_line_bytes(buf, this_line); let Ok(s) = std::str::from_utf8(&line_bytes) else { continue; }; @@ -267,6 +285,23 @@ impl View for TextView { } col += width; } + + // The head of a collapsed region carries a trailing ellipsis + // in the CONTENT area (Q#FD13/FD20): the authoritative, + // layout-neutral fold indicator, present in every gutter + // state, clipped like any long line. + if folds.is_some_and(|m| m.is_head(this_line)) { + for marker in [' ', FOLD_ELLIPSIS] { + if col >= max_cols { + break; + } + let cell = cells.at(CellCoord::new(cell_row, origin.col + col)); + cell.glyph = Glyph::Char(marker); + cell.style = Style::default(); + cell.attachment = None; + col += 1; + } + } } } } @@ -534,6 +569,7 @@ mod tests { cell_origin: CellCoord::new(0, 0), cell_size: CellSize::new(1, 16), gutter_w: 0, + folds: None, }, &mut grid, ); @@ -562,6 +598,7 @@ mod tests { cell_origin: CellCoord::new(0, 0), cell_size: CellSize::new(1, 16), gutter_w: 0, + folds: None, }, &mut grid, ); @@ -593,6 +630,7 @@ mod tests { cell_origin: CellCoord::new(0, 0), cell_size: CellSize::new(5, 5), gutter_w: 0, + folds: None, }, &mut grid, ); @@ -625,6 +663,7 @@ mod tests { cell_origin: CellCoord::new(0, 0), cell_size: CellSize::new(1, 5), gutter_w: 0, + folds: None, }, &mut grid, ); diff --git a/src/view.rs b/src/view.rs index 313f411..424fd9e 100644 --- a/src/view.rs +++ b/src/view.rs @@ -40,6 +40,7 @@ use crate::buffer::{Buffer, BufferError, BufferId, EditOp}; use crate::cell::{CellCoord, CellGrid, CellSize}; +use crate::fold_view::VisibleLineMap; use crate::rope::{Edit, Position}; // --------------------------------------------------------------------------- @@ -127,7 +128,7 @@ impl DisplayCoord { /// cell origin) and hands it to the view. The view fills cells inside that /// origin/size window. #[derive(Copy, Clone, Eq, PartialEq, Debug)] -pub struct Viewport { +pub struct Viewport<'a> { /// First byte in the buffer to consider rendering. pub buffer_start: Position, /// One past the last byte to consider. @@ -142,6 +143,58 @@ pub struct Viewport { /// `cell_origin.col - gutter_w`; overlays that only touch the text area /// ignore it (the origin is already shifted past the gutter). pub gutter_w: u32, + /// The rendering window's collapsed regions (Arc 6 Stage 2, Q#FD12), + /// or `None` when this window's buffer has no folds — the unfolded + /// path then stays byte-identical to the pre-folding renderer. + /// + /// Borrowed rather than owned so `Viewport` stays [`Copy`]: the frame + /// builds **one map per rendered window** (never one per frame — a + /// split may show different buffers) and hands the same shared + /// reference to every painter of that window. + pub folds: Option<&'a VisibleLineMap>, +} + +impl Viewport<'_> { + /// Row offset within this viewport for source `line`, given the + /// viewport's first (visible) source line. + /// + /// `None` when `line` is above `start_line` or collapsed away — a + /// hidden line simply has no row, so its painter skips it. Without a + /// map this is the identity `line - start_line` every consumer used + /// before folding. + /// + /// The result is monotonically non-decreasing in `line`, so a caller + /// that bounds its walk with `row_offset >= rows` may still `break`. + #[must_use] + pub fn row_offset_of(self, start_line: usize, line: usize) -> Option { + let raw = line.checked_sub(start_line)?; + let rows = match self.folds { + Some(map) if !map.is_identity() => { + if map.is_hidden(line) { + return None; + } + map.visible_rows_between(start_line, line) + } + _ => raw, + }; + u32::try_from(rows).ok() + } + + /// The inverse of [`Self::row_offset_of`]: the source line rendered + /// at `row_offset`, given the viewport's first (visible) source line. + /// + /// Painters that walk rows rather than spans (the syntax and LSP + /// style views) use this; the result may exceed the buffer's line + /// count, which those callers already bound. + #[must_use] + pub fn line_at_row_offset(self, start_line: usize, row_offset: u32) -> usize { + match self.folds { + Some(map) if !map.is_identity() => { + map.nth_visible_from(start_line, row_offset as usize) + } + _ => start_line + row_offset as usize, + } + } } // --------------------------------------------------------------------------- @@ -209,7 +262,7 @@ pub trait View { /// /// Default: no-op (used by views that participate in `on_edit` but not /// in rendering). - fn render(&mut self, _buf: &Buffer, _viewport: Viewport, _cells: &mut CellGrid<'_>) {} + fn render(&mut self, _buf: &Buffer, _viewport: Viewport<'_>, _cells: &mut CellGrid<'_>) {} /// Translate a buffer byte position to a display coordinate, if the /// view holds a meaningful mapping for that position. @@ -277,8 +330,45 @@ mod tests { #[test] fn viewport_is_copy() { // Compile-time assertion: Viewport must be Copy so the frontend can - // hand the same descriptor to multiple views without ceremony. + // hand the same descriptor to multiple views without ceremony. Arc 6 + // Stage 2 (Bet B7) keeps this true by borrowing the fold map — a + // shared reference is itself `Copy`. fn assert_copy() {} - assert_copy::(); + assert_copy::>(); + } + + #[test] + fn row_offset_without_folds_is_the_identity_map() { + let vp = Viewport { + buffer_start: 0, + buffer_end: 0, + cell_origin: CellCoord::new(0, 0), + cell_size: CellSize::new(10, 10), + gutter_w: 0, + folds: None, + }; + assert_eq!(vp.row_offset_of(4, 4), Some(0)); + assert_eq!(vp.row_offset_of(4, 9), Some(5)); + assert_eq!(vp.row_offset_of(4, 3), None, "above the viewport"); + } + + #[test] + fn row_offset_skips_hidden_lines_and_compacts_rows() { + // Fold heading line 1, hiding lines 2..=4 (8-byte lines). + let map = + VisibleLineMap::build(&[pmacs_protocol::ByteRange { start: 15, end: 39 }], |off| { + (off / 8) as usize + }); + let vp = Viewport { + buffer_start: 0, + buffer_end: 0, + cell_origin: CellCoord::new(0, 0), + cell_size: CellSize::new(10, 10), + gutter_w: 0, + folds: Some(&map), + }; + assert_eq!(vp.row_offset_of(0, 1), Some(1), "the head keeps its row"); + assert_eq!(vp.row_offset_of(0, 3), None, "hidden lines have no row"); + assert_eq!(vp.row_offset_of(0, 5), Some(2), "rows below shift up"); } } diff --git a/src/window.rs b/src/window.rs index 22add47..e1162c0 100644 --- a/src/window.rs +++ b/src/window.rs @@ -324,6 +324,28 @@ pub struct FrontendView { /// `layout` references (invariant: `layout.iter_ids()` contains /// `active`). pub active: WindowId, + /// Whether this frontend's display *projects* folds — i.e. whether + /// it collapses hidden lines away (Arc 6 Stage 2, Q#FD21). + /// + /// Motion, paging, wheel scrolling, click inverses, and the + /// auto-scroll clamp all live in shared + /// [`EditorCore`](crate::editor_core::EditorCore) code, but Stage 2 + /// collapses only the **grid** renderer — a `semantic_render` (GPU) + /// session still displays every source line until Stage 3, and both + /// kinds may attach to one buffer at once over the same shared fold + /// store. Reckoning in visible lines unconditionally would make that + /// GPU session's cursor skip lines it is still showing, so every + /// command/event-time visible-line reckoning is gated on the + /// **acting** frontend's flag. Render-time clamps need no gate: a + /// semantic session never enters `paint_frame`. + /// + /// Set at attach from the negotiated selected-render bit (grid ⇒ + /// `true`, semantic ⇒ `false`), cleared with the view at detach, and + /// `true` for [`FrontendId::LOCAL`](crate::protocol::FrontendId). + /// Deliberately has no `Default`: every construction site chooses + /// explicitly, so the projection is never inferred from a + /// `FrontendId` (**Bet B8**). + pub fold_projection: bool, } impl Layout { diff --git a/tests/compile_mode_acceptance.rs b/tests/compile_mode_acceptance.rs index 56be46c..377b094 100644 --- a/tests/compile_mode_acceptance.rs +++ b/tests/compile_mode_acceptance.rs @@ -250,6 +250,7 @@ fn render_active_window_to_grid( cell_origin: rect.origin, cell_size: CellSize::new(rect.size.rows, rect.size.cols), gutter_w: 0, + folds: None, }; let mut grid = CellGrid { cells: &mut backing, diff --git a/tests/folding_stage2_acceptance.rs b/tests/folding_stage2_acceptance.rs new file mode 100644 index 0000000..4f1a657 --- /dev/null +++ b/tests/folding_stage2_acceptance.rs @@ -0,0 +1,1265 @@ +// folding_stage2_acceptance.rs --- Arc 6 Stage 2 acceptance +// (docs/folding-stage2-framing.md, acceptance items 1–14). + +//! Grid (daemon-rendered) collapse. +//! +//! Every claim about what the user sees is asserted on the **rendered +//! cell grid** through the real `paint_frame` — the same pipeline the +//! daemon ships to a terminal client — not on the fold store or on a +//! painter in isolation. Folds are created through the real +//! `pmacs.fold` data API so the stored ranges are normalized exactly as +//! a user command would leave them. +//! +//! The fixture buffer is twelve four-byte lines (`L00\n` … `L11\n`), so +//! line `n` starts at `4n` and its content ends at `4n + 3` — which is +//! precisely a fold's `ByteRange::start` for a head line `n`. + +use crossterm::event::{ + KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers, MouseEvent, MouseEventKind, +}; +use pmacs::buffer::{BufferId, EditOp}; +use pmacs::cell::{Cell, CellCoord, CellGrid, CellSize, Glyph}; +use pmacs::editor::EditorState; +use pmacs::protocol::FrontendId; +use pmacs::window::{FrontendView, Layout, Window, WindowId}; + +// --------------------------------------------------------------------------- +// Harness +// --------------------------------------------------------------------------- + +/// Terminal geometry: 12 rows × 40 cols. `paint_frame` reserves the last +/// row for the status line and each window's last row for its mode line, +/// so a single window paints text into grid rows 0..=9. +const ROWS: u32 = 12; +const COLS: u32 = 40; +const TEXT_ROWS: u32 = 10; + +/// Bytes per fixture line (`"Lnn\n"`). +const LINE_BYTES: u64 = 4; + +fn fixture() -> String { + (0..12).fold(String::new(), |mut acc, n| { + use std::fmt::Write as _; + let _ = writeln!(acc, "L{n:02}"); + acc + }) +} + +/// A taller fixture for paging/scrolling: 80 five-byte lines +/// (`"Mnnn\n"`), so line `n` starts at `5n`. +fn long_fixture() -> String { + (0..80).fold(String::new(), |mut acc, n| { + use std::fmt::Write as _; + let _ = writeln!(acc, "M{n:03}"); + acc + }) +} + +/// Content-end byte of fixture line `n` — a fold's head `start`. +fn end_of(line: usize) -> u64 { + line as u64 * LINE_BYTES + 3 +} + +fn editor() -> EditorState { + let s = EditorState::new(); + exec(&s, "pmacs.lsp.config = {}"); + s +} + +fn exec(s: &EditorState, src: &str) { + s.lua_host.lua().load(src.to_string()).exec().unwrap(); +} + +fn eval(s: &EditorState, src: &str) -> T { + s.lua_host.lua().load(src.to_string()).eval().unwrap() +} + +fn active_id(s: &EditorState) -> BufferId { + s.core.borrow().active_buffer_id() +} + +/// Insert `text` at the start of `id` and notify every window, so the +/// per-window `TextView` line index matches what we are about to paint. +fn seed(s: &EditorState, id: BufferId, text: &str) { + let edit = { + let core = s.core.borrow(); + let registry = core.registry.clone(); + let mut reg = registry.borrow_mut(); + reg.get_mut(id) + .unwrap() + .apply_edit(EditOp::Insert { + pos: 0, + bytes: text.as_bytes(), + }) + .unwrap() + }; + s.core.borrow_mut().notify_buffer_edit(id, &edit); +} + +/// A buffer holding the fixture, seeded into the active window. +fn seeded() -> (EditorState, BufferId) { + let s = editor(); + let id = active_id(&s); + seed(&s, id, &fixture()); + (s, id) +} + +/// Collapse fixture lines `head + 1 ..= last_hidden` through the real +/// `pmacs.fold` data API. Panics if the range is rejected. +fn fold_lines(s: &EditorState, buffer: &str, head: usize, last_hidden: usize) { + let ok: bool = eval( + s, + &format!( + "return pmacs.fold.fold({buffer}, {{ start = {}, ['end'] = {} }})", + end_of(head), + end_of(last_hidden) + ), + ); + assert!(ok, "fold({head}..={last_hidden}) must be accepted"); +} + +/// Collapse in the *active* buffer. +fn fold_active(s: &EditorState, head: usize, last_hidden: usize) { + fold_lines(s, "pmacs.window.buffer()", head, last_hidden); +} + +/// Paint the full frame for `fid` and return the backing cells. +fn paint_for(state: &EditorState, fid: FrontendId) -> Vec { + let mut backing = vec![Cell::default(); (ROWS * COLS) as usize]; + let mut grid = CellGrid { + cells: &mut backing, + stride: COLS, + size: CellSize::new(ROWS, COLS), + }; + let _cursor = pmacs::editor::paint_frame( + state, + fid, + &std::collections::HashMap::new(), + &mut grid, + CellSize::new(ROWS, COLS), + ); + backing +} + +fn paint(state: &EditorState) -> Vec { + paint_for(state, FrontendId::LOCAL) +} + +/// Paint the full frame and return the terminal caret cell. +fn paint_caret(state: &EditorState) -> Option { + let mut backing = vec![Cell::default(); (ROWS * COLS) as usize]; + let mut grid = CellGrid { + cells: &mut backing, + stride: COLS, + size: CellSize::new(ROWS, COLS), + }; + pmacs::editor::paint_frame( + state, + FrontendId::LOCAL, + &std::collections::HashMap::new(), + &mut grid, + CellSize::new(ROWS, COLS), + ) +} + +fn at(cells: &[Cell], row: u32, col: u32) -> &Cell { + &cells[(row * COLS + col) as usize] +} + +fn row_text(cells: &[Cell], row: u32) -> String { + (0..COLS) + .map(|c| match at(cells, row, c).glyph { + Glyph::Char(ch) => ch, + _ => ' ', + }) + .collect::() + .trim_end() + .to_string() +} + +/// The text rows of a single-window frame, trimmed. +fn text_rows(cells: &[Cell]) -> Vec { + (0..TEXT_ROWS).map(|r| row_text(cells, r)).collect() +} + +fn key(code: KeyCode, mods: KeyModifiers) -> KeyEvent { + KeyEvent { + code, + modifiers: mods, + kind: KeyEventKind::Press, + state: KeyEventState::NONE, + } +} + +fn press(s: &mut EditorState, code: KeyCode) { + s.dispatch_key(FrontendId::LOCAL, key(code, KeyModifiers::NONE)); +} + +fn ctrl(s: &mut EditorState, c: char) { + s.dispatch_key( + FrontendId::LOCAL, + key(KeyCode::Char(c), KeyModifiers::CONTROL), + ); +} + +fn alt(s: &mut EditorState, c: char) { + s.dispatch_key(FrontendId::LOCAL, key(KeyCode::Char(c), KeyModifiers::ALT)); +} + +fn type_str(s: &mut EditorState, text: &str) { + for ch in text.chars() { + s.dispatch_key( + FrontendId::LOCAL, + key(KeyCode::Char(ch), KeyModifiers::NONE), + ); + } +} + +fn cursor_line(s: &EditorState) -> usize { + s.core.borrow().cursor_line() +} + +fn set_cursor(s: &EditorState, pos: u64) { + s.core.borrow_mut().set_cursor_byte(pos); +} + +fn view_top(s: &EditorState) -> usize { + s.core.borrow().active_window().view_top +} + +fn fold_count(s: &EditorState) -> usize { + let id = active_id(s); + s.fold_registry.folds(id).len() +} + +fn mouse(kind: MouseEventKind, row: u16, col: u16) -> MouseEvent { + MouseEvent { + kind, + column: col, + row, + modifiers: KeyModifiers::NONE, + } +} + +// --------------------------------------------------------------------------- +// 1. Collapse (framing acceptance 1) +// --------------------------------------------------------------------------- + +#[test] +fn collapse_omits_hidden_lines_and_shifts_rows_up() { + let (s, _id) = seeded(); + // Head = line 2, hidden = lines 3..=5. + fold_active(&s, 2, 5); + let cells = paint(&s); + let rows = text_rows(&cells); + + assert_eq!(rows[0], "L00"); + assert_eq!(rows[1], "L01"); + assert_eq!( + rows[2], "L02 …", + "the head keeps its text plus the ellipsis" + ); + // Lines 3..=5 are gone; the rows below shifted up. + assert_eq!(rows[3], "L06"); + assert_eq!(rows[4], "L07"); + assert_eq!(rows[5], "L08"); + assert!( + !rows + .iter() + .any(|r| r.starts_with("L03") || r.starts_with("L04") || r.starts_with("L05")), + "no hidden line renders any row: {rows:?}" + ); + // 12 source lines − 3 hidden = 9 content rows; row 9 is past the end. + assert_eq!(rows[8], "L11"); + assert_eq!( + rows[9], "", + "content row count equals the visible-line count" + ); +} + +#[test] +fn unfolded_frame_is_identical_to_the_pre_folding_baseline() { + // The `None` map path must be byte-identical, not merely equivalent. + let (s, _id) = seeded(); + let baseline = paint(&s); + fold_active(&s, 2, 5); + let folded = paint(&s); + assert_ne!(baseline, folded, "the fixture actually folds"); + + let (s2, _id2) = seeded(); + assert_eq!(baseline, paint(&s2), "no folds ⇒ unchanged rendering"); +} + +// --------------------------------------------------------------------------- +// 2. Head marker in both gutter states (framing acceptance 2, round-1 F3) +// --------------------------------------------------------------------------- + +#[test] +fn head_marker_gutter_off_is_ellipsis_only() { + let (s, _id) = seeded(); + fold_active(&s, 2, 5); + // Line numbers default to Off ⇒ gutter_width() == 0 ⇒ no sign cell. + let cells = paint(&s); + assert_eq!(row_text(&cells, 2), "L02 …"); + assert_eq!( + at(&cells, 2, 0).glyph, + Glyph::Char('L'), + "with no gutter the text still starts at column 0 — no width change" + ); +} + +#[test] +fn head_marker_gutter_on_adds_the_fold_glyph() { + let (s, _id) = seeded(); + fold_active(&s, 2, 5); + exec(&s, "pmacs.window.set_line_numbers('absolute')"); + let cells = paint(&s); + // 12 lines ⇒ 2 digits + 2 pad = 4-cell gutter; the fold glyph takes + // the leading pad cell, the number is right-aligned before the + // trailing pad, and the text begins at column 4. + assert_eq!( + at(&cells, 2, 0).glyph, + Glyph::Char('▸'), + "head row carries the gutter fold glyph" + ); + assert_eq!(row_text(&cells, 2), "▸ 3 L02 …"); + // A non-head row has no glyph. + assert_eq!(at(&cells, 1, 0).glyph, Glyph::Char(' ')); + assert_eq!(row_text(&cells, 1), " 2 L01"); +} + +#[test] +fn a_diagnostic_on_the_head_row_beats_the_fold_glyph() { + let (s, _id) = seeded(); + fold_active(&s, 2, 5); + exec(&s, "pmacs.window.set_line_numbers('absolute')"); + attach_diags( + &s, + vec![diag_on(pmacs::diag::DiagnosticSeverity::Error, 2, 2)], + ); + let cells = paint(&s); + assert_eq!( + at(&cells, 2, 0).glyph, + Glyph::Char('E'), + "the diagnostic sign wins the shared cell (Q#FD20)" + ); +} + +// --------------------------------------------------------------------------- +// 3. Line numbers (framing acceptance 3, Q#FD14) +// --------------------------------------------------------------------------- + +#[test] +fn absolute_numbers_skip_hidden_lines() { + let (s, _id) = seeded(); + fold_active(&s, 2, 5); + exec(&s, "pmacs.window.set_line_numbers('absolute')"); + let cells = paint(&s); + // Head shows 3, then the column jumps straight to 7 (line 6). + assert_eq!(row_text(&cells, 2), "▸ 3 L02 …"); + assert_eq!(row_text(&cells, 3), " 7 L06"); + assert_eq!(row_text(&cells, 4), " 8 L07"); +} + +#[test] +fn relative_numbers_measure_visible_distance_across_a_fold() { + let (s, _id) = seeded(); + fold_active(&s, 2, 5); + set_cursor(&s, 0); // cursor on line 0 + exec(&s, "pmacs.window.set_line_numbers('relative')"); + let cells = paint(&s); + // Visible order: 0, 1, 2(head), 6, 7 … so line 6 is 3 visible steps + // from the cursor line, not 6 raw lines. + assert_eq!(row_text(&cells, 0), " 0 L00"); + assert_eq!(row_text(&cells, 1), " 1 L01"); + assert_eq!(row_text(&cells, 2), "▸ 2 L02 …"); + assert_eq!(row_text(&cells, 3), " 3 L06"); + assert_eq!(row_text(&cells, 4), " 4 L07"); +} + +#[test] +fn hybrid_shows_absolute_on_the_visible_cursor_row() { + let (s, _id) = seeded(); + fold_active(&s, 2, 5); + // Cursor hidden on line 4 (a shared fold left it there). + set_cursor(&s, end_of(4)); + exec(&s, "pmacs.window.set_line_numbers('hybrid')"); + let cells = paint(&s); + // The anchor is the visible head (line 2), which shows absolute 3. + assert_eq!(row_text(&cells, 2), "▸ 3 L02 …"); + assert_eq!(row_text(&cells, 1), " 1 L01"); + assert_eq!(row_text(&cells, 3), " 1 L06", "one visible step below"); +} + +// --------------------------------------------------------------------------- +// 4. Diagnostic clamp (framing acceptance 4, Q#FD15) +// --------------------------------------------------------------------------- + +fn diag_on( + severity: pmacs::diag::DiagnosticSeverity, + line: u32, + end_col: u32, +) -> pmacs::diag::Diagnostic { + pmacs::diag::Diagnostic { + start_line: line, + start_col: 0, + end_line: line, + end_col, + severity, + message: "boom".into(), + source: None, + code: None, + } +} + +/// Attach the diagnostic overlay through the real Lua path and publish +/// `diags` for the active buffer. +fn attach_diags(state: &EditorState, diags: Vec) { + let uri: String = eval( + state, + r#" + local buf = pmacs.window.buffer() + local uri = "file:///tmp/folding_stage2_diag.rs" + assert(pmacs.diag._attach_view(buf, uri)) + return uri + "#, + ); + { + let core = state.core.borrow(); + let registry = core.registry.clone(); + let mut reg = registry.borrow_mut(); + let buf = reg.get_mut(core.active_buffer_id()).unwrap(); + buf.set_file_path(Some(std::path::PathBuf::from( + "/tmp/folding_stage2_diag.rs", + ))); + } + state + .lsp_manager + .borrow() + .diag_store() + .lock() + .expect("diag store lock") + .set(uri, diags); +} + +#[test] +fn a_hidden_diagnostic_clamps_to_the_outermost_visible_head() { + let (s, _id) = seeded(); + // Outer fold: head 0, hides 1..=9. Inner fold: head 3, hides 4..=6. + fold_active(&s, 0, 9); + fold_active(&s, 3, 6); + exec(&s, "pmacs.window.set_line_numbers('absolute')"); + // A warning on the outer body and an ERROR on a NESTED inner line. + attach_diags( + &s, + vec![ + diag_on(pmacs::diag::DiagnosticSeverity::Warning, 2, 3), + diag_on(pmacs::diag::DiagnosticSeverity::Error, 5, 3), + ], + ); + let cells = paint(&s); + assert_eq!( + at(&cells, 0, 0).glyph, + Glyph::Char('E'), + "most-severe of the head and every line its fold hides, including \ + a nested inner-fold line, surfaces on the OUTERMOST visible head" + ); + // Nothing leaks onto the rows below the collapse. + assert_eq!(row_text(&cells, 1), " 11 L10"); + assert_eq!(at(&cells, 1, 0).glyph, Glyph::Char(' ')); +} + +// --------------------------------------------------------------------------- +// 5. Nested fold / shared cursor (framing acceptance 5, round-1 F1) +// --------------------------------------------------------------------------- + +#[test] +fn nested_fold_with_a_deeply_hidden_cursor_resolves_outermost() { + let (s, _id) = seeded(); + fold_active(&s, 0, 9); // outer: hides 1..=9 + fold_active(&s, 3, 6); // inner: head 3 is itself hidden + // A second frontend folded through the shared store while this + // window's logical cursor sat deep inside the nest. + set_cursor(&s, end_of(5)); + exec(&s, "pmacs.window.set_line_numbers('relative')"); + + let caret = paint_caret(&s).expect("caret is on screen"); + assert_eq!(caret.row, 0, "caret renders on the OUTERMOST visible head"); + + let cells = paint(&s); + assert_eq!(row_text(&cells, 0), "▸ 0 L00 …", "relative anchors there"); + assert_eq!(row_text(&cells, 1), " 1 L10"); +} + +#[test] +fn a_view_top_inside_a_nest_clamps_backward_to_the_head() { + let (s, _id) = seeded(); + fold_active(&s, 0, 9); + fold_active(&s, 3, 6); + s.core.borrow_mut().active_window_mut().view_top = 5; + let cells = paint(&s); + assert_eq!( + view_top(&s), + 0, + "clamped BACKWARD to the head, not forward past the fold" + ); + assert_eq!(row_text(&cells, 0), "L00 …"); +} + +// --------------------------------------------------------------------------- +// 6. Caret / selection / peer column projection +// (framing acceptance 6, round-2 F3 + round-3 F2) +// --------------------------------------------------------------------------- + +#[test] +fn a_hidden_caret_lands_at_the_heads_end_of_content_column() { + let s = editor(); + let id = active_id(&s); + // Line 0 is deliberately SHORT and the hidden line is LONG, so a + // row-only clamp would leave the caret at a column the head does not + // even have. + seed(&s, id, "ab\nxxxxxxxxxxxxxxxxxxxx\ncd\nef\n"); + let ok: bool = eval( + &s, + "return pmacs.fold.fold(pmacs.window.buffer(), { start = 2, ['end'] = 23 })", + ); + assert!(ok); + // Cursor at column 15 of the hidden line 1. + set_cursor(&s, 3 + 15); + let caret = paint_caret(&s).expect("caret on screen"); + assert_eq!(caret.row, 0, "the head row"); + assert_eq!( + caret.col, 2, + "the head's end-of-content column, never the raw hidden column" + ); +} + +#[test] +fn crossing_folds_project_a_point_to_the_first_visible_head() { + // Round-3 F2. A hides lines 1..=3 (head 0); B is headed on line 2 — + // itself hidden by A — and hides 3..=5. A point on line 5 is directly + // inside only B, whose `range.start` is hidden. + let (s, _id) = seeded(); + fold_active(&s, 0, 3); + fold_active(&s, 2, 5); + assert_eq!(fold_count(&s), 2, "both crossing folds are stored"); + + set_cursor(&s, end_of(5)); + let caret = paint_caret(&s).expect("caret on screen"); + assert_eq!(caret.row, 0, "A's visible head, never B's hidden start"); + assert_eq!(caret.col, 3, "L00's end-of-content column"); + + let cells = paint(&s); + let rows = text_rows(&cells); + assert_eq!(rows[0], "L00 …"); + assert_eq!(rows[1], "L06", "lines 1..=5 all collapse under head 0"); +} + +#[test] +fn a_selection_spanning_a_fold_paints_only_visible_rows() { + let (s, _id) = seeded(); + fold_active(&s, 2, 5); + // Select from the middle of line 1 through the middle of line 7. + { + let mut core = s.core.borrow_mut(); + let aw = core.active_window_mut(); + aw.selection = Some(pmacs::window::Selection { anchor: 5 }); + aw.cursor = end_of(7) - 1; + } + let cells = paint(&s); + let washed = |row: u32| { + (0..COLS) + .filter(|c| at(&cells, row, *c).style.reverse) + .count() + }; + assert!(washed(1) > 0, "row 1 (L01) is partly selected"); + assert!(washed(2) > 0, "row 2 (the visible head) is selected"); + assert!(washed(3) > 0, "row 3 (L06, shifted up) is selected"); + // Every painted row is a visible line; nothing renders for 3..=5 at + // all, so there is no hidden row to wash. + assert_eq!(row_text(&cells, 3), "L06"); + assert!(washed(5) == 0, "past the selection end"); +} + +#[test] +fn a_click_never_lands_on_a_hidden_line() { + let (s, _id) = seeded(); + fold_active(&s, 2, 5); + let mut s = s; + // Grid row 3 shows L06 (line 6) after the collapse. + s.dispatch_mouse( + FrontendId::LOCAL, + mouse( + MouseEventKind::Down(crossterm::event::MouseButton::Left), + 3, + 0, + ), + CellSize::new(ROWS, COLS), + ); + assert_eq!(cursor_line(&s), 6, "the 3rd VISIBLE line, not line 3"); +} + +#[test] +fn a_peer_cursor_on_a_hidden_line_clamps_to_the_head_position() { + let (s, _id) = seeded(); + let id = active_id(&s); + fold_active(&s, 2, 5); + let presence = pmacs::overlay_paint::OtherPresence { + frontend_id: FrontendId(2), + snapshot: pmacs::presence::PresenceSnapshot { + buffer_id: id, + cursor: end_of(4), // hidden line 4 + selection: None, + }, + color_slot: 0, + }; + let mut backing = vec![Cell::default(); (ROWS * COLS) as usize]; + let mut grid = CellGrid { + cells: &mut backing, + stride: COLS, + size: CellSize::new(ROWS, COLS), + }; + pmacs::overlay_paint::paint_other_frontend_overlays( + &s, + &mut grid, + CellSize::new(ROWS, COLS), + &[presence], + ); + // The peer paints on the head row at the head's end-of-content column. + assert!( + backing[(2 * COLS + 3) as usize].style.reverse, + "peer cursor clamped onto the visible head row/column" + ); + for row in 3..6u32 { + assert!( + (0..COLS).all(|c| !backing[(row * COLS + c) as usize].style.reverse), + "nothing painted on a row a hidden line would have owned ({row})" + ); + } +} + +// --------------------------------------------------------------------------- +// 7. Ordinary overlays across a fold (framing acceptance 7) +// --------------------------------------------------------------------------- + +#[test] +fn a_search_wash_across_a_fold_paints_only_visible_rows_correctly() { + let (s, _id) = seeded(); + fold_active(&s, 2, 5); + let mut s = s; + // Search for "L0" — matches on lines 0..=9, several of them hidden. + ctrl(&mut s, 's'); + type_str(&mut s, "L07"); + press(&mut s, KeyCode::Enter); + + let cells = paint(&s); + // L07 is on grid row 4 after the collapse (0,1,2,L06,L07). + assert_eq!(row_text(&cells, 4), "L07"); + let washed_row_4 = (0..COLS) + .filter(|c| at(&cells, 4, *c).style != pmacs::cell::Style::default()) + .count(); + assert!( + washed_row_4 > 0, + "the match washes the row the folded frame actually put it on" + ); + // Row 1 (L01) holds no match and stays unwashed. + assert!( + (0..COLS).all(|c| at(&cells, 1, c).style == pmacs::cell::Style::default()), + "no wash bleeds onto an unmatched visible row" + ); +} + +// --------------------------------------------------------------------------- +// 8. Viewport / paging / indicator (framing acceptance 8, Q#FD18) +// --------------------------------------------------------------------------- + +#[test] +fn page_down_advances_by_a_screenful_of_visible_lines() { + let s = editor(); + let id = active_id(&s); + let long = long_fixture(); + seed(&s, id, &long); + // Hide lines 1..=40 under head 0. + let ok: bool = eval( + &s, + "return pmacs.fold.fold(pmacs.window.buffer(), { start = 4, ['end'] = 204 })", + ); + assert!(ok); + let _ = paint(&s); // establish last_visible_rows + let before = cursor_line(&s); + assert_eq!(before, 0); + s.core.borrow_mut().move_page_down(); + let after = cursor_line(&s); + assert!( + after > 40, + "a screenful of VISIBLE lines steps past the whole collapse (landed on {after})" + ); + assert!( + !s.fold_registry + .folds(id) + .iter() + .any(|f| f.start < end_of_line_start(after) && end_of_line_start(after) <= f.end), + "the cursor never comes to rest on a hidden line" + ); +} + +/// Byte at the start of fixture-independent line `n` for a 5-byte line +/// fixture (`"Mnnn\n"`). +fn end_of_line_start(line: usize) -> u64 { + line as u64 * 5 +} + +#[test] +fn the_mode_line_indicator_reckons_in_visible_lines() { + let (s, _id) = seeded(); + // 12 lines in a 10-row window: not All. + let plain = paint(&s); + assert!( + row_text(&plain, 10).contains("Top"), + "unfolded 12 lines in 10 rows reads Top: {}", + row_text(&plain, 10) + ); + // Collapse 3 lines away → 9 visible lines fit in 10 rows → All. + fold_active(&s, 2, 5); + let folded = paint(&s); + assert!( + row_text(&folded, 10).contains("All"), + "9 visible lines fit the viewport: {}", + row_text(&folded, 10) + ); +} + +#[test] +fn goto_line_into_a_fold_leaves_its_head_visible() { + let s = editor(); + let id = active_id(&s); + let long = long_fixture(); + seed(&s, id, &long); + // Hide lines 51..=70 under head 50. + let ok: bool = eval( + &s, + "return pmacs.fold.fold(pmacs.window.buffer(), { start = 254, ['end'] = 354 })", + ); + assert!(ok); + s.core.borrow_mut().move_to_line(60); // a hidden line + let _ = paint(&s); + let top = view_top(&s); + assert!(top <= 50, "view_top is at or above the head (got {top})"); + assert!( + top + 10 > 50, + "and the head itself is inside the viewport (top {top})" + ); + assert_eq!(fold_count(&s), 1, "goto-line does NOT auto-unfold"); +} + +// --------------------------------------------------------------------------- +// 9. Vertical motion (framing acceptance 9, Q#FD17) +// --------------------------------------------------------------------------- + +#[test] +fn next_line_steps_across_a_collapsed_region_in_one_motion() { + let (s, _id) = seeded(); + fold_active(&s, 2, 5); + let mut s = s; + set_cursor(&s, 0); + press(&mut s, KeyCode::Down); + assert_eq!(cursor_line(&s), 1); + press(&mut s, KeyCode::Down); + assert_eq!(cursor_line(&s), 2, "the head"); + press(&mut s, KeyCode::Down); + assert_eq!(cursor_line(&s), 6, "one step clears the whole collapse"); + press(&mut s, KeyCode::Up); + assert_eq!(cursor_line(&s), 2, "and one step back returns to the head"); +} + +#[test] +fn motion_from_a_hidden_cursor_normalizes_to_the_visible_head_first() { + let (s, _id) = seeded(); + fold_active(&s, 2, 5); + let mut s = s; + // A shared fold left the logical cursor deep inside the collapse. + set_cursor(&s, end_of(4)); + press(&mut s, KeyCode::Down); + assert_eq!( + cursor_line(&s), + 6, + "normalize to head 2, then step to the next visible line" + ); + + set_cursor(&s, end_of(4)); + press(&mut s, KeyCode::Up); + assert_eq!( + cursor_line(&s), + 1, + "normalize to head 2, then step to the previous visible line" + ); +} + +// --------------------------------------------------------------------------- +// 10. Interactive unfold widening (framing acceptance 10, Q#FD19) +// --------------------------------------------------------------------------- + +#[test] +fn yank_at_a_point_inside_a_fold_unfolds_it() { + let (s, _id) = seeded(); + let mut s = s; + // Kill line 0's text so the kill ring has content. + set_cursor(&s, 0); + ctrl(&mut s, 'k'); + // Re-fold with the shortened buffer: head 2, hidden 3..=5 of the + // remaining text. Recompute from the live line index. + let (head_end, last_end) = line_ends(&s, 2, 5); + fold_range(&s, head_end, last_end); + assert_eq!(fold_count(&s), 1); + + // Put point INSIDE the fold and yank. + set_cursor(&s, last_end); + ctrl(&mut s, 'y'); + assert_eq!( + fold_count(&s), + 0, + "yank at a point inside a fold unfolds it" + ); +} + +#[test] +fn query_replace_inside_a_fold_unfolds_it() { + let (s, _id) = seeded(); + let mut s = s; + fold_active(&s, 2, 5); + assert_eq!(fold_count(&s), 1); + set_cursor(&s, 0); + // M-% L04 RET Q04 RET, then `y` to replace the first match — which + // is on hidden line 4. + alt(&mut s, '%'); + type_str(&mut s, "L04"); + press(&mut s, KeyCode::Enter); + type_str(&mut s, "Q04"); + press(&mut s, KeyCode::Enter); + type_str(&mut s, "y"); + assert_eq!( + fold_count(&s), + 0, + "the replacement's edit point was inside the fold" + ); +} + +/// Define a command whose body is `body`, then run it the way a user +/// does — `M-x RET`. The **Rust** dispatch path is what installs +/// the `InteractiveCommandOrigin` scope (`pmacs.command.invoke_interactive` +/// alone does not), so the widening must be driven through it. +fn define(s: &EditorState, name: &str, body: &str) { + exec( + s, + &format!( + "pmacs.command.define {{ name = '{name}', \ + description = 'stage 2 test', fn = function() {body} end }}" + ), + ); +} + +fn m_x(s: &mut EditorState, name: &str) { + alt(s, 'x'); + type_str(s, name); + press(s, KeyCode::Enter); +} + +#[test] +fn an_interactive_lua_mutator_unfolds_but_a_programmatic_one_does_not() { + let (s, _id) = seeded(); + let mut s = s; + fold_active(&s, 2, 5); + set_cursor(&s, end_of(4)); // point inside the fold + + // A PROGRAMMATIC data-API edit: no interactive command in scope. + // (Insert at 0 is before the fold, so it only translates it right.) + exec(&s, "pmacs.window.buffer():insert(0, 'x')"); + assert_eq!( + fold_count(&s), + 1, + "a bare data-API mutation stays programmatic — no unfold" + ); + + // The same call from INSIDE an interactive command unfolds. + define(&s, "test.poke", "pmacs.window.buffer():insert(0, 'y')"); + set_cursor(&s, end_of(4) + 1); // still inside the shifted fold + m_x(&mut s, "test.poke"); + assert_eq!( + fold_count(&s), + 0, + "an interactive command's edit at the point unfolds (Q#FD19)" + ); +} + +#[test] +fn a_bypass_intercept_interactive_edit_also_unfolds() { + // Pins the seam at `run_buffer_edit`, above the managed/bypass split: + // hooking only `run_managed_edit` would let this one escape. + let (s, _id) = seeded(); + let mut s = s; + fold_active(&s, 2, 5); + set_cursor(&s, end_of(4)); + define( + &s, + "test.bypass", + "pmacs.window.buffer():insert(0, 'z', { bypass_intercept = true })", + ); + m_x(&mut s, "test.bypass"); + assert_eq!( + fold_count(&s), + 0, + "a bypass_intercept interactive edit must not escape the widening" + ); +} + +#[test] +fn an_interactive_edit_to_an_inactive_buffer_does_not_unfold() { + let (s, _id) = seeded(); + let mut s = s; + fold_active(&s, 2, 5); + set_cursor(&s, end_of(4)); + // A second, NOT-displayed buffer; the command edits that one. + exec( + &s, + "other = pmacs.buffer.from_bytes('other.txt', 'aaa\\nbbb\\n')", + ); + define(&s, "test.other", "other:insert(0, 'q')"); + m_x(&mut s, "test.other"); + assert_eq!( + fold_count(&s), + 1, + "an explicit inactive-buffer mutation stays programmatic" + ); +} + +#[test] +fn undo_and_redo_do_not_unfold() { + // Explicitly DEFERRED in the framing (round-1 F5 ruling); pinned so + // the deferral is a decision, not an accident. + let (s, _id) = seeded(); + let mut s = s; + set_cursor(&s, end_of(7)); + type_str(&mut s, "Z"); // an edit to undo, outside any fold + fold_active(&s, 2, 5); + set_cursor(&s, end_of(4)); + ctrl(&mut s, '_'); // undo + assert_eq!(fold_count(&s), 1, "undo does not unfold (deferred)"); +} + +/// Content-end bytes of the current buffer's lines `a` and `b`. +fn line_ends(s: &EditorState, a: usize, b: usize) -> (u64, u64) { + let core = s.core.borrow(); + let registry = core.registry.clone(); + let reg = registry.borrow(); + let buf = reg.get(core.active_buffer_id()).unwrap(); + let tv = &core.active_window().text_view; + let end = |line: usize| tv.line_offset(line).unwrap() + tv.line_len(buf, line).unwrap(); + (end(a), end(b)) +} + +fn fold_range(s: &EditorState, start: u64, end: u64) { + let ok: bool = eval( + s, + &format!( + "return pmacs.fold.fold(pmacs.window.buffer(), {{ start = {start}, ['end'] = {end} }})" + ), + ); + assert!(ok, "fold({start}..{end}) must be accepted"); +} + +// --------------------------------------------------------------------------- +// 11. No wire / protocol change (framing acceptance 11, Bet B6) +// --------------------------------------------------------------------------- + +#[test] +fn stage_2_bumps_no_protocol_version() { + assert_eq!( + pmacs::protocol::PROTOCOL_VERSION, + 19, + "Stage 2 is entirely daemon-side: the TUI collapse ships no new wire data" + ); + assert_eq!( + *pmacs::protocol::SUPPORTED_PROTOCOL_VERSIONS.last().unwrap(), + 19 + ); +} + +// --------------------------------------------------------------------------- +// 12. Shared store, independent viewports (framing acceptance 12) +// --------------------------------------------------------------------------- + +#[test] +fn two_windows_on_one_buffer_both_collapse_with_their_own_view_tops() { + let (s, _id) = seeded(); + fold_active(&s, 2, 5); + exec(&s, "pmacs.window.split_horizontal()"); + // Give the second window a different view_top (visible line 2 = head). + let ids: Vec = s.core.borrow().windows.keys().copied().collect(); + assert_eq!(ids.len(), 2, "the split produced two windows"); + s.core + .borrow_mut() + .windows + .get_mut(&ids[1]) + .unwrap() + .view_top = 1; + + let cells = paint(&s); + // 12 rows: status row 11; two stacked windows of 5 and 6 rows, each + // with a mode line. Window A text rows 0..=3, window B text rows + // 5..=9 (its own mode line last). + assert_eq!(row_text(&cells, 0), "L00"); + assert_eq!(row_text(&cells, 2), "L02 …", "window A collapses"); + let b_rows: Vec = (5..10).map(|r| row_text(&cells, r)).collect(); + assert!( + b_rows.iter().any(|r| r == "L02 …"), + "window B collapses too, from its own view_top: {b_rows:?}" + ); + assert!( + !b_rows.iter().any(|r| r.starts_with("L03")), + "no hidden line renders in the second window either: {b_rows:?}" + ); + // Both windows keep their own view_top. + assert_eq!(s.core.borrow().windows[&ids[1]].view_top, 1); +} + +// --------------------------------------------------------------------------- +// 13. Frontend-scoped motion (framing acceptance 13, round-2 F1 / Q#FD21) +// --------------------------------------------------------------------------- + +/// Register a second frontend on the SAME buffer, with an explicit +/// projection choice — the attach-time decision the daemon makes from the +/// negotiated selected-render bit. +fn attach_frontend(s: &EditorState, fid: FrontendId, fold_projection: bool) -> WindowId { + let mut core = s.core.borrow_mut(); + let buffer_id = core.active_buffer_id(); + let text_view = { + let registry = core.registry.clone(); + let reg = registry.borrow(); + pmacs::text_view::TextView::new(reg.get(buffer_id).unwrap()) + }; + let win_id = WindowId::next(); + core.windows + .insert(win_id, Window::new(win_id, buffer_id, text_view)); + core.register_frontend_view( + fid, + FrontendView { + layout: Layout::single(win_id), + active: win_id, + fold_projection, + }, + ); + win_id +} + +/// Move down once as `fid` and report that frontend's resulting line. +fn move_down_as(s: &EditorState, fid: FrontendId, win_id: WindowId) -> usize { + let mut core = s.core.borrow_mut(); + core.active_frontend = fid; + core.move_down(); + let win = &core.windows[&win_id]; + win.text_view.line_at_offset(win.cursor) +} + +fn move_up_as(s: &EditorState, fid: FrontendId, win_id: WindowId) -> usize { + let mut core = s.core.borrow_mut(); + core.active_frontend = fid; + core.move_up(); + let win = &core.windows[&win_id]; + win.text_view.line_at_offset(win.cursor) +} + +#[test] +fn a_semantic_frontend_keeps_raw_line_motion_while_the_tui_folds() { + let (s, _id) = seeded(); + fold_active(&s, 2, 5); + // A grid session and a semantic session on the same buffer, sharing + // the fold store (daemon.rs: a frontend holds exactly one of a + // RenderState or a SemanticRenderState; both may attach at once). + let grid_win = attach_frontend(&s, FrontendId(2), true); + let sem_win = attach_frontend(&s, FrontendId(3), false); + for win in [grid_win, sem_win] { + s.core.borrow_mut().windows.get_mut(&win).unwrap().cursor = end_of(2); + } + + assert_eq!( + move_down_as(&s, FrontendId(2), grid_win), + 6, + "the grid session steps by VISIBLE lines, skipping the collapse" + ); + assert_eq!( + move_down_as(&s, FrontendId(3), sem_win), + 3, + "the semantic session still DISPLAYS line 3, so it must land there" + ); + assert_eq!( + move_up_as(&s, FrontendId(3), sem_win), + 2, + "and steps back by raw lines too" + ); +} + +#[test] +fn semantic_paging_stays_raw_while_the_grid_pages_visibly() { + let s = editor(); + let id = active_id(&s); + let long = long_fixture(); + seed(&s, id, &long); + // Hide lines 1..=40 under head 0. + let ok: bool = eval( + &s, + "return pmacs.fold.fold(pmacs.window.buffer(), { start = 4, ['end'] = 204 })", + ); + assert!(ok); + let grid_win = attach_frontend(&s, FrontendId(2), true); + let sem_win = attach_frontend(&s, FrontendId(3), false); + + let page_as = |fid: FrontendId, win: WindowId| { + let mut core = s.core.borrow_mut(); + core.active_frontend = fid; + core.move_page_down(); + let w = &core.windows[&win]; + w.text_view.line_at_offset(w.cursor) + }; + let grid_line = page_as(FrontendId(2), grid_win); + let sem_line = page_as(FrontendId(3), sem_win); + assert!( + grid_line > 40, + "a grid page clears the whole collapse (landed on {grid_line})" + ); + assert!( + sem_line <= 40, + "a semantic page counts the lines it still shows (landed on {sem_line})" + ); +} + +#[test] +fn detaching_the_semantic_frontend_leaves_the_grid_unaffected() { + let (s, _id) = seeded(); + fold_active(&s, 2, 5); + let grid_win = attach_frontend(&s, FrontendId(2), true); + let sem_win = attach_frontend(&s, FrontendId(3), false); + s.core + .borrow_mut() + .windows + .get_mut(&sem_win) + .unwrap() + .cursor = end_of(2); + assert_eq!(move_down_as(&s, FrontendId(3), sem_win), 3); + + s.core.borrow_mut().unregister_frontend_view(FrontendId(3)); + s.core + .borrow_mut() + .windows + .get_mut(&grid_win) + .unwrap() + .cursor = end_of(2); + assert_eq!( + move_down_as(&s, FrontendId(2), grid_win), + 6, + "the flag is per-FrontendView — the grid never inherited it" + ); +} + +// --------------------------------------------------------------------------- +// 14. Split of different buffers, one folded (framing acceptance 14, +// round-2 F2 + round-3 F1) +// --------------------------------------------------------------------------- + +/// A vertical split showing buffer A (folded, active) beside buffer B +/// (unfolded). Returns `(a_window, b_window)`. +fn split_two_buffers(s: &EditorState) -> (WindowId, WindowId) { + let a_win = s.core.borrow().active_window_id(); + exec(s, "pmacs.window.split_vertical()"); + let ids: Vec = s.core.borrow().windows.keys().copied().collect(); + let b_win = *ids.iter().find(|w| **w != a_win).expect("second window"); + // Point B at a different buffer. + exec( + s, + "other = pmacs.buffer.from_bytes('other.txt', 'B0\\nB1\\nB2\\nB3\\nB4\\nB5\\nB6\\nB7\\n')", + ); + let other: BufferId = { + let core = s.core.borrow(); + let registry = core.registry.clone(); + let reg = registry.borrow(); + reg.find_by_name("other.txt").expect("other.txt present") + }; + let text_view = { + let core = s.core.borrow(); + let registry = core.registry.clone(); + let reg = registry.borrow(); + pmacs::text_view::TextView::new(reg.get(other).unwrap()) + }; + let mut core = s.core.borrow_mut(); + let w = core.windows.get_mut(&b_win).unwrap(); + w.buffer_id = other; + w.text_view = text_view; + w.cursor = 0; + drop(core); + s.core.borrow_mut().set_active_window_id(a_win); + (a_win, b_win) +} + +#[test] +fn an_unfolded_pane_beside_a_folded_one_is_byte_identical_to_its_baseline() { + let (s, _id) = seeded(); + let (_a_win, b_win) = split_two_buffers(&s); + let baseline = paint(&s); + + // Now fold buffer A only. + fold_active(&s, 2, 5); + let folded = paint(&s); + + // Window A collapsed … + let a_rows: Vec = (0..TEXT_ROWS).map(|r| row_text(&folded, r)).collect(); + assert!( + a_rows.iter().any(|r| r.contains("L02 …")), + "the folded pane collapsed: {a_rows:?}" + ); + // … while every cell of window B is unchanged. B occupies the right + // half of a vertical split. + let b_origin = COLS / 2; + for row in 0..TEXT_ROWS { + for col in b_origin..COLS { + assert_eq!( + at(&baseline, row, col), + at(&folded, row, col), + "buffer B's window leaked A's fold map at ({row},{col})" + ); + } + } + assert_eq!(s.core.borrow().windows[&b_win].view_top, 0); +} + +#[test] +fn wheel_over_an_inactive_unfolded_pane_uses_that_windows_map() { + let (s, _id) = seeded(); + let (a_win, b_win) = split_two_buffers(&s); + fold_active(&s, 2, 5); // buffer A (active) folds; B does not + let mut s = s; + let _ = paint(&s); + + // Wheel down over the RIGHT half — buffer B's pane, which the wheel + // does NOT activate. + s.dispatch_mouse( + FrontendId::LOCAL, + mouse(MouseEventKind::ScrollDown, 1, (COLS / 2 + 2) as u16), + CellSize::new(ROWS, COLS), + ); + + assert_eq!( + s.core.borrow().active_window_id(), + a_win, + "a wheel event does not move focus" + ); + let b_top = s.core.borrow().windows[&b_win].view_top; + assert_eq!( + b_top, 3, + "B scrolled by raw lines through ITS OWN map (SCROLL_LINES), \ + not through the folded active buffer's" + ); + assert_eq!( + s.core.borrow().windows[&a_win].view_top, + 0, + "the folded pane did not scroll" + ); +} diff --git a/tests/m4_acceptance.rs b/tests/m4_acceptance.rs index 69c8500..ca44980 100644 --- a/tests/m4_acceptance.rs +++ b/tests/m4_acceptance.rs @@ -498,6 +498,7 @@ fn render_active_window_to_grid( cell_origin: rect.origin, cell_size: CellSize::new(rect.size.rows, rect.size.cols), gutter_w: 0, + folds: None, }; let mut grid = CellGrid { cells: &mut backing, diff --git a/tests/statusline_segments_acceptance.rs b/tests/statusline_segments_acceptance.rs index 8d6f1d2..2963fea 100644 --- a/tests/statusline_segments_acceptance.rs +++ b/tests/statusline_segments_acceptance.rs @@ -464,6 +464,7 @@ fn a05_08_evaluator_latches_reentrancy_contexts_and_mutation_guards() { pmacs::window::FrontendView { layout: pmacs::window::Layout::single(window_id), active: window_id, + fold_projection: true, }, ); } diff --git a/tests/tab_width_acceptance.rs b/tests/tab_width_acceptance.rs index ba15f52..e153983 100644 --- a/tests/tab_width_acceptance.rs +++ b/tests/tab_width_acceptance.rs @@ -8,13 +8,14 @@ use pmacs::overlay::{BufferStyleOverlay, BufferStyleSpan, SharedBufferStyleSpans use pmacs::text_view::TextView; use pmacs::view::{DisplayCoord, View, Viewport}; -fn viewport(rows: u32, cols: u32, buffer_end: u64) -> Viewport { +fn viewport(rows: u32, cols: u32, buffer_end: u64) -> Viewport<'static> { Viewport { buffer_start: 0, buffer_end, cell_origin: CellCoord::new(0, 0), cell_size: CellSize::new(rows, cols), gutter_w: 0, + folds: None, } } diff --git a/tests/vterm_stage3_acceptance.rs b/tests/vterm_stage3_acceptance.rs index 4a170af..8d7eb46 100644 --- a/tests/vterm_stage3_acceptance.rs +++ b/tests/vterm_stage3_acceptance.rs @@ -81,6 +81,7 @@ fn attach_view( FrontendView { layout: Layout::single(window_id), active: window_id, + fold_projection: true, }, ); window_id