1012 lines · 38.4 KB · Rust
RawDownload
use super::{    stack_frame_list::{StackFrameList, StackFrameListEvent},    variable_list::VariableList,};use alacritty_terminal::vte::ansi;use anyhow::Result;use collections::HashMap;use dap::{CompletionItem, CompletionItemType, OutputEvent};use editor::{    Bias, CompletionProvider, Editor, EditorElement, EditorMode, EditorStyle, ExcerptId,    MultiBufferOffset, SizingBehavior,};use fuzzy::StringMatchCandidate;use gpui::{    Action as _, AppContext, Context, Corner, Entity, FocusHandle, Focusable, HighlightStyle, Hsla,    Render, Subscription, Task, TextStyle, WeakEntity, actions,};use language::{Anchor, Buffer, CharScopeContext, CodeLabel, TextBufferSnapshot, ToOffset};use menu::{Confirm, SelectNext, SelectPrevious};use project::{    CompletionDisplayOptions, CompletionResponse,    debugger::session::{CompletionsQuery, OutputToken, Session},    lsp_store::CompletionDocumentation,    search_history::{SearchHistory, SearchHistoryCursor},};use settings::Settings;use std::fmt::Write;use std::{ops::Range, rc::Rc, usize};use theme::{Theme, ThemeSettings};use ui::{ContextMenu, Divider, PopoverMenu, SplitButton, Tooltip, prelude::*};use util::ResultExt; actions!(    console,    [        /// Adds an expression to the watch list.        WatchExpression    ]); pub struct Console {    console: Entity<Editor>,    query_bar: Entity<Editor>,    session: Entity<Session>,    _subscriptions: Vec<Subscription>,    variable_list: Entity<VariableList>,    stack_frame_list: Entity<StackFrameList>,    last_token: OutputToken,    update_output_task: Option<Task<()>>,    focus_handle: FocusHandle,    history: SearchHistory,    cursor: SearchHistoryCursor,} impl Console {    pub fn new(        session: Entity<Session>,        stack_frame_list: Entity<StackFrameList>,        variable_list: Entity<VariableList>,        window: &mut Window,        cx: &mut Context<Self>,    ) -> Self {        let console = cx.new(|cx| {            let mut editor = Editor::multi_line(window, cx);            editor.set_mode(EditorMode::Full {                scale_ui_elements_with_buffer_font_size: true,                show_active_line_background: true,                sizing_behavior: SizingBehavior::ExcludeOverscrollMargin,            });            editor.move_to_end(&editor::actions::MoveToEnd, window, cx);            editor.set_read_only(true);            editor.disable_scrollbars_and_minimap(window, cx);            editor.set_show_gutter(false, cx);            editor.set_show_runnables(false, cx);            editor.set_show_breakpoints(false, cx);            editor.set_show_code_actions(false, cx);            editor.set_show_line_numbers(false, cx);            editor.set_show_git_diff_gutter(false, cx);            editor.set_autoindent(false);            editor.set_input_enabled(false);            editor.set_use_autoclose(false);            editor.set_show_wrap_guides(false, cx);            editor.set_show_indent_guides(false, cx);            editor.set_show_edit_predictions(Some(false), window, cx);            editor.set_use_modal_editing(false);            editor.set_soft_wrap_mode(language::language_settings::SoftWrap::EditorWidth, cx);            editor        });        let focus_handle = cx.focus_handle();         let this = cx.weak_entity();        let query_bar = cx.new(|cx| {            let mut editor = Editor::single_line(window, cx);            editor.set_placeholder_text("Evaluate an expression", window, cx);            editor.set_use_autoclose(false);            editor.set_show_gutter(false, cx);            editor.set_show_wrap_guides(false, cx);            editor.set_show_indent_guides(false, cx);            editor.set_completion_provider(Some(Rc::new(ConsoleQueryBarCompletionProvider(this))));             editor        });         let _subscriptions = vec![            cx.subscribe(&stack_frame_list, Self::handle_stack_frame_list_events),            cx.on_focus(&focus_handle, window, |console, window, cx| {                if console.is_running(cx) {                    console.query_bar.focus_handle(cx).focus(window);                }            }),        ];         Self {            session,            console,            query_bar,            variable_list,            _subscriptions,            stack_frame_list,            update_output_task: None,            last_token: OutputToken(0),            focus_handle,            history: SearchHistory::new(                None,                project::search_history::QueryInsertionBehavior::ReplacePreviousIfContains,            ),            cursor: Default::default(),        }    }     #[cfg(test)]    pub(crate) fn editor(&self) -> &Entity<Editor> {        &self.console    }     fn is_running(&self, cx: &Context<Self>) -> bool {        self.session.read(cx).is_started()    }     fn handle_stack_frame_list_events(        &mut self,        _: Entity<StackFrameList>,        event: &StackFrameListEvent,        cx: &mut Context<Self>,    ) {        match event {            StackFrameListEvent::SelectedStackFrameChanged(_) => cx.notify(),            StackFrameListEvent::BuiltEntries => {}        }    }     pub(crate) fn show_indicator(&self, cx: &App) -> bool {        self.session.read(cx).has_new_output(self.last_token)    }     fn add_messages(        &mut self,        events: Vec<OutputEvent>,        window: &mut Window,        cx: &mut App,    ) -> Task<Result<()>> {        self.console.update(cx, |_, cx| {            cx.spawn_in(window, async move |console, cx| {                let mut len = console                    .update(cx, |this, cx| this.buffer().read(cx).len(cx))?                    .0;                let (output, spans, background_spans) = cx                    .background_spawn(async move {                        let mut all_spans = Vec::new();                        let mut all_background_spans = Vec::new();                        let mut to_insert = String::new();                        let mut scratch = String::new();                         for event in &events {                            scratch.clear();                            let mut ansi_handler = ConsoleHandler::default();                            let mut ansi_processor =                                ansi::Processor::<ansi::StdSyncHandler>::default();                             let trimmed_output = event.output.trim_end();                            let _ = writeln!(&mut scratch, "{trimmed_output}");                            ansi_processor.advance(&mut ansi_handler, scratch.as_bytes());                            let output = std::mem::take(&mut ansi_handler.output);                            to_insert.extend(output.chars());                            let mut spans = std::mem::take(&mut ansi_handler.spans);                            let mut background_spans =                                std::mem::take(&mut ansi_handler.background_spans);                            if ansi_handler.current_range_start < output.len() {                                spans.push((                                    ansi_handler.current_range_start..output.len(),                                    ansi_handler.current_color,                                ));                            }                            if ansi_handler.current_background_range_start < output.len() {                                background_spans.push((                                    ansi_handler.current_background_range_start..output.len(),                                    ansi_handler.current_background_color,                                ));                            }                             for (range, _) in spans.iter_mut() {                                let start_offset = len + range.start;                                *range = start_offset..len + range.end;                            }                             for (range, _) in background_spans.iter_mut() {                                let start_offset = len + range.start;                                *range = start_offset..len + range.end;                            }                             len += output.len();                             all_spans.extend(spans);                            all_background_spans.extend(background_spans);                        }                        (to_insert, all_spans, all_background_spans)                    })                    .await;                console.update_in(cx, |console, window, cx| {                    console.set_read_only(false);                    console.move_to_end(&editor::actions::MoveToEnd, window, cx);                    console.insert(&output, window, cx);                    console.set_read_only(true);                     struct ConsoleAnsiHighlight;                     let buffer = console.buffer().read(cx).snapshot(cx);                     for (range, color) in spans {                        let Some(color) = color else { continue };                        let start_offset = range.start;                        let range = buffer.anchor_after(MultiBufferOffset(range.start))                            ..buffer.anchor_before(MultiBufferOffset(range.end));                        let style = HighlightStyle {                            color: Some(terminal_view::terminal_element::convert_color(                                &color,                                cx.theme(),                            )),                            ..Default::default()                        };                        console.highlight_text_key::<ConsoleAnsiHighlight>(                            start_offset,                            vec![range],                            style,                            false,                            cx,                        );                    }                     for (range, color) in background_spans {                        let Some(color) = color else { continue };                        let start_offset = range.start;                        let range = buffer.anchor_after(MultiBufferOffset(range.start))                            ..buffer.anchor_before(MultiBufferOffset(range.end));                        let color_fn = color_fetcher(color);                        console.highlight_background_key::<ConsoleAnsiHighlight>(                            start_offset,                            &[range],                            move |_, theme| color_fn(theme),                            cx,                        );                    }                     cx.notify();                })?;                 Ok(())            })        })    }     pub fn watch_expression(        &mut self,        _: &WatchExpression,        window: &mut Window,        cx: &mut Context<Self>,    ) {        let expression = self.query_bar.update(cx, |editor, cx| {            let expression = editor.text(cx);            cx.defer_in(window, |editor, window, cx| {                editor.clear(window, cx);            });             expression        });        self.history.add(&mut self.cursor, expression.clone());        self.cursor.reset();        self.session.update(cx, |session, cx| {            session                .evaluate(                    expression.clone(),                    Some(dap::EvaluateArgumentsContext::Repl),                    self.stack_frame_list.read(cx).opened_stack_frame_id(),                    None,                    cx,                )                .detach();             if let Some(stack_frame_id) = self.stack_frame_list.read(cx).opened_stack_frame_id() {                session                    .add_watcher(expression.into(), stack_frame_id, cx)                    .detach();            }        });    }     fn previous_query(&mut self, _: &SelectPrevious, window: &mut Window, cx: &mut Context<Self>) {        let prev = self.history.previous(&mut self.cursor);        if let Some(prev) = prev {            self.query_bar.update(cx, |editor, cx| {                editor.set_text(prev, window, cx);            });        }    }     fn next_query(&mut self, _: &SelectNext, window: &mut Window, cx: &mut Context<Self>) {        let next = self.history.next(&mut self.cursor);        let query = next.unwrap_or_else(|| {            self.cursor.reset();            ""        });         self.query_bar.update(cx, |editor, cx| {            editor.set_text(query, window, cx);        });    }     fn evaluate(&mut self, _: &Confirm, window: &mut Window, cx: &mut Context<Self>) {        let expression = self.query_bar.update(cx, |editor, cx| {            let expression = editor.text(cx);            cx.defer_in(window, |editor, window, cx| {                editor.clear(window, cx);            });             expression        });         self.history.add(&mut self.cursor, expression.clone());        self.cursor.reset();        self.session.update(cx, |session, cx| {            session                .evaluate(                    expression,                    Some(dap::EvaluateArgumentsContext::Repl),                    self.stack_frame_list.read(cx).opened_stack_frame_id(),                    None,                    cx,                )                .detach();        });    }     fn render_submit_menu(        &self,        id: impl Into<ElementId>,        keybinding_target: Option<FocusHandle>,        cx: &App,    ) -> impl IntoElement {        PopoverMenu::new(id.into())            .trigger(                ui::ButtonLike::new_rounded_right("console-confirm-split-button-right")                    .layer(ui::ElevationIndex::ModalSurface)                    .size(ui::ButtonSize::None)                    .child(                        div()                            .px_1()                            .child(Icon::new(IconName::ChevronDown).size(IconSize::XSmall)),                    ),            )            .when(                self.stack_frame_list                    .read(cx)                    .opened_stack_frame_id()                    .is_some(),                |this| {                    this.menu(move |window, cx| {                        Some(ContextMenu::build(window, cx, |context_menu, _, _| {                            context_menu                                .when_some(keybinding_target.clone(), |el, keybinding_target| {                                    el.context(keybinding_target)                                })                                .action("Watch Expression", WatchExpression.boxed_clone())                        }))                    })                },            )            .anchor(Corner::TopRight)    }     fn render_console(&self, cx: &Context<Self>) -> impl IntoElement {        EditorElement::new(&self.console, Self::editor_style(&self.console, cx))    }     fn editor_style(editor: &Entity<Editor>, cx: &Context<Self>) -> EditorStyle {        let is_read_only = editor.read(cx).read_only(cx);        let settings = ThemeSettings::get_global(cx);        let theme = cx.theme();        let text_style = TextStyle {            color: if is_read_only {                theme.colors().text_muted            } else {                theme.colors().text            },            font_family: settings.buffer_font.family.clone(),            font_features: settings.buffer_font.features.clone(),            font_size: settings.buffer_font_size(cx).into(),            font_weight: settings.buffer_font.weight,            line_height: relative(settings.buffer_line_height.value()),            ..Default::default()        };        EditorStyle {            background: theme.colors().editor_background,            local_player: theme.players().local(),            text: text_style,            ..Default::default()        }    }     fn render_query_bar(&self, cx: &Context<Self>) -> impl IntoElement {        EditorElement::new(&self.query_bar, Self::editor_style(&self.query_bar, cx))    }     pub(crate) fn update_output(&mut self, window: &mut Window, cx: &mut Context<Self>) {        if self.update_output_task.is_some() {            return;        }        let session = self.session.clone();        let token = self.last_token;        self.update_output_task = Some(cx.spawn_in(window, async move |this, cx| {            let Some((last_processed_token, task)) = session                .update_in(cx, |session, window, cx| {                    let (output, last_processed_token) = session.output(token);                     this.update(cx, |this, cx| {                        if last_processed_token == this.last_token {                            return None;                        }                        Some((                            last_processed_token,                            this.add_messages(output.cloned().collect(), window, cx),                        ))                    })                    .ok()                    .flatten()                })                .ok()                .flatten()            else {                _ = this.update(cx, |this, _| {                    this.update_output_task.take();                });                return;            };            _ = task.await.log_err();            _ = this.update(cx, |this, _| {                this.last_token = last_processed_token;                this.update_output_task.take();            });        }));    }} impl Render for Console {    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {        let query_focus_handle = self.query_bar.focus_handle(cx);        self.update_output(window, cx);         v_flex()            .track_focus(&self.focus_handle)            .key_context("DebugConsole")            .on_action(cx.listener(Self::evaluate))            .on_action(cx.listener(Self::watch_expression))            .size_full()            .border_2()            .bg(cx.theme().colors().editor_background)            .child(self.render_console(cx))            .when(self.is_running(cx), |this| {                this.child(Divider::horizontal()).child(                    h_flex()                        .on_action(cx.listener(Self::previous_query))                        .on_action(cx.listener(Self::next_query))                        .p_1()                        .gap_1()                        .bg(cx.theme().colors().editor_background)                        .child(self.render_query_bar(cx))                        .child(SplitButton::new(                            ui::ButtonLike::new_rounded_all(ElementId::Name(                                "split-button-left-confirm-button".into(),                            ))                            .on_click(move |_, window, cx| {                                window.dispatch_action(Box::new(Confirm), cx)                            })                            .layer(ui::ElevationIndex::ModalSurface)                            .size(ui::ButtonSize::Compact)                            .child(Label::new("Evaluate"))                            .tooltip({                                let query_focus_handle = query_focus_handle.clone();                                 move |_window, cx| {                                    Tooltip::for_action_in(

Lines 1–500 of 1012.