croft-software 0.2.182

VSCode-style TUI written in Rust
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
//! The Run and Debug view: its keys, the Run or Debug button, the paused-state tree (call
//! stack, variables), clicks and the row menu with Copy Value and Copy Call Stack,
//! revealing the view, and how a launch failure shows.

use super::*;

/// Append variable rows to the debug tree, recursing into any expanded
/// (`variablesReference` in `expanded`) container whose children are loaded.
/// Pulled out as a free function so it can borrow the session immutably while
/// the caller mutates the row vector.
pub(in crate::app) fn push_variable_rows(
    rows: &mut Vec<crate::widgets::run_debug::DebugRow>,
    session: &crate::dap::session::DapSession,
    expanded: &std::collections::HashSet<i64>,
    vars: &[crate::dap::session::Variable],
    indent: u8,
) {
    use crate::widgets::run_debug::{DebugRow, DebugRowKind};
    for v in vars {
        let expandable = v.variables_ref > 0;
        let is_open = expandable && expanded.contains(&v.variables_ref);
        rows.push(DebugRow {
            indent,
            kind: DebugRowKind::Variable {
                reference: v.variables_ref,
                expandable,
                expanded: is_open,
                name: v.name.clone(),
                value: v.value.clone(),
                type_name: v.type_name.clone(),
                evaluate_name: v.evaluate_name.clone(),
            },
        });
        if is_open && let Some(children) = session.variables.get(&v.variables_ref) {
            push_variable_rows(rows, session, expanded, children, indent.saturating_add(1));
        }
    }
}

impl App {
    pub(in crate::app) fn handle_run_debug_key(&mut self, key: KeyEvent) {
        // While a session is live the panel is a REPL: text edits the prompt and
        // Enter evaluates it. Esc always leaves the view.
        if self.run_debug.debug_active {
            match key.code {
                KeyCode::Esc => self.set_sidebar_view(SidebarView::Explorer),
                KeyCode::Enter => {
                    self.debug_repl_submit();
                    self.refresh_debug_panel();
                }
                KeyCode::Backspace => {
                    self.run_debug.repl_input.pop();
                }
                KeyCode::Char(c) if !key.modifiers.contains(KeyModifiers::CONTROL) => {
                    self.run_debug.repl_input.push(c);
                }
                _ => {}
            }
            return;
        }
        match key.code {
            KeyCode::Esc => self.set_sidebar_view(SidebarView::Explorer),
            KeyCode::Enter => self.run_debug_button_action(),
            _ => {}
        }
    }

    pub(in crate::app) fn refresh_run_debug(&mut self) {
        let path = self.editor.path.clone();
        // Decide the button: scripts and executables are "Run" (terminal);
        // compiled source (no script runner, but a debug adapter) is "Debug"
        // (build + lldb). Files that are neither (e.g. `.git/config`) get no
        // button. run_spec_for already covers executables (the +x bit), so a
        // compiled binary is "Run" — run it directly.
        let (runnable, is_debug) = match path.as_deref() {
            Some(p) => {
                let ext = p
                    .extension()
                    .and_then(|e| e.to_str())
                    .unwrap_or("")
                    .to_ascii_lowercase();
                if Self::run_spec_for(
                    p,
                    self.roots
                        .owning_root(p)
                        .unwrap_or_else(|| self.roots.primary()),
                )
                .is_some()
                {
                    (true, false)
                } else if crate::dap::registry::adapter_for_extension(&ext).is_some() {
                    (true, true)
                } else {
                    (false, false)
                }
            }
            None => (false, false),
        };
        self.run_debug.runnable = runnable;
        self.run_debug.run_is_debug = is_debug;
        self.run_debug.set_active_file(path);
        self.run_debug.feedback = None;
        self.run_debug.feedback_is_error = false;
        // The launch.json config row (#250): count + F5 selection. Discovery
        // re-reads the same two small files once per helper — four reads — and
        // this only runs on active-file changes.
        // Compounds count too — the row is the documented route to the picker,
        // and the picker lists them, so counting only configurations makes the
        // row vanish for a compounds-only workspace and undercount otherwise.
        //
        // A HIDDEN compound is not listed (#318), so it must not be counted
        // either: the row and the picker have to agree, or a workspace whose
        // only entry is hidden advertises "1 debug entry" over a picker that
        // offers nothing. Same filter as `open_debug_config_picker`.
        let root = self.active_workspace_root();
        // The same filter the picker applies, so the badge and the picker
        // cannot disagree: a `presentation.hidden` entry of EITHER kind is
        // absent from both (#318).
        self.run_debug.config_count = crate::dap::configs::discover_configs(&root)
            .iter()
            .filter(|c| !c.hidden())
            .count()
            + crate::dap::configs::discover_compounds(&root)
                .iter()
                .filter(|c| !c.hidden())
                .count();
        self.run_debug.selected_config = self
            .selected_debug_compound
            .clone()
            .or_else(|| self.selected_debug_config.clone());
    }

