strop-editor 0.3.0

strop — a modal text editor in Rust: see the cut before you make it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! Pane rendering — one text renderer for every pane (0010 §3).
//! Active panes read live editor state with overlays; inactive panes
//! read their saved snapshot without. Same gutter, same guides, same
//! diff rows — the duplicated inactive-pane loop is gone, so panes
//! cannot drift apart again.

use ratatui::layout::Rect;
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::Paragraph;
use ratatui::Frame;

use crate::editor::{Editor, LayoutDir};

use super::diff;
use super::{class_color, in_range, ACCENT, BASE, FLASH_BG, MUTED, PREVIEW_BG, SELECT_BG, TEXT};

/// Width of the standard gutter: sign column + 3-digit number + space.
pub(crate) const GUTTER: u16 = 5;

/// One pane's view of a buffer. `overlays` is false for inactive panes:
/// preview/search/selection/flash belong to the pane being driven.
struct PaneView {
    buffer: usize,
    cursor: usize,
    view_top: usize,
    overlays: bool,
}

/// Render all panes and return the active pane's rect (the native
/// cursor lives there — offsets included, which the full-area version
/// got wrong in splits).
pub(crate) fn render_panes(editor: &mut Editor, frame: &mut Frame, area: Rect) -> Rect {
    let n = editor.panes.len();
    let is_row = editor.layout == LayoutDir::Row;
    let total_w = area.width as usize;
    let total_h = area.height as usize - 1; // statusline
    let dividers = n - 1;
    let (mut x, mut y) = (area.x, area.y);
    let mut active_rect = Rect {
        x,
        y,
        width: total_w as u16,
        height: total_h as u16,
    };
    for i in 0..n {
        let (w, h): (u16, u16) = if is_row {
            let w = ((total_w - dividers) / n) as u16;
            let w = if i == n - 1 {
                (total_w - dividers) as u16 - w * (n as u16 - 1)
            } else {
                w
            };
            (w, total_h as u16)
        } else {
            let h = ((total_h - dividers) / n) as u16;
            let h = if i == n - 1 {
                (total_h - dividers) as u16 - h * (n as u16 - 1)
            } else {
                h
            };
            (total_w as u16, h)
        };
        let rect = Rect {
            x,
            y,
            width: w,
            height: h,
        };
        let view = if i == editor.active_pane {
            PaneView {
                buffer: editor.current,
                cursor: editor.cursor,
                view_top: editor.view_top,
                overlays: true,
            }
        } else {
            let pane = &editor.panes[i];
            PaneView {
                buffer: pane.buffer.min(editor.buffers.len().saturating_sub(1)),
                cursor: pane.cursor,
                view_top: pane.view_top,
                overlays: false,
            }
        };
        render_pane(editor, frame, rect, &view);
        if i == editor.active_pane {
            active_rect = rect;
            render_extra_cursors(editor, frame, rect, &view);
        } else {
            render_static_caret(editor, frame, rect, &view);
        }
        if i < n - 1 {
            // divider column/row
            if is_row {
                let dx = x + w;
                for dy in y..y + h {
                    let cell = &mut frame.buffer_mut()[(dx, dy)];
                    cell.set_symbol("");
                    cell.set_fg(Color::Rgb(0x3a, 0x3d, 0x4d));
                }
                x = dx + 1;
            } else {
                let dy = y + h;
                for dx in x..x + w {
                    let cell = &mut frame.buffer_mut()[(dx, dy)];
                    cell.set_symbol("");
                    cell.set_fg(Color::Rgb(0x3a, 0x3d, 0x4d));
                }
                y = dy + 1;
            }
        }
    }
    active_rect
}

/// The inactive pane's position, unfocused: a muted block on the saved
/// cursor cell, offsets pane-local (unlike the native cursor).
fn render_static_caret(editor: &Editor, frame: &mut Frame, area: Rect, view: &PaneView) {
    let buf = &editor.buffers[view.buffer];
    let line = buf.line_of(view.cursor);
    let row = line.saturating_sub(view.view_top) as u16;
    let gutter = diff::left_inset(editor, view.buffer) as u16;
    let col = gutter + buf.col_of(view.cursor) as u16;
    if row < area.height && col < area.width {
        let cell = &mut frame.buffer_mut()[(area.x + col, area.y + row)];
        cell.set_bg(Color::Rgb(0x3a, 0x3d, 0x4d));
    }
}

