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