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
//! Map/Dictionary control for editing key-value pairs
//!
//! Renders as an expandable list of entries:
//! ```text
//! Languages:
//!   > rust
//!   > python
//!   ▼ javascript (expanded)
//!       tab_size: 2
//!       auto_indent: [x]
//!   [+ Add entry...]
//! ```
//!
//! This module provides a complete map control with:
//! - State management (`MapState`)
//! - Rendering (`render_map`)
//! - Input handling (`MapState::handle_mouse`, `handle_key`)
//! - Layout/hit testing (`MapLayout`)

mod input;
mod render;

use ratatui::layout::Rect;
use ratatui::style::Color;

pub use input::MapEvent;
pub use render::render_map;

use super::FocusState;

/// State for a map/dictionary control
#[derive(Debug, Clone)]
pub struct MapState {
    /// Map entries as (key, value) pairs where value is JSON
    pub entries: Vec<(String, serde_json::Value)>,
    /// Currently focused entry index (None = add-new field)
    pub focused_entry: Option<usize>,
    /// Text in the "add new" key field
    pub new_key_text: String,
    /// Cursor position in the new key field
    pub cursor: usize,
    /// Label for this map
    pub label: String,
    /// Focus state
    pub focus: FocusState,
    /// Expanded entry indices (for viewing/editing nested values)
    pub expanded: Vec<usize>,
    /// Schema for value type (for creating new entries)
    pub value_schema: Option<Box<crate::view::settings::schema::SettingSchema>>,
    /// JSON pointer to field within value to display as preview (e.g., "/command")
    pub display_field: Option<String>,
}

impl MapState {
    /// Create a new map state
    pub fn new(label: impl Into<String>) -> Self {
        Self {
            entries: Vec::new(),
            focused_entry: None,
            new_key_text: String::new(),
            cursor: 0,
            label: label.into(),
            focus: FocusState::Normal,
            expanded: Vec::new(),
            value_schema: None,
            display_field: None,
        }
    }

    /// Set the display field (JSON pointer to field within value to show as preview)
    pub fn with_display_field(mut self, field: String) -> Self {
        self.display_field = Some(field);
        self
    }

    /// Get the display value for an entry (either from display_field or fallback to field count)
    pub fn get_display_value(&self, value: &serde_json::Value) -> String {
        if let Some(ref field) = self.display_field {
            if let Some(v) = value.pointer(field) {
                return match v {
                    serde_json::Value::String(s) => s.clone(),
                    serde_json::Value::Bool(b) => b.to_string(),
                    serde_json::Value::Number(n) => n.to_string(),
                    serde_json::Value::Null => "null".to_string(),
                    serde_json::Value::Array(arr) => format!("[{} items]", arr.len()),
                    serde_json::Value::Object(obj) => format!("{{{} fields}}", obj.len()),
                };
            }
        }
        // Fallback to showing field count
        match value {
            serde_json::Value::Object(obj) => format!("{{{} fields}}", obj.len()),
            serde_json::Value::Array(arr) => format!("[{} items]", arr.len()),
            other => other.to_string(),
        }
    }

    /// Set the entries from JSON value
    pub fn with_entries(mut self, value: &serde_json::Value) -> Self {
        if let Some(obj) = value.as_object() {
            self.entries = obj.iter().map(|(k, v)| (k.clone(), v.clone())).collect();
            // Sort by key for consistent ordering
            self.entries.sort_by(|a, b| a.0.cmp(&b.0));
            // Default to first entry if any exist
            if !self.entries.is_empty() {
                self.focused_entry = Some(0);
            }
        }
        self
    }

    /// Set the value schema for creating new entries
    pub fn with_value_schema(
        mut self,
        schema: crate::view::settings::schema::SettingSchema,
    ) -> Self {
        self.value_schema = Some(Box::new(schema));
        self
    }

    /// Set the focus state
    pub fn with_focus(mut self, focus: FocusState) -> Self {
        self.focus = focus;
        self
    }

    /// Check if the control is enabled
    pub fn is_enabled(&self) -> bool {
        self.focus != FocusState::Disabled
    }

    /// Add a new entry with the given key and default value
    pub fn add_entry(&mut self, key: String, value: serde_json::Value) {
        if self.focus == FocusState::Disabled || key.is_empty() {
            return;
        }
        // Check for duplicate key
        if self.entries.iter().any(|(k, _)| k == &key) {
            return;
        }
        self.entries.push((key, value));
        self.entries.sort_by(|a, b| a.0.cmp(&b.0));
    }

    /// Add entry from the new_key_text field with default value
    pub fn add_entry_from_input(&mut self) {
        if self.new_key_text.is_empty() {
            return;
        }
        let key = std::mem::take(&mut self.new_key_text);
        self.cursor = 0;
        // Use an empty object as default value
        self.add_entry(key, serde_json::json!({}));
    }