    /// Rebuild the Run & Debug panel's paused-state tree from the session, or
    /// deactivate it when not paused. Cheap; called after each `poll_dap`.
    pub(in crate::app) fn refresh_debug_panel(&mut self) {
        let (active, status, rows) = self.build_debug_view();
        self.run_debug.debug_active = active;
        self.run_debug.debug_paused = self
            .debug_sessions
            .focused()
            .is_some_and(|s| s.phase == crate::dap::session::SessionPhase::Stopped);
        if active {
            self.run_debug.debug_status = status;
            self.run_debug.debug_rows = rows;
            let max_scroll = self.run_debug.debug_rows.len().saturating_sub(1);
            if self.run_debug.debug_scroll > max_scroll {
                self.run_debug.debug_scroll = max_scroll;
            }
            // Show the console tail (most recent output / REPL lines).
            let n = self.debug_console.len();
            self.run_debug.console_tail = self.debug_console[n.saturating_sub(50)..].to_vec();
        } else {
            self.run_debug.debug_rows.clear();
            self.run_debug.debug_scroll = 0;
            self.run_debug.repl_input.clear();
            // Retain the just-ended session's console output until the next run;
            // otherwise the idle Run panel shows nothing.
            if self.run_debug.session_ended && !self.debug_console.is_empty() {
                let n = self.debug_console.len();
                self.run_debug.console_tail = self.debug_console[n.saturating_sub(50)..].to_vec();
            } else {
                self.run_debug.console_tail.clear();
            }
        }
    }

    /// Build the (active, status, rows) tuple for the debug panel from the
    /// session. `&self` so it can borrow the session and expansion set without
    /// conflicting with the `&mut self` assignment in `refresh_debug_panel`.
    pub(in crate::app) fn build_debug_view(
        &self,
    ) -> (bool, String, Vec<crate::widgets::run_debug::DebugRow>) {
        use crate::dap::session::SessionPhase;
        use crate::widgets::run_debug::{DebugRow, DebugRowKind};
        let Some(session) = self.debug_sessions.focused() else {
            return (false, String::new(), Vec::new());
        };
        // Active for any live session: while Running the tree is empty but the
        // console still shows output; while Stopped the full tree is built.
        if session.phase == SessionPhase::Terminated {
            return (false, String::new(), Vec::new());
        }
        if session.phase != SessionPhase::Stopped {
            let status = self
                .run_debug
                .feedback
                .clone()
                .unwrap_or_else(|| String::from("Running"));
            return (
                true,
                status,
                breakpoint_section_rows(self.breakpoint_items().len()),
            );
        }
        let mut rows = Vec::new();
        rows.push(DebugRow {
            indent: 0,
            kind: DebugRowKind::Header {
                title: String::from("CALL STACK"),
            },
        });
        // With more than one thread (goroutines, under delve), each gets a
        // row and the selected one's frames sit under it, as in VS Code
        // (#264). A single thread keeps the plain frame list.
        let threaded = session.threads.len() > 1;
        let mut frames_at = rows.len();
        if threaded {
            for (id, name) in &session.threads {
                let selected = session.stopped_thread == Some(*id);
                rows.push(DebugRow {
                    indent: 1,
                    kind: DebugRowKind::Thread {
                        id: *id,
                        name: name.clone(),
                        selected,
                    },
                });
                if selected {
                    frames_at = rows.len();
                }
            }
        }
        let mut frame_rows = Vec::new();
        for f in &session.stack_frames {
            let loc = f
                .path
                .as_ref()
                .and_then(|p| p.file_name())
                .map(|n| n.to_string_lossy().into_owned())
                .unwrap_or_default();
            frame_rows.push(DebugRow {
                indent: if threaded { 2 } else { 1 },
                kind: DebugRowKind::Frame {
                    id: f.id,
                    selected: Some(f.id) == session.selected_frame,
                    name: f.name.clone(),
                    location: format!("{loc}:{}", f.line),
                },
            });
        }
        rows.splice(frames_at..frames_at, frame_rows);
        rows.push(DebugRow {
            indent: 0,
            kind: DebugRowKind::Header {
                title: String::from("VARIABLES"),
            },
        });
        for (index, scope) in session.scopes.iter().enumerate() {
            let open = session.scope_open(index);
            rows.push(DebugRow {
                indent: 1,
                kind: DebugRowKind::Scope {
                    name: scope.name.clone(),
                    index,
                    open: scope.expensive.then_some(open),
                },
            });
            if !open {
                continue;
            }
            if let Some(vars) = session.variables.get(&scope.variables_ref) {
                push_variable_rows(&mut rows, session, &self.debug_expanded, vars, 2);
            }
        }
        rows.push(DebugRow {
            indent: 0,
            kind: DebugRowKind::Header {
                title: String::from("WATCH"),
            },
        });
        for (widx, expr) in self.watch_exprs.iter().enumerate() {
            let (value, available, pending) = match self.watch_vals.get(expr) {
                Some((v, ok)) => (v.clone(), *ok, false),
                None => (String::new(), true, true),
            };
            let changed = available
                && !pending
                && self.watch_prev.get(expr).is_some_and(|prev| prev != &value);
            rows.push(DebugRow {
                indent: 1,
                kind: DebugRowKind::Watch {
                    index: widx,
                    expression: expr.clone(),
                    value,
                    available,
                    changed,
                    pending,
                },
            });
        }
        rows.push(DebugRow {
            indent: 1,
            kind: DebugRowKind::WatchAdd,
        });
        // Last, so `sync_breakpoint_list` can swap it without a rebuild.
        rows.extend(breakpoint_section_rows(self.breakpoint_items().len()));
        let status = self
            .run_debug
            .feedback
            .clone()
            .unwrap_or_else(|| String::from("Paused"));
        (true, status, rows)
    }

