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