    /// Remove an entry by index
    pub fn remove_entry(&mut self, index: usize) {
        if self.focus == FocusState::Disabled || index >= self.entries.len() {
            return;
        }
        self.entries.remove(index);
        // Adjust focused_entry if needed
        if let Some(focused) = self.focused_entry {
            if focused >= self.entries.len() {
                self.focused_entry = if self.entries.is_empty() {
                    None
                } else {
                    Some(self.entries.len() - 1)
                };
            }
        }
        // Remove from expanded list
        self.expanded.retain(|&idx| idx != index);
        // Adjust expanded indices
        self.expanded = self
            .expanded
            .iter()
            .map(|&idx| if idx > index { idx - 1 } else { idx })
            .collect();
    }

    /// Focus on an entry
    pub fn focus_entry(&mut self, index: usize) {
        if index < self.entries.len() {
            self.focused_entry = Some(index);
        }
    }

    /// Focus on the new entry field
    pub fn focus_new_entry(&mut self) {
        self.focused_entry = None;
        self.cursor = self.new_key_text.len();
    }

    /// Toggle expansion of an entry
    pub fn toggle_expand(&mut self, index: usize) {
        if index >= self.entries.len() {
            return;
        }
        if let Some(pos) = self.expanded.iter().position(|&i| i == index) {
            self.expanded.remove(pos);
        } else {
            self.expanded.push(index);
        }
    }

    /// Check if an entry is expanded
    pub fn is_expanded(&self, index: usize) -> bool {
        self.expanded.contains(&index)
    }

    /// Insert a character in the new key field
    pub fn insert(&mut self, c: char) {
        if self.focus == FocusState::Disabled || self.focused_entry.is_some() {
            return;
        }
        self.new_key_text.insert(self.cursor, c);
        self.cursor += 1;
    }

    /// Backspace in the new key field
    pub fn backspace(&mut self) {
        if self.focus == FocusState::Disabled || self.focused_entry.is_some() || self.cursor == 0 {
            return;
        }
        self.cursor -= 1;
        self.new_key_text.remove(self.cursor);
    }

    /// Move cursor left
    pub fn move_left(&mut self) {
        if self.cursor > 0 {
            self.cursor -= 1;
        }
    }

    /// Move cursor right
    pub fn move_right(&mut self) {
        if self.cursor < self.new_key_text.len() {
            self.cursor += 1;
        }
    }

    /// Move focus to previous entry. Returns true if handled, false if should move to prev item.
    pub fn focus_prev(&mut self) -> bool {
        match self.focused_entry {
            Some(0) => false, // At first entry, move to previous item
            Some(idx) => {
                self.focused_entry = Some(idx - 1);
                true
            }
            None if !self.entries.is_empty() => {
                self.focused_entry = Some(self.entries.len() - 1);
                true
            }
            None => false, // Empty map, move to previous item
        }
    }

    /// Move focus to next entry. Returns true if handled, false if should move to next item.
    pub fn focus_next(&mut self) -> bool {
        match self.focused_entry {
            Some(idx) if idx + 1 < self.entries.len() => {
                self.focused_entry = Some(idx + 1);
                true
            }
            Some(_) => {
                self.focused_entry = None;
                self.cursor = self.new_key_text.len();
                true
            }
            None => false, // At add-new, move to next item
        }
    }

    /// Initialize focus when entering the control.
    /// `from_above`: true = start at first entry, false = start at add-new field
    pub fn init_focus(&mut self, from_above: bool) {
        if from_above && !self.entries.is_empty() {
            self.focused_entry = Some(0);
        } else {
            self.focused_entry = None;
            self.cursor = self.new_key_text.len();
        }
    }

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

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

    /// Convert entries to JSON value
    pub fn to_value(&self) -> serde_json::Value {
        let map: serde_json::Map<String, serde_json::Value> = self
            .entries
            .iter()
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect();
        serde_json::Value::Object(map)
    }
}

/// Colors for the map control
#[derive(Debug, Clone, Copy)]
pub struct MapColors {
    pub label: Color,
    pub key: Color,
    pub value_preview: Color,
    pub border: Color,
    pub remove_button: Color,
    pub add_button: Color,
    pub focused: Color,
    pub cursor: Color,
    pub disabled: Color,
    pub expand_arrow: Color,
}

impl Default for MapColors {
    fn default() -> Self {
        Self {
            label: Color::White,
            key: Color::Cyan,
            value_preview: Color::Gray,
            border: Color::Gray,
            remove_button: Color::Red,
            add_button: Color::Green,
            focused: Color::Yellow,
            cursor: Color::Yellow,
            disabled: Color::DarkGray,
            expand_arrow: Color::White,
        }
    }
}

impl MapColors {
    pub fn from_theme(theme: &crate::view::theme::Theme) -> Self {
        Self {
            label: theme.editor_fg,
            key: theme.menu_highlight_fg,
            value_preview: theme.line_number_fg,
            border: theme.line_number_fg,
            remove_button: theme.diagnostic_error_fg,
            add_button: theme.diagnostic_info_fg,
            focused: theme.selection_bg,
            cursor: theme.cursor,
            disabled: theme.line_number_fg,
            expand_arrow: theme.editor_fg,
        }
    }
}

