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