iced-code-editor 0.4.0

A custom code editor widget for the Iced GUI framework with syntax highlighting, line numbers, and scrolling support.
Documentation
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
//! Rendering caches: memoized wrapped visual lines, per-line syntax
//! highlighting, per-line bracket-nesting depth, and the maximum
//! content-width index used to size the horizontal scrollbar.

use iced::Color;
use std::collections::{BTreeMap, HashSet};
use std::rc::Rc;
use syntect::highlighting::HighlightState;
use syntect::parsing::ParseState;

use crate::buffer::TextBuffer;
use crate::canvas_editor::features::bracket_match;
use crate::canvas_editor::render::wrapping;
use crate::canvas_editor::{CodeEditor, measure_text_width};

#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) struct VisualLinesKey {
    pub(crate) buffer_revision: u64,
    /// `f32::to_bits()` is used so the cache key is stable and exact:
    /// - no epsilon comparisons are required
    /// - NaN payloads (if any) do not collapse unexpectedly
    viewport_width_bits: u32,
    gutter_width_bits: u32,
    wrap_enabled: bool,
    wrap_column: Option<usize>,
    folding_enabled: bool,
    fold_revision: u64,
    full_char_width_bits: u32,
    char_width_bits: u32,
}

pub(crate) struct VisualLinesCache {
    pub(crate) key: VisualLinesKey,
    visual_lines: Rc<Vec<wrapping::VisualLine>>,
    buffer_line_count: usize,
}

/// Per-line widths plus a counted ordered index for O(log n) max updates.
pub(crate) struct MaxContentWidthCache {
    revision: u64,
    line_widths: Vec<f32>,
    width_counts: BTreeMap<u32, usize>,
}

impl MaxContentWidthCache {
    fn add_width(&mut self, width: f32) {
        *self.width_counts.entry(width.to_bits()).or_insert(0) += 1;
    }

    fn remove_width(&mut self, width: f32) {
        let bits = width.to_bits();
        let remove_entry = if let Some(count) = self.width_counts.get_mut(&bits)
        {
            *count = count.saturating_sub(1);
            *count == 0
        } else {
            false
        };
        if remove_entry {
            self.width_counts.remove(&bits);
        }
    }

    fn max_width(&self) -> f32 {
        self.width_counts
            .last_key_value()
            .map_or(0.0, |(bits, _)| f32::from_bits(*bits))
    }
}

/// One highlighted logical line together with the syntect parser state *after*
/// it, so highlighting can resume sequentially from any cached line.
///
/// Storing the post-line state is what makes multi-line constructs (block
/// comments, multi-line strings) highlight correctly: line `N` is highlighted
/// starting from the state left by line `N - 1`.
struct CachedHighlightLine {
    /// Colored token spans covering the full logical line.
    spans: Rc<Vec<(Color, String)>>,
    /// Syntect parse state after this line (start state for the next line).
    parse_state: ParseState,
    /// Syntect highlight state after this line (start state for the next line).
    highlight_state: HighlightState,
}

/// Sequential per-line syntax-highlight cache.
///
/// `lines` holds a dense, valid prefix: `lines[i]` is the highlight of logical
/// line `i`. The prefix is extended lazily as deeper lines become visible and
/// truncated from the first edited line on each edit (see
/// [`CodeEditor::invalidate_highlight_from`]), so an edit never forces a full
/// re-parse from the top of the file.
pub(crate) struct HighlightCache {
    /// Active syntax/language identifier these lines were highlighted with.
    syntax: String,
    /// Dense valid prefix of highlighted lines (vector index = logical line).
    lines: Vec<CachedHighlightLine>,
}

impl HighlightCache {
    /// Creates an empty cache for the given syntax identifier.
    ///
    /// # Arguments
    ///
    /// * `syntax` - Active syntax/language identifier the cache is built for.
    pub(crate) fn new(syntax: String) -> Self {
        Self { syntax, lines: Vec::new() }
    }

    /// Returns the syntax identifier these lines were highlighted with.
    pub(crate) fn syntax(&self) -> &str {
        &self.syntax
    }

    /// Returns the number of highlighted logical lines (valid prefix length).
    pub(crate) fn valid_len(&self) -> usize {
        self.lines.len()
    }