/// Secondary cursors (0013 §4): solid blocks on the active pane, like
/// the native block cursor but painted.
fn render_extra_cursors(editor: &Editor, frame: &mut Frame, area: Rect, view: &PaneView) {
    if view.buffer != editor.current || editor.extra_cursors.is_empty() {
        return;
    }
    let buf = &editor.buffers[view.buffer];
    let inset = diff::left_inset(editor, view.buffer) as u16;
    for &c in &editor.extra_cursors {
        let line = buf.line_of(c);
        if line < view.view_top {
            continue;
        }
        let row = (line - view.view_top) as u16;
        let col = inset + buf.col_of(c) as u16;
        if row < area.height && col < area.width {
            let cell = &mut frame.buffer_mut()[(area.x + col, area.y + row)];
            cell.set_bg(TEXT);
            cell.set_fg(BASE);
        }
    }
}

/// Render one pane's rows: gutter, syntax/decoration, overlays, guides.
fn render_pane(editor: &mut Editor, frame: &mut Frame, area: Rect, view: &PaneView) {
    let text_rows = area.height as usize;
    let buf = &editor.buffers[view.buffer];
    let cur_line = buf.line_of(view.cursor);
    let surface = editor.surfaces.get(view.buffer).and_then(|s| s.as_ref());

    // tree-sitter spans for the visible window (base layer, 0001 §5.8)
    let first_byte = buf.line_start(view.view_top);
    let last_line = (view.view_top + text_rows).min(buf.len_lines());
    let last_byte = buf.line_end(last_line.saturating_sub(1));
    let rope = editor.buffers[view.buffer].rope.clone();
    let syn_spans: Vec<strop_syntax::Span> = match editor.highlighters.get_mut(view.buffer) {
        Some(h) => h
            .as_mut()
            .map(|h| h.highlight(&rope, first_byte, last_byte))
            .unwrap_or_default(),
        None => Vec::new(),
    };

    // overlays read live editor state; only the active pane shows them
    let mut row_style = RowStyle {
        syn_spans: &syn_spans,
        preview: view
            .overlays
            .then(|| editor.preview())
            .flatten()
            .map(|r| r.range),
        flash: view.overlays.then(|| editor.flash_range()).flatten(),
        selection: view.overlays.then(|| editor.visual_range()).flatten(),
        search_hits: &[],
        find: view.overlays.then(|| editor.find_candidates()).flatten(),
        diff_line: None,
    };
    let search_hits: Vec<usize> = if view.overlays {
        editor
            .search_pattern()
            .map(|p| strop_grammar::search_all(editor.buf(), p))
            .unwrap_or_default()
    } else {
        Vec::new()
    };
    row_style.search_hits = &search_hits;

    let mut lines: Vec<Line> = Vec::with_capacity(text_rows);
    let diff_digits = diff_digits(surface);
    // 0011 left-margin columns: the commit file sidebar (Diff surfaces
    // from the dive chain) and the blame gutter (file buffers) prepend
    // to every row; content width shrinks by what they take
    let sidebar = match surface {
        Some(crate::editor::Surface::Diff {
            commit: Some(cf),
            label,
            ..
        }) => Some((cf.files.as_slice(), label.as_str())),
        _ => None,
    };
    let sidebar_w = sidebar.map_or(0, |(files, _)| diff::sidebar_width(files) + 1);
    let blame = editor.blame_gutter_for(view.buffer);
    let blame_w = if blame.is_some() { diff::BLAME_W } else { 0 };
    let content_width = area.width.saturating_sub((sidebar_w + blame_w) as u16);
    for row in 0..text_rows {
        let line_idx = view.view_top + row;
        // the margin columns: sidebar cell (or blank), then the blame
        // cell (or blank past the buffer's lines)
        let mut left: Vec<Span> = sidebar
            .map(|(files, label)| diff::sidebar_spans(files, label, line_idx))
            .unwrap_or_default();
        if let Some(gutter) = blame {
            left.push(match gutter.lines.get(line_idx) {
                Some(bl) => diff::blame_spans(bl),
                None => diff::blame_blank(),
            });
        }
        if line_idx > buf.last_content_line() {
            left.push(Span::styled("~", Style::default().fg(MUTED)));
            lines.push(Line::from(left));
            continue;
        }
        let start = buf.line_start(line_idx);
        let text = buf.line_text(line_idx);

        // git memory surfaces decorate their rows from typed data
        // (0010 §4/§5): diff rows re-gutter, log/files rows re-color
        match diff::diff_row(surface, line_idx) {
            Some(diff::DiffRow::Stats | diff::DiffRow::HunkHeader) => {
                let mut line = diff::structural_row(surface.unwrap(), line_idx, content_width);
                line.spans.splice(0..0, left);
                lines.push(line);
                continue;
            }
            Some(diff::DiffRow::Line(dl)) => {
                let mut spans = diff::diff_gutter(dl, line_idx == cur_line, diff_digits);
                row_style.diff_line = Some(dl);
                spans.extend(content_spans(
                    editor,
                    view,
                    start,
                    &text,
                    &row_style,
                    content_width,
                ));
                left.extend(spans);
                lines.push(Line::from(left));
                continue;
            }
            None => {}
        }

        let num_style = if line_idx == cur_line {
            Style::default().fg(ACCENT)
        } else {
            Style::default().fg(MUTED)
        };
        // Helix-grade gutter: a colored ▎ bar in the leftmost column —
        // diagnostics first, then git signs (green/amber/red)
        let (bar, bar_color) = gutter_mark(editor, view, line_idx);
        left.push(Span::styled(
            bar,
            Style::default().fg(bar_color).add_modifier(Modifier::BOLD),
        ));
        left.push(Span::styled(format!("{:>3} ", line_idx + 1), num_style));
        row_style.diff_line = None;
        if let Some(content) = diff::surface_content_spans(surface, line_idx, content_width) {
            left.extend(content);
        } else {
            left.extend(content_spans(
                editor,
                view,
                start,
                &text,
                &row_style,
                content_width,
            ));
        }
        lines.push(Line::from(left));
    }
    frame.render_widget(
        Paragraph::new(lines).style(Style::default().bg(BASE)),
        Rect {
            height: text_rows as u16,
            ..area
        },
    );
}

