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