debtmap 0.20.0

Code complexity and technical debt analyzer
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
// Simplified fixture mimicking Zed's editor.rs structure
// Real editor.rs has 675 methods, 152 fields
// This fixture has a smaller but representative subset

pub struct Editor {
    // Display-related fields
    display_map: DisplayMap,
    style: EditorStyle,
    scroll_manager: ScrollManager,
    cursor: Cursor,
    gutter: Gutter,

    // Event handling fields
    focus_handle: FocusHandle,
    event_handlers: EventHandlers,
    input_buffer: InputBuffer,

    // Buffer and state fields
    buffer: Buffer,
    selections: Vec<Selection>,
    undo_stack: UndoStack,

    // Configuration fields
    settings: EditorSettings,
    theme: Theme,
    language: Language,
}

impl Editor {
    // Lifecycle methods
    pub fn new(buffer: Buffer, settings: EditorSettings) -> Self {
        Self {
            display_map: DisplayMap::new(),
            style: EditorStyle::default(),
            scroll_manager: ScrollManager::new(),
            cursor: Cursor::default(),
            gutter: Gutter::new(),
            focus_handle: FocusHandle::new(),
            event_handlers: EventHandlers::default(),
            input_buffer: InputBuffer::new(),
            buffer,
            selections: Vec::new(),
            undo_stack: UndoStack::new(),
            settings,
            theme: Theme::default(),
            language: Language::default(),
        }
    }

    pub fn init(&mut self) {
        self.display_map.initialize();
        self.scroll_manager.reset();
        self.setup_event_handlers();
    }

    pub fn shutdown(&mut self) {
        self.cleanup_handlers();
        self.buffer.flush();
    }

    // Rendering methods (cohesive group 1)
    pub fn render(&self) -> RenderOutput {
        let mut output = RenderOutput::new();
        self.render_gutter(&mut output);
        self.paint_highlighted_ranges(&mut output);
        self.draw_cursor(&mut output);
        self.paint_background(&mut output);
        output
    }

    pub fn render_gutter(&self, output: &mut RenderOutput) {
        output.add_gutter(self.gutter.render());
    }

    pub fn paint_highlighted_ranges(&self, output: &mut RenderOutput) {
        for selection in &self.selections {
            output.add_highlight(selection.range());
        }
    }

    pub fn draw_cursor(&self, output: &mut RenderOutput) {
        output.add_cursor(self.cursor.position());
    }

    pub fn paint_background(&self, output: &mut RenderOutput) {
        output.set_background(self.theme.background_color());
    }

    pub fn update_view(&mut self) {
        self.display_map.update();
        self.scroll_manager.adjust();
    }

    pub fn show_completions(&mut self) {
        // Show completion popup
    }

    pub fn display_diagnostics(&self) -> Vec<Diagnostic> {
        self.buffer.diagnostics()
    }

    pub fn format_line(&self, line: usize) -> String {
        self.buffer.line(line).to_string()
    }

    // Event handling methods (cohesive group 2)
    pub fn handle_keypress(&mut self, key: Key) {
        match key {
            Key::Enter => self.insert_newline(),
            Key::Backspace => self.delete_backward(),
            Key::Delete => self.delete_forward(),
            _ => self.insert_char(key.char()),
        }
    }

    pub fn on_mouse_down(&mut self, event: MouseEvent) {
        let position = self.display_map.point_from_screen(event.position);
        self.move_cursor_to(position);
    }

    pub fn on_scroll(&mut self, delta: f64) {
        self.scroll_manager.scroll_by(delta);
    }

    pub fn handle_input_event(&mut self, event: InputEvent) {
        self.input_buffer.push(event);
        self.process_input_buffer();
    }

    pub fn dispatch_action(&mut self, action: Action) {
        self.event_handlers.dispatch(action);
    }

    pub fn trigger_completion(&mut self) {
        self.show_completions();
    }

    pub fn process_event(&mut self, event: Event) {
        match event {
            Event::Key(key) => self.handle_keypress(key),
            Event::Mouse(mouse) => self.on_mouse_down(mouse),
            Event::Scroll(delta) => self.on_scroll(delta),
            _ => {}
        }
    }

    pub fn handle_paste(&mut self, text: String) {
        self.insert_text(text);
    }