/// Digits per side for a Diff surface's number columns.
fn diff_digits(surface: Option<&crate::editor::Surface>) -> usize {
    let width = diff::gutter_width(surface);
    if width == super::buffer::GUTTER as usize {
        3
    } else {
        (width - 3) / 2
    }
}

/// The sign column: diagnostics win over git signs (merged gutter,
/// 0009), and only the pane's own buffer shows them.
fn gutter_mark(editor: &Editor, view: &PaneView, line_idx: usize) -> (&'static str, Color) {
    if let Some(sev) = editor.diag_severity_at(view.buffer, line_idx + 1) {
        return match sev {
            1 => ("E", Color::Rgb(0xe8, 0x67, 0x7a)),
            2 => ("W", ACCENT),
            _ => ("", Color::Rgb(0x7f, 0xb4, 0xca)),
        };
    }
    // git signs: + add, ~ change, - deletion below (only for the
    // working buffer — surfaces have no path, so no leak)
    if view.buffer == editor.current {
        match editor.sign_at(line_idx + 1) {
            Some('+') => return ("", Color::Rgb(0xa9, 0xc4, 0x7c)),
            Some('~') => return ("", ACCENT),
            Some('-') => return ("", Color::Rgb(0xe8, 0x67, 0x7a)),
            _ => {}
        }
    }
    (" ", MUTED)
}

