termi 0.1.0

A modal terminal code editor written in Rust
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
//! # Editor widget
//!
//! **Purpose:** draw the text.
//!
//! **Responsibility:** the line-number gutter, the visible slice of the
//! document, the current-line highlight and the selection. It writes cells
//! directly instead of building `Line`/`Span` values, because a full-screen
//! redraw touches every cell every frame and the intermediate allocations are
//! the one thing that shows up on large files.
//!
//! **Public API:** [`EditorView`], [`gutter_width`], [`scroll_into_view`].

use ratatui::buffer::Buffer as Surface;
use ratatui::layout::Rect;
use ratatui::style::Style;
use ratatui::widgets::Widget;

use crate::config::Config;
use crate::editor::buffer::Buffer;
use crate::editor::selection::Range;
use crate::search::{LineMatch, Search};
use crate::syntax::Highlight;
use crate::theme::Theme;
use crate::ui::text::DisplayLine;

/// Columns reserved for the line-number gutter, including its trailing space.
///
/// Returns `0` when line numbers are off, which makes the text area logic
/// uniform — there is no special case for "no gutter".
#[must_use]
pub fn gutter_width(config: &Config, line_count: usize) -> u16 {
    if !config.line_numbers {
        return 0;
    }
    let digits = line_count.max(1).ilog10() as u16 + 1;
    digits.max(3) + 2
}

/// Scroll the buffer's view so the primary caret is on screen.
///
/// Lives here rather than in `View` because the amount of usable width depends
/// on the gutter, and the caret's *display* column depends on tab expansion —
/// both of which are rendering concerns.
pub fn scroll_into_view(buffer: &mut Buffer, config: &Config, area: Rect) {
    let gutter = gutter_width(config, buffer.document.len_lines());
    let width = usize::from(area.width.saturating_sub(gutter));
    let height = usize::from(area.height);
    let head = buffer.cursor().head;

    if !config.word_wrap {
        let line = DisplayLine::new(&buffer.document.line_string(head.line), config.tab_width);
        buffer.view.scroll_to(
            head.line,
            line.column_of(head.col),
            height,
            width,
            config.scrolloff,
        );
        return;
    }

    // With wrapping, a line is worth an unknown number of rows, so "does the
    // caret fit?" has to be measured instead of computed. Walking the top of the
    // view forward one line at a time is bounded by the window height.
    buffer.view.left_col = 0;
    if head.line < buffer.view.top_line {
        buffer.view.top_line = head.line;
    }
    if width == 0 || height == 0 {
        return;
    }
    let rows_of = |line: usize| {
        DisplayLine::new(&buffer.document.line_string(line), config.tab_width)
            .wrap(width)
            .len()
    };
    while buffer.view.top_line < head.line {
        let rows: usize = (buffer.view.top_line..=head.line).map(rows_of).sum();
        if rows <= height {
            break;
        }
        buffer.view.top_line += 1;
    }
}

/// One screen row's slice of a document line.
///
/// Grouped into a struct because the row, the line and the cell range always
/// travel together, and passing them separately made the drawing routine's
/// signature hard to read.
#[derive(Debug, Clone, Copy)]
struct Row {
    /// Terminal row to draw on.
    y: u16,
    /// Document line being drawn.
    line: usize,
    /// Half-open range of display cells this row shows.
    cells: (usize, usize),
}

/// The text area and its gutter.
pub struct EditorView<'a> {
    /// Buffer to draw.
    pub buffer: &'a Buffer,
    /// Colours.
    pub theme: &'a Theme,
    /// Tab width, gutter and highlight settings.
    pub config: &'a Config,
    /// Span to paint with the selection style, if any.
    pub selection: Option<Range>,
    /// Active search, used to highlight matches on the visible lines only.
    pub search: Option<&'a Search>,
    /// The match the caret is sitting on, painted more strongly than the rest.
    pub active_match: Option<Range>,
}