    /// Act on a click of debug-panel row `idx`: select a call-stack frame (load
    /// its scopes/variables) or toggle an expandable variable open/closed.
    pub(in crate::app) fn debug_panel_click(&mut self, idx: usize) {
        use crate::widgets::run_debug::DebugRowKind;
        let Some(row) = self.run_debug.debug_rows.get(idx) else {
            return;
        };
        match row.kind.clone() {
            DebugRowKind::Thread { id, .. } => {
                if let Some(session) = self.debug_sessions.focused_mut() {
                    session.select_thread(id);
                }
                self.debug_expanded.clear();
            }
            DebugRowKind::Frame { id, .. } => {
                if let Some(session) = self.debug_sessions.focused_mut() {
                    session.load_frame(id);
                }
                // Collapse expansions: variable refs are frame-scoped.
                self.debug_expanded.clear();
            }
            DebugRowKind::Variable {
                reference,
                expandable: true,
                ..
            } => {
                if self.debug_expanded.contains(&reference) {
                    self.debug_expanded.remove(&reference);
                } else {
                    self.debug_expanded.insert(reference);
                    if let Some(session) = self.debug_sessions.focused_mut() {
                        session.expand_variable(reference);
                    }
                }
            }
            DebugRowKind::WatchAdd => {
                self.open_add_watch_prompt();
            }
            // An expensive scope opens (and is fetched) on click (#1421).
            DebugRowKind::Scope {
                index,
                open: Some(_),
                ..
            } => {
                if let Some(session) = self.debug_sessions.focused_mut() {
                    session.toggle_expensive_scope(index);
                }
            }
            _ => {}
        }
        self.refresh_debug_panel();
    }