/// The per-pane, per-frame style inputs one content row composes:
/// base layers (syntax spans or a diff line) plus the active pane's
/// overlays. Inactive panes get the default (no overlays).
#[derive(Default)]
struct RowStyle<'a> {
    syn_spans: &'a [strop_syntax::Span],
    preview: Option<strop_core::Range>,
    flash: Option<strop_core::Range>,
    selection: Option<strop_core::Range>,
    search_hits: &'a [usize],
    find: Option<(u8, bool)>,
    /// Set on diff-surface rows: typed origin drives colors (0010 §4).
    diff_line: Option<&'a strop_git::DiffLine>,
}

/// Content spans for one row: syntax or diff decoration, then overlays
/// composed on top (search < preview < flash, 0001 §5.8). Diff rows get
/// a full-width background pad.
fn content_spans(
    editor: &Editor,
    view: &PaneView,
    start: usize,
    text: &str,
    style: &RowStyle,
    width: u16,
) -> Vec<Span<'static>> {
    let buf = &editor.buffers[view.buffer];
    let cur_line = buf.line_of(view.cursor);
    // indent guides: dim │ at each indent level within leading
    // whitespace (spaces only, v1)
    let lead_ws = if editor.config.indent_guides {
        text.chars().take_while(|c| *c == ' ').count()
    } else {
        0
    };
    let tab = editor.config.tab_size.max(1);
    let syn_spans = style.syn_spans;
    let mut syn_idx = syn_spans.partition_point(|s| s.end <= start);
    let mut spans = Vec::with_capacity(text.len() / 2 + 4);
    let mut chars = 0usize;
    for (i, ch) in text.chars().enumerate() {
        let pos = start + i; // prototype is ASCII-honest (0001 §5.9 later)
        while syn_idx < syn_spans.len() && syn_spans[syn_idx].end <= pos {
            syn_idx += 1;
        }
        let mut cell = Style::default().fg(TEXT);
        if let Some(dl) = style.diff_line {
            cell = cell.fg(diff::origin_fg(dl.origin));
            if let Some(bg) = diff::origin_bg(dl.origin) {
                cell = cell.bg(bg);
            }
        } else if syn_idx < syn_spans.len() && syn_spans[syn_idx].start <= pos {
            let class = syn_spans[syn_idx].class;
            cell = cell.fg(class_color(class));
            if class == strop_syntax::Class::Comment {
                cell = cell.add_modifier(Modifier::ITALIC);
            }
        }
        if style.selection.is_some_and(|r| in_range(r, pos)) {
            cell = cell.bg(SELECT_BG);
        }
        if style
            .search_hits
            .iter()
            .any(|&h| pos >= h && pos < h + editor.search_pattern().map_or(0, str::len))
        {
            cell = cell.fg(ACCENT).add_modifier(Modifier::BOLD);
        }
        if let Some((_, backward)) = style.find {
            // leap-style: candidates bold-accent on the pending side
            let on_line = buf.line_of(pos) == cur_line;
            let ahead = if backward {
                pos < view.cursor
            } else {
                pos > view.cursor
            };
            if on_line && ahead && !ch.is_whitespace() {
                cell = cell.fg(ACCENT).add_modifier(Modifier::BOLD);
            }
        }
        if style.preview.is_some_and(|r| in_range(r, pos)) {
            cell = cell.fg(ACCENT).bg(PREVIEW_BG);
        }
        if style.flash.is_some_and(|r| in_range(r, pos)) {
            cell = cell.bg(FLASH_BG);
        }
        let is_guide = i < lead_ws && (i + 1) % tab == 0;
        if is_guide {
            spans.push(Span::styled("", cell.fg(Color::Rgb(0x2e, 0x30, 0x42))));
        } else {
            spans.push(Span::styled(ch.to_string(), cell));
        }
        chars += 1;
    }
    // full-row backgrounds for add/del rows run past the text (0010 §4)
    if let Some(dl) = style.diff_line {
        if let Some(bg) = diff::origin_bg(dl.origin) {
            let used =
                diff::gutter_width(editor.surfaces.get(view.buffer).and_then(|s| s.as_ref()))
                    + chars;
            let pad = (width as usize).saturating_sub(used);
            if pad > 0 {
                spans.push(Span::styled(
                    " ".repeat(pad),
                    Style::default().fg(bg).bg(bg),
                ));
            }
        }
    }
    spans
}