    pub fn on_focus(&mut self) {
        self.focus_handle.acquire();
    }

    pub fn on_blur(&mut self) {
        self.focus_handle.release();
    }

    // Buffer manipulation methods (cohesive group 3)
    pub fn insert_text(&mut self, text: String) {
        self.buffer.insert(&self.cursor.position(), &text);
        self.undo_stack.push(Action::Insert(text));
    }

    pub fn insert_newline(&mut self) {
        self.insert_text("\n".to_string());
    }

    pub fn insert_char(&mut self, c: char) {
        self.insert_text(c.to_string());
    }

    pub fn delete_backward(&mut self) {
        if let Some(deleted) = self.buffer.delete_backward(&self.cursor.position()) {
            self.undo_stack.push(Action::Delete(deleted));
        }
    }

    pub fn delete_forward(&mut self) {
        if let Some(deleted) = self.buffer.delete_forward(&self.cursor.position()) {
            self.undo_stack.push(Action::Delete(deleted));
        }
    }

    pub fn undo(&mut self) {
        if let Some(action) = self.undo_stack.pop() {
            action.undo(&mut self.buffer);
        }
    }

    pub fn redo(&mut self) {
        if let Some(action) = self.undo_stack.redo() {
            action.apply(&mut self.buffer);
        }
    }

    // State management methods (cohesive group 4)
    pub fn get_selection(&self) -> &[Selection] {
        &self.selections
    }

    pub fn set_selection(&mut self, selections: Vec<Selection>) {
        self.selections = selections;
    }

    pub fn update_cursor(&mut self, position: Position) {
        self.cursor.set_position(position);
    }

    pub fn mutate_buffer(&mut self, f: impl FnOnce(&mut Buffer)) {
        f(&mut self.buffer);
    }

    pub fn state_snapshot(&self) -> EditorState {
        EditorState {
            cursor: self.cursor.clone(),
            selections: self.selections.clone(),
            scroll: self.scroll_manager.offset(),
        }
    }

    pub fn restore_state(&mut self, state: EditorState) {
        self.cursor = state.cursor;
        self.selections = state.selections;
        self.scroll_manager.set_offset(state.scroll);
    }

    // Validation methods (cohesive group 5)
    pub fn validate_position(&self, pos: Position) -> bool {
        self.buffer.is_valid_position(pos)
    }

    pub fn check_syntax(&self) -> Vec<SyntaxError> {
        self.buffer.parse_errors()
    }

    pub fn verify_selections(&self) -> bool {
        self.selections.iter().all(|s| s.is_valid())
    }

    pub fn ensure_valid_state(&self) -> Result<(), String> {
        if !self.verify_selections() {
            return Err("Invalid selections".to_string());
        }
        Ok(())
    }

    pub fn is_valid_edit(&self, edit: &Edit) -> bool {
        self.validate_position(edit.position())
    }

    // Persistence methods (cohesive group 6)
    pub fn save(&mut self, path: &str) -> Result<(), String> {
        self.buffer.write_to_file(path)
    }

    pub fn load(path: &str) -> Result<Self, String> {
        let buffer = Buffer::read_from_file(path)?;
        let settings = EditorSettings::default();
        Ok(Self::new(buffer, settings))
    }

    pub fn serialize(&self) -> String {
        serde_json::to_string(&self.state_snapshot()).unwrap()
    }

    pub fn deserialize(data: &str) -> Result<EditorState, String> {
        serde_json::from_str(data).map_err(|e| e.to_string())
    }

    // Helper methods
    fn setup_event_handlers(&mut self) {
        self.event_handlers.register("keypress", |e| {});
    }

    fn cleanup_handlers(&mut self) {
        self.event_handlers.clear();
    }

    fn move_cursor_to(&mut self, position: Position) {
        self.cursor.set_position(position);
    }

    fn process_input_buffer(&mut self) {
        while let Some(event) = self.input_buffer.pop() {
            self.handle_input_event(event);
        }
    }
}

// Supporting types
pub struct DisplayMap;
impl DisplayMap {
    fn new() -> Self { Self }
    fn initialize(&mut self) {}
    fn update(&mut self) {}
    fn point_from_screen(&self, _pos: (f64, f64)) -> Position { Position::default() }
}

pub struct EditorStyle;
impl Default for EditorStyle {
    fn default() -> Self { Self }
}