    /// Returns the cached spans for `logical_line`, if within the valid prefix.
    ///
    /// # Arguments
    ///
    /// * `logical_line` - Index of the logical line to look up.
    pub(crate) fn spans(
        &self,
        logical_line: usize,
    ) -> Option<Rc<Vec<(Color, String)>>> {
        self.lines.get(logical_line).map(|line| Rc::clone(&line.spans))
    }

    /// Returns the syntect state to resume highlighting the next line from.
    ///
    /// This is the state left after the last cached line, or `None` when the
    /// cache is empty (highlighting then starts from the syntax's initial
    /// state).
    pub(crate) fn resume_state(&self) -> Option<(ParseState, HighlightState)> {
        self.lines.last().map(|line| {
            (line.parse_state.clone(), line.highlight_state.clone())
        })
    }

    /// Appends one highlighted line and its post-line state to the prefix.
    ///
    /// # Arguments
    ///
    /// * `spans` - The colored token spans of the line.
    /// * `parse_state` - Syntect parse state after the line.
    /// * `highlight_state` - Syntect highlight state after the line.
    pub(crate) fn push_line(
        &mut self,
        spans: Rc<Vec<(Color, String)>>,
        parse_state: ParseState,
        highlight_state: HighlightState,
    ) {
        self.lines.push(CachedHighlightLine {
            spans,
            parse_state,
            highlight_state,
        });
    }

    /// Truncates the valid prefix to `line`, discarding lines at index `line`
    /// and beyond so they are re-highlighted on next access.
    ///
    /// # Arguments
    ///
    /// * `line` - First logical line to invalidate.
    pub(crate) fn truncate(&mut self, line: usize) {
        self.lines.truncate(line);
    }
}

/// Sequential per-line bracket-nesting-depth cache used by bracket-pair
/// colorization.
///
/// `depths[i]` is the bracket nesting depth entering logical line `i`;
/// `depths[0]` is always `0`. Like [`HighlightCache`], the prefix is
/// extended lazily as deeper lines are drawn and truncated after an edit, so
/// an edit never forces a full rescan of the file. Unlike [`HighlightCache`],
/// no syntect state is involved: depth only depends on bracket characters, so
/// extending the prefix is a cheap plain-text scan.
pub(crate) struct BracketDepthCache {
    /// Dense valid prefix of "depth entering line `i`" (vector index = logical line).
    depths: Vec<usize>,
}

impl BracketDepthCache {
    /// Creates a cache seeded with `depths[0] == 0` (the file starts unnested).
    pub(crate) fn new() -> Self {
        Self { depths: vec![0] }
    }

    /// Returns the bracket nesting depth entering `line`, extending the
    /// cached prefix as needed.
    ///
    /// # Arguments
    ///
    /// * `buffer` - The text buffer to scan for missing lines.
    /// * `line` - Logical line whose entering depth is requested.
    pub(crate) fn depth_at_line_start(
        &mut self,
        buffer: &TextBuffer,
        line: usize,
    ) -> usize {
        let target = line.min(buffer.line_count().saturating_sub(1));
        while self.depths.len() <= target {
            let idx = self.depths.len() - 1;
            let start = self.depths[idx];
            let end = bracket_match::bracket_depth_after_line(
                buffer.line(idx),
                start,
            );
            self.depths.push(end);
        }
        self.depths[target]
    }

    /// Truncates the cached prefix so depths entering `line` and beyond are
    /// recomputed on next access.
    ///
    /// Depths before `line` are unaffected since they only depend on earlier,
    /// unedited lines. Has no effect when the cache is already shorter.
    ///
    /// # Arguments
    ///
    /// * `line` - First logical line whose entering depth may have changed.
    pub(crate) fn truncate_from(&mut self, line: usize) {
        let keep = line.saturating_add(1).max(1);
        if self.depths.len() > keep {
            self.depths.truncate(keep);
        }
    }
}

