stack_frame_list.rs

  1use std::path::Path;
  2use std::sync::Arc;
  3use std::time::Duration;
  4
  5use anyhow::{Context as _, Result, anyhow};
  6use dap::StackFrameId;
  7use dap::adapters::DebugAdapterName;
  8use db::kvp::KEY_VALUE_STORE;
  9use gpui::{
 10    Action, AnyElement, Entity, EventEmitter, FocusHandle, Focusable, FontWeight, ListState,
 11    Subscription, Task, WeakEntity, list,
 12};
 13use util::{
 14    debug_panic,
 15    paths::{PathStyle, is_absolute},
 16};
 17
 18use crate::ToggleUserFrames;
 19use language::PointUtf16;
 20use project::debugger::breakpoint_store::ActiveStackFrame;
 21use project::debugger::session::{Session, SessionEvent, StackFrame, ThreadStatus};
 22use project::{ProjectItem, ProjectPath};
 23use ui::{Tooltip, WithScrollbar, prelude::*};
 24use workspace::{Workspace, WorkspaceId};
 25
 26use super::RunningState;
 27
 28#[derive(Debug)]
 29pub enum StackFrameListEvent {
 30    SelectedStackFrameChanged(StackFrameId),
 31    BuiltEntries,
 32}
 33
 34/// Represents the filter applied to the stack frame list
 35#[derive(PartialEq, Eq, Copy, Clone, Debug)]
 36pub(crate) enum StackFrameFilter {
 37    /// Show all frames
 38    All,
 39    /// Show only frames from the user's code
 40    OnlyUserFrames,
 41}
 42
 43impl StackFrameFilter {
 44    fn from_str_or_default(s: impl AsRef<str>) -> Self {
 45        match s.as_ref() {
 46            "user" => StackFrameFilter::OnlyUserFrames,
 47            "all" => StackFrameFilter::All,
 48            _ => StackFrameFilter::All,
 49        }
 50    }
 51}
 52
 53impl From<StackFrameFilter> for String {
 54    fn from(filter: StackFrameFilter) -> Self {
 55        match filter {
 56            StackFrameFilter::All => "all".to_string(),
 57            StackFrameFilter::OnlyUserFrames => "user".to_string(),
 58        }
 59    }
 60}
 61
 62pub(crate) fn stack_frame_filter_key(
 63    adapter_name: &DebugAdapterName,
 64    workspace_id: WorkspaceId,
 65) -> String {
 66    let database_id: i64 = workspace_id.into();
 67    format!("stack-frame-list-filter-{}-{}", adapter_name.0, database_id)
 68}
 69
 70pub struct StackFrameList {
 71    focus_handle: FocusHandle,
 72    _subscription: Subscription,
 73    session: Entity<Session>,
 74    state: WeakEntity<RunningState>,
 75    entries: Vec<StackFrameEntry>,
 76    workspace: WeakEntity<Workspace>,
 77    selected_ix: Option<usize>,
 78    opened_stack_frame_id: Option<StackFrameId>,
 79    list_state: ListState,
 80    list_filter: StackFrameFilter,
 81    filter_entries_indices: Vec<usize>,
 82    error: Option<SharedString>,
 83    _refresh_task: Task<()>,
 84}
 85
 86#[derive(Debug, PartialEq, Eq)]
 87pub enum StackFrameEntry {
 88    Normal(dap::StackFrame),
 89    /// Used to indicate that the frame is artificial and is a visual label or separator
 90    Label(dap::StackFrame),
 91    Collapsed(Vec<dap::StackFrame>),
 92}
 93
 94impl StackFrameList {
 95    pub fn new(
 96        workspace: WeakEntity<Workspace>,
 97        session: Entity<Session>,
 98        state: WeakEntity<RunningState>,
 99        window: &mut Window,
100        cx: &mut Context<Self>,
101    ) -> Self {
102        let focus_handle = cx.focus_handle();
103
104        let _subscription =
105            cx.subscribe_in(&session, window, |this, _, event, window, cx| match event {
106                SessionEvent::Threads => {
107                    this.schedule_refresh(false, window, cx);
108                }
109                SessionEvent::Stopped(..)
110                | SessionEvent::StackTrace
111                | SessionEvent::HistoricSnapshotSelected => {
112                    this.schedule_refresh(true, window, cx);
113                }
114                _ => {}
115            });
116
117        let list_state = ListState::new(0, gpui::ListAlignment::Top, px(1000.));
118
119        let list_filter = workspace
120            .read_with(cx, |workspace, _| workspace.database_id())
121            .ok()
122            .flatten()
123            .and_then(|database_id| {
124                let key = stack_frame_filter_key(&session.read(cx).adapter(), database_id);
125                KEY_VALUE_STORE
126                    .read_kvp(&key)
127                    .ok()
128                    .flatten()
129                    .map(StackFrameFilter::from_str_or_default)
130            })
131            .unwrap_or(StackFrameFilter::All);
132
133        let mut this = Self {
134            session,
135            workspace,
136            focus_handle,
137            state,
138            _subscription,
139            entries: Default::default(),
140            filter_entries_indices: Vec::default(),
141            error: None,
142            selected_ix: None,
143            opened_stack_frame_id: None,
144            list_filter,
145            list_state,
146            _refresh_task: Task::ready(()),
147        };
148        this.schedule_refresh(true, window, cx);
149        this
150    }
151
152    #[cfg(test)]
153    pub(crate) fn entries(&self) -> &Vec<StackFrameEntry> {
154        &self.entries
155    }
156
157    #[cfg(test)]
158    pub(crate) fn flatten_entries(
159        &self,
160        show_collapsed: bool,
161        show_labels: bool,
162    ) -> Vec<dap::StackFrame> {
163        self.entries
164            .iter()
165            .enumerate()
166            .filter(|(ix, _)| {
167                self.list_filter == StackFrameFilter::All
168                    || self
169                        .filter_entries_indices
170                        .binary_search_by_key(&ix, |ix| ix)
171                        .is_ok()
172            })
173            .flat_map(|(_, frame)| match frame {
174                StackFrameEntry::Normal(frame) => vec![frame.clone()],
175                StackFrameEntry::Label(frame) if show_labels => vec![frame.clone()],
176                StackFrameEntry::Collapsed(frames) if show_collapsed => frames.clone(),
177                _ => vec![],
178            })
179            .collect::<Vec<_>>()
180    }
181
182    fn stack_frames(&self, cx: &mut App) -> Result<Vec<StackFrame>> {
183        if let Ok(Some(thread_id)) = self.state.read_with(cx, |state, _| state.thread_id) {
184            self.session
185                .update(cx, |this, cx| this.stack_frames(thread_id, cx))
186        } else {
187            Ok(Vec::default())
188        }
189    }
190
191    #[cfg(test)]
192    pub(crate) fn dap_stack_frames(&self, cx: &mut App) -> Vec<dap::StackFrame> {
193        match self.list_filter {
194            StackFrameFilter::All => self
195                .stack_frames(cx)
196                .unwrap_or_default()
197                .into_iter()
198                .map(|stack_frame| stack_frame.dap)
199                .collect(),
200            StackFrameFilter::OnlyUserFrames => self
201                .filter_entries_indices
202                .iter()
203                .map(|ix| match &self.entries[*ix] {
204                    StackFrameEntry::Label(label) => label,
205                    StackFrameEntry::Collapsed(_) => panic!("Collapsed tabs should not be visible"),
206                    StackFrameEntry::Normal(frame) => frame,
207                })
208                .cloned()
209                .collect(),
210        }
211    }
212
213    #[cfg(test)]
214    pub(crate) fn list_filter(&self) -> StackFrameFilter {
215        self.list_filter
216    }
217
218    pub fn opened_stack_frame_id(&self) -> Option<StackFrameId> {
219        self.opened_stack_frame_id
220    }
221
222    pub(super) fn schedule_refresh(
223        &mut self,
224        select_first: bool,
225        window: &mut Window,
226        cx: &mut Context<Self>,
227    ) {
228        const REFRESH_DEBOUNCE: Duration = Duration::from_millis(20);
229
230        self._refresh_task = cx.spawn_in(window, async move |this, cx| {
231            let debounce = this
232                .update(cx, |this, cx| {
233                    let new_stack_frames = this.stack_frames(cx);
234                    new_stack_frames.unwrap_or_default().is_empty() && !this.entries.is_empty()
235                })
236                .ok()
237                .unwrap_or_default();
238
239            if debounce {
240                cx.background_executor().timer(REFRESH_DEBOUNCE).await;
241            }
242            this.update_in(cx, |this, window, cx| {
243                this.build_entries(select_first, window, cx);
244            })
245            .ok();
246        })
247    }
248
249    pub fn build_entries(
250        &mut self,
251        open_first_stack_frame: bool,
252        window: &mut Window,
253        cx: &mut Context<Self>,
254    ) {
255        let old_selected_frame_id = self
256            .selected_ix
257            .and_then(|ix| self.entries.get(ix))
258            .and_then(|entry| match entry {
259                StackFrameEntry::Normal(stack_frame) => Some(stack_frame.id),
260                StackFrameEntry::Collapsed(_) | StackFrameEntry::Label(_) => None,
261            });
262        let mut entries = Vec::new();
263        let mut collapsed_entries = Vec::new();
264        let mut first_stack_frame = None;
265        let mut first_stack_frame_with_path = None;
266
267        let stack_frames = match self.stack_frames(cx) {
268            Ok(stack_frames) => stack_frames,
269            Err(e) => {
270                self.error = Some(format!("{}", e).into());
271                self.entries.clear();
272                self.selected_ix = None;
273                self.list_state.reset(0);
274                self.filter_entries_indices.clear();
275                cx.emit(StackFrameListEvent::BuiltEntries);
276                cx.notify();
277                return;
278            }
279        };
280
281        let worktree_prefixes: Vec<_> = self
282            .workspace
283            .read_with(cx, |workspace, cx| {
284                workspace
285                    .visible_worktrees(cx)
286                    .map(|tree| tree.read(cx).abs_path())
287                    .collect()
288            })
289            .unwrap_or_default();
290
291        let mut filter_entries_indices = Vec::default();
292        for stack_frame in stack_frames.iter() {
293            let frame_in_visible_worktree = stack_frame.dap.source.as_ref().is_some_and(|source| {
294                source.path.as_ref().is_some_and(|path| {
295                    worktree_prefixes
296                        .iter()
297                        .filter_map(|tree| tree.to_str())
298                        .any(|tree| path.starts_with(tree))
299                })
300            });
301
302            match stack_frame.dap.presentation_hint {
303                Some(dap::StackFramePresentationHint::Deemphasize)
304                | Some(dap::StackFramePresentationHint::Subtle) => {
305                    collapsed_entries.push(stack_frame.dap.clone());
306                }
307                Some(dap::StackFramePresentationHint::Label) => {
308                    entries.push(StackFrameEntry::Label(stack_frame.dap.clone()));
309                }
310                _ => {
311                    let collapsed_entries = std::mem::take(&mut collapsed_entries);
312                    if !collapsed_entries.is_empty() {
313                        entries.push(StackFrameEntry::Collapsed(collapsed_entries.clone()));
314                    }
315
316                    first_stack_frame.get_or_insert(entries.len());
317
318                    if stack_frame
319                        .dap
320                        .source
321                        .as_ref()
322                        .is_some_and(|source| source.path.is_some())
323                    {
324                        first_stack_frame_with_path.get_or_insert(entries.len());
325                    }
326                    entries.push(StackFrameEntry::Normal(stack_frame.dap.clone()));
327                    if frame_in_visible_worktree {
328                        filter_entries_indices.push(entries.len() - 1);
329                    }
330                }
331            }
332        }
333
334        let collapsed_entries = std::mem::take(&mut collapsed_entries);
335        if !collapsed_entries.is_empty() {
336            entries.push(StackFrameEntry::Collapsed(collapsed_entries));
337        }
338        self.entries = entries;
339        self.filter_entries_indices = filter_entries_indices;
340
341        if let Some(ix) = first_stack_frame_with_path
342            .or(first_stack_frame)
343            .filter(|_| open_first_stack_frame)
344        {
345            self.select_ix(Some(ix), cx);
346            self.activate_selected_entry(window, cx);
347        } else if let Some(old_selected_frame_id) = old_selected_frame_id {
348            let ix = self.entries.iter().position(|entry| match entry {
349                StackFrameEntry::Normal(frame) => frame.id == old_selected_frame_id,
350                StackFrameEntry::Collapsed(_) | StackFrameEntry::Label(_) => false,
351            });
352            self.selected_ix = ix;
353        }
354
355        match self.list_filter {
356            StackFrameFilter::All => {
357                self.list_state.reset(self.entries.len());
358            }
359            StackFrameFilter::OnlyUserFrames => {
360                self.list_state.reset(self.filter_entries_indices.len());
361            }
362        }
363        cx.emit(StackFrameListEvent::BuiltEntries);
364        cx.notify();
365    }
366
367    pub fn go_to_stack_frame(
368        &mut self,
369        stack_frame_id: StackFrameId,
370        window: &mut Window,
371        cx: &mut Context<Self>,
372    ) -> Task<Result<()>> {
373        let Some(stack_frame) = self
374            .entries
375            .iter()
376            .flat_map(|entry| match entry {
377                StackFrameEntry::Label(stack_frame) => std::slice::from_ref(stack_frame),
378                StackFrameEntry::Normal(stack_frame) => std::slice::from_ref(stack_frame),
379                StackFrameEntry::Collapsed(stack_frames) => stack_frames.as_slice(),
380            })
381            .find(|stack_frame| stack_frame.id == stack_frame_id)
382            .cloned()
383        else {
384            return Task::ready(Err(anyhow!("No stack frame for ID")));
385        };
386        self.go_to_stack_frame_inner(stack_frame, window, cx)
387    }
388
389    fn go_to_stack_frame_inner(
390        &mut self,
391        stack_frame: dap::StackFrame,
392        window: &mut Window,
393        cx: &mut Context<Self>,
394    ) -> Task<Result<()>> {
395        let stack_frame_id = stack_frame.id;
396        self.opened_stack_frame_id = Some(stack_frame_id);
397        let Some(abs_path) = Self::abs_path_from_stack_frame(&stack_frame) else {
398            return Task::ready(Err(anyhow!("Project path not found")));
399        };
400        let row = stack_frame.line.saturating_sub(1) as u32;
401        cx.emit(StackFrameListEvent::SelectedStackFrameChanged(
402            stack_frame_id,
403        ));
404        cx.spawn_in(window, async move |this, cx| {
405            let (worktree, relative_path) = this
406                .update(cx, |this, cx| {
407                    this.workspace.update(cx, |workspace, cx| {
408                        workspace.project().update(cx, |this, cx| {
409                            this.find_or_create_worktree(&abs_path, false, cx)
410                        })
411                    })
412                })??
413                .await?;
414            let buffer = this
415                .update(cx, |this, cx| {
416                    this.workspace.update(cx, |this, cx| {
417                        this.project().update(cx, |this, cx| {
418                            let worktree_id = worktree.read(cx).id();
419                            this.open_buffer(
420                                ProjectPath {
421                                    worktree_id,
422                                    path: relative_path,
423                                },
424                                cx,
425                            )
426                        })
427                    })
428                })??
429                .await?;
430            let position = buffer.read_with(cx, |this, _| {
431                this.snapshot().anchor_after(PointUtf16::new(row, 0))
432            });
433            let opened_item = this
434                .update_in(cx, |this, window, cx| {
435                    this.workspace.update(cx, |workspace, cx| {
436                        let project_path = buffer
437                            .read(cx)
438                            .project_path(cx)
439                            .context("Could not select a stack frame for unnamed buffer")?;
440
441                        let open_preview = true;
442
443                        let active_debug_line_pane = workspace
444                            .project()
445                            .read(cx)
446                            .breakpoint_store()
447                            .read(cx)
448                            .active_debug_line_pane_id()
449                            .and_then(|id| workspace.pane_for_entity_id(id));
450
451                        let debug_pane = if let Some(pane) = active_debug_line_pane {
452                            Some(pane.downgrade())
453                        } else {
454                            // No debug pane set yet. Find a pane where the target file
455                            // is already the active tab so we don't disrupt other panes.
456                            let pane_with_active_file = workspace.panes().iter().find(|pane| {
457                                pane.read(cx)
458                                    .active_item()
459                                    .and_then(|item| item.project_path(cx))
460                                    .is_some_and(|path| path == project_path)
461                            });
462
463                            pane_with_active_file.map(|pane| pane.downgrade())
464                        };
465
466                        anyhow::Ok(workspace.open_path_preview(
467                            project_path,
468                            debug_pane,
469                            true,
470                            true,
471                            open_preview,
472                            window,
473                            cx,
474                        ))
475                    })
476                })???
477                .await?;
478
479            this.update(cx, |this, cx| {
480                let thread_id = this.state.read_with(cx, |state, _| {
481                    state.thread_id.context("No selected thread ID found")
482                })??;
483
484                this.workspace.update(cx, |workspace, cx| {
485                    if let Some(pane_id) = workspace
486                        .pane_for(&*opened_item)
487                        .map(|pane| pane.entity_id())
488                    {
489                        workspace
490                            .project()
491                            .read(cx)
492                            .breakpoint_store()
493                            .update(cx, |store, _cx| {
494                                store.set_active_debug_pane_id(pane_id);
495                            });
496                    }
497
498                    let breakpoint_store = workspace.project().read(cx).breakpoint_store();
499
500                    breakpoint_store.update(cx, |store, cx| {
501                        store.set_active_position(
502                            ActiveStackFrame {
503                                session_id: this.session.read(cx).session_id(),
504                                thread_id,
505                                stack_frame_id,
506                                path: abs_path,
507                                position,
508                            },
509                            cx,
510                        );
511                    })
512                })
513            })?
514        })
515    }
516
517    pub(crate) fn abs_path_from_stack_frame(stack_frame: &dap::StackFrame) -> Option<Arc<Path>> {
518        stack_frame.source.as_ref().and_then(|s| {
519            s.path
520                .as_deref()
521                .filter(|path| {
522                    // Since we do not know if we are debugging on the host or (a remote/WSL) target,
523                    // we need to check if either the path is absolute as Posix or Windows.
524                    is_absolute(path, PathStyle::Posix) || is_absolute(path, PathStyle::Windows)
525                })
526                .map(|path| Arc::<Path>::from(Path::new(path)))
527        })
528    }
529
530    pub fn restart_stack_frame(&mut self, stack_frame_id: u64, cx: &mut Context<Self>) {
531        self.session.update(cx, |state, cx| {
532            state.restart_stack_frame(stack_frame_id, cx)
533        });
534    }
535
536    fn render_label_entry(
537        &self,
538        stack_frame: &dap::StackFrame,
539        _cx: &mut Context<Self>,
540    ) -> AnyElement {
541        h_flex()
542            .rounded_md()
543            .justify_between()
544            .w_full()
545            .group("")
546            .id(("label-stack-frame", stack_frame.id))
547            .p_1()
548            .on_any_mouse_down(|_, _, cx| {
549                cx.stop_propagation();
550            })
551            .child(
552                v_flex().justify_center().gap_0p5().child(
553                    Label::new(stack_frame.name.clone())
554                        .size(LabelSize::Small)
555                        .weight(FontWeight::BOLD)
556                        .truncate()
557                        .color(Color::Info),
558                ),
559            )
560            .into_any()
561    }
562
563    fn render_normal_entry(
564        &self,
565        ix: usize,
566        stack_frame: &dap::StackFrame,
567        cx: &mut Context<Self>,
568    ) -> AnyElement {
569        let source = stack_frame.source.clone();
570        let is_selected_frame = Some(ix) == self.selected_ix;
571
572        let path = source.and_then(|s| s.path.or(s.name));
573        let formatted_path = path.map(|path| format!("{}:{}", path, stack_frame.line,));
574        let formatted_path = formatted_path.map(|path| {
575            Label::new(path)
576                .size(LabelSize::XSmall)
577                .line_height_style(LineHeightStyle::UiLabel)
578                .truncate()
579                .color(Color::Muted)
580        });
581
582        let supports_frame_restart = self
583            .session
584            .read(cx)
585            .capabilities()
586            .supports_restart_frame
587            .unwrap_or_default();
588
589        let should_deemphasize = matches!(
590            stack_frame.presentation_hint,
591            Some(
592                dap::StackFramePresentationHint::Subtle
593                    | dap::StackFramePresentationHint::Deemphasize
594            )
595        );
596        h_flex()
597            .rounded_md()
598            .justify_between()
599            .w_full()
600            .group("")
601            .id(("stack-frame", stack_frame.id))
602            .p_1()
603            .when(is_selected_frame, |this| {
604                this.bg(cx.theme().colors().element_hover)
605            })
606            .on_any_mouse_down(|_, _, cx| {
607                cx.stop_propagation();
608            })
609            .on_click(cx.listener(move |this, _, window, cx| {
610                this.selected_ix = Some(ix);
611                this.activate_selected_entry(window, cx);
612            }))
613            .hover(|style| style.bg(cx.theme().colors().element_hover).cursor_pointer())
614            .overflow_x_scroll()
615            .child(
616                v_flex()
617                    .gap_0p5()
618                    .child(
619                        Label::new(stack_frame.name.clone())
620                            .size(LabelSize::Small)
621                            .truncate()
622                            .when(should_deemphasize, |this| this.color(Color::Muted)),
623                    )
624                    .children(formatted_path),
625            )
626            .when(
627                supports_frame_restart && stack_frame.can_restart.unwrap_or(true),
628                |this| {
629                    this.child(
630                        h_flex()
631                            .id(("restart-stack-frame", stack_frame.id))
632                            .visible_on_hover("")
633                            .absolute()
634                            .right_2()
635                            .overflow_hidden()
636                            .rounded_md()
637                            .border_1()
638                            .border_color(cx.theme().colors().element_selected)
639                            .bg(cx.theme().colors().element_background)
640                            .hover(|style| {
641                                style
642                                    .bg(cx.theme().colors().ghost_element_hover)
643                                    .cursor_pointer()
644                            })
645                            .child(
646                                IconButton::new(
647                                    ("restart-stack-frame", stack_frame.id),
648                                    IconName::RotateCcw,
649                                )
650                                .icon_size(IconSize::Small)
651                                .on_click(cx.listener({
652                                    let stack_frame_id = stack_frame.id;
653                                    move |this, _, _window, cx| {
654                                        this.restart_stack_frame(stack_frame_id, cx);
655                                    }
656                                }))
657                                .tooltip(move |window, cx| {
658                                    Tooltip::text("Restart Stack Frame")(window, cx)
659                                }),
660                            ),
661                    )
662                },
663            )
664            .into_any()
665    }
666
667    pub(crate) fn expand_collapsed_entry(&mut self, ix: usize, cx: &mut Context<Self>) {
668        let Some(StackFrameEntry::Collapsed(stack_frames)) = self.entries.get_mut(ix) else {
669            return;
670        };
671        let entries = std::mem::take(stack_frames)
672            .into_iter()
673            .map(StackFrameEntry::Normal);
674        // HERE
675        let entries_len = entries.len();
676        self.entries.splice(ix..ix + 1, entries);
677        let (Ok(filtered_indices_start) | Err(filtered_indices_start)) =
678            self.filter_entries_indices.binary_search(&ix);
679
680        for idx in &mut self.filter_entries_indices[filtered_indices_start..] {
681            *idx += entries_len - 1;
682        }
683
684        self.selected_ix = Some(ix);
685        self.list_state.reset(self.entries.len());
686        cx.emit(StackFrameListEvent::BuiltEntries);
687        cx.notify();
688    }
689
690    fn render_collapsed_entry(
691        &self,
692        ix: usize,
693        stack_frames: &Vec<dap::StackFrame>,
694        cx: &mut Context<Self>,
695    ) -> AnyElement {
696        let first_stack_frame = &stack_frames[0];
697        let is_selected = Some(ix) == self.selected_ix;
698
699        h_flex()
700            .rounded_md()
701            .justify_between()
702            .w_full()
703            .group("")
704            .id(("stack-frame", first_stack_frame.id))
705            .p_1()
706            .when(is_selected, |this| {
707                this.bg(cx.theme().colors().element_hover)
708            })
709            .on_any_mouse_down(|_, _, cx| {
710                cx.stop_propagation();
711            })
712            .on_click(cx.listener(move |this, _, window, cx| {
713                this.selected_ix = Some(ix);
714                this.activate_selected_entry(window, cx);
715            }))
716            .hover(|style| style.bg(cx.theme().colors().element_hover).cursor_pointer())
717            .child(
718                v_flex()
719                    .text_ui_sm(cx)
720                    .truncate()
721                    .text_color(cx.theme().colors().text_muted)
722                    .child(format!(
723                        "Show {} more{}",
724                        stack_frames.len(),
725                        first_stack_frame
726                            .source
727                            .as_ref()
728                            .and_then(|source| source.origin.as_ref())
729                            .map_or(String::new(), |origin| format!(": {}", origin))
730                    )),
731            )
732            .into_any()
733    }
734
735    fn render_entry(&self, ix: usize, cx: &mut Context<Self>) -> AnyElement {
736        let ix = match self.list_filter {
737            StackFrameFilter::All => ix,
738            StackFrameFilter::OnlyUserFrames => self.filter_entries_indices[ix],
739        };
740
741        match &self.entries[ix] {
742            StackFrameEntry::Label(stack_frame) => self.render_label_entry(stack_frame, cx),
743            StackFrameEntry::Normal(stack_frame) => self.render_normal_entry(ix, stack_frame, cx),
744            StackFrameEntry::Collapsed(stack_frames) => {
745                self.render_collapsed_entry(ix, stack_frames, cx)
746            }
747        }
748    }
749
750    fn select_ix(&mut self, ix: Option<usize>, cx: &mut Context<Self>) {
751        self.selected_ix = ix;
752        cx.notify();
753    }
754
755    fn select_next(&mut self, _: &menu::SelectNext, _window: &mut Window, cx: &mut Context<Self>) {
756        let ix = match self.selected_ix {
757            _ if self.entries.is_empty() => None,
758            None => Some(0),
759            Some(ix) => {
760                if ix == self.entries.len() - 1 {
761                    Some(0)
762                } else {
763                    Some(ix + 1)
764                }
765            }
766        };
767        self.select_ix(ix, cx);
768    }
769
770    fn select_previous(
771        &mut self,
772        _: &menu::SelectPrevious,
773        _window: &mut Window,
774        cx: &mut Context<Self>,
775    ) {
776        let ix = match self.selected_ix {
777            _ if self.entries.is_empty() => None,
778            None => Some(self.entries.len() - 1),
779            Some(ix) => {
780                if ix == 0 {
781                    Some(self.entries.len() - 1)
782                } else {
783                    Some(ix - 1)
784                }
785            }
786        };
787        self.select_ix(ix, cx);
788    }
789
790    fn select_first(
791        &mut self,
792        _: &menu::SelectFirst,
793        _window: &mut Window,
794        cx: &mut Context<Self>,
795    ) {
796        let ix = if !self.entries.is_empty() {
797            Some(0)
798        } else {
799            None
800        };
801        self.select_ix(ix, cx);
802    }
803
804    fn select_last(&mut self, _: &menu::SelectLast, _window: &mut Window, cx: &mut Context<Self>) {
805        let ix = if !self.entries.is_empty() {
806            Some(self.entries.len() - 1)
807        } else {
808            None
809        };
810        self.select_ix(ix, cx);
811    }
812
813    fn activate_selected_entry(&mut self, window: &mut Window, cx: &mut Context<Self>) {
814        let Some(ix) = self.selected_ix else {
815            return;
816        };
817        let Some(entry) = self.entries.get_mut(ix) else {
818            return;
819        };
820        match entry {
821            StackFrameEntry::Normal(stack_frame) => {
822                let stack_frame = stack_frame.clone();
823                self.go_to_stack_frame_inner(stack_frame, window, cx)
824                    .detach_and_log_err(cx)
825            }
826            StackFrameEntry::Label(_) => {
827                debug_panic!("You should not be able to select a label stack frame")
828            }
829            StackFrameEntry::Collapsed(_) => self.expand_collapsed_entry(ix, cx),
830        }
831        cx.notify();
832    }
833
834    fn confirm(&mut self, _: &menu::Confirm, window: &mut Window, cx: &mut Context<Self>) {
835        self.activate_selected_entry(window, cx);
836    }
837
838    pub(crate) fn toggle_frame_filter(
839        &mut self,
840        thread_status: Option<ThreadStatus>,
841        cx: &mut Context<Self>,
842    ) {
843        self.list_filter = match self.list_filter {
844            StackFrameFilter::All => StackFrameFilter::OnlyUserFrames,
845            StackFrameFilter::OnlyUserFrames => StackFrameFilter::All,
846        };
847
848        if let Some(database_id) = self
849            .workspace
850            .read_with(cx, |workspace, _| workspace.database_id())
851            .ok()
852            .flatten()
853        {
854            let key = stack_frame_filter_key(&self.session.read(cx).adapter(), database_id);
855            let save_task = KEY_VALUE_STORE.write_kvp(key, self.list_filter.into());
856            cx.background_spawn(save_task).detach();
857        }
858
859        if let Some(ThreadStatus::Stopped) = thread_status {
860            match self.list_filter {
861                StackFrameFilter::All => {
862                    self.list_state.reset(self.entries.len());
863                }
864                StackFrameFilter::OnlyUserFrames => {
865                    self.list_state.reset(self.filter_entries_indices.len());
866                    if !self
867                        .selected_ix
868                        .map(|ix| self.filter_entries_indices.contains(&ix))
869                        .unwrap_or_default()
870                    {
871                        self.selected_ix = None;
872                    }
873                }
874            }
875
876            if let Some(ix) = self.selected_ix {
877                let scroll_to = match self.list_filter {
878                    StackFrameFilter::All => ix,
879                    StackFrameFilter::OnlyUserFrames => self
880                        .filter_entries_indices
881                        .binary_search_by_key(&ix, |ix| *ix)
882                        .expect("This index will always exist"),
883                };
884                self.list_state.scroll_to_reveal_item(scroll_to);
885            }
886
887            cx.emit(StackFrameListEvent::BuiltEntries);
888            cx.notify();
889        }
890    }
891
892    fn render_list(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
893        div().p_1().size_full().child(
894            list(
895                self.list_state.clone(),
896                cx.processor(|this, ix, _window, cx| this.render_entry(ix, cx)),
897            )
898            .size_full(),
899        )
900    }
901
902    pub(crate) fn render_control_strip(&self) -> AnyElement {
903        let tooltip_title = match self.list_filter {
904            StackFrameFilter::All => "Show stack frames from your project",
905            StackFrameFilter::OnlyUserFrames => "Show all stack frames",
906        };
907
908        h_flex()
909            .child(
910                IconButton::new(
911                    "filter-by-visible-worktree-stack-frame-list",
912                    IconName::ListFilter,
913                )
914                .tooltip(move |_window, cx| {
915                    Tooltip::for_action(tooltip_title, &ToggleUserFrames, cx)
916                })
917                .toggle_state(self.list_filter == StackFrameFilter::OnlyUserFrames)
918                .icon_size(IconSize::Small)
919                .on_click(|_, window, cx| {
920                    window.dispatch_action(ToggleUserFrames.boxed_clone(), cx)
921                }),
922            )
923            .into_any_element()
924    }
925}
926
927impl Render for StackFrameList {
928    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
929        div()
930            .track_focus(&self.focus_handle)
931            .size_full()
932            .on_action(cx.listener(Self::select_next))
933            .on_action(cx.listener(Self::select_previous))
934            .on_action(cx.listener(Self::select_first))
935            .on_action(cx.listener(Self::select_last))
936            .on_action(cx.listener(Self::confirm))
937            .when_some(self.error.clone(), |el, error| {
938                el.child(
939                    h_flex()
940                        .bg(cx.theme().status().warning_background)
941                        .border_b_1()
942                        .border_color(cx.theme().status().warning_border)
943                        .pl_1()
944                        .child(Icon::new(IconName::Warning).color(Color::Warning))
945                        .gap_2()
946                        .child(
947                            Label::new(error)
948                                .size(LabelSize::Small)
949                                .color(Color::Warning),
950                        ),
951                )
952            })
953            .child(self.render_list(window, cx))
954            .vertical_scrollbar_for(&self.list_state, window, cx)
955    }
956}
957
958impl Focusable for StackFrameList {
959    fn focus_handle(&self, _: &gpui::App) -> gpui::FocusHandle {
960        self.focus_handle.clone()
961    }
962}
963
964impl EventEmitter<StackFrameListEvent> for StackFrameList {}