pub struct ScrollManager;
impl ScrollManager {
    fn new() -> Self { Self }
    fn reset(&mut self) {}
    fn adjust(&mut self) {}
    fn scroll_by(&mut self, _delta: f64) {}
    fn offset(&self) -> f64 { 0.0 }
    fn set_offset(&mut self, _offset: f64) {}
}

#[derive(Clone, Default)]
pub struct Cursor;
impl Cursor {
    fn position(&self) -> Position { Position::default() }
    fn set_position(&mut self, _pos: Position) {}
}

pub struct Gutter;
impl Gutter {
    fn new() -> Self { Self }
    fn render(&self) -> String { String::new() }
}

pub struct FocusHandle;
impl FocusHandle {
    fn new() -> Self { Self }
    fn acquire(&mut self) {}
    fn release(&mut self) {}
}

pub struct EventHandlers;
impl Default for EventHandlers {
    fn default() -> Self { Self }
}
impl EventHandlers {
    fn dispatch(&self, _action: Action) {}
    fn register(&mut self, _name: &str, _handler: impl Fn(Event)) {}
    fn clear(&mut self) {}
}

pub struct InputBuffer;
impl InputBuffer {
    fn new() -> Self { Self }
    fn push(&mut self, _event: InputEvent) {}
    fn pop(&mut self) -> Option<InputEvent> { None }
}

pub struct Buffer;
impl Buffer {
    fn flush(&mut self) {}
    fn insert(&mut self, _pos: &Position, _text: &str) {}
    fn delete_backward(&mut self, _pos: &Position) -> Option<String> { None }
    fn delete_forward(&mut self, _pos: &Position) -> Option<String> { None }
    fn is_valid_position(&self, _pos: Position) -> bool { true }
    fn parse_errors(&self) -> Vec<SyntaxError> { Vec::new() }
    fn diagnostics(&self) -> Vec<Diagnostic> { Vec::new() }
    fn line(&self, _n: usize) -> &str { "" }
    fn write_to_file(&self, _path: &str) -> Result<(), String> { Ok(()) }
    fn read_from_file(_path: &str) -> Result<Self, String> { Ok(Self) }
}

#[derive(Clone)]
pub struct Selection;
impl Selection {
    fn range(&self) -> (Position, Position) { (Position::default(), Position::default()) }
    fn is_valid(&self) -> bool { true }
}

pub struct UndoStack;
impl UndoStack {
    fn new() -> Self { Self }
    fn push(&mut self, _action: Action) {}
    fn pop(&mut self) -> Option<Action> { None }
    fn redo(&mut self) -> Option<Action> { None }
}

pub struct EditorSettings;
impl Default for EditorSettings {
    fn default() -> Self { Self }
}

pub struct Theme;
impl Default for Theme {
    fn default() -> Self { Self }
}
impl Theme {
    fn background_color(&self) -> String { "#ffffff".to_string() }
}

pub struct Language;
impl Default for Language {
    fn default() -> Self { Self }
}

#[derive(Clone, Default)]
pub struct Position;

pub struct RenderOutput;
impl RenderOutput {
    fn new() -> Self { Self }
    fn add_gutter(&mut self, _s: String) {}
    fn add_highlight(&mut self, _range: (Position, Position)) {}
    fn add_cursor(&mut self, _pos: Position) {}
    fn set_background(&mut self, _color: String) {}
}

pub struct Diagnostic;
pub struct SyntaxError;

pub enum Key {
    Enter,
    Backspace,
    Delete,
    Char(char),
}

impl Key {
    fn char(&self) -> char {
        match self {
            Key::Char(c) => *c,
            _ => '\0',
        }
    }
}

pub struct MouseEvent {
    position: (f64, f64),
}

pub struct InputEvent;

pub enum Event {
    Key(Key),
    Mouse(MouseEvent),
    Scroll(f64),
}

pub enum Action {
    Insert(String),
    Delete(String),
}

impl Action {
    fn undo(&self, _buffer: &mut Buffer) {}
    fn apply(&self, _buffer: &mut Buffer) {}
}

pub struct Edit;
impl Edit {
    fn position(&self) -> Position { Position::default() }
}

#[derive(Clone)]
pub struct EditorState {
    cursor: Cursor,
    selections: Vec<Selection>,
    scroll: f64,
}