fresh-editor 0.1.74

A lightweight, fast terminal-based text editor with LSP support and TypeScript plugins
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
//! Composite buffer for displaying multiple source buffers in a single view
//!
//! A composite buffer synthesizes its view from multiple source buffers,
//! enabling side-by-side diff, unified diff, 3-way merge, and code review views
//! within a single tab.

use crate::model::event::BufferId;
use serde::{Deserialize, Serialize};
use std::ops::Range;

/// A buffer that composes content from multiple source buffers
#[derive(Debug, Clone)]
pub struct CompositeBuffer {
    /// Unique ID for this composite buffer
    pub id: BufferId,

    /// Display name (shown in tab bar)
    pub name: String,

    /// Layout mode for this composite
    pub layout: CompositeLayout,

    /// Source buffer configurations
    pub sources: Vec<SourcePane>,

    /// Line alignment map (for side-by-side diff)
    /// Maps display_line -> (left_source_line, right_source_line)
    pub alignment: LineAlignment,

    /// Which pane currently has focus (for input routing)
    pub active_pane: usize,

    /// Mode for keybindings
    pub mode: String,
}

impl CompositeBuffer {
    /// Create a new composite buffer
    pub fn new(
        id: BufferId,
        name: String,
        mode: String,
        layout: CompositeLayout,
        sources: Vec<SourcePane>,
    ) -> Self {
        let pane_count = sources.len();
        Self {
            id,
            name,
            mode,
            layout,
            sources,
            alignment: LineAlignment::empty(pane_count),
            active_pane: 0,
        }
    }

    /// Get the number of source panes
    pub fn pane_count(&self) -> usize {
        self.sources.len()
    }

    /// Get the source pane at the given index
    pub fn get_pane(&self, index: usize) -> Option<&SourcePane> {
        self.sources.get(index)
    }

    /// Get the currently focused pane
    pub fn focused_pane(&self) -> Option<&SourcePane> {
        self.sources.get(self.active_pane)
    }

    /// Switch focus to the next pane
    pub fn focus_next(&mut self) {
        if !self.sources.is_empty() {
            self.active_pane = (self.active_pane + 1) % self.sources.len();
        }
    }

    /// Switch focus to the previous pane
    pub fn focus_prev(&mut self) {
        if !self.sources.is_empty() {
            self.active_pane = (self.active_pane + self.sources.len() - 1) % self.sources.len();
        }
    }

    /// Set the line alignment
    pub fn set_alignment(&mut self, alignment: LineAlignment) {
        self.alignment = alignment;
    }

    /// Get the total number of display rows
    pub fn row_count(&self) -> usize {
        self.alignment.rows.len()
    }
}

/// How the composite buffer arranges its source panes
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CompositeLayout {
    /// Side-by-side columns (for diff view)
    SideBySide {
        /// Width ratio for each pane (must sum to 1.0)
        ratios: Vec<f32>,
        /// Show separator between panes
        show_separator: bool,
    },
    /// Vertically stacked sections (for notebook cells)
    Stacked {
        /// Spacing between sections (in lines)
        spacing: u16,
    },
    /// Interleaved lines (for unified diff)
    Unified,
}

impl Default for CompositeLayout {
    fn default() -> Self {
        CompositeLayout::SideBySide {
            ratios: vec![0.5, 0.5],
            show_separator: true,
        }
    }
}

/// Configuration for a single source pane within the composite
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SourcePane {
    /// ID of the source buffer
    pub buffer_id: BufferId,

    /// Human-readable label (e.g., "OLD", "NEW", "BASE")
    pub label: String,

    /// Whether this pane accepts edits
    pub editable: bool,

    /// Visual style for this pane
    pub style: PaneStyle,

    /// Byte range in source buffer to display (None = entire buffer)
    pub range: Option<Range<usize>>,
}

impl SourcePane {
    /// Create a new source pane
    pub fn new(buffer_id: BufferId, label: impl Into<String>, editable: bool) -> Self {
        Self {
            buffer_id,
            label: label.into(),
            editable,
            style: PaneStyle::default(),
            range: None,
        }
    }

    /// Set the visual style
    pub fn with_style(mut self, style: PaneStyle) -> Self {
        self.style = style;
        self
    }

    /// Set the byte range to display
    pub fn with_range(mut self, range: Range<usize>) -> Self {
        self.range = Some(range);
        self
    }
}

/// Visual styling for a pane
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PaneStyle {
    /// Background color for added lines (RGB)
    pub add_bg: Option<(u8, u8, u8)>,
    /// Background color for removed lines (RGB)
    pub remove_bg: Option<(u8, u8, u8)>,
    /// Background color for modified lines (RGB)
    pub modify_bg: Option<(u8, u8, u8)>,
    /// Gutter indicator style
    pub gutter_style: GutterStyle,
}

