console.rs

   1use super::{
   2    stack_frame_list::{StackFrameList, StackFrameListEvent},
   3    variable_list::VariableList,
   4};
   5use alacritty_terminal::vte::ansi;
   6use anyhow::Result;
   7use collections::HashMap;
   8use dap::{CompletionItem, CompletionItemType, OutputEvent};
   9use editor::{
  10    Bias, CompletionProvider, Editor, EditorElement, EditorMode, EditorStyle, ExcerptId,
  11    MultiBufferOffset, SizingBehavior,
  12};
  13use fuzzy::StringMatchCandidate;
  14use gpui::{
  15    Action as _, AppContext, Context, Corner, Entity, FocusHandle, Focusable, HighlightStyle, Hsla,
  16    Render, Subscription, Task, TextStyle, WeakEntity, actions,
  17};
  18use language::{Anchor, Buffer, CharScopeContext, CodeLabel, TextBufferSnapshot, ToOffset};
  19use menu::{Confirm, SelectNext, SelectPrevious};
  20use project::{
  21    Completion, CompletionDisplayOptions, CompletionResponse,
  22    debugger::session::{CompletionsQuery, OutputToken, Session},
  23    lsp_store::CompletionDocumentation,
  24    search_history::{SearchHistory, SearchHistoryCursor},
  25};
  26use settings::Settings;
  27use std::fmt::Write;
  28use std::{cell::RefCell, ops::Range, rc::Rc, usize};
  29use theme::{Theme, ThemeSettings};
  30use ui::{ContextMenu, Divider, PopoverMenu, SplitButton, Tooltip, prelude::*};
  31use util::ResultExt;
  32
  33actions!(
  34    console,
  35    [
  36        /// Adds an expression to the watch list.
  37        WatchExpression
  38    ]
  39);
  40
  41pub struct Console {
  42    console: Entity<Editor>,
  43    query_bar: Entity<Editor>,
  44    session: Entity<Session>,
  45    _subscriptions: Vec<Subscription>,
  46    variable_list: Entity<VariableList>,
  47    stack_frame_list: Entity<StackFrameList>,
  48    last_token: OutputToken,
  49    update_output_task: Option<Task<()>>,
  50    focus_handle: FocusHandle,
  51    history: SearchHistory,
  52    cursor: SearchHistoryCursor,
  53}
  54
  55impl Console {
  56    pub fn new(
  57        session: Entity<Session>,
  58        stack_frame_list: Entity<StackFrameList>,
  59        variable_list: Entity<VariableList>,
  60        window: &mut Window,
  61        cx: &mut Context<Self>,
  62    ) -> Self {
  63        let console = cx.new(|cx| {
  64            let mut editor = Editor::multi_line(window, cx);
  65            editor.set_mode(EditorMode::Full {
  66                scale_ui_elements_with_buffer_font_size: true,
  67                show_active_line_background: true,
  68                sizing_behavior: SizingBehavior::ExcludeOverscrollMargin,
  69            });
  70            editor.move_to_end(&editor::actions::MoveToEnd, window, cx);
  71            editor.set_read_only(true);
  72            editor.disable_scrollbars_and_minimap(window, cx);
  73            editor.set_show_gutter(false, cx);
  74            editor.set_show_runnables(false, cx);
  75            editor.set_show_breakpoints(false, cx);
  76            editor.set_show_code_actions(false, cx);
  77            editor.set_show_line_numbers(false, cx);
  78            editor.set_show_git_diff_gutter(false, cx);
  79            editor.set_autoindent(false);
  80            editor.set_input_enabled(false);
  81            editor.set_use_autoclose(false);
  82            editor.set_show_wrap_guides(false, cx);
  83            editor.set_show_indent_guides(false, cx);
  84            editor.set_show_edit_predictions(Some(false), window, cx);
  85            editor.set_use_modal_editing(false);
  86            editor.set_soft_wrap_mode(language::language_settings::SoftWrap::EditorWidth, cx);
  87            editor
  88        });
  89        let focus_handle = cx.focus_handle();
  90
  91        let this = cx.weak_entity();
  92        let query_bar = cx.new(|cx| {
  93            let mut editor = Editor::single_line(window, cx);
  94            editor.set_placeholder_text("Evaluate an expression", window, cx);
  95            editor.set_use_autoclose(false);
  96            editor.set_show_gutter(false, cx);
  97            editor.set_show_wrap_guides(false, cx);
  98            editor.set_show_indent_guides(false, cx);
  99            editor.set_completion_provider(Some(Rc::new(ConsoleQueryBarCompletionProvider(this))));
 100
 101            editor
 102        });
 103
 104        let _subscriptions = vec![
 105            cx.subscribe(&stack_frame_list, Self::handle_stack_frame_list_events),
 106            cx.on_focus(&focus_handle, window, |console, window, cx| {
 107                if console.is_running(cx) {
 108                    console.query_bar.focus_handle(cx).focus(window);
 109                }
 110            }),
 111        ];
 112
 113        Self {
 114            session,
 115            console,
 116            query_bar,
 117            variable_list,
 118            _subscriptions,
 119            stack_frame_list,
 120            update_output_task: None,
 121            last_token: OutputToken(0),
 122            focus_handle,
 123            history: SearchHistory::new(
 124                None,
 125                project::search_history::QueryInsertionBehavior::ReplacePreviousIfContains,
 126            ),
 127            cursor: Default::default(),
 128        }
 129    }
 130
 131    #[cfg(test)]
 132    pub(crate) fn editor(&self) -> &Entity<Editor> {
 133        &self.console
 134    }
 135
 136    fn is_running(&self, cx: &Context<Self>) -> bool {
 137        self.session.read(cx).is_started()
 138    }
 139
 140    fn handle_stack_frame_list_events(
 141        &mut self,
 142        _: Entity<StackFrameList>,
 143        event: &StackFrameListEvent,
 144        cx: &mut Context<Self>,
 145    ) {
 146        match event {
 147            StackFrameListEvent::SelectedStackFrameChanged(_) => cx.notify(),
 148            StackFrameListEvent::BuiltEntries => {}
 149        }
 150    }
 151
 152    pub(crate) fn show_indicator(&self, cx: &App) -> bool {
 153        self.session.read(cx).has_new_output(self.last_token)
 154    }
 155
 156    fn add_messages(
 157        &mut self,
 158        events: Vec<OutputEvent>,
 159        window: &mut Window,
 160        cx: &mut App,
 161    ) -> Task<Result<()>> {
 162        self.console.update(cx, |_, cx| {
 163            cx.spawn_in(window, async move |console, cx| {
 164                let mut len = console
 165                    .update(cx, |this, cx| this.buffer().read(cx).len(cx))?
 166                    .0;
 167                let (output, spans, background_spans) = cx
 168                    .background_spawn(async move {
 169                        let mut all_spans = Vec::new();
 170                        let mut all_background_spans = Vec::new();
 171                        let mut to_insert = String::new();
 172                        let mut scratch = String::new();
 173
 174                        for event in &events {
 175                            scratch.clear();
 176                            let mut ansi_handler = ConsoleHandler::default();
 177                            let mut ansi_processor =
 178                                ansi::Processor::<ansi::StdSyncHandler>::default();
 179
 180                            let trimmed_output = event.output.trim_end();
 181                            let _ = writeln!(&mut scratch, "{trimmed_output}");
 182                            ansi_processor.advance(&mut ansi_handler, scratch.as_bytes());
 183                            let output = std::mem::take(&mut ansi_handler.output);
 184                            to_insert.extend(output.chars());
 185                            let mut spans = std::mem::take(&mut ansi_handler.spans);
 186                            let mut background_spans =
 187                                std::mem::take(&mut ansi_handler.background_spans);
 188                            if ansi_handler.current_range_start < output.len() {
 189                                spans.push((
 190                                    ansi_handler.current_range_start..output.len(),
 191                                    ansi_handler.current_color,
 192                                ));
 193                            }
 194                            if ansi_handler.current_background_range_start < output.len() {
 195                                background_spans.push((
 196                                    ansi_handler.current_background_range_start..output.len(),
 197                                    ansi_handler.current_background_color,
 198                                ));
 199                            }
 200
 201                            for (range, _) in spans.iter_mut() {
 202                                let start_offset = len + range.start;
 203                                *range = start_offset..len + range.end;
 204                            }
 205
 206                            for (range, _) in background_spans.iter_mut() {
 207                                let start_offset = len + range.start;
 208                                *range = start_offset..len + range.end;
 209                            }
 210
 211                            len += output.len();
 212
 213                            all_spans.extend(spans);
 214                            all_background_spans.extend(background_spans);
 215                        }
 216                        (to_insert, all_spans, all_background_spans)
 217                    })
 218                    .await;
 219                console.update_in(cx, |console, window, cx| {
 220                    console.set_read_only(false);
 221                    console.move_to_end(&editor::actions::MoveToEnd, window, cx);
 222                    console.insert(&output, window, cx);
 223                    console.set_read_only(true);
 224
 225                    struct ConsoleAnsiHighlight;
 226
 227                    let buffer = console.buffer().read(cx).snapshot(cx);
 228
 229                    for (range, color) in spans {
 230                        let Some(color) = color else { continue };
 231                        let start_offset = range.start;
 232                        let range = buffer.anchor_after(MultiBufferOffset(range.start))
 233                            ..buffer.anchor_before(MultiBufferOffset(range.end));
 234                        let style = HighlightStyle {
 235                            color: Some(terminal_view::terminal_element::convert_color(
 236                                &color,
 237                                cx.theme(),
 238                            )),
 239                            ..Default::default()
 240                        };
 241                        console.highlight_text_key::<ConsoleAnsiHighlight>(
 242                            start_offset,
 243                            vec![range],
 244                            style,
 245                            false,
 246                            cx,
 247                        );
 248                    }
 249
 250                    for (range, color) in background_spans {
 251                        let Some(color) = color else { continue };
 252                        let start_offset = range.start;
 253                        let range = buffer.anchor_after(MultiBufferOffset(range.start))
 254                            ..buffer.anchor_before(MultiBufferOffset(range.end));
 255                        console.highlight_background_key::<ConsoleAnsiHighlight>(
 256                            start_offset,
 257                            &[range],
 258                            color_fetcher(color),
 259                            cx,
 260                        );
 261                    }
 262
 263                    cx.notify();
 264                })?;
 265
 266                Ok(())
 267            })
 268        })
 269    }
 270
 271    pub fn watch_expression(
 272        &mut self,
 273        _: &WatchExpression,
 274        window: &mut Window,
 275        cx: &mut Context<Self>,
 276    ) {
 277        let expression = self.query_bar.update(cx, |editor, cx| {
 278            let expression = editor.text(cx);
 279            cx.defer_in(window, |editor, window, cx| {
 280                editor.clear(window, cx);
 281            });
 282
 283            expression
 284        });
 285        self.history.add(&mut self.cursor, expression.clone());
 286        self.cursor.reset();
 287        self.session.update(cx, |session, cx| {
 288            session
 289                .evaluate(
 290                    expression.clone(),
 291                    Some(dap::EvaluateArgumentsContext::Repl),
 292                    self.stack_frame_list.read(cx).opened_stack_frame_id(),
 293                    None,
 294                    cx,
 295                )
 296                .detach();
 297
 298            if let Some(stack_frame_id) = self.stack_frame_list.read(cx).opened_stack_frame_id() {
 299                session
 300                    .add_watcher(expression.into(), stack_frame_id, cx)
 301                    .detach();
 302            }
 303        });
 304    }
 305
 306    fn previous_query(&mut self, _: &SelectPrevious, window: &mut Window, cx: &mut Context<Self>) {
 307        let prev = self.history.previous(&mut self.cursor);
 308        if let Some(prev) = prev {
 309            self.query_bar.update(cx, |editor, cx| {
 310                editor.set_text(prev, window, cx);
 311            });
 312        }
 313    }
 314
 315    fn next_query(&mut self, _: &SelectNext, window: &mut Window, cx: &mut Context<Self>) {
 316        let next = self.history.next(&mut self.cursor);
 317        let query = next.unwrap_or_else(|| {
 318            self.cursor.reset();
 319            ""
 320        });
 321
 322        self.query_bar.update(cx, |editor, cx| {
 323            editor.set_text(query, window, cx);
 324        });
 325    }
 326
 327    fn evaluate(&mut self, _: &Confirm, window: &mut Window, cx: &mut Context<Self>) {
 328        let expression = self.query_bar.update(cx, |editor, cx| {
 329            let expression = editor.text(cx);
 330            cx.defer_in(window, |editor, window, cx| {
 331                editor.clear(window, cx);
 332            });
 333
 334            expression
 335        });
 336
 337        self.history.add(&mut self.cursor, expression.clone());
 338        self.cursor.reset();
 339        self.session.update(cx, |session, cx| {
 340            session
 341                .evaluate(
 342                    expression,
 343                    Some(dap::EvaluateArgumentsContext::Repl),
 344                    self.stack_frame_list.read(cx).opened_stack_frame_id(),
 345                    None,
 346                    cx,
 347                )
 348                .detach();
 349        });
 350    }
 351
 352    fn render_submit_menu(
 353        &self,
 354        id: impl Into<ElementId>,
 355        keybinding_target: Option<FocusHandle>,
 356        cx: &App,
 357    ) -> impl IntoElement {
 358        PopoverMenu::new(id.into())
 359            .trigger(
 360                ui::ButtonLike::new_rounded_right("console-confirm-split-button-right")
 361                    .layer(ui::ElevationIndex::ModalSurface)
 362                    .size(ui::ButtonSize::None)
 363                    .child(
 364                        div()
 365                            .px_1()
 366                            .child(Icon::new(IconName::ChevronDown).size(IconSize::XSmall)),
 367                    ),
 368            )
 369            .when(
 370                self.stack_frame_list
 371                    .read(cx)
 372                    .opened_stack_frame_id()
 373                    .is_some(),
 374                |this| {
 375                    this.menu(move |window, cx| {
 376                        Some(ContextMenu::build(window, cx, |context_menu, _, _| {
 377                            context_menu
 378                                .when_some(keybinding_target.clone(), |el, keybinding_target| {
 379                                    el.context(keybinding_target)
 380                                })
 381                                .action("Watch Expression", WatchExpression.boxed_clone())
 382                        }))
 383                    })
 384                },
 385            )
 386            .anchor(Corner::TopRight)
 387    }
 388
 389    fn render_console(&self, cx: &Context<Self>) -> impl IntoElement {
 390        EditorElement::new(&self.console, Self::editor_style(&self.console, cx))
 391    }
 392
 393    fn editor_style(editor: &Entity<Editor>, cx: &Context<Self>) -> EditorStyle {
 394        let is_read_only = editor.read(cx).read_only(cx);
 395        let settings = ThemeSettings::get_global(cx);
 396        let theme = cx.theme();
 397        let text_style = TextStyle {
 398            color: if is_read_only {
 399                theme.colors().text_muted
 400            } else {
 401                theme.colors().text
 402            },
 403            font_family: settings.buffer_font.family.clone(),
 404            font_features: settings.buffer_font.features.clone(),
 405            font_size: settings.buffer_font_size(cx).into(),
 406            font_weight: settings.buffer_font.weight,
 407            line_height: relative(settings.buffer_line_height.value()),
 408            ..Default::default()
 409        };
 410        EditorStyle {
 411            background: theme.colors().editor_background,
 412            local_player: theme.players().local(),
 413            text: text_style,
 414            ..Default::default()
 415        }
 416    }
 417
 418    fn render_query_bar(&self, cx: &Context<Self>) -> impl IntoElement {
 419        EditorElement::new(&self.query_bar, Self::editor_style(&self.query_bar, cx))
 420    }
 421
 422    pub(crate) fn update_output(&mut self, window: &mut Window, cx: &mut Context<Self>) {
 423        if self.update_output_task.is_some() {
 424            return;
 425        }
 426        let session = self.session.clone();
 427        let token = self.last_token;
 428        self.update_output_task = Some(cx.spawn_in(window, async move |this, cx| {
 429            let Some((last_processed_token, task)) = session
 430                .update_in(cx, |session, window, cx| {
 431                    let (output, last_processed_token) = session.output(token);
 432
 433                    this.update(cx, |this, cx| {
 434                        if last_processed_token == this.last_token {
 435                            return None;
 436                        }
 437                        Some((
 438                            last_processed_token,
 439                            this.add_messages(output.cloned().collect(), window, cx),
 440                        ))
 441                    })
 442                    .ok()
 443                    .flatten()
 444                })
 445                .ok()
 446                .flatten()
 447            else {
 448                _ = this.update(cx, |this, _| {
 449                    this.update_output_task.take();
 450                });
 451                return;
 452            };
 453            _ = task.await.log_err();
 454            _ = this.update(cx, |this, _| {
 455                this.last_token = last_processed_token;
 456                this.update_output_task.take();
 457            });
 458        }));
 459    }
 460}
 461
 462impl Render for Console {
 463    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
 464        let query_focus_handle = self.query_bar.focus_handle(cx);
 465        self.update_output(window, cx);
 466
 467        v_flex()
 468            .track_focus(&self.focus_handle)
 469            .key_context("DebugConsole")
 470            .on_action(cx.listener(Self::evaluate))
 471            .on_action(cx.listener(Self::watch_expression))
 472            .size_full()
 473            .border_2()
 474            .bg(cx.theme().colors().editor_background)
 475            .child(self.render_console(cx))
 476            .when(self.is_running(cx), |this| {
 477                this.child(Divider::horizontal()).child(
 478                    h_flex()
 479                        .on_action(cx.listener(Self::previous_query))
 480                        .on_action(cx.listener(Self::next_query))
 481                        .p_1()
 482                        .gap_1()
 483                        .bg(cx.theme().colors().editor_background)
 484                        .child(self.render_query_bar(cx))
 485                        .child(SplitButton::new(
 486                            ui::ButtonLike::new_rounded_all(ElementId::Name(
 487                                "split-button-left-confirm-button".into(),
 488                            ))
 489                            .on_click(move |_, window, cx| {
 490                                window.dispatch_action(Box::new(Confirm), cx)
 491                            })
 492                            .layer(ui::ElevationIndex::ModalSurface)
 493                            .size(ui::ButtonSize::Compact)
 494                            .child(Label::new("Evaluate"))
 495                            .tooltip({
 496                                let query_focus_handle = query_focus_handle.clone();
 497
 498                                move |_window, cx| {
 499                                    Tooltip::for_action_in(
 500                                        "Evaluate",
 501                                        &Confirm,
 502                                        &query_focus_handle,
 503                                        cx,
 504                                    )
 505                                }
 506                            }),
 507                            self.render_submit_menu(
 508                                ElementId::Name("split-button-right-confirm-button".into()),
 509                                Some(query_focus_handle.clone()),
 510                                cx,
 511                            )
 512                            .into_any_element(),
 513                        )),
 514                )
 515            })
 516    }
 517}
 518
 519impl Focusable for Console {
 520    fn focus_handle(&self, _cx: &App) -> gpui::FocusHandle {
 521        self.focus_handle.clone()
 522    }
 523}
 524
 525struct ConsoleQueryBarCompletionProvider(WeakEntity<Console>);
 526
 527impl CompletionProvider for ConsoleQueryBarCompletionProvider {
 528    fn completions(
 529        &self,
 530        _excerpt_id: ExcerptId,
 531        buffer: &Entity<Buffer>,
 532        buffer_position: language::Anchor,
 533        _trigger: editor::CompletionContext,
 534        _window: &mut Window,
 535        cx: &mut Context<Editor>,
 536    ) -> Task<Result<Vec<CompletionResponse>>> {
 537        let Some(console) = self.0.upgrade() else {
 538            return Task::ready(Ok(Vec::new()));
 539        };
 540
 541        let support_completions = console
 542            .read(cx)
 543            .session
 544            .read(cx)
 545            .capabilities()
 546            .supports_completions_request
 547            .unwrap_or_default();
 548
 549        if support_completions {
 550            self.client_completions(&console, buffer, buffer_position, cx)
 551        } else {
 552            self.variable_list_completions(&console, buffer, buffer_position, cx)
 553        }
 554    }
 555
 556    fn apply_additional_edits_for_completion(
 557        &self,
 558        _buffer: Entity<Buffer>,
 559        _completions: Rc<RefCell<Box<[Completion]>>>,
 560        _completion_index: usize,
 561        _push_to_history: bool,
 562        _cx: &mut Context<Editor>,
 563    ) -> gpui::Task<anyhow::Result<Option<language::Transaction>>> {
 564        Task::ready(Ok(None))
 565    }
 566
 567    fn is_completion_trigger(
 568        &self,
 569        buffer: &Entity<Buffer>,
 570        position: language::Anchor,
 571        text: &str,
 572        trigger_in_words: bool,
 573        menu_is_open: bool,
 574        cx: &mut Context<Editor>,
 575    ) -> bool {
 576        let mut chars = text.chars();
 577        let char = if let Some(char) = chars.next() {
 578            char
 579        } else {
 580            return false;
 581        };
 582
 583        let snapshot = buffer.read(cx).snapshot();
 584        if !menu_is_open && !snapshot.settings_at(position, cx).show_completions_on_input {
 585            return false;
 586        }
 587
 588        let classifier = snapshot
 589            .char_classifier_at(position)
 590            .scope_context(Some(CharScopeContext::Completion));
 591        if trigger_in_words && classifier.is_word(char) {
 592            return true;
 593        }
 594
 595        self.0
 596            .read_with(cx, |console, cx| {
 597                console
 598                    .session
 599                    .read(cx)
 600                    .capabilities()
 601                    .completion_trigger_characters
 602                    .as_ref()
 603                    .map(|triggers| triggers.contains(&text.to_string()))
 604            })
 605            .ok()
 606            .flatten()
 607            .unwrap_or(true)
 608    }
 609}
 610
 611impl ConsoleQueryBarCompletionProvider {
 612    fn variable_list_completions(
 613        &self,
 614        console: &Entity<Console>,
 615        buffer: &Entity<Buffer>,
 616        buffer_position: language::Anchor,
 617        cx: &mut Context<Editor>,
 618    ) -> Task<Result<Vec<CompletionResponse>>> {
 619        let (variables, string_matches) = console.update(cx, |console, cx| {
 620            let mut variables = HashMap::default();
 621            let mut string_matches = Vec::default();
 622
 623            for variable in console.variable_list.update(cx, |variable_list, cx| {
 624                variable_list.completion_variables(cx)
 625            }) {
 626                if let Some(evaluate_name) = &variable.evaluate_name
 627                    && variables
 628                        .insert(evaluate_name.clone(), variable.value.clone())
 629                        .is_none()
 630                {
 631                    string_matches.push(StringMatchCandidate {
 632                        id: 0,
 633                        string: evaluate_name.clone(),
 634                        char_bag: evaluate_name.chars().collect(),
 635                    });
 636                }
 637
 638                if variables
 639                    .insert(variable.name.clone(), variable.value.clone())
 640                    .is_none()
 641                {
 642                    string_matches.push(StringMatchCandidate {
 643                        id: 0,
 644                        string: variable.name.clone(),
 645                        char_bag: variable.name.chars().collect(),
 646                    });
 647                }
 648            }
 649
 650            (variables, string_matches)
 651        });
 652
 653        let snapshot = buffer.read(cx).text_snapshot();
 654        let buffer_text = snapshot.text();
 655
 656        cx.spawn(async move |_, cx| {
 657            const LIMIT: usize = 10;
 658            let matches = fuzzy::match_strings(
 659                &string_matches,
 660                &buffer_text,
 661                true,
 662                true,
 663                LIMIT,
 664                &Default::default(),
 665                cx.background_executor().clone(),
 666            )
 667            .await;
 668
 669            let completions = matches
 670                .iter()
 671                .filter_map(|string_match| {
 672                    let variable_value = variables.get(&string_match.string)?;
 673
 674                    Some(project::Completion {
 675                        replace_range: Self::replace_range_for_completion(
 676                            &buffer_text,
 677                            buffer_position,
 678                            string_match.string.as_bytes(),
 679                            &snapshot,
 680                        ),
 681                        new_text: string_match.string.clone(),
 682                        label: CodeLabel::plain(string_match.string.clone(), None),
 683                        match_start: None,
 684                        snippet_deduplication_key: None,
 685                        icon_path: None,
 686                        documentation: Some(CompletionDocumentation::MultiLineMarkdown(
 687                            variable_value.into(),
 688                        )),
 689                        confirm: None,
 690                        source: project::CompletionSource::Custom,
 691                        insert_text_mode: None,
 692                    })
 693                })
 694                .collect::<Vec<_>>();
 695
 696            Ok(vec![project::CompletionResponse {
 697                is_incomplete: completions.len() >= LIMIT,
 698                display_options: CompletionDisplayOptions::default(),
 699                completions,
 700            }])
 701        })
 702    }
 703
 704    fn replace_range_for_completion(
 705        buffer_text: &String,
 706        buffer_position: Anchor,
 707        new_bytes: &[u8],
 708        snapshot: &TextBufferSnapshot,
 709    ) -> Range<Anchor> {
 710        let buffer_offset = buffer_position.to_offset(snapshot);
 711        let buffer_bytes = &buffer_text.as_bytes()[0..buffer_offset];
 712
 713        let mut prefix_len = 0;
 714        for i in (0..new_bytes.len()).rev() {
 715            if buffer_bytes.ends_with(&new_bytes[0..i]) {
 716                prefix_len = i;
 717                break;
 718            }
 719        }
 720
 721        let start = snapshot.clip_offset(buffer_offset - prefix_len, Bias::Left);
 722
 723        snapshot.anchor_before(start)..buffer_position
 724    }
 725
 726    const fn completion_type_score(completion_type: CompletionItemType) -> usize {
 727        match completion_type {
 728            CompletionItemType::Field | CompletionItemType::Property => 0,
 729            CompletionItemType::Variable | CompletionItemType::Value => 1,
 730            CompletionItemType::Method
 731            | CompletionItemType::Function
 732            | CompletionItemType::Constructor => 2,
 733            CompletionItemType::Class
 734            | CompletionItemType::Interface
 735            | CompletionItemType::Module => 3,
 736            _ => 4,
 737        }
 738    }
 739
 740    fn completion_item_sort_text(completion_item: &CompletionItem) -> String {
 741        completion_item.sort_text.clone().unwrap_or_else(|| {
 742            format!(
 743                "{:03}_{}",
 744                Self::completion_type_score(
 745                    completion_item.type_.unwrap_or(CompletionItemType::Text)
 746                ),
 747                completion_item.label.to_ascii_lowercase()
 748            )
 749        })
 750    }
 751
 752    fn client_completions(
 753        &self,
 754        console: &Entity<Console>,
 755        buffer: &Entity<Buffer>,
 756        buffer_position: language::Anchor,
 757        cx: &mut Context<Editor>,
 758    ) -> Task<Result<Vec<CompletionResponse>>> {
 759        let completion_task = console.update(cx, |console, cx| {
 760            console.session.update(cx, |state, cx| {
 761                let frame_id = console.stack_frame_list.read(cx).opened_stack_frame_id();
 762
 763                state.completions(
 764                    CompletionsQuery::new(buffer.read(cx), buffer_position, frame_id),
 765                    cx,
 766                )
 767            })
 768        });
 769        let snapshot = buffer.read(cx).text_snapshot();
 770        cx.background_executor().spawn(async move {
 771            let completions = completion_task.await?;
 772
 773            let buffer_text = snapshot.text();
 774
 775            let completions = completions
 776                .into_iter()
 777                .map(|completion| {
 778                    let sort_text = Self::completion_item_sort_text(&completion);
 779                    let new_text = completion
 780                        .text
 781                        .as_ref()
 782                        .unwrap_or(&completion.label)
 783                        .to_owned();
 784
 785                    project::Completion {
 786                        replace_range: Self::replace_range_for_completion(
 787                            &buffer_text,
 788                            buffer_position,
 789                            new_text.as_bytes(),
 790                            &snapshot,
 791                        ),
 792                        new_text,
 793                        label: CodeLabel::plain(completion.label, None),
 794                        icon_path: None,
 795                        documentation: completion.detail.map(|detail| {
 796                            CompletionDocumentation::MultiLineMarkdown(detail.into())
 797                        }),
 798                        match_start: None,
 799                        snippet_deduplication_key: None,
 800                        confirm: None,
 801                        source: project::CompletionSource::Dap { sort_text },
 802                        insert_text_mode: None,
 803                    }
 804                })
 805                .collect();
 806
 807            Ok(vec![project::CompletionResponse {
 808                completions,
 809                display_options: CompletionDisplayOptions::default(),
 810                is_incomplete: false,
 811            }])
 812        })
 813    }
 814}
 815
 816#[derive(Default)]
 817struct ConsoleHandler {
 818    output: String,
 819    spans: Vec<(Range<usize>, Option<ansi::Color>)>,
 820    background_spans: Vec<(Range<usize>, Option<ansi::Color>)>,
 821    current_range_start: usize,
 822    current_background_range_start: usize,
 823    current_color: Option<ansi::Color>,
 824    current_background_color: Option<ansi::Color>,
 825    pos: usize,
 826}
 827
 828impl ConsoleHandler {
 829    fn break_span(&mut self, color: Option<ansi::Color>) {
 830        self.spans.push((
 831            self.current_range_start..self.output.len(),
 832            self.current_color,
 833        ));
 834        self.current_color = color;
 835        self.current_range_start = self.pos;
 836    }
 837
 838    fn break_background_span(&mut self, color: Option<ansi::Color>) {
 839        self.background_spans.push((
 840            self.current_background_range_start..self.output.len(),
 841            self.current_background_color,
 842        ));
 843        self.current_background_color = color;
 844        self.current_background_range_start = self.pos;
 845    }
 846}
 847
 848impl ansi::Handler for ConsoleHandler {
 849    fn input(&mut self, c: char) {
 850        self.output.push(c);
 851        self.pos += c.len_utf8();
 852    }
 853
 854    fn linefeed(&mut self) {
 855        self.output.push('\n');
 856        self.pos += 1;
 857    }
 858
 859    fn put_tab(&mut self, count: u16) {
 860        self.output
 861            .extend(std::iter::repeat('\t').take(count as usize));
 862        self.pos += count as usize;
 863    }
 864
 865    fn terminal_attribute(&mut self, attr: ansi::Attr) {
 866        match attr {
 867            ansi::Attr::Foreground(color) => {
 868                self.break_span(Some(color));
 869            }
 870            ansi::Attr::Background(color) => {
 871                self.break_background_span(Some(color));
 872            }
 873            ansi::Attr::Reset => {
 874                self.break_span(None);
 875                self.break_background_span(None);
 876            }
 877            _ => {}
 878        }
 879    }
 880}
 881
 882fn color_fetcher(color: ansi::Color) -> fn(&Theme) -> Hsla {
 883    let color_fetcher: fn(&Theme) -> Hsla = match color {
 884        // Named and theme defined colors
 885        ansi::Color::Named(n) => match n {
 886            ansi::NamedColor::Black => |theme| theme.colors().terminal_ansi_black,
 887            ansi::NamedColor::Red => |theme| theme.colors().terminal_ansi_red,
 888            ansi::NamedColor::Green => |theme| theme.colors().terminal_ansi_green,
 889            ansi::NamedColor::Yellow => |theme| theme.colors().terminal_ansi_yellow,
 890            ansi::NamedColor::Blue => |theme| theme.colors().terminal_ansi_blue,
 891            ansi::NamedColor::Magenta => |theme| theme.colors().terminal_ansi_magenta,
 892            ansi::NamedColor::Cyan => |theme| theme.colors().terminal_ansi_cyan,
 893            ansi::NamedColor::White => |theme| theme.colors().terminal_ansi_white,
 894            ansi::NamedColor::BrightBlack => |theme| theme.colors().terminal_ansi_bright_black,
 895            ansi::NamedColor::BrightRed => |theme| theme.colors().terminal_ansi_bright_red,
 896            ansi::NamedColor::BrightGreen => |theme| theme.colors().terminal_ansi_bright_green,
 897            ansi::NamedColor::BrightYellow => |theme| theme.colors().terminal_ansi_bright_yellow,
 898            ansi::NamedColor::BrightBlue => |theme| theme.colors().terminal_ansi_bright_blue,
 899            ansi::NamedColor::BrightMagenta => |theme| theme.colors().terminal_ansi_bright_magenta,
 900            ansi::NamedColor::BrightCyan => |theme| theme.colors().terminal_ansi_bright_cyan,
 901            ansi::NamedColor::BrightWhite => |theme| theme.colors().terminal_ansi_bright_white,
 902            ansi::NamedColor::Foreground => |theme| theme.colors().terminal_foreground,
 903            ansi::NamedColor::Background => |theme| theme.colors().terminal_background,
 904            ansi::NamedColor::Cursor => |theme| theme.players().local().cursor,
 905            ansi::NamedColor::DimBlack => |theme| theme.colors().terminal_ansi_dim_black,
 906            ansi::NamedColor::DimRed => |theme| theme.colors().terminal_ansi_dim_red,
 907            ansi::NamedColor::DimGreen => |theme| theme.colors().terminal_ansi_dim_green,
 908            ansi::NamedColor::DimYellow => |theme| theme.colors().terminal_ansi_dim_yellow,
 909            ansi::NamedColor::DimBlue => |theme| theme.colors().terminal_ansi_dim_blue,
 910            ansi::NamedColor::DimMagenta => |theme| theme.colors().terminal_ansi_dim_magenta,
 911            ansi::NamedColor::DimCyan => |theme| theme.colors().terminal_ansi_dim_cyan,
 912            ansi::NamedColor::DimWhite => |theme| theme.colors().terminal_ansi_dim_white,
 913            ansi::NamedColor::BrightForeground => |theme| theme.colors().terminal_bright_foreground,
 914            ansi::NamedColor::DimForeground => |theme| theme.colors().terminal_dim_foreground,
 915        },
 916        // 'True' colors
 917        ansi::Color::Spec(_) => |theme| theme.colors().editor_background,
 918        // 8 bit, indexed colors
 919        ansi::Color::Indexed(i) => {
 920            match i {
 921                // 0-15 are the same as the named colors above
 922                0 => |theme| theme.colors().terminal_ansi_black,
 923                1 => |theme| theme.colors().terminal_ansi_red,
 924                2 => |theme| theme.colors().terminal_ansi_green,
 925                3 => |theme| theme.colors().terminal_ansi_yellow,
 926                4 => |theme| theme.colors().terminal_ansi_blue,
 927                5 => |theme| theme.colors().terminal_ansi_magenta,
 928                6 => |theme| theme.colors().terminal_ansi_cyan,
 929                7 => |theme| theme.colors().terminal_ansi_white,
 930                8 => |theme| theme.colors().terminal_ansi_bright_black,
 931                9 => |theme| theme.colors().terminal_ansi_bright_red,
 932                10 => |theme| theme.colors().terminal_ansi_bright_green,
 933                11 => |theme| theme.colors().terminal_ansi_bright_yellow,
 934                12 => |theme| theme.colors().terminal_ansi_bright_blue,
 935                13 => |theme| theme.colors().terminal_ansi_bright_magenta,
 936                14 => |theme| theme.colors().terminal_ansi_bright_cyan,
 937                15 => |theme| theme.colors().terminal_ansi_bright_white,
 938                // 16-231 are a 6x6x6 RGB color cube, mapped to 0-255 using steps defined by XTerm.
 939                // See: https://github.com/xterm-x11/xterm-snapshots/blob/master/256colres.pl
 940                // 16..=231 => {
 941                //     let (r, g, b) = rgb_for_index(index as u8);
 942                //     rgba_color(
 943                //         if r == 0 { 0 } else { r * 40 + 55 },
 944                //         if g == 0 { 0 } else { g * 40 + 55 },
 945                //         if b == 0 { 0 } else { b * 40 + 55 },
 946                //     )
 947                // }
 948                // 232-255 are a 24-step grayscale ramp from (8, 8, 8) to (238, 238, 238).
 949                // 232..=255 => {
 950                //     let i = index as u8 - 232; // Align index to 0..24
 951                //     let value = i * 10 + 8;
 952                //     rgba_color(value, value, value)
 953                // }
 954                // For compatibility with the alacritty::Colors interface
 955                // See: https://github.com/alacritty/alacritty/blob/master/alacritty_terminal/src/term/color.rs
 956                _ => |_| gpui::black(),
 957            }
 958        }
 959    };
 960    color_fetcher
 961}
 962
 963#[cfg(test)]
 964mod tests {
 965    use super::*;
 966    use crate::tests::init_test;
 967    use editor::{MultiBufferOffset, test::editor_test_context::EditorTestContext};
 968    use gpui::TestAppContext;
 969    use language::Point;
 970
 971    #[track_caller]
 972    fn assert_completion_range(
 973        input: &str,
 974        expect: &str,
 975        replacement: &str,
 976        cx: &mut EditorTestContext,
 977    ) {
 978        cx.set_state(input);
 979
 980        let buffer_position = cx.editor(|editor, _, cx| {
 981            editor
 982                .selections
 983                .newest::<Point>(&editor.display_snapshot(cx))
 984                .start
 985        });
 986
 987        let snapshot = &cx.buffer_snapshot();
 988
 989        let replace_range = ConsoleQueryBarCompletionProvider::replace_range_for_completion(
 990            &cx.buffer_text(),
 991            snapshot.anchor_before(buffer_position),
 992            replacement.as_bytes(),
 993            snapshot,
 994        );
 995
 996        cx.update_editor(|editor, _, cx| {
 997            editor.edit(
 998                vec![(
 999                    MultiBufferOffset(snapshot.offset_for_anchor(&replace_range.start))
1000                        ..MultiBufferOffset(snapshot.offset_for_anchor(&replace_range.end)),
1001                    replacement,
1002                )],
1003                cx,
1004            );
1005        });
1006
1007        pretty_assertions::assert_eq!(expect, cx.display_text());
1008    }
1009
1010    #[gpui::test]
1011    async fn test_determine_completion_replace_range(cx: &mut TestAppContext) {
1012        init_test(cx);
1013
1014        let mut cx = EditorTestContext::new(cx).await;
1015
1016        assert_completion_range("resˇ", "result", "result", &mut cx);
1017        assert_completion_range("print(resˇ)", "print(result)", "result", &mut cx);
1018        assert_completion_range("$author->nˇ", "$author->name", "$author->name", &mut cx);
1019        assert_completion_range(
1020            "$author->books[ˇ",
1021            "$author->books[0]",
1022            "$author->books[0]",
1023            &mut cx,
1024        );
1025    }
1026}