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