1use crate::{
2 acp::completion_provider::{ContextPickerCompletionProvider, SlashCommandCompletion},
3 context_picker::{ContextPickerAction, fetch_context_picker::fetch_url_content},
4};
5use acp_thread::{MentionUri, selection_name};
6use agent_client_protocol as acp;
7use agent_servers::{AgentServer, AgentServerDelegate};
8use agent2::HistoryStore;
9use anyhow::{Result, anyhow};
10use assistant_slash_commands::codeblock_fence_for_path;
11use collections::{HashMap, HashSet};
12use editor::{
13 Addon, Anchor, AnchorRangeExt, ContextMenuOptions, ContextMenuPlacement, Editor, EditorElement,
14 EditorEvent, EditorMode, EditorSnapshot, EditorStyle, ExcerptId, FoldPlaceholder, InlayId,
15 MultiBuffer, ToOffset,
16 actions::Paste,
17 display_map::{Crease, CreaseId, FoldId, Inlay},
18};
19use futures::{
20 FutureExt as _,
21 future::{Shared, join_all},
22};
23use gpui::{
24 Animation, AnimationExt as _, AppContext, ClipboardEntry, Context, Entity, EntityId,
25 EventEmitter, FocusHandle, Focusable, Image, ImageFormat, Img, KeyContext, Subscription, Task,
26 TextStyle, WeakEntity, pulsating_between,
27};
28use language::{Buffer, Language, language_settings::InlayHintKind};
29use language_model::LanguageModelImage;
30use postage::stream::Stream as _;
31use project::{
32 CompletionIntent, InlayHint, InlayHintLabel, Project, ProjectItem, ProjectPath, Worktree,
33};
34use prompt_store::{PromptId, PromptStore};
35use rope::Point;
36use settings::Settings;
37use std::{
38 cell::{Cell, RefCell},
39 ffi::OsStr,
40 fmt::Write,
41 ops::{Range, RangeInclusive},
42 path::{Path, PathBuf},
43 rc::Rc,
44 sync::Arc,
45 time::Duration,
46};
47use text::OffsetRangeExt;
48use theme::ThemeSettings;
49use ui::{
50 ActiveTheme, AnyElement, App, ButtonCommon, ButtonLike, ButtonStyle, Color, Element as _,
51 FluentBuilder as _, Icon, IconName, IconSize, InteractiveElement, IntoElement, Label,
52 LabelCommon, LabelSize, ParentElement, Render, SelectableButton, SharedString, Styled,
53 TextSize, TintColor, Toggleable, Window, div, h_flex,
54};
55use util::{ResultExt, debug_panic};
56use workspace::{Workspace, notifications::NotifyResultExt as _};
57use zed_actions::agent::Chat;
58
59pub struct MessageEditor {
60 mention_set: MentionSet,
61 editor: Entity<Editor>,
62 project: Entity<Project>,
63 workspace: WeakEntity<Workspace>,
64 history_store: Entity<HistoryStore>,
65 prompt_store: Option<Entity<PromptStore>>,
66 prompt_capabilities: Rc<Cell<acp::PromptCapabilities>>,
67 available_commands: Rc<RefCell<Vec<acp::AvailableCommand>>>,
68 _subscriptions: Vec<Subscription>,
69 _parse_slash_command_task: Task<()>,
70}
71
72#[derive(Clone, Copy, Debug)]
73pub enum MessageEditorEvent {
74 Send,
75 Cancel,
76 Focus,
77 LostFocus,
78}
79
80impl EventEmitter<MessageEditorEvent> for MessageEditor {}
81
82const COMMAND_HINT_INLAY_ID: usize = 0;
83
84impl MessageEditor {
85 pub fn new(
86 workspace: WeakEntity<Workspace>,
87 project: Entity<Project>,
88 history_store: Entity<HistoryStore>,
89 prompt_store: Option<Entity<PromptStore>>,
90 prompt_capabilities: Rc<Cell<acp::PromptCapabilities>>,
91 available_commands: Rc<RefCell<Vec<acp::AvailableCommand>>>,
92 placeholder: impl Into<Arc<str>>,
93 mode: EditorMode,
94 window: &mut Window,
95 cx: &mut Context<Self>,
96 ) -> Self {
97 let language = Language::new(
98 language::LanguageConfig {
99 completion_query_characters: HashSet::from_iter(['.', '-', '_', '@']),
100 ..Default::default()
101 },
102 None,
103 );
104 let completion_provider = Rc::new(ContextPickerCompletionProvider::new(
105 cx.weak_entity(),
106 workspace.clone(),
107 history_store.clone(),
108 prompt_store.clone(),
109 prompt_capabilities.clone(),
110 available_commands.clone(),
111 ));
112 let mention_set = MentionSet::default();
113 let editor = cx.new(|cx| {
114 let buffer = cx.new(|cx| Buffer::local("", cx).with_language(Arc::new(language), cx));
115 let buffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx));
116
117 let mut editor = Editor::new(mode, buffer, None, window, cx);
118 editor.set_placeholder_text(placeholder, cx);
119 editor.set_show_indent_guides(false, cx);
120 editor.set_soft_wrap();
121 editor.set_use_modal_editing(true);
122 editor.set_completion_provider(Some(completion_provider.clone()));
123 editor.set_context_menu_options(ContextMenuOptions {
124 min_entries_visible: 12,
125 max_entries_visible: 12,
126 placement: Some(ContextMenuPlacement::Above),
127 });
128 editor.register_addon(MessageEditorAddon::new());
129 editor
130 });
131
132 cx.on_focus_in(&editor.focus_handle(cx), window, |_, _, cx| {
133 cx.emit(MessageEditorEvent::Focus)
134 })
135 .detach();
136 cx.on_focus_out(&editor.focus_handle(cx), window, |_, _, _, cx| {
137 cx.emit(MessageEditorEvent::LostFocus)
138 })
139 .detach();
140
141 let mut has_hint = false;
142 let mut subscriptions = Vec::new();
143
144 subscriptions.push(cx.subscribe_in(&editor, window, {
145 move |this, editor, event, window, cx| {
146 if let EditorEvent::Edited { .. } = event {
147 let snapshot = editor.update(cx, |editor, cx| {
148 let new_hints = this
149 .command_hint(editor.buffer(), cx)
150 .into_iter()
151 .collect::<Vec<_>>();
152 let has_new_hint = !new_hints.is_empty();
153 editor.splice_inlays(
154 if has_hint {
155 &[InlayId::Hint(COMMAND_HINT_INLAY_ID)]
156 } else {
157 &[]
158 },
159 new_hints,
160 cx,
161 );
162 has_hint = has_new_hint;
163
164 editor.snapshot(window, cx)
165 });
166 this.mention_set.remove_invalid(snapshot);
167
168 cx.notify();
169 }
170 }
171 }));
172
173 Self {
174 editor,
175 project,
176 mention_set,
177 workspace,
178 history_store,
179 prompt_store,
180 prompt_capabilities,
181 available_commands,
182 _subscriptions: subscriptions,
183 _parse_slash_command_task: Task::ready(()),
184 }
185 }
186
187 fn command_hint(&self, buffer: &Entity<MultiBuffer>, cx: &App) -> Option<Inlay> {
188 let available_commands = self.available_commands.borrow();
189 if available_commands.is_empty() {
190 return None;
191 }
192
193 let snapshot = buffer.read(cx).snapshot(cx);
194 let parsed_command = SlashCommandCompletion::try_parse(&snapshot.text(), 0)?;
195 if parsed_command.argument.is_some() {
196 return None;
197 }
198
199 let command_name = parsed_command.command?;
200 let available_command = available_commands
201 .iter()
202 .find(|command| command.name == command_name)?;
203
204 let acp::AvailableCommandInput::Unstructured { mut hint } =
205 available_command.input.clone()?;
206
207 let mut hint_pos = parsed_command.source_range.end + 1;
208 if hint_pos > snapshot.len() {
209 hint_pos = snapshot.len();
210 hint.insert(0, ' ');
211 }
212
213 let hint_pos = snapshot.anchor_after(hint_pos);
214
215 Some(Inlay::hint(
216 COMMAND_HINT_INLAY_ID,
217 hint_pos,
218 &InlayHint {
219 position: hint_pos.text_anchor,
220 label: InlayHintLabel::String(hint),
221 kind: Some(InlayHintKind::Parameter),
222 padding_left: false,
223 padding_right: false,
224 tooltip: None,
225 resolve_state: project::ResolveState::Resolved,
226 },
227 ))
228 }
229
230 pub fn insert_thread_summary(
231 &mut self,
232 thread: agent2::DbThreadMetadata,
233 window: &mut Window,
234 cx: &mut Context<Self>,
235 ) {
236 let start = self.editor.update(cx, |editor, cx| {
237 editor.set_text(format!("{}\n", thread.title), window, cx);
238 editor
239 .buffer()
240 .read(cx)
241 .snapshot(cx)
242 .anchor_before(Point::zero())
243 .text_anchor
244 });
245
246 self.confirm_mention_completion(
247 thread.title.clone(),
248 start,
249 thread.title.len(),
250 MentionUri::Thread {
251 id: thread.id.clone(),
252 name: thread.title.to_string(),
253 },
254 window,
255 cx,
256 )
257 .detach();
258 }
259
260 #[cfg(test)]
261 pub(crate) fn editor(&self) -> &Entity<Editor> {
262 &self.editor
263 }
264
265 #[cfg(test)]
266 pub(crate) fn mention_set(&mut self) -> &mut MentionSet {
267 &mut self.mention_set
268 }
269
270 pub fn is_empty(&self, cx: &App) -> bool {
271 self.editor.read(cx).is_empty(cx)
272 }
273
274 pub fn mentions(&self) -> HashSet<MentionUri> {
275 self.mention_set
276 .mentions
277 .values()
278 .map(|(uri, _)| uri.clone())
279 .collect()
280 }
281
282 pub fn confirm_mention_completion(
283 &mut self,
284 crease_text: SharedString,
285 start: text::Anchor,
286 content_len: usize,
287 mention_uri: MentionUri,
288 window: &mut Window,
289 cx: &mut Context<Self>,
290 ) -> Task<()> {
291 let snapshot = self
292 .editor
293 .update(cx, |editor, cx| editor.snapshot(window, cx));
294 let Some((excerpt_id, _, _)) = snapshot.buffer_snapshot.as_singleton() else {
295 return Task::ready(());
296 };
297 let Some(start_anchor) = snapshot
298 .buffer_snapshot
299 .anchor_in_excerpt(*excerpt_id, start)
300 else {
301 return Task::ready(());
302 };
303 let end_anchor = snapshot
304 .buffer_snapshot
305 .anchor_before(start_anchor.to_offset(&snapshot.buffer_snapshot) + content_len + 1);
306
307 let crease = if let MentionUri::File { abs_path } = &mention_uri
308 && let Some(extension) = abs_path.extension()
309 && let Some(extension) = extension.to_str()
310 && Img::extensions().contains(&extension)
311 && !extension.contains("svg")
312 {
313 let Some(project_path) = self
314 .project
315 .read(cx)
316 .project_path_for_absolute_path(&abs_path, cx)
317 else {
318 log::error!("project path not found");
319 return Task::ready(());
320 };
321 let image = self
322 .project
323 .update(cx, |project, cx| project.open_image(project_path, cx));
324 let image = cx
325 .spawn(async move |_, cx| {
326 let image = image.await.map_err(|e| e.to_string())?;
327 let image = image
328 .update(cx, |image, _| image.image.clone())
329 .map_err(|e| e.to_string())?;
330 Ok(image)
331 })
332 .shared();
333 insert_crease_for_mention(
334 *excerpt_id,
335 start,
336 content_len,
337 mention_uri.name().into(),
338 IconName::Image.path().into(),
339 Some(image),
340 self.editor.clone(),
341 window,
342 cx,
343 )
344 } else {
345 insert_crease_for_mention(
346 *excerpt_id,
347 start,
348 content_len,
349 crease_text,
350 mention_uri.icon_path(cx),
351 None,
352 self.editor.clone(),
353 window,
354 cx,
355 )
356 };
357 let Some((crease_id, tx)) = crease else {
358 return Task::ready(());
359 };
360
361 let task = match mention_uri.clone() {
362 MentionUri::Fetch { url } => self.confirm_mention_for_fetch(url, cx),
363 MentionUri::Directory { abs_path } => self.confirm_mention_for_directory(abs_path, cx),
364 MentionUri::Thread { id, .. } => self.confirm_mention_for_thread(id, cx),
365 MentionUri::TextThread { path, .. } => self.confirm_mention_for_text_thread(path, cx),
366 MentionUri::File { abs_path } => self.confirm_mention_for_file(abs_path, cx),
367 MentionUri::Symbol {
368 abs_path,
369 line_range,
370 ..
371 } => self.confirm_mention_for_symbol(abs_path, line_range, cx),
372 MentionUri::Rule { id, .. } => self.confirm_mention_for_rule(id, cx),
373 MentionUri::PastedImage => {
374 debug_panic!("pasted image URI should not be included in completions");
375 Task::ready(Err(anyhow!(
376 "pasted imaged URI should not be included in completions"
377 )))
378 }
379 MentionUri::Selection { .. } => {
380 // Handled elsewhere
381 debug_panic!("unexpected selection URI");
382 Task::ready(Err(anyhow!("unexpected selection URI")))
383 }
384 };
385 let task = cx
386 .spawn(async move |_, _| task.await.map_err(|e| e.to_string()))
387 .shared();
388 self.mention_set
389 .mentions
390 .insert(crease_id, (mention_uri, task.clone()));
391
392 // Notify the user if we failed to load the mentioned context
393 cx.spawn_in(window, async move |this, cx| {
394 let result = task.await.notify_async_err(cx);
395 drop(tx);
396 if result.is_none() {
397 this.update(cx, |this, cx| {
398 this.editor.update(cx, |editor, cx| {
399 // Remove mention
400 editor.edit([(start_anchor..end_anchor, "")], cx);
401 });
402 this.mention_set.mentions.remove(&crease_id);
403 })
404 .ok();
405 }
406 })
407 }
408
409 fn confirm_mention_for_file(
410 &mut self,
411 abs_path: PathBuf,
412 cx: &mut Context<Self>,
413 ) -> Task<Result<Mention>> {
414 let Some(project_path) = self
415 .project
416 .read(cx)
417 .project_path_for_absolute_path(&abs_path, cx)
418 else {
419 return Task::ready(Err(anyhow!("project path not found")));
420 };
421 let extension = abs_path
422 .extension()
423 .and_then(OsStr::to_str)
424 .unwrap_or_default();
425
426 if Img::extensions().contains(&extension) && !extension.contains("svg") {
427 if !self.prompt_capabilities.get().image {
428 return Task::ready(Err(anyhow!("This model does not support images yet")));
429 }
430 let task = self
431 .project
432 .update(cx, |project, cx| project.open_image(project_path, cx));
433 return cx.spawn(async move |_, cx| {
434 let image = task.await?;
435 let image = image.update(cx, |image, _| image.image.clone())?;
436 let format = image.format;
437 let image = cx
438 .update(|cx| LanguageModelImage::from_image(image, cx))?
439 .await;
440 if let Some(image) = image {
441 Ok(Mention::Image(MentionImage {
442 data: image.source,
443 format,
444 }))
445 } else {
446 Err(anyhow!("Failed to convert image"))
447 }
448 });
449 }
450
451 let buffer = self
452 .project
453 .update(cx, |project, cx| project.open_buffer(project_path, cx));
454 cx.spawn(async move |_, cx| {
455 let buffer = buffer.await?;
456 let mention = buffer.update(cx, |buffer, cx| Mention::Text {
457 content: buffer.text(),
458 tracked_buffers: vec![cx.entity()],
459 })?;
460 anyhow::Ok(mention)
461 })
462 }
463
464 fn confirm_mention_for_directory(
465 &mut self,
466 abs_path: PathBuf,
467 cx: &mut Context<Self>,
468 ) -> Task<Result<Mention>> {
469 fn collect_files_in_path(worktree: &Worktree, path: &Path) -> Vec<(Arc<Path>, PathBuf)> {
470 let mut files = Vec::new();
471
472 for entry in worktree.child_entries(path) {
473 if entry.is_dir() {
474 files.extend(collect_files_in_path(worktree, &entry.path));
475 } else if entry.is_file() {
476 files.push((entry.path.clone(), worktree.full_path(&entry.path)));
477 }
478 }
479
480 files
481 }
482
483 let Some(project_path) = self
484 .project
485 .read(cx)
486 .project_path_for_absolute_path(&abs_path, cx)
487 else {
488 return Task::ready(Err(anyhow!("project path not found")));
489 };
490 let Some(entry) = self.project.read(cx).entry_for_path(&project_path, cx) else {
491 return Task::ready(Err(anyhow!("project entry not found")));
492 };
493 let Some(worktree) = self.project.read(cx).worktree_for_entry(entry.id, cx) else {
494 return Task::ready(Err(anyhow!("worktree not found")));
495 };
496 let project = self.project.clone();
497 cx.spawn(async move |_, cx| {
498 let directory_path = entry.path.clone();
499
500 let worktree_id = worktree.read_with(cx, |worktree, _| worktree.id())?;
501 let file_paths = worktree.read_with(cx, |worktree, _cx| {
502 collect_files_in_path(worktree, &directory_path)
503 })?;
504 let descendants_future = cx.update(|cx| {
505 join_all(file_paths.into_iter().map(|(worktree_path, full_path)| {
506 let rel_path = worktree_path
507 .strip_prefix(&directory_path)
508 .log_err()
509 .map_or_else(|| worktree_path.clone(), |rel_path| rel_path.into());
510
511 let open_task = project.update(cx, |project, cx| {
512 project.buffer_store().update(cx, |buffer_store, cx| {
513 let project_path = ProjectPath {
514 worktree_id,
515 path: worktree_path,
516 };
517 buffer_store.open_buffer(project_path, cx)
518 })
519 });
520
521 // TODO: report load errors instead of just logging
522 let rope_task = cx.spawn(async move |cx| {
523 let buffer = open_task.await.log_err()?;
524 let rope = buffer
525 .read_with(cx, |buffer, _cx| buffer.as_rope().clone())
526 .log_err()?;
527 Some((rope, buffer))
528 });
529
530 cx.background_spawn(async move {
531 let (rope, buffer) = rope_task.await?;
532 Some((rel_path, full_path, rope.to_string(), buffer))
533 })
534 }))
535 })?;
536
537 let contents = cx
538 .background_spawn(async move {
539 let (contents, tracked_buffers) = descendants_future
540 .await
541 .into_iter()
542 .flatten()
543 .map(|(rel_path, full_path, rope, buffer)| {
544 ((rel_path, full_path, rope), buffer)
545 })
546 .unzip();
547 Mention::Text {
548 content: render_directory_contents(contents),
549 tracked_buffers,
550 }
551 })
552 .await;
553 anyhow::Ok(contents)
554 })
555 }
556
557 fn confirm_mention_for_fetch(
558 &mut self,
559 url: url::Url,
560 cx: &mut Context<Self>,
561 ) -> Task<Result<Mention>> {
562 let http_client = match self
563 .workspace
564 .update(cx, |workspace, _| workspace.client().http_client())
565 {
566 Ok(http_client) => http_client,
567 Err(e) => return Task::ready(Err(e)),
568 };
569 cx.background_executor().spawn(async move {
570 let content = fetch_url_content(http_client, url.to_string()).await?;
571 Ok(Mention::Text {
572 content,
573 tracked_buffers: Vec::new(),
574 })
575 })
576 }
577
578 fn confirm_mention_for_symbol(
579 &mut self,
580 abs_path: PathBuf,
581 line_range: RangeInclusive<u32>,
582 cx: &mut Context<Self>,
583 ) -> Task<Result<Mention>> {
584 let Some(project_path) = self
585 .project
586 .read(cx)
587 .project_path_for_absolute_path(&abs_path, cx)
588 else {
589 return Task::ready(Err(anyhow!("project path not found")));
590 };
591 let buffer = self
592 .project
593 .update(cx, |project, cx| project.open_buffer(project_path, cx));
594 cx.spawn(async move |_, cx| {
595 let buffer = buffer.await?;
596 let mention = buffer.update(cx, |buffer, cx| {
597 let start = Point::new(*line_range.start(), 0).min(buffer.max_point());
598 let end = Point::new(*line_range.end() + 1, 0).min(buffer.max_point());
599 let content = buffer.text_for_range(start..end).collect();
600 Mention::Text {
601 content,
602 tracked_buffers: vec![cx.entity()],
603 }
604 })?;
605 anyhow::Ok(mention)
606 })
607 }
608
609 fn confirm_mention_for_rule(
610 &mut self,
611 id: PromptId,
612 cx: &mut Context<Self>,
613 ) -> Task<Result<Mention>> {
614 let Some(prompt_store) = self.prompt_store.clone() else {
615 return Task::ready(Err(anyhow!("missing prompt store")));
616 };
617 let prompt = prompt_store.read(cx).load(id, cx);
618 cx.spawn(async move |_, _| {
619 let prompt = prompt.await?;
620 Ok(Mention::Text {
621 content: prompt,
622 tracked_buffers: Vec::new(),
623 })
624 })
625 }
626
627 pub fn confirm_mention_for_selection(
628 &mut self,
629 source_range: Range<text::Anchor>,
630 selections: Vec<(Entity<Buffer>, Range<text::Anchor>, Range<usize>)>,
631 window: &mut Window,
632 cx: &mut Context<Self>,
633 ) {
634 let snapshot = self.editor.read(cx).buffer().read(cx).snapshot(cx);
635 let Some((&excerpt_id, _, _)) = snapshot.as_singleton() else {
636 return;
637 };
638 let Some(start) = snapshot.anchor_in_excerpt(excerpt_id, source_range.start) else {
639 return;
640 };
641
642 let offset = start.to_offset(&snapshot);
643
644 for (buffer, selection_range, range_to_fold) in selections {
645 let range = snapshot.anchor_after(offset + range_to_fold.start)
646 ..snapshot.anchor_after(offset + range_to_fold.end);
647
648 let abs_path = buffer
649 .read(cx)
650 .project_path(cx)
651 .and_then(|project_path| self.project.read(cx).absolute_path(&project_path, cx));
652 let snapshot = buffer.read(cx).snapshot();
653
654 let text = snapshot
655 .text_for_range(selection_range.clone())
656 .collect::<String>();
657 let point_range = selection_range.to_point(&snapshot);
658 let line_range = point_range.start.row..=point_range.end.row;
659
660 let uri = MentionUri::Selection {
661 abs_path: abs_path.clone(),
662 line_range: line_range.clone(),
663 };
664 let crease = crate::context_picker::crease_for_mention(
665 selection_name(abs_path.as_deref(), &line_range).into(),
666 uri.icon_path(cx),
667 range,
668 self.editor.downgrade(),
669 );
670
671 let crease_id = self.editor.update(cx, |editor, cx| {
672 let crease_ids = editor.insert_creases(vec![crease.clone()], cx);
673 editor.fold_creases(vec![crease], false, window, cx);
674 crease_ids.first().copied().unwrap()
675 });
676
677 self.mention_set.mentions.insert(
678 crease_id,
679 (
680 uri,
681 Task::ready(Ok(Mention::Text {
682 content: text,
683 tracked_buffers: vec![buffer],
684 }))
685 .shared(),
686 ),
687 );
688 }
689 }
690
691 fn confirm_mention_for_thread(
692 &mut self,
693 id: acp::SessionId,
694 cx: &mut Context<Self>,
695 ) -> Task<Result<Mention>> {
696 let server = Rc::new(agent2::NativeAgentServer::new(
697 self.project.read(cx).fs().clone(),
698 self.history_store.clone(),
699 ));
700 let delegate = AgentServerDelegate::new(self.project.clone(), None);
701 let connection = server.connect(Path::new(""), delegate, cx);
702 cx.spawn(async move |_, cx| {
703 let agent = connection.await?;
704 let agent = agent.downcast::<agent2::NativeAgentConnection>().unwrap();
705 let summary = agent
706 .0
707 .update(cx, |agent, cx| agent.thread_summary(id, cx))?
708 .await?;
709 anyhow::Ok(Mention::Text {
710 content: summary.to_string(),
711 tracked_buffers: Vec::new(),
712 })
713 })
714 }
715
716 fn confirm_mention_for_text_thread(
717 &mut self,
718 path: PathBuf,
719 cx: &mut Context<Self>,
720 ) -> Task<Result<Mention>> {
721 let context = self.history_store.update(cx, |text_thread_store, cx| {
722 text_thread_store.load_text_thread(path.as_path().into(), cx)
723 });
724 cx.spawn(async move |_, cx| {
725 let context = context.await?;
726 let xml = context.update(cx, |context, cx| context.to_xml(cx))?;
727 Ok(Mention::Text {
728 content: xml,
729 tracked_buffers: Vec::new(),
730 })
731 })
732 }
733
734 pub fn contents(
735 &self,
736 cx: &mut Context<Self>,
737 ) -> Task<Result<(Vec<acp::ContentBlock>, Vec<Entity<Buffer>>)>> {
738 let contents = self
739 .mention_set
740 .contents(&self.prompt_capabilities.get(), cx);
741 let editor = self.editor.clone();
742
743 cx.spawn(async move |_, cx| {
744 let contents = contents.await?;
745 let mut all_tracked_buffers = Vec::new();
746
747 editor.update(cx, |editor, cx| {
748 let mut ix = 0;
749 let mut chunks: Vec<acp::ContentBlock> = Vec::new();
750 let text = editor.text(cx);
751 editor.display_map.update(cx, |map, cx| {
752 let snapshot = map.snapshot(cx);
753 for (crease_id, crease) in snapshot.crease_snapshot.creases() {
754 let Some((uri, mention)) = contents.get(&crease_id) else {
755 continue;
756 };
757
758 let crease_range = crease.range().to_offset(&snapshot.buffer_snapshot);
759 if crease_range.start > ix {
760 //todo(): Custom slash command ContentBlock?
761 // let chunk = if prevent_slash_commands
762 // && ix == 0
763 // && parse_slash_command(&text[ix..]).is_some()
764 // {
765 // format!(" {}", &text[ix..crease_range.start]).into()
766 // } else {
767 // text[ix..crease_range.start].into()
768 // };
769 let chunk = text[ix..crease_range.start].into();
770 chunks.push(chunk);
771 }
772 let chunk = match mention {
773 Mention::Text {
774 content,
775 tracked_buffers,
776 } => {
777 all_tracked_buffers.extend(tracked_buffers.iter().cloned());
778 acp::ContentBlock::Resource(acp::EmbeddedResource {
779 annotations: None,
780 resource: acp::EmbeddedResourceResource::TextResourceContents(
781 acp::TextResourceContents {
782 mime_type: None,
783 text: content.clone(),
784 uri: uri.to_uri().to_string(),
785 },
786 ),
787 })
788 }
789 Mention::Image(mention_image) => {
790 let uri = match uri {
791 MentionUri::File { .. } => Some(uri.to_uri().to_string()),
792 MentionUri::PastedImage => None,
793 other => {
794 debug_panic!(
795 "unexpected mention uri for image: {:?}",
796 other
797 );
798 None
799 }
800 };
801 acp::ContentBlock::Image(acp::ImageContent {
802 annotations: None,
803 data: mention_image.data.to_string(),
804 mime_type: mention_image.format.mime_type().into(),
805 uri,
806 })
807 }
808 Mention::UriOnly => {
809 acp::ContentBlock::ResourceLink(acp::ResourceLink {
810 name: uri.name(),
811 uri: uri.to_uri().to_string(),
812 annotations: None,
813 description: None,
814 mime_type: None,
815 size: None,
816 title: None,
817 })
818 }
819 };
820 chunks.push(chunk);
821 ix = crease_range.end;
822 }
823
824 if ix < text.len() {
825 //todo(): Custom slash command ContentBlock?
826 // let last_chunk = if prevent_slash_commands
827 // && ix == 0
828 // && parse_slash_command(&text[ix..]).is_some()
829 // {
830 // format!(" {}", text[ix..].trim_end())
831 // } else {
832 // text[ix..].trim_end().to_owned()
833 // };
834 let last_chunk = text[ix..].trim_end().to_owned();
835 if !last_chunk.is_empty() {
836 chunks.push(last_chunk.into());
837 }
838 }
839 });
840
841 (chunks, all_tracked_buffers)
842 })
843 })
844 }
845
846 pub fn clear(&mut self, window: &mut Window, cx: &mut Context<Self>) {
847 self.editor.update(cx, |editor, cx| {
848 editor.clear(window, cx);
849 editor.remove_creases(
850 self.mention_set
851 .mentions
852 .drain()
853 .map(|(crease_id, _)| crease_id),
854 cx,
855 )
856 });
857 }
858
859 fn send(&mut self, _: &Chat, _: &mut Window, cx: &mut Context<Self>) {
860 if self.is_empty(cx) {
861 return;
862 }
863 cx.emit(MessageEditorEvent::Send)
864 }
865
866 fn cancel(&mut self, _: &editor::actions::Cancel, _: &mut Window, cx: &mut Context<Self>) {
867 cx.emit(MessageEditorEvent::Cancel)
868 }
869
870 fn paste(&mut self, _: &Paste, window: &mut Window, cx: &mut Context<Self>) {
871 if !self.prompt_capabilities.get().image {
872 return;
873 }
874
875 let images = cx
876 .read_from_clipboard()
877 .map(|item| {
878 item.into_entries()
879 .filter_map(|entry| {
880 if let ClipboardEntry::Image(image) = entry {
881 Some(image)
882 } else {
883 None
884 }
885 })
886 .collect::<Vec<_>>()
887 })
888 .unwrap_or_default();
889
890 if images.is_empty() {
891 return;
892 }
893 cx.stop_propagation();
894
895 let replacement_text = MentionUri::PastedImage.as_link().to_string();
896 for image in images {
897 let (excerpt_id, text_anchor, multibuffer_anchor) =
898 self.editor.update(cx, |message_editor, cx| {
899 let snapshot = message_editor.snapshot(window, cx);
900 let (excerpt_id, _, buffer_snapshot) =
901 snapshot.buffer_snapshot.as_singleton().unwrap();
902
903 let text_anchor = buffer_snapshot.anchor_before(buffer_snapshot.len());
904 let multibuffer_anchor = snapshot
905 .buffer_snapshot
906 .anchor_in_excerpt(*excerpt_id, text_anchor);
907 message_editor.edit(
908 [(
909 multi_buffer::Anchor::max()..multi_buffer::Anchor::max(),
910 format!("{replacement_text} "),
911 )],
912 cx,
913 );
914 (*excerpt_id, text_anchor, multibuffer_anchor)
915 });
916
917 let content_len = replacement_text.len();
918 let Some(start_anchor) = multibuffer_anchor else {
919 continue;
920 };
921 let end_anchor = self.editor.update(cx, |editor, cx| {
922 let snapshot = editor.buffer().read(cx).snapshot(cx);
923 snapshot.anchor_before(start_anchor.to_offset(&snapshot) + content_len)
924 });
925 let image = Arc::new(image);
926 let Some((crease_id, tx)) = insert_crease_for_mention(
927 excerpt_id,
928 text_anchor,
929 content_len,
930 MentionUri::PastedImage.name().into(),
931 IconName::Image.path().into(),
932 Some(Task::ready(Ok(image.clone())).shared()),
933 self.editor.clone(),
934 window,
935 cx,
936 ) else {
937 continue;
938 };
939 let task = cx
940 .spawn_in(window, {
941 async move |_, cx| {
942 let format = image.format;
943 let image = cx
944 .update(|_, cx| LanguageModelImage::from_image(image, cx))
945 .map_err(|e| e.to_string())?
946 .await;
947 drop(tx);
948 if let Some(image) = image {
949 Ok(Mention::Image(MentionImage {
950 data: image.source,
951 format,
952 }))
953 } else {
954 Err("Failed to convert image".into())
955 }
956 }
957 })
958 .shared();
959
960 self.mention_set
961 .mentions
962 .insert(crease_id, (MentionUri::PastedImage, task.clone()));
963
964 cx.spawn_in(window, async move |this, cx| {
965 if task.await.notify_async_err(cx).is_none() {
966 this.update(cx, |this, cx| {
967 this.editor.update(cx, |editor, cx| {
968 editor.edit([(start_anchor..end_anchor, "")], cx);
969 });
970 this.mention_set.mentions.remove(&crease_id);
971 })
972 .ok();
973 }
974 })
975 .detach();
976 }
977 }
978
979 pub fn insert_dragged_files(
980 &mut self,
981 paths: Vec<project::ProjectPath>,
982 added_worktrees: Vec<Entity<Worktree>>,
983 window: &mut Window,
984 cx: &mut Context<Self>,
985 ) {
986 let buffer = self.editor.read(cx).buffer().clone();
987 let Some(buffer) = buffer.read(cx).as_singleton() else {
988 return;
989 };
990 let mut tasks = Vec::new();
991 for path in paths {
992 let Some(entry) = self.project.read(cx).entry_for_path(&path, cx) else {
993 continue;
994 };
995 let Some(abs_path) = self.project.read(cx).absolute_path(&path, cx) else {
996 continue;
997 };
998 let path_prefix = abs_path
999 .file_name()
1000 .unwrap_or(path.path.as_os_str())
1001 .display()
1002 .to_string();
1003 let (file_name, _) =
1004 crate::context_picker::file_context_picker::extract_file_name_and_directory(
1005 &path.path,
1006 &path_prefix,
1007 );
1008
1009 let uri = if entry.is_dir() {
1010 MentionUri::Directory { abs_path }
1011 } else {
1012 MentionUri::File { abs_path }
1013 };
1014
1015 let new_text = format!("{} ", uri.as_link());
1016 let content_len = new_text.len() - 1;
1017
1018 let anchor = buffer.update(cx, |buffer, _cx| buffer.anchor_before(buffer.len()));
1019
1020 self.editor.update(cx, |message_editor, cx| {
1021 message_editor.edit(
1022 [(
1023 multi_buffer::Anchor::max()..multi_buffer::Anchor::max(),
1024 new_text,
1025 )],
1026 cx,
1027 );
1028 });
1029 tasks.push(self.confirm_mention_completion(
1030 file_name,
1031 anchor,
1032 content_len,
1033 uri,
1034 window,
1035 cx,
1036 ));
1037 }
1038 cx.spawn(async move |_, _| {
1039 join_all(tasks).await;
1040 drop(added_worktrees);
1041 })
1042 .detach();
1043 }
1044
1045 pub fn insert_selections(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1046 let buffer = self.editor.read(cx).buffer().clone();
1047 let Some(buffer) = buffer.read(cx).as_singleton() else {
1048 return;
1049 };
1050 let anchor = buffer.update(cx, |buffer, _cx| buffer.anchor_before(buffer.len()));
1051 let Some(workspace) = self.workspace.upgrade() else {
1052 return;
1053 };
1054 let Some(completion) = ContextPickerCompletionProvider::completion_for_action(
1055 ContextPickerAction::AddSelections,
1056 anchor..anchor,
1057 cx.weak_entity(),
1058 &workspace,
1059 cx,
1060 ) else {
1061 return;
1062 };
1063 self.editor.update(cx, |message_editor, cx| {
1064 message_editor.edit(
1065 [(
1066 multi_buffer::Anchor::max()..multi_buffer::Anchor::max(),
1067 completion.new_text,
1068 )],
1069 cx,
1070 );
1071 });
1072 if let Some(confirm) = completion.confirm {
1073 confirm(CompletionIntent::Complete, window, cx);
1074 }
1075 }
1076
1077 pub fn set_read_only(&mut self, read_only: bool, cx: &mut Context<Self>) {
1078 self.editor.update(cx, |message_editor, cx| {
1079 message_editor.set_read_only(read_only);
1080 cx.notify()
1081 })
1082 }
1083
1084 pub fn set_mode(&mut self, mode: EditorMode, cx: &mut Context<Self>) {
1085 self.editor.update(cx, |editor, cx| {
1086 editor.set_mode(mode);
1087 cx.notify()
1088 });
1089 }
1090
1091 pub fn set_message(
1092 &mut self,
1093 message: Vec<acp::ContentBlock>,
1094 window: &mut Window,
1095 cx: &mut Context<Self>,
1096 ) {
1097 self.clear(window, cx);
1098
1099 let mut text = String::new();
1100 let mut mentions = Vec::new();
1101
1102 for chunk in message {
1103 match chunk {
1104 acp::ContentBlock::Text(text_content) => {
1105 text.push_str(&text_content.text);
1106 }
1107 acp::ContentBlock::Resource(acp::EmbeddedResource {
1108 resource: acp::EmbeddedResourceResource::TextResourceContents(resource),
1109 ..
1110 }) => {
1111 let Some(mention_uri) = MentionUri::parse(&resource.uri).log_err() else {
1112 continue;
1113 };
1114 let start = text.len();
1115 write!(&mut text, "{}", mention_uri.as_link()).ok();
1116 let end = text.len();
1117 mentions.push((
1118 start..end,
1119 mention_uri,
1120 Mention::Text {
1121 content: resource.text,
1122 tracked_buffers: Vec::new(),
1123 },
1124 ));
1125 }
1126 acp::ContentBlock::ResourceLink(resource) => {
1127 if let Some(mention_uri) = MentionUri::parse(&resource.uri).log_err() {
1128 let start = text.len();
1129 write!(&mut text, "{}", mention_uri.as_link()).ok();
1130 let end = text.len();
1131 mentions.push((start..end, mention_uri, Mention::UriOnly));
1132 }
1133 }
1134 acp::ContentBlock::Image(acp::ImageContent {
1135 uri,
1136 data,
1137 mime_type,
1138 annotations: _,
1139 }) => {
1140 let mention_uri = if let Some(uri) = uri {
1141 MentionUri::parse(&uri)
1142 } else {
1143 Ok(MentionUri::PastedImage)
1144 };
1145 let Some(mention_uri) = mention_uri.log_err() else {
1146 continue;
1147 };
1148 let Some(format) = ImageFormat::from_mime_type(&mime_type) else {
1149 log::error!("failed to parse MIME type for image: {mime_type:?}");
1150 continue;
1151 };
1152 let start = text.len();
1153 write!(&mut text, "{}", mention_uri.as_link()).ok();
1154 let end = text.len();
1155 mentions.push((
1156 start..end,
1157 mention_uri,
1158 Mention::Image(MentionImage {
1159 data: data.into(),
1160 format,
1161 }),
1162 ));
1163 }
1164 acp::ContentBlock::Audio(_) | acp::ContentBlock::Resource(_) => {}
1165 }
1166 }
1167
1168 let snapshot = self.editor.update(cx, |editor, cx| {
1169 editor.set_text(text, window, cx);
1170 editor.buffer().read(cx).snapshot(cx)
1171 });
1172
1173 for (range, mention_uri, mention) in mentions {
1174 let anchor = snapshot.anchor_before(range.start);
1175 let Some((crease_id, tx)) = insert_crease_for_mention(
1176 anchor.excerpt_id,
1177 anchor.text_anchor,
1178 range.end - range.start,
1179 mention_uri.name().into(),
1180 mention_uri.icon_path(cx),
1181 None,
1182 self.editor.clone(),
1183 window,
1184 cx,
1185 ) else {
1186 continue;
1187 };
1188 drop(tx);
1189
1190 self.mention_set.mentions.insert(
1191 crease_id,
1192 (mention_uri.clone(), Task::ready(Ok(mention)).shared()),
1193 );
1194 }
1195 cx.notify();
1196 }
1197
1198 pub fn text(&self, cx: &App) -> String {
1199 self.editor.read(cx).text(cx)
1200 }
1201
1202 #[cfg(test)]
1203 pub fn set_text(&mut self, text: &str, window: &mut Window, cx: &mut Context<Self>) {
1204 self.editor.update(cx, |editor, cx| {
1205 editor.set_text(text, window, cx);
1206 });
1207 }
1208}
1209
1210fn render_directory_contents(entries: Vec<(Arc<Path>, PathBuf, String)>) -> String {
1211 let mut output = String::new();
1212 for (_relative_path, full_path, content) in entries {
1213 let fence = codeblock_fence_for_path(Some(&full_path), None);
1214 write!(output, "\n{fence}\n{content}\n```").unwrap();
1215 }
1216 output
1217}
1218
1219impl Focusable for MessageEditor {
1220 fn focus_handle(&self, cx: &App) -> FocusHandle {
1221 self.editor.focus_handle(cx)
1222 }
1223}
1224
1225impl Render for MessageEditor {
1226 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
1227 div()
1228 .key_context("MessageEditor")
1229 .on_action(cx.listener(Self::send))
1230 .on_action(cx.listener(Self::cancel))
1231 .capture_action(cx.listener(Self::paste))
1232 .flex_1()
1233 .child({
1234 let settings = ThemeSettings::get_global(cx);
1235 let font_size = TextSize::Small
1236 .rems(cx)
1237 .to_pixels(settings.agent_font_size(cx));
1238 let line_height = settings.buffer_line_height.value() * font_size;
1239
1240 let text_style = TextStyle {
1241 color: cx.theme().colors().text,
1242 font_family: settings.buffer_font.family.clone(),
1243 font_fallbacks: settings.buffer_font.fallbacks.clone(),
1244 font_features: settings.buffer_font.features.clone(),
1245 font_size: font_size.into(),
1246 line_height: line_height.into(),
1247 ..Default::default()
1248 };
1249
1250 EditorElement::new(
1251 &self.editor,
1252 EditorStyle {
1253 background: cx.theme().colors().editor_background,
1254 local_player: cx.theme().players().local(),
1255 text: text_style,
1256 syntax: cx.theme().syntax().clone(),
1257 inlay_hints_style: editor::make_inlay_hints_style(cx),
1258 ..Default::default()
1259 },
1260 )
1261 })
1262 }
1263}
1264
1265pub(crate) fn insert_crease_for_mention(
1266 excerpt_id: ExcerptId,
1267 anchor: text::Anchor,
1268 content_len: usize,
1269 crease_label: SharedString,
1270 crease_icon: SharedString,
1271 // abs_path: Option<Arc<Path>>,
1272 image: Option<Shared<Task<Result<Arc<Image>, String>>>>,
1273 editor: Entity<Editor>,
1274 window: &mut Window,
1275 cx: &mut App,
1276) -> Option<(CreaseId, postage::barrier::Sender)> {
1277 let (tx, rx) = postage::barrier::channel();
1278
1279 let crease_id = editor.update(cx, |editor, cx| {
1280 let snapshot = editor.buffer().read(cx).snapshot(cx);
1281
1282 let start = snapshot.anchor_in_excerpt(excerpt_id, anchor)?;
1283
1284 let start = start.bias_right(&snapshot);
1285 let end = snapshot.anchor_before(start.to_offset(&snapshot) + content_len);
1286
1287 let placeholder = FoldPlaceholder {
1288 render: render_mention_fold_button(
1289 crease_label,
1290 crease_icon,
1291 start..end,
1292 rx,
1293 image,
1294 cx.weak_entity(),
1295 cx,
1296 ),
1297 merge_adjacent: false,
1298 ..Default::default()
1299 };
1300
1301 let crease = Crease::Inline {
1302 range: start..end,
1303 placeholder,
1304 render_toggle: None,
1305 render_trailer: None,
1306 metadata: None,
1307 };
1308
1309 let ids = editor.insert_creases(vec![crease.clone()], cx);
1310 editor.fold_creases(vec![crease], false, window, cx);
1311
1312 Some(ids[0])
1313 })?;
1314
1315 Some((crease_id, tx))
1316}
1317
1318fn render_mention_fold_button(
1319 label: SharedString,
1320 icon: SharedString,
1321 range: Range<Anchor>,
1322 mut loading_finished: postage::barrier::Receiver,
1323 image_task: Option<Shared<Task<Result<Arc<Image>, String>>>>,
1324 editor: WeakEntity<Editor>,
1325 cx: &mut App,
1326) -> Arc<dyn Send + Sync + Fn(FoldId, Range<Anchor>, &mut App) -> AnyElement> {
1327 let loading = cx.new(|cx| {
1328 let loading = cx.spawn(async move |this, cx| {
1329 loading_finished.recv().await;
1330 this.update(cx, |this: &mut LoadingContext, cx| {
1331 this.loading = None;
1332 cx.notify();
1333 })
1334 .ok();
1335 });
1336 LoadingContext {
1337 id: cx.entity_id(),
1338 label,
1339 icon,
1340 range,
1341 editor,
1342 loading: Some(loading),
1343 image: image_task.clone(),
1344 }
1345 });
1346 Arc::new(move |_fold_id, _fold_range, _cx| loading.clone().into_any_element())
1347}
1348
1349struct LoadingContext {
1350 id: EntityId,
1351 label: SharedString,
1352 icon: SharedString,
1353 range: Range<Anchor>,
1354 editor: WeakEntity<Editor>,
1355 loading: Option<Task<()>>,
1356 image: Option<Shared<Task<Result<Arc<Image>, String>>>>,
1357}
1358
1359impl Render for LoadingContext {
1360 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
1361 let is_in_text_selection = self
1362 .editor
1363 .update(cx, |editor, cx| editor.is_range_selected(&self.range, cx))
1364 .unwrap_or_default();
1365 ButtonLike::new(("loading-context", self.id))
1366 .style(ButtonStyle::Filled)
1367 .selected_style(ButtonStyle::Tinted(TintColor::Accent))
1368 .toggle_state(is_in_text_selection)
1369 .when_some(self.image.clone(), |el, image_task| {
1370 el.hoverable_tooltip(move |_, cx| {
1371 let image = image_task.peek().cloned().transpose().ok().flatten();
1372 let image_task = image_task.clone();
1373 cx.new::<ImageHover>(|cx| ImageHover {
1374 image,
1375 _task: cx.spawn(async move |this, cx| {
1376 if let Ok(image) = image_task.clone().await {
1377 this.update(cx, |this, cx| {
1378 if this.image.replace(image).is_none() {
1379 cx.notify();
1380 }
1381 })
1382 .ok();
1383 }
1384 }),
1385 })
1386 .into()
1387 })
1388 })
1389 .child(
1390 h_flex()
1391 .gap_1()
1392 .child(
1393 Icon::from_path(self.icon.clone())
1394 .size(IconSize::XSmall)
1395 .color(Color::Muted),
1396 )
1397 .child(
1398 Label::new(self.label.clone())
1399 .size(LabelSize::Small)
1400 .buffer_font(cx)
1401 .single_line(),
1402 )
1403 .map(|el| {
1404 if self.loading.is_some() {
1405 el.with_animation(
1406 "loading-context-crease",
1407 Animation::new(Duration::from_secs(2))
1408 .repeat()
1409 .with_easing(pulsating_between(0.4, 0.8)),
1410 |label, delta| label.opacity(delta),
1411 )
1412 .into_any()
1413 } else {
1414 el.into_any()
1415 }
1416 }),
1417 )
1418 }
1419}
1420
1421struct ImageHover {
1422 image: Option<Arc<Image>>,
1423 _task: Task<()>,
1424}
1425
1426impl Render for ImageHover {
1427 fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
1428 if let Some(image) = self.image.clone() {
1429 gpui::img(image).max_w_96().max_h_96().into_any_element()
1430 } else {
1431 gpui::Empty.into_any_element()
1432 }
1433 }
1434}
1435
1436#[derive(Debug, Clone, Eq, PartialEq)]
1437pub enum Mention {
1438 Text {
1439 content: String,
1440 tracked_buffers: Vec<Entity<Buffer>>,
1441 },
1442 Image(MentionImage),
1443 UriOnly,
1444}
1445
1446#[derive(Clone, Debug, Eq, PartialEq)]
1447pub struct MentionImage {
1448 pub data: SharedString,
1449 pub format: ImageFormat,
1450}
1451
1452#[derive(Default)]
1453pub struct MentionSet {
1454 mentions: HashMap<CreaseId, (MentionUri, Shared<Task<Result<Mention, String>>>)>,
1455}
1456
1457impl MentionSet {
1458 fn contents(
1459 &self,
1460 prompt_capabilities: &acp::PromptCapabilities,
1461 cx: &mut App,
1462 ) -> Task<Result<HashMap<CreaseId, (MentionUri, Mention)>>> {
1463 if !prompt_capabilities.embedded_context {
1464 let mentions = self
1465 .mentions
1466 .iter()
1467 .map(|(crease_id, (uri, _))| (*crease_id, (uri.clone(), Mention::UriOnly)))
1468 .collect();
1469
1470 return Task::ready(Ok(mentions));
1471 }
1472
1473 let mentions = self.mentions.clone();
1474 cx.spawn(async move |_cx| {
1475 let mut contents = HashMap::default();
1476 for (crease_id, (mention_uri, task)) in mentions {
1477 contents.insert(
1478 crease_id,
1479 (mention_uri, task.await.map_err(|e| anyhow!("{e}"))?),
1480 );
1481 }
1482 Ok(contents)
1483 })
1484 }
1485
1486 fn remove_invalid(&mut self, snapshot: EditorSnapshot) {
1487 for (crease_id, crease) in snapshot.crease_snapshot.creases() {
1488 if !crease.range().start.is_valid(&snapshot.buffer_snapshot) {
1489 self.mentions.remove(&crease_id);
1490 }
1491 }
1492 }
1493}
1494
1495pub struct MessageEditorAddon {}
1496
1497impl MessageEditorAddon {
1498 pub fn new() -> Self {
1499 Self {}
1500 }
1501}
1502
1503impl Addon for MessageEditorAddon {
1504 fn to_any(&self) -> &dyn std::any::Any {
1505 self
1506 }
1507
1508 fn to_any_mut(&mut self) -> Option<&mut dyn std::any::Any> {
1509 Some(self)
1510 }
1511
1512 fn extend_key_context(&self, key_context: &mut KeyContext, cx: &App) {
1513 let settings = agent_settings::AgentSettings::get_global(cx);
1514 if settings.use_modifier_to_send {
1515 key_context.add("use_modifier_to_send");
1516 }
1517 }
1518}
1519
1520#[cfg(test)]
1521mod tests {
1522 use std::{
1523 cell::{Cell, RefCell},
1524 ops::Range,
1525 path::Path,
1526 rc::Rc,
1527 sync::Arc,
1528 };
1529
1530 use acp_thread::MentionUri;
1531 use agent_client_protocol as acp;
1532 use agent2::HistoryStore;
1533 use assistant_context::ContextStore;
1534 use editor::{AnchorRangeExt as _, Editor, EditorMode};
1535 use fs::FakeFs;
1536 use futures::StreamExt as _;
1537 use gpui::{
1538 AppContext, Entity, EventEmitter, FocusHandle, Focusable, TestAppContext, VisualTestContext,
1539 };
1540 use lsp::{CompletionContext, CompletionTriggerKind};
1541 use project::{CompletionIntent, Project, ProjectPath};
1542 use serde_json::json;
1543 use text::Point;
1544 use ui::{App, Context, IntoElement, Render, SharedString, Window};
1545 use util::{path, uri};
1546 use workspace::{AppState, Item, Workspace};
1547
1548 use crate::acp::{
1549 message_editor::{Mention, MessageEditor},
1550 thread_view::tests::init_test,
1551 };
1552
1553 #[gpui::test]
1554 async fn test_at_mention_removal(cx: &mut TestAppContext) {
1555 init_test(cx);
1556
1557 let fs = FakeFs::new(cx.executor());
1558 fs.insert_tree("/project", json!({"file": ""})).await;
1559 let project = Project::test(fs, [Path::new(path!("/project"))], cx).await;
1560
1561 let (workspace, cx) =
1562 cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));
1563
1564 let context_store = cx.new(|cx| ContextStore::fake(project.clone(), cx));
1565 let history_store = cx.new(|cx| HistoryStore::new(context_store, cx));
1566
1567 let message_editor = cx.update(|window, cx| {
1568 cx.new(|cx| {
1569 MessageEditor::new(
1570 workspace.downgrade(),
1571 project.clone(),
1572 history_store.clone(),
1573 None,
1574 Default::default(),
1575 Default::default(),
1576 "Test",
1577 EditorMode::AutoHeight {
1578 min_lines: 1,
1579 max_lines: None,
1580 },
1581 window,
1582 cx,
1583 )
1584 })
1585 });
1586 let editor = message_editor.update(cx, |message_editor, _| message_editor.editor.clone());
1587
1588 cx.run_until_parked();
1589
1590 let excerpt_id = editor.update(cx, |editor, cx| {
1591 editor
1592 .buffer()
1593 .read(cx)
1594 .excerpt_ids()
1595 .into_iter()
1596 .next()
1597 .unwrap()
1598 });
1599 let completions = editor.update_in(cx, |editor, window, cx| {
1600 editor.set_text("Hello @file ", window, cx);
1601 let buffer = editor.buffer().read(cx).as_singleton().unwrap();
1602 let completion_provider = editor.completion_provider().unwrap();
1603 completion_provider.completions(
1604 excerpt_id,
1605 &buffer,
1606 text::Anchor::MAX,
1607 CompletionContext {
1608 trigger_kind: CompletionTriggerKind::TRIGGER_CHARACTER,
1609 trigger_character: Some("@".into()),
1610 },
1611 window,
1612 cx,
1613 )
1614 });
1615 let [_, completion]: [_; 2] = completions
1616 .await
1617 .unwrap()
1618 .into_iter()
1619 .flat_map(|response| response.completions)
1620 .collect::<Vec<_>>()
1621 .try_into()
1622 .unwrap();
1623
1624 editor.update_in(cx, |editor, window, cx| {
1625 let snapshot = editor.buffer().read(cx).snapshot(cx);
1626 let start = snapshot
1627 .anchor_in_excerpt(excerpt_id, completion.replace_range.start)
1628 .unwrap();
1629 let end = snapshot
1630 .anchor_in_excerpt(excerpt_id, completion.replace_range.end)
1631 .unwrap();
1632 editor.edit([(start..end, completion.new_text)], cx);
1633 (completion.confirm.unwrap())(CompletionIntent::Complete, window, cx);
1634 });
1635
1636 cx.run_until_parked();
1637
1638 // Backspace over the inserted crease (and the following space).
1639 editor.update_in(cx, |editor, window, cx| {
1640 editor.backspace(&Default::default(), window, cx);
1641 editor.backspace(&Default::default(), window, cx);
1642 });
1643
1644 let (content, _) = message_editor
1645 .update(cx, |message_editor, cx| message_editor.contents(cx))
1646 .await
1647 .unwrap();
1648
1649 // We don't send a resource link for the deleted crease.
1650 pretty_assertions::assert_matches!(content.as_slice(), [acp::ContentBlock::Text { .. }]);
1651 }
1652
1653 struct MessageEditorItem(Entity<MessageEditor>);
1654
1655 impl Item for MessageEditorItem {
1656 type Event = ();
1657
1658 fn include_in_nav_history() -> bool {
1659 false
1660 }
1661
1662 fn tab_content_text(&self, _detail: usize, _cx: &App) -> SharedString {
1663 "Test".into()
1664 }
1665 }
1666
1667 impl EventEmitter<()> for MessageEditorItem {}
1668
1669 impl Focusable for MessageEditorItem {
1670 fn focus_handle(&self, cx: &App) -> FocusHandle {
1671 self.0.read(cx).focus_handle(cx)
1672 }
1673 }
1674
1675 impl Render for MessageEditorItem {
1676 fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
1677 self.0.clone().into_any_element()
1678 }
1679 }
1680
1681 #[gpui::test]
1682 async fn test_completion_provider_commands(cx: &mut TestAppContext) {
1683 init_test(cx);
1684
1685 let app_state = cx.update(AppState::test);
1686
1687 cx.update(|cx| {
1688 language::init(cx);
1689 editor::init(cx);
1690 workspace::init(app_state.clone(), cx);
1691 Project::init_settings(cx);
1692 });
1693
1694 let project = Project::test(app_state.fs.clone(), [path!("/dir").as_ref()], cx).await;
1695 let window = cx.add_window(|window, cx| Workspace::test_new(project.clone(), window, cx));
1696 let workspace = window.root(cx).unwrap();
1697
1698 let mut cx = VisualTestContext::from_window(*window, cx);
1699
1700 let context_store = cx.new(|cx| ContextStore::fake(project.clone(), cx));
1701 let history_store = cx.new(|cx| HistoryStore::new(context_store, cx));
1702 let prompt_capabilities = Rc::new(Cell::new(acp::PromptCapabilities::default()));
1703 let available_commands = Rc::new(RefCell::new(vec![
1704 acp::AvailableCommand {
1705 name: "quick-math".to_string(),
1706 description: "2 + 2 = 4 - 1 = 3".to_string(),
1707 input: None,
1708 },
1709 acp::AvailableCommand {
1710 name: "say-hello".to_string(),
1711 description: "Say hello to whoever you want".to_string(),
1712 input: Some(acp::AvailableCommandInput::Unstructured {
1713 hint: "<name>".to_string(),
1714 }),
1715 },
1716 ]));
1717
1718 let editor = workspace.update_in(&mut cx, |workspace, window, cx| {
1719 let workspace_handle = cx.weak_entity();
1720 let message_editor = cx.new(|cx| {
1721 MessageEditor::new(
1722 workspace_handle,
1723 project.clone(),
1724 history_store.clone(),
1725 None,
1726 prompt_capabilities.clone(),
1727 available_commands.clone(),
1728 "Test",
1729 EditorMode::AutoHeight {
1730 max_lines: None,
1731 min_lines: 1,
1732 },
1733 window,
1734 cx,
1735 )
1736 });
1737 workspace.active_pane().update(cx, |pane, cx| {
1738 pane.add_item(
1739 Box::new(cx.new(|_| MessageEditorItem(message_editor.clone()))),
1740 true,
1741 true,
1742 None,
1743 window,
1744 cx,
1745 );
1746 });
1747 message_editor.read(cx).focus_handle(cx).focus(window);
1748 message_editor.read(cx).editor().clone()
1749 });
1750
1751 cx.simulate_input("/");
1752
1753 editor.update_in(&mut cx, |editor, window, cx| {
1754 assert_eq!(editor.text(cx), "/");
1755 assert!(editor.has_visible_completions_menu());
1756
1757 assert_eq!(
1758 current_completion_labels_with_documentation(editor),
1759 &[
1760 ("quick-math".into(), "2 + 2 = 4 - 1 = 3".into()),
1761 ("say-hello".into(), "Say hello to whoever you want".into())
1762 ]
1763 );
1764 editor.set_text("", window, cx);
1765 });
1766
1767 cx.simulate_input("/qui");
1768
1769 editor.update_in(&mut cx, |editor, window, cx| {
1770 assert_eq!(editor.text(cx), "/qui");
1771 assert!(editor.has_visible_completions_menu());
1772
1773 assert_eq!(
1774 current_completion_labels_with_documentation(editor),
1775 &[("quick-math".into(), "2 + 2 = 4 - 1 = 3".into())]
1776 );
1777 editor.set_text("", window, cx);
1778 });
1779
1780 editor.update_in(&mut cx, |editor, window, cx| {
1781 assert!(editor.has_visible_completions_menu());
1782 editor.confirm_completion(&editor::actions::ConfirmCompletion::default(), window, cx);
1783 });
1784
1785 cx.run_until_parked();
1786
1787 editor.update_in(&mut cx, |editor, window, cx| {
1788 assert_eq!(editor.display_text(cx), "/quick-math ");
1789 assert!(!editor.has_visible_completions_menu());
1790 editor.set_text("", window, cx);
1791 });
1792
1793 cx.simulate_input("/say");
1794
1795 editor.update_in(&mut cx, |editor, _window, cx| {
1796 assert_eq!(editor.display_text(cx), "/say");
1797 assert!(editor.has_visible_completions_menu());
1798
1799 assert_eq!(
1800 current_completion_labels_with_documentation(editor),
1801 &[("say-hello".into(), "Say hello to whoever you want".into())]
1802 );
1803 });
1804
1805 editor.update_in(&mut cx, |editor, window, cx| {
1806 assert!(editor.has_visible_completions_menu());
1807 editor.confirm_completion(&editor::actions::ConfirmCompletion::default(), window, cx);
1808 });
1809
1810 cx.run_until_parked();
1811
1812 editor.update_in(&mut cx, |editor, _window, cx| {
1813 assert_eq!(editor.text(cx), "/say-hello ");
1814 assert_eq!(editor.display_text(cx), "/say-hello <name>");
1815 assert!(editor.has_visible_completions_menu());
1816
1817 assert_eq!(
1818 current_completion_labels_with_documentation(editor),
1819 &[("say-hello".into(), "Say hello to whoever you want".into())]
1820 );
1821 });
1822
1823 cx.simulate_input("GPT5");
1824
1825 editor.update_in(&mut cx, |editor, window, cx| {
1826 assert!(editor.has_visible_completions_menu());
1827 editor.confirm_completion(&editor::actions::ConfirmCompletion::default(), window, cx);
1828 });
1829
1830 cx.run_until_parked();
1831
1832 editor.update_in(&mut cx, |editor, window, cx| {
1833 assert_eq!(editor.text(cx), "/say-hello GPT5");
1834 assert_eq!(editor.display_text(cx), "/say-hello GPT5");
1835 assert!(!editor.has_visible_completions_menu());
1836
1837 // Delete argument
1838 for _ in 0..4 {
1839 editor.backspace(&editor::actions::Backspace, window, cx);
1840 }
1841 });
1842
1843 cx.run_until_parked();
1844
1845 editor.update_in(&mut cx, |editor, window, cx| {
1846 assert_eq!(editor.text(cx), "/say-hello ");
1847 // Hint is visible because argument was deleted
1848 assert_eq!(editor.display_text(cx), "/say-hello <name>");
1849
1850 // Delete last command letter
1851 editor.backspace(&editor::actions::Backspace, window, cx);
1852 editor.backspace(&editor::actions::Backspace, window, cx);
1853 });
1854
1855 cx.run_until_parked();
1856
1857 editor.update_in(&mut cx, |editor, _window, cx| {
1858 // Hint goes away once command no longer matches an available one
1859 assert_eq!(editor.text(cx), "/say-hell");
1860 assert_eq!(editor.display_text(cx), "/say-hell");
1861 assert!(!editor.has_visible_completions_menu());
1862 });
1863 }
1864
1865 #[gpui::test]
1866 async fn test_context_completion_provider_mentions(cx: &mut TestAppContext) {
1867 init_test(cx);
1868
1869 let app_state = cx.update(AppState::test);
1870
1871 cx.update(|cx| {
1872 language::init(cx);
1873 editor::init(cx);
1874 workspace::init(app_state.clone(), cx);
1875 Project::init_settings(cx);
1876 });
1877
1878 app_state
1879 .fs
1880 .as_fake()
1881 .insert_tree(
1882 path!("/dir"),
1883 json!({
1884 "editor": "",
1885 "a": {
1886 "one.txt": "1",
1887 "two.txt": "2",
1888 "three.txt": "3",
1889 "four.txt": "4"
1890 },
1891 "b": {
1892 "five.txt": "5",
1893 "six.txt": "6",
1894 "seven.txt": "7",
1895 "eight.txt": "8",
1896 },
1897 "x.png": "",
1898 }),
1899 )
1900 .await;
1901
1902 let project = Project::test(app_state.fs.clone(), [path!("/dir").as_ref()], cx).await;
1903 let window = cx.add_window(|window, cx| Workspace::test_new(project.clone(), window, cx));
1904 let workspace = window.root(cx).unwrap();
1905
1906 let worktree = project.update(cx, |project, cx| {
1907 let mut worktrees = project.worktrees(cx).collect::<Vec<_>>();
1908 assert_eq!(worktrees.len(), 1);
1909 worktrees.pop().unwrap()
1910 });
1911 let worktree_id = worktree.read_with(cx, |worktree, _| worktree.id());
1912
1913 let mut cx = VisualTestContext::from_window(*window, cx);
1914
1915 let paths = vec![
1916 path!("a/one.txt"),
1917 path!("a/two.txt"),
1918 path!("a/three.txt"),
1919 path!("a/four.txt"),
1920 path!("b/five.txt"),
1921 path!("b/six.txt"),
1922 path!("b/seven.txt"),
1923 path!("b/eight.txt"),
1924 ];
1925
1926 let mut opened_editors = Vec::new();
1927 for path in paths {
1928 let buffer = workspace
1929 .update_in(&mut cx, |workspace, window, cx| {
1930 workspace.open_path(
1931 ProjectPath {
1932 worktree_id,
1933 path: Path::new(path).into(),
1934 },
1935 None,
1936 false,
1937 window,
1938 cx,
1939 )
1940 })
1941 .await
1942 .unwrap();
1943 opened_editors.push(buffer);
1944 }
1945
1946 let context_store = cx.new(|cx| ContextStore::fake(project.clone(), cx));
1947 let history_store = cx.new(|cx| HistoryStore::new(context_store, cx));
1948 let prompt_capabilities = Rc::new(Cell::new(acp::PromptCapabilities::default()));
1949
1950 let (message_editor, editor) = workspace.update_in(&mut cx, |workspace, window, cx| {
1951 let workspace_handle = cx.weak_entity();
1952 let message_editor = cx.new(|cx| {
1953 MessageEditor::new(
1954 workspace_handle,
1955 project.clone(),
1956 history_store.clone(),
1957 None,
1958 prompt_capabilities.clone(),
1959 Default::default(),
1960 "Test",
1961 EditorMode::AutoHeight {
1962 max_lines: None,
1963 min_lines: 1,
1964 },
1965 window,
1966 cx,
1967 )
1968 });
1969 workspace.active_pane().update(cx, |pane, cx| {
1970 pane.add_item(
1971 Box::new(cx.new(|_| MessageEditorItem(message_editor.clone()))),
1972 true,
1973 true,
1974 None,
1975 window,
1976 cx,
1977 );
1978 });
1979 message_editor.read(cx).focus_handle(cx).focus(window);
1980 let editor = message_editor.read(cx).editor().clone();
1981 (message_editor, editor)
1982 });
1983
1984 cx.simulate_input("Lorem @");
1985
1986 editor.update_in(&mut cx, |editor, window, cx| {
1987 assert_eq!(editor.text(cx), "Lorem @");
1988 assert!(editor.has_visible_completions_menu());
1989
1990 assert_eq!(
1991 current_completion_labels(editor),
1992 &[
1993 "eight.txt dir/b/",
1994 "seven.txt dir/b/",
1995 "six.txt dir/b/",
1996 "five.txt dir/b/",
1997 ]
1998 );
1999 editor.set_text("", window, cx);
2000 });
2001
2002 prompt_capabilities.set(acp::PromptCapabilities {
2003 image: true,
2004 audio: true,
2005 embedded_context: true,
2006 });
2007
2008 cx.simulate_input("Lorem ");
2009
2010 editor.update(&mut cx, |editor, cx| {
2011 assert_eq!(editor.text(cx), "Lorem ");
2012 assert!(!editor.has_visible_completions_menu());
2013 });
2014
2015 cx.simulate_input("@");
2016
2017 editor.update(&mut cx, |editor, cx| {
2018 assert_eq!(editor.text(cx), "Lorem @");
2019 assert!(editor.has_visible_completions_menu());
2020 assert_eq!(
2021 current_completion_labels(editor),
2022 &[
2023 "eight.txt dir/b/",
2024 "seven.txt dir/b/",
2025 "six.txt dir/b/",
2026 "five.txt dir/b/",
2027 "Files & Directories",
2028 "Symbols",
2029 "Threads",
2030 "Fetch"
2031 ]
2032 );
2033 });
2034
2035 // Select and confirm "File"
2036 editor.update_in(&mut cx, |editor, window, cx| {
2037 assert!(editor.has_visible_completions_menu());
2038 editor.context_menu_next(&editor::actions::ContextMenuNext, window, cx);
2039 editor.context_menu_next(&editor::actions::ContextMenuNext, window, cx);
2040 editor.context_menu_next(&editor::actions::ContextMenuNext, window, cx);
2041 editor.context_menu_next(&editor::actions::ContextMenuNext, window, cx);
2042 editor.confirm_completion(&editor::actions::ConfirmCompletion::default(), window, cx);
2043 });
2044
2045 cx.run_until_parked();
2046
2047 editor.update(&mut cx, |editor, cx| {
2048 assert_eq!(editor.text(cx), "Lorem @file ");
2049 assert!(editor.has_visible_completions_menu());
2050 });
2051
2052 cx.simulate_input("one");
2053
2054 editor.update(&mut cx, |editor, cx| {
2055 assert_eq!(editor.text(cx), "Lorem @file one");
2056 assert!(editor.has_visible_completions_menu());
2057 assert_eq!(current_completion_labels(editor), vec!["one.txt dir/a/"]);
2058 });
2059
2060 editor.update_in(&mut cx, |editor, window, cx| {
2061 assert!(editor.has_visible_completions_menu());
2062 editor.confirm_completion(&editor::actions::ConfirmCompletion::default(), window, cx);
2063 });
2064
2065 let url_one = uri!("file:///dir/a/one.txt");
2066 editor.update(&mut cx, |editor, cx| {
2067 let text = editor.text(cx);
2068 assert_eq!(text, format!("Lorem [@one.txt]({url_one}) "));
2069 assert!(!editor.has_visible_completions_menu());
2070 assert_eq!(fold_ranges(editor, cx).len(), 1);
2071 });
2072
2073 let all_prompt_capabilities = acp::PromptCapabilities {
2074 image: true,
2075 audio: true,
2076 embedded_context: true,
2077 };
2078
2079 let contents = message_editor
2080 .update(&mut cx, |message_editor, cx| {
2081 message_editor
2082 .mention_set()
2083 .contents(&all_prompt_capabilities, cx)
2084 })
2085 .await
2086 .unwrap()
2087 .into_values()
2088 .collect::<Vec<_>>();
2089
2090 {
2091 let [(uri, Mention::Text { content, .. })] = contents.as_slice() else {
2092 panic!("Unexpected mentions");
2093 };
2094 pretty_assertions::assert_eq!(content, "1");
2095 pretty_assertions::assert_eq!(uri, &url_one.parse::<MentionUri>().unwrap());
2096 }
2097
2098 let contents = message_editor
2099 .update(&mut cx, |message_editor, cx| {
2100 message_editor
2101 .mention_set()
2102 .contents(&acp::PromptCapabilities::default(), cx)
2103 })
2104 .await
2105 .unwrap()
2106 .into_values()
2107 .collect::<Vec<_>>();
2108
2109 {
2110 let [(uri, Mention::UriOnly)] = contents.as_slice() else {
2111 panic!("Unexpected mentions");
2112 };
2113 pretty_assertions::assert_eq!(uri, &url_one.parse::<MentionUri>().unwrap());
2114 }
2115
2116 cx.simulate_input(" ");
2117
2118 editor.update(&mut cx, |editor, cx| {
2119 let text = editor.text(cx);
2120 assert_eq!(text, format!("Lorem [@one.txt]({url_one}) "));
2121 assert!(!editor.has_visible_completions_menu());
2122 assert_eq!(fold_ranges(editor, cx).len(), 1);
2123 });
2124
2125 cx.simulate_input("Ipsum ");
2126
2127 editor.update(&mut cx, |editor, cx| {
2128 let text = editor.text(cx);
2129 assert_eq!(text, format!("Lorem [@one.txt]({url_one}) Ipsum "),);
2130 assert!(!editor.has_visible_completions_menu());
2131 assert_eq!(fold_ranges(editor, cx).len(), 1);
2132 });
2133
2134 cx.simulate_input("@file ");
2135
2136 editor.update(&mut cx, |editor, cx| {
2137 let text = editor.text(cx);
2138 assert_eq!(text, format!("Lorem [@one.txt]({url_one}) Ipsum @file "),);
2139 assert!(editor.has_visible_completions_menu());
2140 assert_eq!(fold_ranges(editor, cx).len(), 1);
2141 });
2142
2143 editor.update_in(&mut cx, |editor, window, cx| {
2144 editor.confirm_completion(&editor::actions::ConfirmCompletion::default(), window, cx);
2145 });
2146
2147 cx.run_until_parked();
2148
2149 let contents = message_editor
2150 .update(&mut cx, |message_editor, cx| {
2151 message_editor
2152 .mention_set()
2153 .contents(&all_prompt_capabilities, cx)
2154 })
2155 .await
2156 .unwrap()
2157 .into_values()
2158 .collect::<Vec<_>>();
2159
2160 let url_eight = uri!("file:///dir/b/eight.txt");
2161
2162 {
2163 let [_, (uri, Mention::Text { content, .. })] = contents.as_slice() else {
2164 panic!("Unexpected mentions");
2165 };
2166 pretty_assertions::assert_eq!(content, "8");
2167 pretty_assertions::assert_eq!(uri, &url_eight.parse::<MentionUri>().unwrap());
2168 }
2169
2170 editor.update(&mut cx, |editor, cx| {
2171 assert_eq!(
2172 editor.text(cx),
2173 format!("Lorem [@one.txt]({url_one}) Ipsum [@eight.txt]({url_eight}) ")
2174 );
2175 assert!(!editor.has_visible_completions_menu());
2176 assert_eq!(fold_ranges(editor, cx).len(), 2);
2177 });
2178
2179 let plain_text_language = Arc::new(language::Language::new(
2180 language::LanguageConfig {
2181 name: "Plain Text".into(),
2182 matcher: language::LanguageMatcher {
2183 path_suffixes: vec!["txt".to_string()],
2184 ..Default::default()
2185 },
2186 ..Default::default()
2187 },
2188 None,
2189 ));
2190
2191 // Register the language and fake LSP
2192 let language_registry = project.read_with(&cx, |project, _| project.languages().clone());
2193 language_registry.add(plain_text_language);
2194
2195 let mut fake_language_servers = language_registry.register_fake_lsp(
2196 "Plain Text",
2197 language::FakeLspAdapter {
2198 capabilities: lsp::ServerCapabilities {
2199 workspace_symbol_provider: Some(lsp::OneOf::Left(true)),
2200 ..Default::default()
2201 },
2202 ..Default::default()
2203 },
2204 );
2205
2206 // Open the buffer to trigger LSP initialization
2207 let buffer = project
2208 .update(&mut cx, |project, cx| {
2209 project.open_local_buffer(path!("/dir/a/one.txt"), cx)
2210 })
2211 .await
2212 .unwrap();
2213
2214 // Register the buffer with language servers
2215 let _handle = project.update(&mut cx, |project, cx| {
2216 project.register_buffer_with_language_servers(&buffer, cx)
2217 });
2218
2219 cx.run_until_parked();
2220
2221 let fake_language_server = fake_language_servers.next().await.unwrap();
2222 fake_language_server.set_request_handler::<lsp::WorkspaceSymbolRequest, _, _>(
2223 move |_, _| async move {
2224 Ok(Some(lsp::WorkspaceSymbolResponse::Flat(vec![
2225 #[allow(deprecated)]
2226 lsp::SymbolInformation {
2227 name: "MySymbol".into(),
2228 location: lsp::Location {
2229 uri: lsp::Uri::from_file_path(path!("/dir/a/one.txt")).unwrap(),
2230 range: lsp::Range::new(
2231 lsp::Position::new(0, 0),
2232 lsp::Position::new(0, 1),
2233 ),
2234 },
2235 kind: lsp::SymbolKind::CONSTANT,
2236 tags: None,
2237 container_name: None,
2238 deprecated: None,
2239 },
2240 ])))
2241 },
2242 );
2243
2244 cx.simulate_input("@symbol ");
2245
2246 editor.update(&mut cx, |editor, cx| {
2247 assert_eq!(
2248 editor.text(cx),
2249 format!("Lorem [@one.txt]({url_one}) Ipsum [@eight.txt]({url_eight}) @symbol ")
2250 );
2251 assert!(editor.has_visible_completions_menu());
2252 assert_eq!(current_completion_labels(editor), &["MySymbol"]);
2253 });
2254
2255 editor.update_in(&mut cx, |editor, window, cx| {
2256 editor.confirm_completion(&editor::actions::ConfirmCompletion::default(), window, cx);
2257 });
2258
2259 let contents = message_editor
2260 .update(&mut cx, |message_editor, cx| {
2261 message_editor
2262 .mention_set()
2263 .contents(&all_prompt_capabilities, cx)
2264 })
2265 .await
2266 .unwrap()
2267 .into_values()
2268 .collect::<Vec<_>>();
2269
2270 {
2271 let [_, _, (uri, Mention::Text { content, .. })] = contents.as_slice() else {
2272 panic!("Unexpected mentions");
2273 };
2274 pretty_assertions::assert_eq!(content, "1");
2275 pretty_assertions::assert_eq!(
2276 uri,
2277 &format!("{url_one}?symbol=MySymbol#L1:1")
2278 .parse::<MentionUri>()
2279 .unwrap()
2280 );
2281 }
2282
2283 cx.run_until_parked();
2284
2285 editor.read_with(&cx, |editor, cx| {
2286 assert_eq!(
2287 editor.text(cx),
2288 format!("Lorem [@one.txt]({url_one}) Ipsum [@eight.txt]({url_eight}) [@MySymbol]({url_one}?symbol=MySymbol#L1:1) ")
2289 );
2290 });
2291
2292 // Try to mention an "image" file that will fail to load
2293 cx.simulate_input("@file x.png");
2294
2295 editor.update(&mut cx, |editor, cx| {
2296 assert_eq!(
2297 editor.text(cx),
2298 format!("Lorem [@one.txt]({url_one}) Ipsum [@eight.txt]({url_eight}) [@MySymbol]({url_one}?symbol=MySymbol#L1:1) @file x.png")
2299 );
2300 assert!(editor.has_visible_completions_menu());
2301 assert_eq!(current_completion_labels(editor), &["x.png dir/"]);
2302 });
2303
2304 editor.update_in(&mut cx, |editor, window, cx| {
2305 editor.confirm_completion(&editor::actions::ConfirmCompletion::default(), window, cx);
2306 });
2307
2308 // Getting the message contents fails
2309 message_editor
2310 .update(&mut cx, |message_editor, cx| {
2311 message_editor
2312 .mention_set()
2313 .contents(&all_prompt_capabilities, cx)
2314 })
2315 .await
2316 .expect_err("Should fail to load x.png");
2317
2318 cx.run_until_parked();
2319
2320 // Mention was removed
2321 editor.read_with(&cx, |editor, cx| {
2322 assert_eq!(
2323 editor.text(cx),
2324 format!("Lorem [@one.txt]({url_one}) Ipsum [@eight.txt]({url_eight}) [@MySymbol]({url_one}?symbol=MySymbol#L1:1) ")
2325 );
2326 });
2327
2328 // Once more
2329 cx.simulate_input("@file x.png");
2330
2331 editor.update(&mut cx, |editor, cx| {
2332 assert_eq!(
2333 editor.text(cx),
2334 format!("Lorem [@one.txt]({url_one}) Ipsum [@eight.txt]({url_eight}) [@MySymbol]({url_one}?symbol=MySymbol#L1:1) @file x.png")
2335 );
2336 assert!(editor.has_visible_completions_menu());
2337 assert_eq!(current_completion_labels(editor), &["x.png dir/"]);
2338 });
2339
2340 editor.update_in(&mut cx, |editor, window, cx| {
2341 editor.confirm_completion(&editor::actions::ConfirmCompletion::default(), window, cx);
2342 });
2343
2344 // This time don't immediately get the contents, just let the confirmed completion settle
2345 cx.run_until_parked();
2346
2347 // Mention was removed
2348 editor.read_with(&cx, |editor, cx| {
2349 assert_eq!(
2350 editor.text(cx),
2351 format!("Lorem [@one.txt]({url_one}) Ipsum [@eight.txt]({url_eight}) [@MySymbol]({url_one}?symbol=MySymbol#L1:1) ")
2352 );
2353 });
2354
2355 // Now getting the contents succeeds, because the invalid mention was removed
2356 let contents = message_editor
2357 .update(&mut cx, |message_editor, cx| {
2358 message_editor
2359 .mention_set()
2360 .contents(&all_prompt_capabilities, cx)
2361 })
2362 .await
2363 .unwrap();
2364 assert_eq!(contents.len(), 3);
2365 }
2366
2367 fn fold_ranges(editor: &Editor, cx: &mut App) -> Vec<Range<Point>> {
2368 let snapshot = editor.buffer().read(cx).snapshot(cx);
2369 editor.display_map.update(cx, |display_map, cx| {
2370 display_map
2371 .snapshot(cx)
2372 .folds_in_range(0..snapshot.len())
2373 .map(|fold| fold.range.to_point(&snapshot))
2374 .collect()
2375 })
2376 }
2377
2378 fn current_completion_labels(editor: &Editor) -> Vec<String> {
2379 let completions = editor.current_completions().expect("Missing completions");
2380 completions
2381 .into_iter()
2382 .map(|completion| completion.label.text)
2383 .collect::<Vec<_>>()
2384 }
2385
2386 fn current_completion_labels_with_documentation(editor: &Editor) -> Vec<(String, String)> {
2387 let completions = editor.current_completions().expect("Missing completions");
2388 completions
2389 .into_iter()
2390 .map(|completion| {
2391 (
2392 completion.label.text,
2393 completion
2394 .documentation
2395 .map(|d| d.text().to_string())
2396 .unwrap_or_default(),
2397 )
2398 })
2399 .collect::<Vec<_>>()
2400 }
2401}