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, HighlightKey,
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;
30use theme_settings::ThemeSettings;
31use ui::{ContextMenu, Divider, PopoverMenu, SplitButton, Tooltip, prelude::*};
32use util::ResultExt;
33
34actions!(
35 console,
36 [
37 /// Adds an expression to the watch list.
38 WatchExpression
39 ]
40);
41
42pub struct Console {
43 console: Entity<Editor>,
44 query_bar: Entity<Editor>,
45 session: Entity<Session>,
46 _subscriptions: Vec<Subscription>,
47 variable_list: Entity<VariableList>,
48 stack_frame_list: Entity<StackFrameList>,
49 last_token: OutputToken,
50 update_output_task: Option<Task<()>>,
51 focus_handle: FocusHandle,
52 history: SearchHistory,
53 cursor: SearchHistoryCursor,
54}
55
56impl Console {
57 pub fn new(
58 session: Entity<Session>,
59 stack_frame_list: Entity<StackFrameList>,
60 variable_list: Entity<VariableList>,
61 window: &mut Window,
62 cx: &mut Context<Self>,
63 ) -> Self {
64 let console = cx.new(|cx| {
65 let mut editor = Editor::multi_line(window, cx);
66 editor.set_mode(EditorMode::Full {
67 scale_ui_elements_with_buffer_font_size: true,
68 show_active_line_background: true,
69 sizing_behavior: SizingBehavior::ExcludeOverscrollMargin,
70 });
71 editor.move_to_end(&editor::actions::MoveToEnd, window, cx);
72 editor.set_read_only(true);
73 editor.disable_scrollbars_and_minimap(window, cx);
74 editor.set_show_gutter(false, cx);
75 editor.set_show_runnables(false, cx);
76 editor.set_show_breakpoints(false, cx);
77 editor.set_show_code_actions(false, cx);
78 editor.set_show_line_numbers(false, cx);
79 editor.set_show_git_diff_gutter(false, cx);
80 editor.set_autoindent(false);
81 editor.set_input_enabled(false);
82 editor.set_use_autoclose(false);
83 editor.set_show_wrap_guides(false, cx);
84 editor.set_show_indent_guides(false, cx);
85 editor.set_show_edit_predictions(Some(false), window, cx);
86 editor.set_use_modal_editing(false);
87 editor.set_soft_wrap_mode(language::language_settings::SoftWrap::EditorWidth, cx);
88 editor
89 });
90 let focus_handle = cx.focus_handle();
91
92 let this = cx.weak_entity();
93 let query_bar = cx.new(|cx| {
94 let mut editor = Editor::single_line(window, cx);
95 editor.set_placeholder_text("Evaluate an expression", window, cx);
96 editor.set_use_autoclose(false);
97 editor.set_show_gutter(false, cx);
98 editor.set_show_wrap_guides(false, cx);
99 editor.set_show_indent_guides(false, cx);
100 editor.set_completion_provider(Some(Rc::new(ConsoleQueryBarCompletionProvider(this))));
101
102 editor
103 });
104
105 let _subscriptions = vec![
106 cx.subscribe(&stack_frame_list, Self::handle_stack_frame_list_events),
107 cx.on_focus(&focus_handle, window, |console, window, cx| {
108 if console.is_running(cx) {
109 console.query_bar.focus_handle(cx).focus(window, cx);
110 }
111 }),
112 ];
113
114 Self {
115 session,
116 console,
117 query_bar,
118 variable_list,
119 _subscriptions,
120 stack_frame_list,
121 update_output_task: None,
122 last_token: OutputToken(0),
123 focus_handle,
124 history: SearchHistory::new(
125 None,
126 project::search_history::QueryInsertionBehavior::ReplacePreviousIfContains,
127 ),
128 cursor: Default::default(),
129 }
130 }
131
132 #[cfg(test)]
133 pub(crate) fn editor(&self) -> &Entity<Editor> {
134 &self.console
135 }
136
137 fn is_running(&self, cx: &Context<Self>) -> bool {
138 self.session.read(cx).is_started()
139 }
140
141 fn handle_stack_frame_list_events(
142 &mut self,
143 _: Entity<StackFrameList>,
144 event: &StackFrameListEvent,
145 cx: &mut Context<Self>,
146 ) {
147 match event {
148 StackFrameListEvent::SelectedStackFrameChanged(_) => cx.notify(),
149 StackFrameListEvent::BuiltEntries => {}
150 }
151 }
152
153 pub(crate) fn show_indicator(&self, cx: &App) -> bool {
154 self.session.read(cx).has_new_output(self.last_token)
155 }
156
157 fn add_messages(
158 &mut self,
159 events: Vec<OutputEvent>,
160 window: &mut Window,
161 cx: &mut App,
162 ) -> Task<Result<()>> {
163 self.console.update(cx, |_, cx| {
164 cx.spawn_in(window, async move |console, cx| {
165 let mut len = console
166 .update(cx, |this, cx| this.buffer().read(cx).len(cx))?
167 .0;
168 let (output, spans, background_spans) = cx
169 .background_spawn(async move {
170 let mut all_spans = Vec::new();
171 let mut all_background_spans = Vec::new();
172 let mut to_insert = String::new();
173 let mut scratch = String::new();
174
175 for event in &events {
176 scratch.clear();
177 let mut ansi_handler = ConsoleHandler::default();
178 let mut ansi_processor =
179 ansi::Processor::<ansi::StdSyncHandler>::default();
180
181 let trimmed_output = event.output.trim_end();
182 let _ = writeln!(&mut scratch, "{trimmed_output}");
183 ansi_processor.advance(&mut ansi_handler, scratch.as_bytes());
184 let output = std::mem::take(&mut ansi_handler.output);
185 to_insert.extend(output.chars());
186 let mut spans = std::mem::take(&mut ansi_handler.spans);
187 let mut background_spans =
188 std::mem::take(&mut ansi_handler.background_spans);
189 if ansi_handler.current_range_start < output.len() {
190 spans.push((
191 ansi_handler.current_range_start..output.len(),
192 ansi_handler.current_color,
193 ));
194 }
195 if ansi_handler.current_background_range_start < output.len() {
196 background_spans.push((
197 ansi_handler.current_background_range_start..output.len(),
198 ansi_handler.current_background_color,
199 ));
200 }
201
202 for (range, _) in spans.iter_mut() {
203 let start_offset = len + range.start;
204 *range = start_offset..len + range.end;
205 }
206
207 for (range, _) in background_spans.iter_mut() {
208 let start_offset = len + range.start;
209 *range = start_offset..len + range.end;
210 }
211
212 len += output.len();
213
214 all_spans.extend(spans);
215 all_background_spans.extend(background_spans);
216 }
217 (to_insert, all_spans, all_background_spans)
218 })
219 .await;
220 console.update_in(cx, |console, window, cx| {
221 console.set_read_only(false);
222 console.move_to_end(&editor::actions::MoveToEnd, window, cx);
223 console.insert(&output, window, cx);
224 console.set_read_only(true);
225
226 let buffer = console.buffer().read(cx).snapshot(cx);
227
228 for (range, color) in spans {
229 let Some(color) = color else { continue };
230 let start_offset = range.start;
231 let range = buffer.anchor_after(MultiBufferOffset(range.start))
232 ..buffer.anchor_before(MultiBufferOffset(range.end));
233 let style = HighlightStyle {
234 color: Some(terminal_view::terminal_element::convert_color(
235 &color,
236 cx.theme(),
237 )),
238 ..Default::default()
239 };
240 console.highlight_text_key(
241 HighlightKey::ConsoleAnsiHighlight(start_offset),
242 vec![range],
243 style,
244 false,
245 cx,
246 );
247 }
248
249 for (range, color) in background_spans {
250 let Some(color) = color else { continue };
251 let start_offset = range.start;
252 let range = buffer.anchor_after(MultiBufferOffset(range.start))
253 ..buffer.anchor_before(MultiBufferOffset(range.end));
254 let color_fn = color_fetcher(color);
255 console.highlight_background(
256 HighlightKey::ConsoleAnsiHighlight(start_offset),
257 &[range],
258 move |_, theme| color_fn(theme),
259 cx,
260 );
261 }
262
263 cx.notify();
264 })?;
265
266 Ok(())
267 })
268 })
269 }
270
271 pub fn watch_expression(
272 &mut self,
273 _: &WatchExpression,
274 window: &mut Window,
275 cx: &mut Context<Self>,
276 ) {
277 let expression = self.query_bar.update(cx, |editor, cx| {
278 let expression = editor.text(cx);
279 cx.defer_in(window, |editor, window, cx| {
280 editor.clear(window, cx);
281 });
282
283 expression
284 });
285 self.history.add(&mut self.cursor, expression.clone());
286 self.cursor.reset();
287 self.session.update(cx, |session, cx| {
288 session
289 .evaluate(
290 expression.clone(),
291 Some(dap::EvaluateArgumentsContext::Repl),
292 self.stack_frame_list.read(cx).opened_stack_frame_id(),
293 None,
294 cx,
295 )
296 .detach();
297
298 if let Some(stack_frame_id) = self.stack_frame_list.read(cx).opened_stack_frame_id() {
299 session
300 .add_watcher(expression.into(), stack_frame_id, cx)
301 .detach();
302 }
303 });
304 }
305
306 fn previous_query(&mut self, _: &SelectPrevious, window: &mut Window, cx: &mut Context<Self>) {
307 let current_query = self.query_bar.read(cx).text(cx);
308 let prev = self.history.previous(&mut self.cursor, ¤t_query);
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 buffer: &Entity<Buffer>,
532 buffer_position: language::Anchor,
533 _trigger: editor::CompletionContext,
534 _window: &mut Window,
535 cx: &mut Context<Editor>,
536 ) -> Task<Result<Vec<CompletionResponse>>> {
537 let Some(console) = self.0.upgrade() else {
538 return Task::ready(Ok(Vec::new()));
539 };
540
541 let support_completions = console
542 .read(cx)
543 .session
544 .read(cx)
545 .capabilities()
546 .supports_completions_request
547 .unwrap_or_default();
548
549 if support_completions {
550 self.client_completions(&console, buffer, buffer_position, cx)
551 } else {
552 self.variable_list_completions(&console, buffer, buffer_position, cx)
553 }
554 }
555
556 fn is_completion_trigger(
557 &self,
558 buffer: &Entity<Buffer>,
559 position: language::Anchor,
560 text: &str,
561 trigger_in_words: bool,
562 cx: &mut Context<Editor>,
563 ) -> bool {
564 let mut chars = text.chars();
565 let char = if let Some(char) = chars.next() {
566 char
567 } else {
568 return false;
569 };
570
571 let snapshot = buffer.read(cx).snapshot();
572
573 let classifier = snapshot
574 .char_classifier_at(position)
575 .scope_context(Some(CharScopeContext::Completion));
576 if trigger_in_words && classifier.is_word(char) {
577 return true;
578 }
579
580 self.0
581 .read_with(cx, |console, cx| {
582 console
583 .session
584 .read(cx)
585 .capabilities()
586 .completion_trigger_characters
587 .as_ref()
588 .map(|triggers| triggers.contains(&text.to_string()))
589 })
590 .ok()
591 .flatten()
592 .unwrap_or(true)
593 }
594}
595
596impl ConsoleQueryBarCompletionProvider {
597 fn variable_list_completions(
598 &self,
599 console: &Entity<Console>,
600 buffer: &Entity<Buffer>,
601 buffer_position: language::Anchor,
602 cx: &mut Context<Editor>,
603 ) -> Task<Result<Vec<CompletionResponse>>> {
604 let (variables, string_matches) = console.update(cx, |console, cx| {
605 let mut variables = HashMap::default();
606 let mut string_matches = Vec::default();
607
608 for variable in console.variable_list.update(cx, |variable_list, cx| {
609 variable_list.completion_variables(cx)
610 }) {
611 if let Some(evaluate_name) = &variable.evaluate_name
612 && variables
613 .insert(evaluate_name.clone(), variable.value.clone())
614 .is_none()
615 {
616 string_matches.push(StringMatchCandidate {
617 id: 0,
618 string: evaluate_name.clone(),
619 char_bag: evaluate_name.chars().collect(),
620 });
621 }
622
623 if variables
624 .insert(variable.name.clone(), variable.value.clone())
625 .is_none()
626 {
627 string_matches.push(StringMatchCandidate {
628 id: 0,
629 string: variable.name.clone(),
630 char_bag: variable.name.chars().collect(),
631 });
632 }
633 }
634
635 (variables, string_matches)
636 });
637
638 let snapshot = buffer.read(cx).text_snapshot();
639 let buffer_text = snapshot.text();
640
641 cx.spawn(async move |_, cx| {
642 const LIMIT: usize = 10;
643 let matches = fuzzy::match_strings(
644 &string_matches,
645 &buffer_text,
646 true,
647 true,
648 LIMIT,
649 &Default::default(),
650 cx.background_executor().clone(),
651 )
652 .await;
653
654 let completions = matches
655 .iter()
656 .filter_map(|string_match| {
657 let variable_value = variables.get(&string_match.string)?;
658
659 Some(project::Completion {
660 replace_range: Self::replace_range_for_completion(
661 &buffer_text,
662 buffer_position,
663 string_match.string.as_bytes(),
664 &snapshot,
665 ),
666 new_text: string_match.string.clone(),
667 label: CodeLabel::plain(string_match.string.clone(), None),
668 match_start: None,
669 snippet_deduplication_key: None,
670 icon_path: None,
671 documentation: Some(CompletionDocumentation::MultiLineMarkdown(
672 variable_value.into(),
673 )),
674 confirm: None,
675 source: project::CompletionSource::Custom,
676 insert_text_mode: None,
677 })
678 })
679 .collect::<Vec<_>>();
680
681 Ok(vec![project::CompletionResponse {
682 is_incomplete: completions.len() >= LIMIT,
683 display_options: CompletionDisplayOptions::default(),
684 completions,
685 }])
686 })
687 }
688
689 fn replace_range_for_completion(
690 buffer_text: &String,
691 buffer_position: Anchor,
692 new_bytes: &[u8],
693 snapshot: &TextBufferSnapshot,
694 ) -> Range<Anchor> {
695 let buffer_offset = buffer_position.to_offset(snapshot);
696 let buffer_bytes = &buffer_text.as_bytes()[0..buffer_offset];
697
698 let mut prefix_len = 0;
699 for i in (0..new_bytes.len()).rev() {
700 if buffer_bytes.ends_with(&new_bytes[0..i]) {
701 prefix_len = i;
702 break;
703 }
704 }
705
706 let start = snapshot.clip_offset(buffer_offset - prefix_len, Bias::Left);
707
708 snapshot.anchor_before(start)..buffer_position
709 }
710
711 const fn completion_type_score(completion_type: CompletionItemType) -> usize {
712 match completion_type {
713 CompletionItemType::Field | CompletionItemType::Property => 0,
714 CompletionItemType::Variable | CompletionItemType::Value => 1,
715 CompletionItemType::Method
716 | CompletionItemType::Function
717 | CompletionItemType::Constructor => 2,
718 CompletionItemType::Class
719 | CompletionItemType::Interface
720 | CompletionItemType::Module => 3,
721 _ => 4,
722 }
723 }
724
725 fn completion_item_sort_text(completion_item: &CompletionItem) -> String {
726 completion_item.sort_text.clone().unwrap_or_else(|| {
727 format!(
728 "{:03}_{}",
729 Self::completion_type_score(
730 completion_item.type_.unwrap_or(CompletionItemType::Text)
731 ),
732 completion_item.label.to_ascii_lowercase()
733 )
734 })
735 }
736
737 fn client_completions(
738 &self,
739 console: &Entity<Console>,
740 buffer: &Entity<Buffer>,
741 buffer_position: language::Anchor,
742 cx: &mut Context<Editor>,
743 ) -> Task<Result<Vec<CompletionResponse>>> {
744 let completion_task = console.update(cx, |console, cx| {
745 console.session.update(cx, |state, cx| {
746 let frame_id = console.stack_frame_list.read(cx).opened_stack_frame_id();
747
748 state.completions(
749 CompletionsQuery::new(buffer.read(cx), buffer_position, frame_id),
750 cx,
751 )
752 })
753 });
754 let snapshot = buffer.read(cx).text_snapshot();
755 cx.background_executor().spawn(async move {
756 let completions = completion_task.await?;
757
758 let buffer_text = snapshot.text();
759
760 let completions = completions
761 .into_iter()
762 .map(|completion| {
763 let sort_text = Self::completion_item_sort_text(&completion);
764 let new_text = completion
765 .text
766 .as_ref()
767 .unwrap_or(&completion.label)
768 .to_owned();
769
770 project::Completion {
771 replace_range: Self::replace_range_for_completion(
772 &buffer_text,
773 buffer_position,
774 new_text.as_bytes(),
775 &snapshot,
776 ),
777 new_text,
778 label: CodeLabel::plain(completion.label, None),
779 icon_path: None,
780 documentation: completion.detail.map(|detail| {
781 CompletionDocumentation::MultiLineMarkdown(detail.into())
782 }),
783 match_start: None,
784 snippet_deduplication_key: None,
785 confirm: None,
786 source: project::CompletionSource::Dap { sort_text },
787 insert_text_mode: None,
788 }
789 })
790 .collect();
791
792 Ok(vec![project::CompletionResponse {
793 completions,
794 display_options: CompletionDisplayOptions::default(),
795 is_incomplete: false,
796 }])
797 })
798 }
799}
800
801#[derive(Default)]
802struct ConsoleHandler {
803 output: String,
804 spans: Vec<(Range<usize>, Option<ansi::Color>)>,
805 background_spans: Vec<(Range<usize>, Option<ansi::Color>)>,
806 current_range_start: usize,
807 current_background_range_start: usize,
808 current_color: Option<ansi::Color>,
809 current_background_color: Option<ansi::Color>,
810 pos: usize,
811}
812
813impl ConsoleHandler {
814 fn break_span(&mut self, color: Option<ansi::Color>) {
815 self.spans.push((
816 self.current_range_start..self.output.len(),
817 self.current_color,
818 ));
819 self.current_color = color;
820 self.current_range_start = self.pos;
821 }
822
823 fn break_background_span(&mut self, color: Option<ansi::Color>) {
824 self.background_spans.push((
825 self.current_background_range_start..self.output.len(),
826 self.current_background_color,
827 ));
828 self.current_background_color = color;
829 self.current_background_range_start = self.pos;
830 }
831}
832
833impl ansi::Handler for ConsoleHandler {
834 fn input(&mut self, c: char) {
835 self.output.push(c);
836 self.pos += c.len_utf8();
837 }
838
839 fn linefeed(&mut self) {
840 self.output.push('\n');
841 self.pos += 1;
842 }
843
844 fn put_tab(&mut self, count: u16) {
845 self.output
846 .extend(std::iter::repeat('\t').take(count as usize));
847 self.pos += count as usize;
848 }
849
850 fn terminal_attribute(&mut self, attr: ansi::Attr) {
851 match attr {
852 ansi::Attr::Foreground(color) => {
853 self.break_span(Some(color));
854 }
855 ansi::Attr::Background(color) => {
856 self.break_background_span(Some(color));
857 }
858 ansi::Attr::Reset => {
859 self.break_span(None);
860 self.break_background_span(None);
861 }
862 _ => {}
863 }
864 }
865}
866
867fn color_fetcher(color: ansi::Color) -> fn(&Theme) -> Hsla {
868 let color_fetcher: fn(&Theme) -> Hsla = match color {
869 // Named and theme defined colors
870 ansi::Color::Named(n) => match n {
871 ansi::NamedColor::Black => |theme| theme.colors().terminal_ansi_black,
872 ansi::NamedColor::Red => |theme| theme.colors().terminal_ansi_red,
873 ansi::NamedColor::Green => |theme| theme.colors().terminal_ansi_green,
874 ansi::NamedColor::Yellow => |theme| theme.colors().terminal_ansi_yellow,
875 ansi::NamedColor::Blue => |theme| theme.colors().terminal_ansi_blue,
876 ansi::NamedColor::Magenta => |theme| theme.colors().terminal_ansi_magenta,
877 ansi::NamedColor::Cyan => |theme| theme.colors().terminal_ansi_cyan,
878 ansi::NamedColor::White => |theme| theme.colors().terminal_ansi_white,
879 ansi::NamedColor::BrightBlack => |theme| theme.colors().terminal_ansi_bright_black,
880 ansi::NamedColor::BrightRed => |theme| theme.colors().terminal_ansi_bright_red,
881 ansi::NamedColor::BrightGreen => |theme| theme.colors().terminal_ansi_bright_green,
882 ansi::NamedColor::BrightYellow => |theme| theme.colors().terminal_ansi_bright_yellow,
883 ansi::NamedColor::BrightBlue => |theme| theme.colors().terminal_ansi_bright_blue,
884 ansi::NamedColor::BrightMagenta => |theme| theme.colors().terminal_ansi_bright_magenta,
885 ansi::NamedColor::BrightCyan => |theme| theme.colors().terminal_ansi_bright_cyan,
886 ansi::NamedColor::BrightWhite => |theme| theme.colors().terminal_ansi_bright_white,
887 ansi::NamedColor::Foreground => |theme| theme.colors().terminal_foreground,
888 ansi::NamedColor::Background => |theme| theme.colors().terminal_background,
889 ansi::NamedColor::Cursor => |theme| theme.players().local().cursor,
890 ansi::NamedColor::DimBlack => |theme| theme.colors().terminal_ansi_dim_black,
891 ansi::NamedColor::DimRed => |theme| theme.colors().terminal_ansi_dim_red,
892 ansi::NamedColor::DimGreen => |theme| theme.colors().terminal_ansi_dim_green,
893 ansi::NamedColor::DimYellow => |theme| theme.colors().terminal_ansi_dim_yellow,
894 ansi::NamedColor::DimBlue => |theme| theme.colors().terminal_ansi_dim_blue,
895 ansi::NamedColor::DimMagenta => |theme| theme.colors().terminal_ansi_dim_magenta,
896 ansi::NamedColor::DimCyan => |theme| theme.colors().terminal_ansi_dim_cyan,
897 ansi::NamedColor::DimWhite => |theme| theme.colors().terminal_ansi_dim_white,
898 ansi::NamedColor::BrightForeground => |theme| theme.colors().terminal_bright_foreground,
899 ansi::NamedColor::DimForeground => |theme| theme.colors().terminal_dim_foreground,
900 },
901 // 'True' colors
902 ansi::Color::Spec(_) => |theme| theme.colors().editor_background,
903 // 8 bit, indexed colors
904 ansi::Color::Indexed(i) => {
905 match i {
906 // 0-15 are the same as the named colors above
907 0 => |theme| theme.colors().terminal_ansi_black,
908 1 => |theme| theme.colors().terminal_ansi_red,
909 2 => |theme| theme.colors().terminal_ansi_green,
910 3 => |theme| theme.colors().terminal_ansi_yellow,
911 4 => |theme| theme.colors().terminal_ansi_blue,
912 5 => |theme| theme.colors().terminal_ansi_magenta,
913 6 => |theme| theme.colors().terminal_ansi_cyan,
914 7 => |theme| theme.colors().terminal_ansi_white,
915 8 => |theme| theme.colors().terminal_ansi_bright_black,
916 9 => |theme| theme.colors().terminal_ansi_bright_red,
917 10 => |theme| theme.colors().terminal_ansi_bright_green,
918 11 => |theme| theme.colors().terminal_ansi_bright_yellow,
919 12 => |theme| theme.colors().terminal_ansi_bright_blue,
920 13 => |theme| theme.colors().terminal_ansi_bright_magenta,
921 14 => |theme| theme.colors().terminal_ansi_bright_cyan,
922 15 => |theme| theme.colors().terminal_ansi_bright_white,
923 // 16-231 are a 6x6x6 RGB color cube, mapped to 0-255 using steps defined by XTerm.
924 // See: https://github.com/xterm-x11/xterm-snapshots/blob/master/256colres.pl
925 // 16..=231 => {
926 // let (r, g, b) = rgb_for_index(index as u8);
927 // rgba_color(
928 // if r == 0 { 0 } else { r * 40 + 55 },
929 // if g == 0 { 0 } else { g * 40 + 55 },
930 // if b == 0 { 0 } else { b * 40 + 55 },
931 // )
932 // }
933 // 232-255 are a 24-step grayscale ramp from (8, 8, 8) to (238, 238, 238).
934 // 232..=255 => {
935 // let i = index as u8 - 232; // Align index to 0..24
936 // let value = i * 10 + 8;
937 // rgba_color(value, value, value)
938 // }
939 // For compatibility with the alacritty::Colors interface
940 // See: https://github.com/alacritty/alacritty/blob/master/alacritty_terminal/src/term/color.rs
941 _ => |_| gpui::black(),
942 }
943 }
944 };
945 color_fetcher
946}
947
948#[cfg(test)]
949mod tests {
950 use super::*;
951 use crate::tests::init_test;
952 use editor::{MultiBufferOffset, test::editor_test_context::EditorTestContext};
953 use gpui::TestAppContext;
954 use language::Point;
955
956 #[track_caller]
957 fn assert_completion_range(
958 input: &str,
959 expect: &str,
960 replacement: &str,
961 cx: &mut EditorTestContext,
962 ) {
963 cx.set_state(input);
964
965 let buffer_position = cx.editor(|editor, _, cx| {
966 editor
967 .selections
968 .newest::<Point>(&editor.display_snapshot(cx))
969 .start
970 });
971
972 let snapshot = &cx.buffer_snapshot();
973
974 let replace_range = ConsoleQueryBarCompletionProvider::replace_range_for_completion(
975 &cx.buffer_text(),
976 snapshot.anchor_before(buffer_position),
977 replacement.as_bytes(),
978 snapshot,
979 );
980
981 cx.update_editor(|editor, _, cx| {
982 editor.edit(
983 vec![(
984 MultiBufferOffset(snapshot.offset_for_anchor(&replace_range.start))
985 ..MultiBufferOffset(snapshot.offset_for_anchor(&replace_range.end)),
986 replacement,
987 )],
988 cx,
989 );
990 });
991
992 pretty_assertions::assert_eq!(expect, cx.display_text());
993 }
994
995 #[gpui::test]
996 async fn test_determine_completion_replace_range(cx: &mut TestAppContext) {
997 init_test(cx);
998
999 let mut cx = EditorTestContext::new(cx).await;
1000
1001 assert_completion_range("resˇ", "result", "result", &mut cx);
1002 assert_completion_range("print(resˇ)", "print(result)", "result", &mut cx);
1003 assert_completion_range("$author->nˇ", "$author->name", "$author->name", &mut cx);
1004 assert_completion_range(
1005 "$author->books[ˇ",
1006 "$author->books[0]",
1007 "$author->books[0]",
1008 &mut cx,
1009 );
1010 }
1011}