impl CodeEditor {
    /// Returns the maximum content width across all lines, in pixels.
    ///
    /// Used to size the horizontal scrollbar when `wrap_enabled = false`.
    /// The result is cached keyed by `buffer_revision` so repeated calls are cheap.
    ///
    /// # Returns
    ///
    /// Total width in pixels including gutter, padding and a right margin.
    pub(crate) fn max_content_width(&self) -> f32 {
        let mut cache = self.max_content_width_cache.borrow_mut();
        if cache
            .as_ref()
            .is_none_or(|existing| existing.revision != self.buffer_revision)
        {
            let line_widths: Vec<f32> = (0..self.buffer.line_count())
                .map(|line| {
                    measure_text_width(
                        self.buffer.line(line),
                        self.full_char_width,
                        self.char_width,
                    )
                })
                .collect();
            let mut width_counts = BTreeMap::new();
            for width in &line_widths {
                *width_counts.entry(width.to_bits()).or_insert(0) += 1;
            }
            *cache = Some(MaxContentWidthCache {
                revision: self.buffer_revision,
                line_widths,
                width_counts,
            });
        }

        let gutter = self.gutter_width();
        let max_line_width =
            cache.as_ref().map_or(0.0, MaxContentWidthCache::max_width);

        // gutter + left padding + text + right margin
        gutter + 5.0 + max_line_width + 20.0
    }

    /// Returns wrapped "visual lines" for the current buffer and layout, with memoization.
    ///
    /// The editor frequently needs the wrapped view of the buffer:
    /// - hit-testing (mouse selection, cursor placement)
    /// - mapping logical ↔ visual positions
    /// - rendering (text, line numbers, highlights)
    ///
    /// Computing visual lines is relatively expensive for large files, so we
    /// cache the result keyed by:
    /// - `buffer_revision` (buffer content changes)
    /// - viewport width / gutter width (layout changes)
    /// - wrapping settings (wrap enabled / wrap column)
    /// - measured character widths (font / size changes)
    ///
    /// The returned `Rc<Vec<VisualLine>>` is cheap to clone and allows multiple
    /// rendering passes (content + overlay layers) to share the same computed
    /// layout without extra allocation.
    pub(crate) fn visual_lines_cached(
        &self,
        viewport_width: f32,
    ) -> Rc<Vec<wrapping::VisualLine>> {
        let key = VisualLinesKey {
            buffer_revision: self.buffer_revision,
            viewport_width_bits: viewport_width.to_bits(),
            gutter_width_bits: self.gutter_width().to_bits(),
            wrap_enabled: self.wrap_enabled,
            wrap_column: self.wrap_column,
            folding_enabled: self.folding_enabled,
            fold_revision: self.fold_revision,
            full_char_width_bits: self.full_char_width.to_bits(),
            char_width_bits: self.char_width.to_bits(),
        };

        let mut cache = self.visual_lines_cache.borrow_mut();
        if let Some(existing) = cache.as_ref()
            && existing.key == key
        {
            return existing.visual_lines.clone();
        }

        let hidden = self.hidden_lines_set();
        let wrapping_calc = wrapping::WrappingCalculator::new(
            self.wrap_enabled,
            self.wrap_column,
            self.full_char_width,
            self.char_width,
        );
        let visual_lines = wrapping_calc.calculate_visual_lines(
            &self.buffer,
            viewport_width,
            self.gutter_width(),
            &hidden,
        );
        let visual_lines = Rc::new(visual_lines);

        *cache = Some(VisualLinesCache {
            key,
            visual_lines: visual_lines.clone(),
            buffer_line_count: self.buffer.line_count(),
        });
        visual_lines
    }