/// Layout information for hit testing
#[derive(Debug, Clone, Default)]
pub struct MapLayout {
    pub full_area: Rect,
    pub entry_areas: Vec<MapEntryLayout>,
    pub add_row_area: Option<Rect>,
}

/// Layout for an entry row
#[derive(Debug, Clone)]
pub struct MapEntryLayout {
    pub index: usize,
    pub row_area: Rect,
    pub expand_area: Rect,
    pub key_area: Rect,
    pub remove_area: Rect,
}

impl MapLayout {
    /// Find what was clicked at the given coordinates
    pub fn hit_test(&self, x: u16, y: u16) -> Option<MapHit> {
        // Check entry rows
        for entry in &self.entry_areas {
            if y == entry.row_area.y {
                if x >= entry.remove_area.x && x < entry.remove_area.x + entry.remove_area.width {
                    return Some(MapHit::RemoveButton(entry.index));
                }
                if x >= entry.expand_area.x && x < entry.expand_area.x + entry.expand_area.width {
                    return Some(MapHit::ExpandArrow(entry.index));
                }
                if x >= entry.key_area.x && x < entry.key_area.x + entry.key_area.width {
                    return Some(MapHit::EntryKey(entry.index));
                }
            }
        }

        // Check add row - clicking anywhere on the row focuses the input
        if let Some(ref add_row) = self.add_row_area {
            if y == add_row.y && x >= add_row.x && x < add_row.x + add_row.width {
                return Some(MapHit::AddRow);
            }
        }

        None
    }
}

/// Result of hit testing on a map control
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MapHit {
    /// Clicked on expand/collapse arrow
    ExpandArrow(usize),
    /// Clicked on entry key
    EntryKey(usize),
    /// Clicked on remove button for entry
    RemoveButton(usize),
    /// Clicked on add row (input field or button area)
    AddRow,
}

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

    #[test]
    fn test_map_state_new() {
        let state = MapState::new("Test");
        assert_eq!(state.label, "Test");
        assert!(state.entries.is_empty());
        assert!(state.focused_entry.is_none());
    }

    #[test]
    fn test_map_state_add_entry() {
        let mut state = MapState::new("Test");
        state.add_entry("key1".to_string(), serde_json::json!({"foo": "bar"}));
        assert_eq!(state.entries.len(), 1);
        assert_eq!(state.entries[0].0, "key1");
    }

    #[test]
    fn test_map_state_remove_entry() {
        let mut state = MapState::new("Test");
        state.add_entry("a".to_string(), serde_json::json!({}));
        state.add_entry("b".to_string(), serde_json::json!({}));
        state.remove_entry(0);
        assert_eq!(state.entries.len(), 1);
        assert_eq!(state.entries[0].0, "b");
    }

    #[test]
    fn test_map_state_navigation() {
        let mut state = MapState::new("Test").with_focus(FocusState::Focused);
        state.add_entry("a".to_string(), serde_json::json!({}));
        state.add_entry("b".to_string(), serde_json::json!({}));

        // Start at add-new
        assert!(state.focused_entry.is_none());

        // Go to last entry
        state.focus_prev();
        assert_eq!(state.focused_entry, Some(1));

        // Go to first entry
        state.focus_prev();
        assert_eq!(state.focused_entry, Some(0));

        // Go forward
        state.focus_next();
        assert_eq!(state.focused_entry, Some(1));

        // Go to add-new
        state.focus_next();
        assert!(state.focused_entry.is_none());
    }

    #[test]
    fn test_map_state_expand() {
        let mut state = MapState::new("Test");
        state.add_entry("key1".to_string(), serde_json::json!({}));

        assert!(!state.is_expanded(0));
        state.toggle_expand(0);
        assert!(state.is_expanded(0));
        state.toggle_expand(0);
        assert!(!state.is_expanded(0));
    }

    #[test]
    fn test_map_hit_test() {
        let layout = MapLayout {
            full_area: Rect::new(0, 0, 50, 5),
            entry_areas: vec![MapEntryLayout {
                index: 0,
                row_area: Rect::new(0, 1, 50, 1),
                expand_area: Rect::new(2, 1, 1, 1),
                key_area: Rect::new(4, 1, 10, 1),
                remove_area: Rect::new(40, 1, 3, 1),
            }],
            add_row_area: Some(Rect::new(0, 2, 50, 1)),
        };

        assert_eq!(layout.hit_test(2, 1), Some(MapHit::ExpandArrow(0)));
        assert_eq!(layout.hit_test(5, 1), Some(MapHit::EntryKey(0)));
        assert_eq!(layout.hit_test(40, 1), Some(MapHit::RemoveButton(0)));
        assert_eq!(layout.hit_test(5, 2), Some(MapHit::AddRow));
        assert_eq!(layout.hit_test(13, 2), Some(MapHit::AddRow));
        assert_eq!(layout.hit_test(0, 0), None);
    }
}