    /// The right-click menu of debug-panel row `idx` (#1546), VS Code's:
    /// a variable's Copy Value, Copy as Expression and Add to Watch, a
    /// watch's Copy Value, Copy Expression and Remove, and a frame's Copy
    /// Call Stack. Empty for a row that has none (a header, a scope).
    pub(in crate::app) fn debug_row_menu(&self, idx: usize) -> Vec<(String, MenuAction)> {
        use crate::widgets::run_debug::DebugRowKind;
        let Some(row) = self.run_debug.debug_rows.get(idx) else {
            return Vec::new();
        };
        match &row.kind {
            DebugRowKind::Variable {
                value,
                evaluate_name,
                ..
            } => {
                let mut items = vec![(
                    String::from("Copy Value"),
                    MenuAction::CopyDebugValue {
                        expression: evaluate_name.clone(),
                        value: value.clone(),
                    },
                )];
                if let Some(expr) = evaluate_name {
                    items.push((
                        String::from("Copy as Expression"),
                        MenuAction::CopyDebugExpression(expr.clone()),
                    ));
                    items.push((
                        String::from("Add to Watch"),
                        MenuAction::AddDebugWatch(expr.clone()),
                    ));
                }
                items
            }
            DebugRowKind::Watch {
                index,
                expression,
                value,
                ..
            } => vec![
                (
                    String::from("Copy Value"),
                    MenuAction::CopyDebugValue {
                        expression: Some(expression.clone()),
                        value: value.clone(),
                    },
                ),
                (
                    String::from("Copy Expression"),
                    MenuAction::CopyDebugExpression(expression.clone()),
                ),
                (
                    String::from("Remove Expression"),
                    MenuAction::RemoveDebugWatch(*index),
                ),
            ],
            DebugRowKind::Frame { .. } => {
                vec![(String::from("Copy Call Stack"), MenuAction::CopyCallStack)]
            }
            _ => Vec::new(),
        }
    }

    /// Copy Value (#1546). The row's `value` can be cut short by the
    /// adapter (debugpy elides a long string), so while paused, with an
    /// expression and an adapter that offers it, the value is asked for
    /// again in the `clipboard` context, which is not cut, and copied when
    /// it lands (`poll_dap`). Otherwise the row's value is copied at once.
    pub(in crate::app) fn copy_debug_value(&mut self, expression: Option<String>, value: String) {
        use crate::dap::session::SessionPhase;
        if let Some(expr) = expression
            && let Some(session) = self
                .debug_sessions
                .focused_mut()
                .filter(|s| s.phase == SessionPhase::Stopped)
            && session.supports("supportsClipboardContext")
        {
            session.evaluate(&expr, "clipboard");
            self.debug_copy_pending = Some((expr, value));
            self.status = String::from("Copying value…");
            return;
        }
        self.status = self.copy_where_pasted(&value).status("value");
    }

    /// Copy Call Stack (#1546): every frame of the paused thread, innermost
    /// first, as `name (path:line)`, VS Code's form.
    pub(in crate::app) fn copy_call_stack(&mut self) {
        let Some(session) = self.debug_sessions.focused() else {
            return;
        };
        let text = session
            .stack_frames
            .iter()
            .map(|f| match &f.path {
                Some(p) => format!("{} ({}:{})", f.name, p.display(), f.line),
                None => f.name.clone(),
            })
            .collect::<Vec<_>>()
            .join("\n");
        self.status = self.copy_where_pasted(&text).status("call stack");
    }

    /// The Run/Debug panel button action: debug compiled source (build + lldb),
    /// or run scripts/executables in a terminal — matching the button's verb.
    pub(in crate::app) fn run_debug_button_action(&mut self) {
        if self.run_debug.run_is_debug {
            self.debug_start_or_continue();
        } else {
            self.run_active_file();
        }
    }

    /// Open and focus the Run and Debug view so the call stack, variables,
    /// console, and REPL are visible the instant a session starts (VS Code
    /// reveals its debug view on launch the same way).
    pub(in crate::app) fn reveal_debug_view(&mut self) {
        // A fresh session: drop the previous run's retained console + stop
        // tracking so the "exited without hitting a breakpoint" hint and the
        // console reflect only this run.
        self.debug_console_dropped += self.debug_console.len();
        self.debug_console.clear();
        self.debug_ever_stopped = false;
        self.run_debug.session_ended = false;
        self.show_tree = true;
        self.set_sidebar_view(SidebarView::RunDebug);
        self.focus_pane(Pane::Tree);
    }

    /// Surface a debug-start failure on BOTH the styled Run/Debug panel
    /// feedback line AND the always-visible status bar. The panel line only
    /// renders while the Run/Debug sidebar is the active view (`reveal_debug_view`
    /// is reached only on success), so without the status-bar mirror F5 fails
    /// silently whenever the user is looking at any other sidebar — which read
    /// as "F5 does nothing" on a remote with no debug adapter installed.
    pub(in crate::app) fn debug_error(&mut self, msg: String) {
        // A launch that never happened leaves no session to clean up after,
        // so any breakpoint croft armed for it comes out here (#373) —
        // otherwise it survives in the editor as if the user had set it.
        self.disarm_failure_breakpoint();
        self.run_debug.feedback = Some(msg.clone());
        self.run_debug.feedback_is_error = true;
        self.status = msg;
    }
}