    /// Rebuilds only the logical-line slice affected by the latest edit.
    ///
    /// The common typing path changes one line. Reusing the unchanged prefix
    /// and suffix prevents wrapping work from scaling with total file size.
    /// Collapsed folds intentionally fall back to a full rebuild because an
    /// indentation edit can change which distant lines are hidden.
    pub(crate) fn refresh_visual_lines_after_edit(
        &self,
        previous_revision: u64,
    ) {
        if !self.collapsed_folds.is_empty() {
            *self.visual_lines_cache.borrow_mut() = None;
            return;
        }

        let mut cache_guard = self.visual_lines_cache.borrow_mut();
        let Some(cache) = cache_guard.as_mut() else { return };
        if cache.key.buffer_revision != previous_revision {
            *cache_guard = None;
            return;
        }

        let same_layout = cache.key.gutter_width_bits
            == self.gutter_width().to_bits()
            && cache.key.wrap_enabled == self.wrap_enabled
            && cache.key.wrap_column == self.wrap_column
            && cache.key.folding_enabled == self.folding_enabled
            && cache.key.fold_revision == self.fold_revision
            && cache.key.full_char_width_bits == self.full_char_width.to_bits()
            && cache.key.char_width_bits == self.char_width.to_bits();
        if !same_layout {
            *cache_guard = None;
            return;
        }

        let old_line_count = cache.buffer_line_count;
        let new_line_count = self.buffer.line_count();
        let start_line =
            self.pre_edit_line.saturating_sub(1).min(old_line_count);
        let old_end_line =
            self.pre_edit_last_line.saturating_add(2).min(old_line_count);
        let new_end_line = if new_line_count >= old_line_count {
            old_end_line
                .saturating_add(new_line_count - old_line_count)
                .min(new_line_count)
        } else {
            old_end_line
                .saturating_sub(old_line_count - new_line_count)
                .max(start_line)
                .min(new_line_count)
        };

        let prefix_end = cache
            .visual_lines
            .partition_point(|visual| visual.logical_line < start_line);
        let suffix_start = cache
            .visual_lines
            .partition_point(|visual| visual.logical_line < old_end_line);

        let wrapping_calc = wrapping::WrappingCalculator::new(
            self.wrap_enabled,
            self.wrap_column,
            self.full_char_width,
            self.char_width,
        );
        let changed_visual_lines = wrapping_calc.calculate_visual_lines_range(
            &self.buffer,
            f32::from_bits(cache.key.viewport_width_bits),
            f32::from_bits(cache.key.gutter_width_bits),
            &HashSet::new(),
            start_line..new_end_line,
        );

        let old_segment_count = suffix_start.saturating_sub(prefix_end);
        let new_segment_count = changed_visual_lines.len();
        let visual_lines = Rc::make_mut(&mut cache.visual_lines);

        // The overwhelmingly common typing case keeps both the logical-line
        // count and the number of wrapped segments stable. Update that tiny
        // slice in place, without allocating or moving the rest of the file.
        if new_line_count == old_line_count
            && old_segment_count == new_segment_count
        {
            visual_lines[prefix_end..suffix_start]
                .clone_from_slice(&changed_visual_lines);
        } else {
            visual_lines.splice(prefix_end..suffix_start, changed_visual_lines);

            let shifted_suffix_start = prefix_end + new_segment_count;
            for visual in &mut visual_lines[shifted_suffix_start..] {
                visual.logical_line = if new_line_count >= old_line_count {
                    visual
                        .logical_line
                        .saturating_add(new_line_count - old_line_count)
                } else {
                    visual
                        .logical_line
                        .saturating_sub(old_line_count - new_line_count)
                };
            }
        }

        cache.key.buffer_revision = self.buffer_revision;
        cache.buffer_line_count = new_line_count;
    }

