fresh-editor 0.1.56

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
/// Position history for go back/forward navigation like VS Code
///
/// This module tracks the user's position history across buffers,
/// allowing navigation back and forward through editing locations.
/// Similar to VS Code's Alt+Left/Alt+Right navigation.
///
/// ## Architecture
///
/// Position history consumes MoveCursor events from the event log and coalesces
/// consecutive movements into single "jump" entries. This means:
/// - Many arrow key presses = one jump entry
/// - Each buffer switch = commits pending movement and adds new entry
/// - Idle period = commits pending movement
///
/// This matches VS Code's behavior where you can navigate back through your
/// editing trail, not through every single keystroke.
use crate::model::event::BufferId;

/// A single entry in the position history
#[derive(Clone, Debug, PartialEq)]
pub struct PositionEntry {
    /// The buffer ID
    pub buffer_id: BufferId,

    /// The cursor position (byte offset)
    pub position: usize,

    /// Optional selection anchor
    pub anchor: Option<usize>,
}

impl PositionEntry {
    /// Create a new position entry
    pub fn new(buffer_id: BufferId, position: usize, anchor: Option<usize>) -> Self {
        Self {
            buffer_id,
            position,
            anchor,
        }
    }
}

/// Pending movement that may be coalesced with subsequent movements
#[derive(Clone, Debug)]
struct PendingMovement {
    /// Starting position of this movement sequence
    start_entry: PositionEntry,
}

/// Distance threshold for considering a movement "large" (in bytes)
/// Movements larger than this will not be coalesced
const LARGE_JUMP_THRESHOLD: usize = 50;

/// Position history manager
///
/// This tracks navigation history across the editor, storing positions
/// the user has visited. It maintains a stack with a current index,
/// allowing back/forward navigation.
///
/// Movements are coalesced: consecutive MoveCursor events within a short
/// time period are treated as a single "jump" for navigation purposes.
pub struct PositionHistory {
    /// Stack of position entries
    entries: Vec<PositionEntry>,

    /// Current index in the stack (where we are in history)
    /// Points to the current position
    current_index: Option<usize>,

    /// Maximum number of entries to keep
    max_entries: usize,

    /// Pending movement that hasn't been committed yet
    /// Gets committed when: buffer switches, timeout expires, or significant event
    pending_movement: Option<PendingMovement>,
}

impl PositionHistory {
    /// Create a new position history with default max entries (100)
    pub fn new() -> Self {
        Self::with_capacity(100)
    }

    /// Create a new position history with specified max entries
    pub fn with_capacity(max_entries: usize) -> Self {
        Self {
            entries: Vec::new(),
            current_index: None,
            max_entries,
            pending_movement: None,
        }
    }

    /// Record a cursor movement event
    ///
    /// This is called for EVERY MoveCursor event. Consecutive small movements are coalesced
    /// into a single history entry. The movement is committed to history when:
    /// - Buffer changes
    /// - Large jump detected (> 50 bytes distance from pending start position)
    /// - User triggers back/forward navigation
    pub fn record_movement(&mut self, buffer_id: BufferId, position: usize, anchor: Option<usize>) {
        let entry = PositionEntry::new(buffer_id, position, anchor);

        match &mut self.pending_movement {
            Some(pending) => {
                // Check if this is a continuation of the current movement
                if pending.start_entry.buffer_id == buffer_id {
                    // Calculate distance from the pending movement's start position
                    let distance = if position > pending.start_entry.position {
                        position - pending.start_entry.position
                    } else {
                        pending.start_entry.position - position
                    };

                    // Check if this is a small movement that should be coalesced
                    if distance <= LARGE_JUMP_THRESHOLD {
                        // Small movement - keep coalescing, don't commit yet
                        return;
                    }
                }

                // Different buffer or large jump - commit the pending movement
                self.commit_pending_movement();
            }
            None => {}
        }

        // Start a new pending movement
        self.pending_movement = Some(PendingMovement { start_entry: entry });
    }

    /// Commit any pending movement to history
    ///
    /// This is called when:
    /// - Switching buffers
    /// - Before navigating back/forward
    pub fn commit_pending_movement(&mut self) {
        if let Some(pending) = self.pending_movement.take() {
            // Always call push(), which handles both:
            // 1. Truncating forward history (if we're not at the end)
            // 2. Checking for duplicates before adding
            self.push(pending.start_entry);
        }
    }