impl PaneStyle {
    /// Create a style for the "old" side of a diff
    pub fn old_diff() -> Self {
        Self {
            remove_bg: Some((80, 30, 30)),
            gutter_style: GutterStyle::Both,
            ..Default::default()
        }
    }

    /// Create a style for the "new" side of a diff
    pub fn new_diff() -> Self {
        Self {
            add_bg: Some((30, 80, 30)),
            modify_bg: Some((80, 80, 30)),
            gutter_style: GutterStyle::Both,
            ..Default::default()
        }
    }
}

/// Gutter display style
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum GutterStyle {
    /// Show line numbers
    #[default]
    LineNumbers,
    /// Show diff markers (+/-/~)
    DiffMarkers,
    /// Show both line numbers and markers
    Both,
    /// Hide gutter
    None,
}

// ============================================================================
// Line Alignment
// ============================================================================

/// Alignment information for side-by-side views
#[derive(Debug, Clone, Default)]
pub struct LineAlignment {
    /// Each entry maps a display row to source lines in each pane
    /// None means padding (blank line) for that pane
    pub rows: Vec<AlignedRow>,
}

impl LineAlignment {
    /// Create an empty alignment for the given number of panes
    pub fn empty(_pane_count: usize) -> Self {
        Self { rows: Vec::new() }
    }

    /// Create alignment from simple line-by-line mapping (no diff)
    /// Assumes both buffers have the same number of lines
    pub fn simple(line_count: usize, pane_count: usize) -> Self {
        let rows = (0..line_count)
            .map(|line| AlignedRow {
                pane_lines: (0..pane_count)
                    .map(|_| {
                        Some(SourceLineRef {
                            line,
                            byte_range: 0..0, // Will be filled in during render
                        })
                    })
                    .collect(),
                row_type: RowType::Context,
            })
            .collect();
        Self { rows }
    }

    /// Create alignment from diff hunks
    pub fn from_hunks(hunks: &[DiffHunk], old_line_count: usize, new_line_count: usize) -> Self {
        let mut rows = Vec::new();
        let mut old_line = 0usize;
        let mut new_line = 0usize;

        for hunk in hunks {
            // Add context lines before this hunk
            while old_line < hunk.old_start && new_line < hunk.new_start {
                rows.push(AlignedRow::context(old_line, new_line));
                old_line += 1;
                new_line += 1;
            }

            // Add hunk header
            rows.push(AlignedRow::hunk_header());

            // Process hunk lines
            let old_end = hunk.old_start + hunk.old_count;
            let new_end = hunk.new_start + hunk.new_count;

            // Use a simple alignment: pair lines where possible, then pad
            let old_hunk_lines = old_end - hunk.old_start;
            let new_hunk_lines = new_end - hunk.new_start;
            let max_lines = old_hunk_lines.max(new_hunk_lines);

            for i in 0..max_lines {
                let old_idx = if i < old_hunk_lines {
                    Some(hunk.old_start + i)
                } else {
                    None
                };
                let new_idx = if i < new_hunk_lines {
                    Some(hunk.new_start + i)
                } else {
                    None
                };

                let row_type = match (old_idx, new_idx) {
                    (Some(_), Some(_)) => RowType::Modification,
                    (Some(_), None) => RowType::Deletion,
                    (None, Some(_)) => RowType::Addition,
                    (None, None) => continue,
                };

                rows.push(AlignedRow {
                    pane_lines: vec![
                        old_idx.map(|l| SourceLineRef {
                            line: l,
                            byte_range: 0..0,
                        }),
                        new_idx.map(|l| SourceLineRef {
                            line: l,
                            byte_range: 0..0,
                        }),
                    ],
                    row_type,
                });
            }

            old_line = old_end;
            new_line = new_end;
        }

        // Add remaining context lines after last hunk
        while old_line < old_line_count && new_line < new_line_count {
            rows.push(AlignedRow::context(old_line, new_line));
            old_line += 1;
            new_line += 1;
        }

        // Handle trailing lines in either buffer
        while old_line < old_line_count {
            rows.push(AlignedRow {
                pane_lines: vec![
                    Some(SourceLineRef {
                        line: old_line,
                        byte_range: 0..0,
                    }),
                    None,
                ],
                row_type: RowType::Deletion,
            });
            old_line += 1;
        }
        while new_line < new_line_count {
            rows.push(AlignedRow {
                pane_lines: vec![
                    None,
                    Some(SourceLineRef {
                        line: new_line,
                        byte_range: 0..0,
                    }),
                ],
                row_type: RowType::Addition,
            });
            new_line += 1;
        }

        Self { rows }
    }

    /// Get the aligned row at the given display index
    pub fn get_row(&self, display_row: usize) -> Option<&AlignedRow> {
        self.rows.get(display_row)
    }

    /// Get the number of display rows
    pub fn row_count(&self) -> usize {
        self.rows.len()
    }