    /// Updates the horizontal-width index for the lines affected by an edit.
    ///
    /// This removes the final whole-file pass that used to happen after every
    /// keystroke when wrapping was disabled.
    pub(crate) fn refresh_max_content_width_after_edit(
        &self,
        previous_revision: u64,
    ) {
        let mut cache_guard = self.max_content_width_cache.borrow_mut();
        let Some(cache) = cache_guard.as_mut() else { return };
        if cache.revision != previous_revision {
            *cache_guard = None;
            return;
        }

        let old_line_count = cache.line_widths.len();
        let new_line_count = self.buffer.line_count();
        let start_line =
            self.pre_edit_line.saturating_sub(1).min(old_line_count);
        let old_end_line =
            self.pre_edit_last_line.saturating_add(2).min(old_line_count);
        if start_line == 0 && old_end_line == old_line_count {
            *cache_guard = None;
            return;
        }

        let new_end_line = if new_line_count >= old_line_count {
            old_end_line
                .saturating_add(new_line_count - old_line_count)
                .min(new_line_count)
        } else {
            old_end_line
                .saturating_sub(old_line_count - new_line_count)
                .max(start_line)
                .min(new_line_count)
        };
        let old_widths = cache.line_widths[start_line..old_end_line].to_vec();
        let new_widths: Vec<f32> = (start_line..new_end_line)
            .map(|line| {
                measure_text_width(
                    self.buffer.line(line),
                    self.full_char_width,
                    self.char_width,
                )
            })
            .collect();

        for width in old_widths {
            cache.remove_width(width);
        }
        for width in &new_widths {
            cache.add_width(*width);
        }
        cache.line_widths.splice(start_line..old_end_line, new_widths);
        cache.revision = self.buffer_revision;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::canvas_editor::Message;

    #[test]
    fn test_bracket_depth_cache_extends_and_truncates() {
        let buffer = TextBuffer::new("fn main() {\n    let x = (1);\n}");
        let mut cache = BracketDepthCache::new();

        assert_eq!(cache.depth_at_line_start(&buffer, 0), 0);
        assert_eq!(cache.depth_at_line_start(&buffer, 1), 1);
        assert_eq!(cache.depth_at_line_start(&buffer, 2), 1);

        // Truncating from line 1 keeps depth entering line 1 but forces line 2
        // (and beyond) to be recomputed on next access.
        cache.truncate_from(1);
        assert_eq!(cache.depth_at_line_start(&buffer, 1), 1);
        assert_eq!(cache.depth_at_line_start(&buffer, 2), 1);
    }

    #[test]
    fn test_visual_lines_cached_reuses_cache_for_same_key() {
        let editor = CodeEditor::new("a\nb\nc", "rs");

        let first = editor.visual_lines_cached(800.0);
        let second = editor.visual_lines_cached(800.0);

        assert!(
            Rc::ptr_eq(&first, &second),
            "visual_lines_cached should reuse the cached Rc for identical keys"
        );
    }

    #[test]
    fn test_visual_lines_cached_changes_on_viewport_width_change() {
        let editor = CodeEditor::new("a\nb\nc", "rs");

        let first = editor.visual_lines_cached(800.0);
        let second = editor.visual_lines_cached(801.0);

        assert!(
            !Rc::ptr_eq(&first, &second),
            "visual_lines_cached should recompute when viewport width changes"
        );
    }

    #[test]
    fn test_visual_lines_cached_changes_on_buffer_revision_change() {
        let mut editor = CodeEditor::new("a\nb\nc", "rs");

        let first = editor.visual_lines_cached(800.0);
        editor.buffer_revision = editor.buffer_revision.wrapping_add(1);
        let second = editor.visual_lines_cached(800.0);

        assert!(
            !Rc::ptr_eq(&first, &second),
            "visual_lines_cached should recompute when buffer_revision changes"
        );
    }

    #[test]
    fn test_max_content_width_increases_with_longer_lines() {
        let short = CodeEditor::new("ab", "rs");
        let long =
            CodeEditor::new("abcdefghijklmnopqrstuvwxyz0123456789", "rs");

        assert!(
            long.max_content_width() > short.max_content_width(),
            "Longer lines should produce a greater max_content_width"
        );
    }

    #[test]
    fn test_max_content_width_cached_by_revision() {
        let mut editor = CodeEditor::new("hello", "rs");
        let w1 = editor.max_content_width();

        // Same revision → cache hit
        let w2 = editor.max_content_width();
        assert!(
            (w1 - w2).abs() < f32::EPSILON,
            "Repeated calls with same revision should return identical value"
        );

        // Bump revision to simulate edit
        editor.buffer_revision = editor.buffer_revision.wrapping_add(1);
        // Update the buffer to reflect a longer line
        editor.buffer =
            crate::buffer::TextBuffer::new("hello world with extra content");
        let w3 = editor.max_content_width();
        assert!(
            w3 > w1,
            "After revision bump with longer content, width should increase"
        );
    }

    #[test]
    fn test_max_content_width_cache_updates_incrementally_after_newline() {
        let mut editor =
            CodeEditor::new("short\nthis is the longest line\ntail", "rs");
        editor.set_wrap_enabled(false);
        editor.request_focus();
        editor.has_canvas_focus = true;
        editor.focus_locked = false;
        editor.cursors.primary_mut().position = (1, 7);
        let _ = editor.max_content_width();

        let _ = editor.update(&Message::Enter);
        let incremental = editor.max_content_width();
        let expected = CodeEditor::new(&editor.content(), "rs");

        assert!(
            (incremental - expected.max_content_width()).abs() < f32::EPSILON
        );
        let cache = editor.max_content_width_cache.borrow();
        assert_eq!(
            cache.as_ref().map(|cache| cache.line_widths.len()),
            Some(editor.buffer.line_count())
        );
        assert_eq!(
            cache.as_ref().map(|cache| cache.revision),
            Some(editor.buffer_revision)
        );
    }
}