    /// Push a new position to the history
    ///
    /// This is called when the user makes a significant navigation:
    /// - Switching buffers
    /// - Large cursor movements (e.g., search, go-to-definition)
    /// - Opening a file
    ///
    /// If we're not at the end of history (user has gone back), this
    /// truncates the forward history and adds the new position.
    pub fn push(&mut self, entry: PositionEntry) {
        // If we're not at the end, truncate forward history FIRST
        // This ensures forward history is cleared even if the new entry is a duplicate
        if let Some(current_idx) = self.current_index {
            self.entries.truncate(current_idx + 1);
        }

        // Don't add duplicate consecutive entries
        if let Some(current_idx) = self.current_index {
            if current_idx < self.entries.len() {
                if self.entries[current_idx] == entry {
                    return;
                }
            }
        }

        // Add new entry
        self.entries.push(entry);

        // Limit size
        if self.entries.len() > self.max_entries {
            self.entries.remove(0);
        }

        // Update current index to point to the new entry
        self.current_index = Some(self.entries.len() - 1);
    }

    /// Navigate back in history
    ///
    /// Commits any pending movement first, then returns the previous position.
    /// Returns None if we're at the beginning of history.
    pub fn back(&mut self) -> Option<&PositionEntry> {
        // Commit any pending movement before navigating
        self.commit_pending_movement();

        if self.entries.is_empty() {
            return None;
        }

        match self.current_index {
            None => None,
            Some(0) => None, // Already at the beginning
            Some(idx) => {
                self.current_index = Some(idx - 1);
                Some(&self.entries[idx - 1])
            }
        }
    }

    /// Navigate forward in history
    ///
    /// Returns the next position, or None if we're at the end of history.
    pub fn forward(&mut self) -> Option<&PositionEntry> {
        if self.entries.is_empty() {
            return None;
        }

        match self.current_index {
            None => None,
            Some(idx) if idx >= self.entries.len() - 1 => None, // Already at the end
            Some(idx) => {
                self.current_index = Some(idx + 1);
                Some(&self.entries[idx + 1])
            }
        }
    }

    /// Check if we can go back
    pub fn can_go_back(&self) -> bool {
        match self.current_index {
            Some(idx) => idx > 0,
            None => false,
        }
    }

    /// Check if we can go forward
    pub fn can_go_forward(&self) -> bool {
        match self.current_index {
            Some(idx) => idx < self.entries.len() - 1,
            None => false,
        }
    }

    /// Get the current position entry
    pub fn current(&self) -> Option<&PositionEntry> {
        self.current_index.and_then(|idx| self.entries.get(idx))
    }

    /// Clear all history
    pub fn clear(&mut self) {
        self.entries.clear();
        self.current_index = None;
    }

    /// Get the number of entries in history
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Check if history is empty
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Get current index (for debugging)
    pub fn current_index(&self) -> Option<usize> {
        self.current_index
    }
}