    /// Find the next hunk header row after the given row
    pub fn next_hunk_row(&self, after_row: usize) -> Option<usize> {
        self.rows
            .iter()
            .enumerate()
            .skip(after_row + 1)
            .find(|(_, row)| row.row_type == RowType::HunkHeader)
            .map(|(i, _)| i)
    }

    /// Find the previous hunk header row before the given row
    pub fn prev_hunk_row(&self, before_row: usize) -> Option<usize> {
        self.rows
            .iter()
            .enumerate()
            .take(before_row)
            .rev()
            .find(|(_, row)| row.row_type == RowType::HunkHeader)
            .map(|(i, _)| i)
    }
}

/// A single aligned row mapping display to source lines
#[derive(Debug, Clone)]
pub struct AlignedRow {
    /// Source line for each pane (None = padding)
    pub pane_lines: Vec<Option<SourceLineRef>>,
    /// Type of this row for styling
    pub row_type: RowType,
}

impl AlignedRow {
    /// Create a context row (both sides have content)
    pub fn context(old_line: usize, new_line: usize) -> Self {
        Self {
            pane_lines: vec![
                Some(SourceLineRef {
                    line: old_line,
                    byte_range: 0..0,
                }),
                Some(SourceLineRef {
                    line: new_line,
                    byte_range: 0..0,
                }),
            ],
            row_type: RowType::Context,
        }
    }

    /// Create a hunk header row
    pub fn hunk_header() -> Self {
        Self {
            pane_lines: vec![None, None],
            row_type: RowType::HunkHeader,
        }
    }

    /// Get the source line for a specific pane
    pub fn get_pane_line(&self, pane_index: usize) -> Option<&SourceLineRef> {
        self.pane_lines.get(pane_index).and_then(|opt| opt.as_ref())
    }

    /// Check if this row has content in the given pane
    pub fn has_content(&self, pane_index: usize) -> bool {
        self.pane_lines
            .get(pane_index)
            .map(|opt| opt.is_some())
            .unwrap_or(false)
    }
}

/// Reference to a line in a source buffer
#[derive(Debug, Clone)]
pub struct SourceLineRef {
    /// Line number in source buffer (0-indexed)
    pub line: usize,
    /// Byte range in source buffer (computed during render)
    pub byte_range: Range<usize>,
}

/// Type of an aligned row for styling
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum RowType {
    /// Both sides have matching content
    Context,
    /// Line exists only in left/old (deletion)
    Deletion,
    /// Line exists only in right/new (addition)
    Addition,
    /// Line differs between sides
    Modification,
    /// Hunk separator/header
    HunkHeader,
}

/// A diff hunk describing a contiguous change
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiffHunk {
    /// Starting line in old buffer (0-indexed)
    pub old_start: usize,
    /// Number of lines in old buffer
    pub old_count: usize,
    /// Starting line in new buffer (0-indexed)
    pub new_start: usize,
    /// Number of lines in new buffer
    pub new_count: usize,
    /// Optional header text (function context)
    pub header: Option<String>,
}

impl DiffHunk {
    /// Create a new diff hunk
    pub fn new(old_start: usize, old_count: usize, new_start: usize, new_count: usize) -> Self {
        Self {
            old_start,
            old_count,
            new_start,
            new_count,
            header: None,
        }
    }

    /// Set the header text
    pub fn with_header(mut self, header: impl Into<String>) -> Self {
        self.header = Some(header.into());
        self
    }
}

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

    #[test]
    fn test_line_alignment_from_hunks() {
        // Test with a single hunk: old has 2 lines deleted, new has 3 lines added
        let hunks = vec![DiffHunk::new(2, 2, 2, 3)];
        let alignment = LineAlignment::from_hunks(&hunks, 5, 6);

        // Should have:
        // - 2 context rows (lines 0-1)
        // - 1 hunk header
        // - 3 hunk rows (max of 2 old, 3 new)
        // - 1 context row (old line 4, new line 5)
        assert!(alignment.rows.len() >= 7);

        // First two rows should be context
        assert_eq!(alignment.rows[0].row_type, RowType::Context);
        assert_eq!(alignment.rows[1].row_type, RowType::Context);

        // Third row should be hunk header
        assert_eq!(alignment.rows[2].row_type, RowType::HunkHeader);
    }

    #[test]
    fn test_composite_buffer_focus() {
        let sources = vec![
            SourcePane::new(BufferId(1), "OLD", false),
            SourcePane::new(BufferId(2), "NEW", true),
        ];
        let mut composite = CompositeBuffer::new(
            BufferId(0),
            "Test".to_string(),
            "diff-view".to_string(),
            CompositeLayout::default(),
            sources,
        );

        assert_eq!(composite.active_pane, 0);

        composite.focus_next();
        assert_eq!(composite.active_pane, 1);

        composite.focus_next();
        assert_eq!(composite.active_pane, 0); // Wraps around

        composite.focus_prev();
        assert_eq!(composite.active_pane, 1);
    }
}