impl EditorView<'_> {
    /// Screen position of the primary caret, or `None` when it is scrolled out
    /// of view.
    #[must_use]
    pub fn caret_position(&self, area: Rect) -> Option<(u16, u16)> {
        let head = self.buffer.cursor().head;
        let view = self.buffer.view;
        let gutter = gutter_width(self.config, self.buffer.document.len_lines());
        let width = usize::from(area.width.saturating_sub(gutter));
        let height = usize::from(area.height);

        let display = DisplayLine::new(
            &self.buffer.document.line_string(head.line),
            self.config.tab_width,
        );
        let target = display.column_of(head.col);

        let (row, column) = if self.config.word_wrap {
            // Every line above the caret may occupy several rows, so the row has
            // to be counted rather than subtracted.
            let mut row = 0usize;
            for line in view.top_line..head.line {
                row += self.rows_of(line, width).1.len();
                if row >= height {
                    return None;
                }
            }
            let chunks = display.wrap(width);
            let index = chunks
                .iter()
                .position(|(start, end)| target >= *start && target < *end)
                // A caret resting past the last character belongs on the final row.
                .unwrap_or(chunks.len() - 1);
            (row + index, target - chunks[index].0)
        } else {
            (
                head.line.checked_sub(view.top_line)?,
                target.checked_sub(view.left_col)?,
            )
        };

        if row >= height || column >= width {
            return None;
        }
        Some((
            area.x + gutter + u16::try_from(column).ok()?,
            area.y + u16::try_from(row).ok()?,
        ))
    }

    /// Draw the line number for one row.
    fn render_gutter(&self, surface: &mut Surface, area: Rect, y: u16, line: usize, caret: usize) {
        let width = gutter_width(self.config, self.buffer.document.len_lines());
        if width == 0 {
            return;
        }
        let is_caret_line = line == caret;
        let number = if self.config.relative_line_numbers && !is_caret_line {
            line.abs_diff(caret)
        } else {
            line + 1
        };
        let style = if is_caret_line {
            self.theme.gutter_active
        } else {
            self.theme.gutter
        };

        // Right-align inside the gutter, leaving the final column as padding.
        let label = format!("{number:>width$} ", width = usize::from(width) - 1);
        for (offset, ch) in label.chars().enumerate() {
            let Ok(offset) = u16::try_from(offset) else {
                break;
            };
            if offset >= width {
                break;
            }
            if let Some(cell) = surface.cell_mut((area.x + offset, y)) {
                cell.set_char(ch).set_style(style);
            }
        }
    }

    /// Draw one screen row.
    ///
    /// `first_cell` is where in the expanded line the row starts: the horizontal
    /// scroll offset when wrapping is off, and the start of a wrapped chunk when
    /// it is on. Having one routine for both is what keeps selection, search and
    /// syntax styling identical in the two modes.
    fn render_row(&self, surface: &mut Surface, area: Rect, row: Row, caret: usize) {
        let Row {
            y,
            line,
            cells: (first_cell, last_cell),
        } = row;
        let gutter = gutter_width(self.config, self.buffer.document.len_lines());
        let x0 = area.x + gutter;
        let width = area.width.saturating_sub(gutter);

        let base = if line == caret && self.config.highlight_current_line {
            self.theme.text.patch(self.theme.cursor_line)
        } else {
            self.theme.text
        };

        let text = self.buffer.document.line_string(line);
        let display = DisplayLine::new(&text, self.config.tab_width);
        let line_start = self.buffer.document.line_start(line);
        let left = first_cell;

        // Matching only the visible lines is what keeps search highlighting free
        // on a large file: the cost is bounded by the window, not the document.
        let matches = self
            .search
            .map(|search| search.matches_in_line(&text))
            .unwrap_or_default();
        let syntax = if self.config.syntax_highlighting {
            self.buffer.syntax.highlight(&self.buffer.document, line)
        } else {
            Vec::new()
        };

        for column in 0..width {
            let Some(cell) = surface.cell_mut((x0 + column, y)) else {
                continue;
            };
            let index = left + usize::from(column);
            // A wrapped row stops at its chunk boundary; the rest of the row is
            // padding, not the next chunk's text.
            match display.cells.get(index).filter(|_| index < last_cell) {
                Some(display_cell) => {
                    let style = self.style_for(
                        base,
                        line_start,
                        display_cell.char_index,
                        &matches,
                        &syntax,
                    );
                    match display_cell.glyph {
                        Some(glyph) => {
                            cell.set_char(glyph).set_style(style);
                        }
                        // Trailing half of a wide glyph: the glyph itself already
                        // covers this column, unless it was scrolled off the left
                        // edge, in which case draw a space to keep alignment.
                        None if column == 0 => {
                            cell.set_char(' ').set_style(style);
                        }
                        None => {
                            cell.set_symbol("").set_style(style);
                        }
                    }
                }
                // Past the end of the line: still paint the background so the
                // current-line highlight spans the full width.
                None => {
                    cell.set_char(' ').set_style(base);
                }
            }
        }
    }

    /// Layer the styles that can apply to one character.
    ///
    /// Order matters: the selection is what the user is acting on, so it wins
    /// over a search match, and the match under the caret wins over the rest.
    fn style_for(
        &self,
        base: Style,
        line_start: usize,
        column: usize,
        matches: &[LineMatch],
        syntax: &[Highlight],
    ) -> Style {
        let index = line_start + column;
        if self.selection.is_some_and(|range| range.contains(index)) {
            return base.patch(self.theme.selection);
        }
        if self.active_match.is_some_and(|range| range.contains(index)) {
            return base.patch(self.theme.search_active);
        }
        if matches
            .iter()
            .any(|found| column >= found.start && column < found.end)
        {
            return base.patch(self.theme.search);
        }
        // Syntax is the bottom layer: it colours text, while everything above
        // colours the background, so a keyword inside a selection keeps both.
        match syntax
            .iter()
            .find(|span| column >= span.start && column < span.end)
        {
            Some(span) => base.patch(self.theme.syntax.style_for(span.kind)),
            None => base,
        }
    }

    /// The expanded form of a line, and how it splits into screen rows.
    fn rows_of(&self, line: usize, width: usize) -> (DisplayLine, Vec<(usize, usize)>) {
        let display = DisplayLine::new(
            &self.buffer.document.line_string(line),
            self.config.tab_width,
        );
        let rows = display.wrap(width);
        (display, rows)
    }

    /// Mark rows past the end of the document, like vi's tildes.
    fn render_filler(&self, surface: &mut Surface, area: Rect, from_row: u16) {
        for row in from_row..area.height {
            if let Some(cell) = surface.cell_mut((area.x, area.y + row)) {
                cell.set_char('~').set_style(self.theme.gutter);
            }
        }
    }

    /// One document line per screen row, scrolled horizontally.
    fn render_unwrapped(&self, surface: &mut Surface, area: Rect, caret: usize) {
        let top = self.buffer.view.top_line;
        let left = self.buffer.view.left_col;

        for row in 0..area.height {
            let line = top + usize::from(row);
            if line >= self.buffer.document.len_lines() {
                self.render_filler(surface, area, row);
                return;
            }
            let y = area.y + row;
            self.render_gutter(surface, area, y, line, caret);
            self.render_row(
                surface,
                area,
                Row {
                    y,
                    line,
                    cells: (left, usize::MAX),
                },
                caret,
            );
        }
    }

    /// Long lines continue on the next row; the gutter is only numbered once
    /// per document line, so a wrapped line still reads as one line.
    fn render_wrapped(&self, surface: &mut Surface, area: Rect, caret: usize) {
        let width = usize::from(
            area.width
                .saturating_sub(gutter_width(self.config, self.buffer.document.len_lines())),
        );
        let mut row = 0u16;
        let mut line = self.buffer.view.top_line;

        while row < area.height {
            if line >= self.buffer.document.len_lines() {
                self.render_filler(surface, area, row);
                return;
            }
            let (_, chunks) = self.rows_of(line, width);
            for (index, (start, end)) in chunks.into_iter().enumerate() {
                if row >= area.height {
                    break;
                }
                let y = area.y + row;
                if index == 0 {
                    self.render_gutter(surface, area, y, line, caret);
                } else {
                    self.render_gutter_continuation(surface, area, y);
                }
                self.render_row(
                    surface,
                    area,
                    Row {
                        y,
                        line,
                        cells: (start, end),
                    },
                    caret,
                );
                row += 1;
            }
            line += 1;
        }
    }

    /// Blank gutter for the second and later rows of a wrapped line.
    fn render_gutter_continuation(&self, surface: &mut Surface, area: Rect, y: u16) {
        let width = gutter_width(self.config, self.buffer.document.len_lines());
        for offset in 0..width {
            if let Some(cell) = surface.cell_mut((area.x + offset, y)) {
                cell.set_char(if offset + 2 == width { '·' } else { ' ' })
                    .set_style(self.theme.gutter);
            }
        }
    }
}

impl Widget for EditorView<'_> {
    fn render(self, area: Rect, surface: &mut Surface) {
        if area.is_empty() {
            return;
        }
        surface.set_style(area, self.theme.text);
        let caret = self.buffer.cursor().head.line;

        if self.config.word_wrap {
            self.render_wrapped(surface, area, caret);
        } else {
            self.render_unwrapped(surface, area, caret);
        }
    }
}