diff --git a/pmacs-gpu/src/main.rs b/pmacs-gpu/src/main.rs index f1207ba..22b3acf 100644 --- a/pmacs-gpu/src/main.rs +++ b/pmacs-gpu/src/main.rs @@ -36,8 +36,8 @@ use loro::{ContainerTrait, ExportMode}; use pmacs_protocol::{ AdornmentContent, AdornmentPlacement, BufferId, ByteRange, CrdtOp, Decoration, DecorationKind, DecorationSegment, FrontendId, InlineAdornment, InstanceMessage, InstanceSignal, - Key as ProtocolKey, MenuPromptRow, Modifiers, PointerKind, SelectionSnapshot, StyleSegment, - StyleSpan, + Key as ProtocolKey, LineNumberMode, MenuPromptRow, Modifiers, PointerKind, SelectionSnapshot, + StyleSegment, StyleSpan, cell::{Color as CellColor, Style as CellStyle}, }; use wgpu::MultisampleState; @@ -650,10 +650,12 @@ struct State { /// Minimap vertex bytes cached by [`MinimapCacheKey`] — /// rebuilding rescanned every line shape per frame. minimap_cache: Option<(MinimapCacheKey, Vec)>, - /// Line-number gutter toggle (UX gutter arc, GPU side). Frontend-local - /// (Q#UX5), set from the `--line-numbers` flag. Off ⇒ zero coordinate - /// change: `gutter_width_px()` is 0 and every shift site is a no-op. - line_numbers: bool, + /// Line-number gutter mode (UX gutter arc, GPU side). Shipped by the + /// daemon over `InstanceMessage::LineNumbers` (protocol v14). `Off` ⇒ + /// zero coordinate change: `gutter_width_px()` is 0 and every shift site + /// is a no-op. Relative/Hybrid are rendered locally against the GPU's + /// own cursor line. + line_numbers: LineNumberMode, /// Shaped right-aligned line numbers, one per visible code line — its /// own text layer over the code, aligned row-for-row (same line height). gutter_buffer: Buffer, @@ -1882,7 +1884,7 @@ impl State { mb_text_renderer, mb_bg_vertex_buffer: ReusableVertexBuffer::new(), minimap_cache: None, - line_numbers: false, + line_numbers: LineNumberMode::Off, gutter_buffer, gutter_text_renderer, } @@ -2532,12 +2534,12 @@ impl State { self.request_redraw(); None } - // UX gutter (protocol v13): the daemon owns the per-window - // line-number toggle (`M-x window.toggle-line-numbers`); apply - // it to our local gutter state and repaint on change. - InstanceMessage::LineNumbers { enabled, .. } => { - if self.line_numbers != enabled { - self.line_numbers = enabled; + // UX gutter (protocol v14): the daemon owns the per-window + // line-number mode; apply it to our local gutter state and + // repaint on change. + InstanceMessage::LineNumbers { mode, .. } => { + if self.line_numbers != mode { + self.line_numbers = mode; self.request_redraw(); } None @@ -2812,7 +2814,7 @@ impl State { /// disabled (UX gutter arc, Q#UX3): `digits * advance + gap`. Mirrors /// the TUI's `Window::gutter_width`; the unit here is pixels. fn gutter_width_px(&self) -> f32 { - if !self.line_numbers { + if !self.line_numbers.is_on() { return 0.0; } let lines = self.current_line_starts.len().max(1); @@ -2826,24 +2828,43 @@ impl State { TEXT_LEFT + self.gutter_width_px() } + /// The GPU's own cursor's 0-based buffer line, or `0` when there's no + /// own cursor in the displayed buffer (relative/hybrid then count from + /// the top — a rare transient). Derived from the whole-buffer line + /// table, so it's independent of the shaped slice. + fn cursor_line(&self) -> usize { + let byte = match self.own_cursor.as_ref() { + Some(c) if Some(c.buffer_id) == self.current_buffer_id => c.byte, + _ => 0, + }; + self.current_line_starts + .partition_point(|&start| start <= byte) + .saturating_sub(1) + } + /// Reshape the gutter buffer to the right-aligned line numbers for the /// currently-shaped code lines (UX gutter arc). One number per code /// line starting at `shaped_top`, so the two buffers align row-for-row /// at the same `top` and line height. No-op when the gutter is off. fn refresh_gutter_buffer(&mut self) { use std::fmt::Write as _; - if !self.line_numbers { + if !self.line_numbers.is_on() { return; } let digits = decimal_digits(self.current_line_starts.len().max(1)) as usize; let first = self.shaped_top; + // Relative/Hybrid measure distance from the cursor's buffer line; + // Absolute ignores it. Rebuilt every render, so it tracks the cursor. + let cursor_line = self.cursor_line(); + let mode = self.line_numbers; let n = self.buffer.lines.len(); let mut text = String::new(); for i in 0..n { if i > 0 { text.push('\n'); } - let _ = write!(text, "{:>digits$}", first + i + 1); + let num = mode.number_for(first + i, cursor_line).unwrap_or(0); + let _ = write!(text, "{num:>digits$}"); } self.gutter_buffer.set_text( &mut self.font_system, @@ -4020,7 +4041,7 @@ impl State { // main-text clip-left. Computed here as locals — calling `self.*` // inside the `prepare` args would conflict with its `&mut` borrows. let text_left = self.text_left(); - let gutter_clip_left = if self.line_numbers { + let gutter_clip_left = if self.line_numbers.is_on() { text_left.floor() as i32 } else { 0 @@ -4087,7 +4108,7 @@ impl State { // UX gutter: prepare the line-number layer in the reserved left // strip (empty when off → renders nothing). Same `top` + line // height as the code, so numbers align row-for-row. - let gutter_areas: Vec