impl Default for PositionHistory {
    fn default() -> Self {
        Self::new()
    }
}

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

    fn make_entry(buffer_id: usize, position: usize) -> PositionEntry {
        PositionEntry::new(BufferId(buffer_id), position, None)
    }

    #[test]
    fn test_new_history_is_empty() {
        let history = PositionHistory::new();
        assert!(history.is_empty());
        assert_eq!(history.len(), 0);
        assert!(!history.can_go_back());
        assert!(!history.can_go_forward());
    }

    #[test]
    fn test_push_single_entry() {
        let mut history = PositionHistory::new();
        let entry = make_entry(1, 10);

        history.push(entry.clone());

        assert_eq!(history.len(), 1);
        assert_eq!(history.current(), Some(&entry));
        assert!(!history.can_go_back());
        assert!(!history.can_go_forward());
    }

    #[test]
    fn test_push_multiple_entries() {
        let mut history = PositionHistory::new();
        let entry1 = make_entry(1, 10);
        let entry2 = make_entry(1, 20);
        let entry3 = make_entry(2, 5);

        history.push(entry1.clone());
        history.push(entry2.clone());
        history.push(entry3.clone());

        assert_eq!(history.len(), 3);
        assert_eq!(history.current(), Some(&entry3));
        assert!(history.can_go_back());
        assert!(!history.can_go_forward());
    }

    #[test]
    fn test_back_navigation() {
        let mut history = PositionHistory::new();
        let entry1 = make_entry(1, 10);
        let entry2 = make_entry(1, 20);
        let entry3 = make_entry(2, 5);

        history.push(entry1.clone());
        history.push(entry2.clone());
        history.push(entry3.clone());

        // Go back once
        let back1 = history.back();
        assert_eq!(back1, Some(&entry2));
        assert_eq!(history.current(), Some(&entry2));
        assert!(history.can_go_back());
        assert!(history.can_go_forward());

        // Go back again
        let back2 = history.back();
        assert_eq!(back2, Some(&entry1));
        assert_eq!(history.current(), Some(&entry1));
        assert!(!history.can_go_back());
        assert!(history.can_go_forward());

        // Try to go back at beginning
        let back3 = history.back();
        assert_eq!(back3, None);
        assert_eq!(history.current(), Some(&entry1));
    }

    #[test]
    fn test_forward_navigation() {
        let mut history = PositionHistory::new();
        let entry1 = make_entry(1, 10);
        let entry2 = make_entry(1, 20);
        let entry3 = make_entry(2, 5);

        history.push(entry1.clone());
        history.push(entry2.clone());
        history.push(entry3.clone());

        // Go back twice
        history.back();
        history.back();
        assert_eq!(history.current(), Some(&entry1));

        // Go forward once
        let fwd1 = history.forward();
        assert_eq!(fwd1, Some(&entry2));
        assert_eq!(history.current(), Some(&entry2));

        // Go forward again
        let fwd2 = history.forward();
        assert_eq!(fwd2, Some(&entry3));
        assert_eq!(history.current(), Some(&entry3));

        // Try to go forward at end
        let fwd3 = history.forward();
        assert_eq!(fwd3, None);
        assert_eq!(history.current(), Some(&entry3));
    }

    #[test]
    fn test_push_truncates_forward_history() {
        let mut history = PositionHistory::new();
        let entry1 = make_entry(1, 10);
        let entry2 = make_entry(1, 20);
        let entry3 = make_entry(2, 5);
        let entry4 = make_entry(2, 15);

        history.push(entry1.clone());
        history.push(entry2.clone());
        history.push(entry3.clone());

        // Go back twice
        history.back();
        history.back();
        assert_eq!(history.current(), Some(&entry1));

        // Push new entry - should truncate forward history
        history.push(entry4.clone());

        assert_eq!(history.len(), 2);
        assert_eq!(history.current(), Some(&entry4));
        assert!(history.can_go_back());
        assert!(!history.can_go_forward());

        // Verify we can go back to entry1
        let back = history.back();
        assert_eq!(back, Some(&entry1));
    }

    #[test]
    fn test_duplicate_consecutive_entries_not_added() {
        let mut history = PositionHistory::new();
        let entry1 = make_entry(1, 10);

        history.push(entry1.clone());
        history.push(entry1.clone());
        history.push(entry1.clone());

        assert_eq!(history.len(), 1);
    }

    #[test]
    fn test_max_entries_limit() {
        let mut history = PositionHistory::with_capacity(3);

        for i in 0..5 {
            history.push(make_entry(1, i * 10));
        }

        assert_eq!(history.len(), 3);
        // Should have kept the last 3 entries (20, 30, 40)
        assert_eq!(history.current(), Some(&make_entry(1, 40)));

        history.back();
        assert_eq!(history.current(), Some(&make_entry(1, 30)));

        history.back();
        assert_eq!(history.current(), Some(&make_entry(1, 20)));
    }

    #[test]
    fn test_clear() {
        let mut history = PositionHistory::new();

        history.push(make_entry(1, 10));
        history.push(make_entry(1, 20));

        assert_eq!(history.len(), 2);

        history.clear();

        assert!(history.is_empty());
        assert_eq!(history.len(), 0);
        assert_eq!(history.current(), None);
        assert!(!history.can_go_back());
        assert!(!history.can_go_forward());
    }
}