1use crate::{
2 burn_mode_tooltip::BurnModeTooltip,
3 language_model_selector::{
4 LanguageModelSelector, ToggleModelSelector, language_model_selector,
5 },
6};
7use agent_settings::{AgentSettings, CompletionMode};
8use anyhow::Result;
9use assistant_slash_command::{SlashCommand, SlashCommandOutputSection, SlashCommandWorkingSet};
10use assistant_slash_commands::{
11 DefaultSlashCommand, DocsSlashCommand, DocsSlashCommandArgs, FileSlashCommand,
12 selections_creases,
13};
14use client::{proto, zed_urls};
15use collections::{BTreeSet, HashMap, HashSet, hash_map};
16use editor::{
17 Anchor, Editor, EditorEvent, MenuInlineCompletionsPolicy, MultiBuffer, MultiBufferSnapshot,
18 RowExt, ToOffset as _, ToPoint,
19 actions::{MoveToEndOfLine, Newline, ShowCompletions},
20 display_map::{
21 BlockPlacement, BlockProperties, BlockStyle, Crease, CreaseMetadata, CustomBlockId, FoldId,
22 RenderBlock, ToDisplayPoint,
23 },
24};
25use editor::{FoldPlaceholder, display_map::CreaseId};
26use fs::Fs;
27use futures::FutureExt;
28use gpui::{
29 Action, Animation, AnimationExt, AnyElement, AnyView, App, ClipboardEntry, ClipboardItem,
30 Empty, Entity, EventEmitter, FocusHandle, Focusable, FontWeight, Global, InteractiveElement,
31 IntoElement, ParentElement, Pixels, Render, RenderImage, SharedString, Size,
32 StatefulInteractiveElement, Styled, Subscription, Task, Transformation, WeakEntity, actions,
33 div, img, percentage, point, prelude::*, pulsating_between, size,
34};
35use indexed_docs::IndexedDocsStore;
36use language::{
37 BufferSnapshot, LspAdapterDelegate, ToOffset,
38 language_settings::{SoftWrap, all_language_settings},
39};
40use language_model::{
41 ConfigurationError, LanguageModelExt, LanguageModelImage, LanguageModelProviderTosView,
42 LanguageModelRegistry, Role,
43};
44use multi_buffer::MultiBufferRow;
45use picker::{Picker, popover_menu::PickerPopoverMenu};
46use project::{Project, Worktree};
47use project::{ProjectPath, lsp_store::LocalLspAdapterDelegate};
48use rope::Point;
49use serde::{Deserialize, Serialize};
50use settings::{Settings, SettingsStore, update_settings_file};
51use std::{
52 any::TypeId,
53 cmp,
54 ops::Range,
55 path::{Path, PathBuf},
56 rc::Rc,
57 sync::Arc,
58 time::Duration,
59};
60use text::SelectionGoal;
61use ui::{
62 ButtonLike, Disclosure, ElevationIndex, KeyBinding, PopoverMenuHandle, TintColor, Tooltip,
63 prelude::*,
64};
65use util::{ResultExt, maybe};
66use workspace::{
67 CollaboratorId,
68 searchable::{Direction, SearchableItemHandle},
69};
70use workspace::{
71 Save, Toast, Workspace,
72 item::{self, FollowableItem, Item, ItemHandle},
73 notifications::NotificationId,
74 pane,
75 searchable::{SearchEvent, SearchableItem},
76};
77
78use crate::{slash_command::SlashCommandCompletionProvider, slash_command_picker};
79use assistant_context::{
80 AssistantContext, CacheStatus, Content, ContextEvent, ContextId, InvokedSlashCommandId,
81 InvokedSlashCommandStatus, Message, MessageId, MessageMetadata, MessageStatus,
82 ParsedSlashCommand, PendingSlashCommandStatus, ThoughtProcessOutputSection,
83};
84
85actions!(
86 assistant,
87 [
88 /// Sends the current message to the assistant.
89 Assist,
90 /// Confirms and executes the entered slash command.
91 ConfirmCommand,
92 /// Copies code from the assistant's response to the clipboard.
93 CopyCode,
94 /// Cycles between user and assistant message roles.
95 CycleMessageRole,
96 /// Inserts the selected text into the active editor.
97 InsertIntoEditor,
98 /// Quotes the current selection in the assistant conversation.
99 QuoteSelection,
100 /// Splits the conversation at the current cursor position.
101 Split,
102 ]
103);
104
105/// Inserts files that were dragged and dropped into the assistant conversation.
106#[derive(PartialEq, Clone, Action)]
107#[action(namespace = assistant, no_json, no_register)]
108pub enum InsertDraggedFiles {
109 ProjectPaths(Vec<ProjectPath>),
110 ExternalFiles(Vec<PathBuf>),
111}
112
113#[derive(Copy, Clone, Debug, PartialEq)]
114struct ScrollPosition {
115 offset_before_cursor: gpui::Point<f32>,
116 cursor: Anchor,
117}
118
119type MessageHeader = MessageMetadata;
120
121#[derive(Clone)]
122enum AssistError {
123 PaymentRequired,
124 Message(SharedString),
125}
126
127pub enum ThoughtProcessStatus {
128 Pending,
129 Completed,
130}
131
132pub trait AgentPanelDelegate {
133 fn active_context_editor(
134 &self,
135 workspace: &mut Workspace,
136 window: &mut Window,
137 cx: &mut Context<Workspace>,
138 ) -> Option<Entity<TextThreadEditor>>;
139
140 fn open_saved_context(
141 &self,
142 workspace: &mut Workspace,
143 path: Arc<Path>,
144 window: &mut Window,
145 cx: &mut Context<Workspace>,
146 ) -> Task<Result<()>>;
147
148 fn open_remote_context(
149 &self,
150 workspace: &mut Workspace,
151 context_id: ContextId,
152 window: &mut Window,
153 cx: &mut Context<Workspace>,
154 ) -> Task<Result<Entity<TextThreadEditor>>>;
155
156 fn quote_selection(
157 &self,
158 workspace: &mut Workspace,
159 selection_ranges: Vec<Range<Anchor>>,
160 buffer: Entity<MultiBuffer>,
161 window: &mut Window,
162 cx: &mut Context<Workspace>,
163 );
164}
165
166impl dyn AgentPanelDelegate {
167 /// Returns the global [`AssistantPanelDelegate`], if it exists.
168 pub fn try_global(cx: &App) -> Option<Arc<Self>> {
169 cx.try_global::<GlobalAssistantPanelDelegate>()
170 .map(|global| global.0.clone())
171 }
172
173 /// Sets the global [`AssistantPanelDelegate`].
174 pub fn set_global(delegate: Arc<Self>, cx: &mut App) {
175 cx.set_global(GlobalAssistantPanelDelegate(delegate));
176 }
177}
178
179struct GlobalAssistantPanelDelegate(Arc<dyn AgentPanelDelegate>);
180
181impl Global for GlobalAssistantPanelDelegate {}
182
183pub struct TextThreadEditor {
184 context: Entity<AssistantContext>,
185 fs: Arc<dyn Fs>,
186 slash_commands: Arc<SlashCommandWorkingSet>,
187 workspace: WeakEntity<Workspace>,
188 project: Entity<Project>,
189 lsp_adapter_delegate: Option<Arc<dyn LspAdapterDelegate>>,
190 editor: Entity<Editor>,
191 pending_thought_process: Option<(CreaseId, language::Anchor)>,
192 blocks: HashMap<MessageId, (MessageHeader, CustomBlockId)>,
193 image_blocks: HashSet<CustomBlockId>,
194 scroll_position: Option<ScrollPosition>,
195 remote_id: Option<workspace::ViewId>,
196 pending_slash_command_creases: HashMap<Range<language::Anchor>, CreaseId>,
197 invoked_slash_command_creases: HashMap<InvokedSlashCommandId, CreaseId>,
198 _subscriptions: Vec<Subscription>,
199 last_error: Option<AssistError>,
200 show_accept_terms: bool,
201 pub(crate) slash_menu_handle:
202 PopoverMenuHandle<Picker<slash_command_picker::SlashCommandDelegate>>,
203 // dragged_file_worktrees is used to keep references to worktrees that were added
204 // when the user drag/dropped an external file onto the context editor. Since
205 // the worktree is not part of the project panel, it would be dropped as soon as
206 // the file is opened. In order to keep the worktree alive for the duration of the
207 // context editor, we keep a reference here.
208 dragged_file_worktrees: Vec<Entity<Worktree>>,
209 language_model_selector: Entity<LanguageModelSelector>,
210 language_model_selector_menu_handle: PopoverMenuHandle<LanguageModelSelector>,
211}
212
213const MAX_TAB_TITLE_LEN: usize = 16;
214
215impl TextThreadEditor {
216 pub fn init(cx: &mut App) {
217 workspace::FollowableViewRegistry::register::<TextThreadEditor>(cx);
218
219 cx.observe_new(
220 |workspace: &mut Workspace, _window, _cx: &mut Context<Workspace>| {
221 workspace
222 .register_action(TextThreadEditor::quote_selection)
223 .register_action(TextThreadEditor::insert_selection)
224 .register_action(TextThreadEditor::copy_code)
225 .register_action(TextThreadEditor::handle_insert_dragged_files);
226 },
227 )
228 .detach();
229 }
230
231 pub fn for_context(
232 context: Entity<AssistantContext>,
233 fs: Arc<dyn Fs>,
234 workspace: WeakEntity<Workspace>,
235 project: Entity<Project>,
236 lsp_adapter_delegate: Option<Arc<dyn LspAdapterDelegate>>,
237 window: &mut Window,
238 cx: &mut Context<Self>,
239 ) -> Self {
240 let completion_provider = SlashCommandCompletionProvider::new(
241 context.read(cx).slash_commands().clone(),
242 Some(cx.entity().downgrade()),
243 Some(workspace.clone()),
244 );
245
246 let editor = cx.new(|cx| {
247 let mut editor =
248 Editor::for_buffer(context.read(cx).buffer().clone(), None, window, cx);
249 editor.disable_scrollbars_and_minimap(window, cx);
250 editor.set_soft_wrap_mode(SoftWrap::EditorWidth, cx);
251 editor.set_show_line_numbers(false, cx);
252 editor.set_show_git_diff_gutter(false, cx);
253 editor.set_show_code_actions(false, cx);
254 editor.set_show_runnables(false, cx);
255 editor.set_show_breakpoints(false, cx);
256 editor.set_show_wrap_guides(false, cx);
257 editor.set_show_indent_guides(false, cx);
258 editor.set_completion_provider(Some(Rc::new(completion_provider)));
259 editor.set_menu_inline_completions_policy(MenuInlineCompletionsPolicy::Never);
260 editor.set_collaboration_hub(Box::new(project.clone()));
261
262 let show_edit_predictions = all_language_settings(None, cx)
263 .edit_predictions
264 .enabled_in_text_threads;
265
266 editor.set_show_edit_predictions(Some(show_edit_predictions), window, cx);
267
268 editor
269 });
270
271 let _subscriptions = vec![
272 cx.observe(&context, |_, _, cx| cx.notify()),
273 cx.subscribe_in(&context, window, Self::handle_context_event),
274 cx.subscribe_in(&editor, window, Self::handle_editor_event),
275 cx.subscribe_in(&editor, window, Self::handle_editor_search_event),
276 cx.observe_global_in::<SettingsStore>(window, Self::settings_changed),
277 ];
278
279 let slash_command_sections = context.read(cx).slash_command_output_sections().to_vec();
280 let thought_process_sections = context.read(cx).thought_process_output_sections().to_vec();
281 let slash_commands = context.read(cx).slash_commands().clone();
282 let mut this = Self {
283 context,
284 slash_commands,
285 editor,
286 lsp_adapter_delegate,
287 blocks: Default::default(),
288 image_blocks: Default::default(),
289 scroll_position: None,
290 remote_id: None,
291 pending_thought_process: None,
292 fs: fs.clone(),
293 workspace,
294 project,
295 pending_slash_command_creases: HashMap::default(),
296 invoked_slash_command_creases: HashMap::default(),
297 _subscriptions,
298 last_error: None,
299 show_accept_terms: false,
300 slash_menu_handle: Default::default(),
301 dragged_file_worktrees: Vec::new(),
302 language_model_selector: cx.new(|cx| {
303 language_model_selector(
304 |cx| LanguageModelRegistry::read_global(cx).default_model(),
305 move |model, cx| {
306 update_settings_file::<AgentSettings>(
307 fs.clone(),
308 cx,
309 move |settings, _| settings.set_model(model.clone()),
310 );
311 },
312 window,
313 cx,
314 )
315 }),
316 language_model_selector_menu_handle: PopoverMenuHandle::default(),
317 };
318 this.update_message_headers(cx);
319 this.update_image_blocks(cx);
320 this.insert_slash_command_output_sections(slash_command_sections, false, window, cx);
321 this.insert_thought_process_output_sections(
322 thought_process_sections
323 .into_iter()
324 .map(|section| (section, ThoughtProcessStatus::Completed)),
325 window,
326 cx,
327 );
328 this
329 }
330
331 fn settings_changed(&mut self, window: &mut Window, cx: &mut Context<Self>) {
332 self.editor.update(cx, |editor, cx| {
333 let show_edit_predictions = all_language_settings(None, cx)
334 .edit_predictions
335 .enabled_in_text_threads;
336
337 editor.set_show_edit_predictions(Some(show_edit_predictions), window, cx);
338 });
339 }
340
341 pub fn context(&self) -> &Entity<AssistantContext> {
342 &self.context
343 }
344
345 pub fn editor(&self) -> &Entity<Editor> {
346 &self.editor
347 }
348
349 pub fn insert_default_prompt(&mut self, window: &mut Window, cx: &mut Context<Self>) {
350 let command_name = DefaultSlashCommand.name();
351 self.editor.update(cx, |editor, cx| {
352 editor.insert(&format!("/{command_name}\n\n"), window, cx)
353 });
354 let command = self.context.update(cx, |context, cx| {
355 context.reparse(cx);
356 context.parsed_slash_commands()[0].clone()
357 });
358 self.run_command(
359 command.source_range,
360 &command.name,
361 &command.arguments,
362 false,
363 self.workspace.clone(),
364 window,
365 cx,
366 );
367 }
368
369 fn assist(&mut self, _: &Assist, window: &mut Window, cx: &mut Context<Self>) {
370 if self.sending_disabled(cx) {
371 return;
372 }
373 self.send_to_model(window, cx);
374 }
375
376 fn send_to_model(&mut self, window: &mut Window, cx: &mut Context<Self>) {
377 let provider = LanguageModelRegistry::read_global(cx)
378 .default_model()
379 .map(|default| default.provider);
380 if provider
381 .as_ref()
382 .map_or(false, |provider| provider.must_accept_terms(cx))
383 {
384 self.show_accept_terms = true;
385 cx.notify();
386 return;
387 }
388
389 self.last_error = None;
390
391 if let Some(user_message) = self.context.update(cx, |context, cx| context.assist(cx)) {
392 let new_selection = {
393 let cursor = user_message
394 .start
395 .to_offset(self.context.read(cx).buffer().read(cx));
396 cursor..cursor
397 };
398 self.editor.update(cx, |editor, cx| {
399 editor.change_selections(Default::default(), window, cx, |selections| {
400 selections.select_ranges([new_selection])
401 });
402 });
403 // Avoid scrolling to the new cursor position so the assistant's output is stable.
404 cx.defer_in(window, |this, _, _| this.scroll_position = None);
405 }
406
407 cx.notify();
408 }
409
410 fn cancel(
411 &mut self,
412 _: &editor::actions::Cancel,
413 _window: &mut Window,
414 cx: &mut Context<Self>,
415 ) {
416 self.last_error = None;
417
418 if self
419 .context
420 .update(cx, |context, cx| context.cancel_last_assist(cx))
421 {
422 return;
423 }
424
425 cx.propagate();
426 }
427
428 fn cycle_message_role(
429 &mut self,
430 _: &CycleMessageRole,
431 _window: &mut Window,
432 cx: &mut Context<Self>,
433 ) {
434 let cursors = self.cursors(cx);
435 self.context.update(cx, |context, cx| {
436 let messages = context
437 .messages_for_offsets(cursors, cx)
438 .into_iter()
439 .map(|message| message.id)
440 .collect();
441 context.cycle_message_roles(messages, cx)
442 });
443 }
444
445 fn cursors(&self, cx: &mut App) -> Vec<usize> {
446 let selections = self
447 .editor
448 .update(cx, |editor, cx| editor.selections.all::<usize>(cx));
449 selections
450 .into_iter()
451 .map(|selection| selection.head())
452 .collect()
453 }
454
455 pub fn insert_command(&mut self, name: &str, window: &mut Window, cx: &mut Context<Self>) {
456 if let Some(command) = self.slash_commands.command(name, cx) {
457 self.editor.update(cx, |editor, cx| {
458 editor.transact(window, cx, |editor, window, cx| {
459 editor.change_selections(Default::default(), window, cx, |s| s.try_cancel());
460 let snapshot = editor.buffer().read(cx).snapshot(cx);
461 let newest_cursor = editor.selections.newest::<Point>(cx).head();
462 if newest_cursor.column > 0
463 || snapshot
464 .chars_at(newest_cursor)
465 .next()
466 .map_or(false, |ch| ch != '\n')
467 {
468 editor.move_to_end_of_line(
469 &MoveToEndOfLine {
470 stop_at_soft_wraps: false,
471 },
472 window,
473 cx,
474 );
475 editor.newline(&Newline, window, cx);
476 }
477
478 editor.insert(&format!("/{name}"), window, cx);
479 if command.accepts_arguments() {
480 editor.insert(" ", window, cx);
481 editor.show_completions(&ShowCompletions::default(), window, cx);
482 }
483 });
484 });
485 if !command.requires_argument() {
486 self.confirm_command(&ConfirmCommand, window, cx);
487 }
488 }
489 }
490
491 pub fn confirm_command(
492 &mut self,
493 _: &ConfirmCommand,
494 window: &mut Window,
495 cx: &mut Context<Self>,
496 ) {
497 if self.editor.read(cx).has_visible_completions_menu() {
498 return;
499 }
500
501 let selections = self.editor.read(cx).selections.disjoint_anchors();
502 let mut commands_by_range = HashMap::default();
503 let workspace = self.workspace.clone();
504 self.context.update(cx, |context, cx| {
505 context.reparse(cx);
506 for selection in selections.iter() {
507 if let Some(command) =
508 context.pending_command_for_position(selection.head().text_anchor, cx)
509 {
510 commands_by_range
511 .entry(command.source_range.clone())
512 .or_insert_with(|| command.clone());
513 }
514 }
515 });
516
517 if commands_by_range.is_empty() {
518 cx.propagate();
519 } else {
520 for command in commands_by_range.into_values() {
521 self.run_command(
522 command.source_range,
523 &command.name,
524 &command.arguments,
525 true,
526 workspace.clone(),
527 window,
528 cx,
529 );
530 }
531 cx.stop_propagation();
532 }
533 }
534
535 pub fn run_command(
536 &mut self,
537 command_range: Range<language::Anchor>,
538 name: &str,
539 arguments: &[String],
540 ensure_trailing_newline: bool,
541 workspace: WeakEntity<Workspace>,
542 window: &mut Window,
543 cx: &mut Context<Self>,
544 ) {
545 if let Some(command) = self.slash_commands.command(name, cx) {
546 let context = self.context.read(cx);
547 let sections = context
548 .slash_command_output_sections()
549 .into_iter()
550 .filter(|section| section.is_valid(context.buffer().read(cx)))
551 .cloned()
552 .collect::<Vec<_>>();
553 let snapshot = context.buffer().read(cx).snapshot();
554 let output = command.run(
555 arguments,
556 §ions,
557 snapshot,
558 workspace,
559 self.lsp_adapter_delegate.clone(),
560 window,
561 cx,
562 );
563 self.context.update(cx, |context, cx| {
564 context.insert_command_output(
565 command_range,
566 name,
567 output,
568 ensure_trailing_newline,
569 cx,
570 )
571 });
572 }
573 }
574
575 fn handle_context_event(
576 &mut self,
577 _: &Entity<AssistantContext>,
578 event: &ContextEvent,
579 window: &mut Window,
580 cx: &mut Context<Self>,
581 ) {
582 let context_editor = cx.entity().downgrade();
583
584 match event {
585 ContextEvent::MessagesEdited => {
586 self.update_message_headers(cx);
587 self.update_image_blocks(cx);
588 self.context.update(cx, |context, cx| {
589 context.save(Some(Duration::from_millis(500)), self.fs.clone(), cx);
590 });
591 }
592 ContextEvent::SummaryChanged => {
593 cx.emit(EditorEvent::TitleChanged);
594 self.context.update(cx, |context, cx| {
595 context.save(Some(Duration::from_millis(500)), self.fs.clone(), cx);
596 });
597 }
598 ContextEvent::SummaryGenerated => {}
599 ContextEvent::PathChanged { .. } => {}
600 ContextEvent::StartedThoughtProcess(range) => {
601 let creases = self.insert_thought_process_output_sections(
602 [(
603 ThoughtProcessOutputSection {
604 range: range.clone(),
605 },
606 ThoughtProcessStatus::Pending,
607 )],
608 window,
609 cx,
610 );
611 self.pending_thought_process = Some((creases[0], range.start));
612 }
613 ContextEvent::EndedThoughtProcess(end) => {
614 if let Some((crease_id, start)) = self.pending_thought_process.take() {
615 self.editor.update(cx, |editor, cx| {
616 let multi_buffer_snapshot = editor.buffer().read(cx).snapshot(cx);
617 let (excerpt_id, _, _) = multi_buffer_snapshot.as_singleton().unwrap();
618 let start_anchor = multi_buffer_snapshot
619 .anchor_in_excerpt(*excerpt_id, start)
620 .unwrap();
621
622 editor.display_map.update(cx, |display_map, cx| {
623 display_map.unfold_intersecting(
624 vec![start_anchor..start_anchor],
625 true,
626 cx,
627 );
628 });
629 editor.remove_creases(vec![crease_id], cx);
630 });
631 self.insert_thought_process_output_sections(
632 [(
633 ThoughtProcessOutputSection { range: start..*end },
634 ThoughtProcessStatus::Completed,
635 )],
636 window,
637 cx,
638 );
639 }
640 }
641 ContextEvent::StreamedCompletion => {
642 self.editor.update(cx, |editor, cx| {
643 if let Some(scroll_position) = self.scroll_position {
644 let snapshot = editor.snapshot(window, cx);
645 let cursor_point = scroll_position.cursor.to_display_point(&snapshot);
646 let scroll_top =
647 cursor_point.row().as_f32() - scroll_position.offset_before_cursor.y;
648 editor.set_scroll_position(
649 point(scroll_position.offset_before_cursor.x, scroll_top),
650 window,
651 cx,
652 );
653 }
654 });
655 }
656 ContextEvent::ParsedSlashCommandsUpdated { removed, updated } => {
657 self.editor.update(cx, |editor, cx| {
658 let buffer = editor.buffer().read(cx).snapshot(cx);
659 let (&excerpt_id, _, _) = buffer.as_singleton().unwrap();
660
661 editor.remove_creases(
662 removed
663 .iter()
664 .filter_map(|range| self.pending_slash_command_creases.remove(range)),
665 cx,
666 );
667
668 let crease_ids = editor.insert_creases(
669 updated.iter().map(|command| {
670 let workspace = self.workspace.clone();
671 let confirm_command = Arc::new({
672 let context_editor = context_editor.clone();
673 let command = command.clone();
674 move |window: &mut Window, cx: &mut App| {
675 context_editor
676 .update(cx, |context_editor, cx| {
677 context_editor.run_command(
678 command.source_range.clone(),
679 &command.name,
680 &command.arguments,
681 false,
682 workspace.clone(),
683 window,
684 cx,
685 );
686 })
687 .ok();
688 }
689 });
690 let placeholder = FoldPlaceholder {
691 render: Arc::new(move |_, _, _| Empty.into_any()),
692 ..Default::default()
693 };
694 let render_toggle = {
695 let confirm_command = confirm_command.clone();
696 let command = command.clone();
697 move |row, _, _, _window: &mut Window, _cx: &mut App| {
698 render_pending_slash_command_gutter_decoration(
699 row,
700 &command.status,
701 confirm_command.clone(),
702 )
703 }
704 };
705 let render_trailer = {
706 let command = command.clone();
707 move |row, _unfold, _window: &mut Window, cx: &mut App| {
708 // TODO: In the future we should investigate how we can expose
709 // this as a hook on the `SlashCommand` trait so that we don't
710 // need to special-case it here.
711 if command.name == DocsSlashCommand::NAME {
712 return render_docs_slash_command_trailer(
713 row,
714 command.clone(),
715 cx,
716 );
717 }
718
719 Empty.into_any()
720 }
721 };
722
723 let start = buffer
724 .anchor_in_excerpt(excerpt_id, command.source_range.start)
725 .unwrap();
726 let end = buffer
727 .anchor_in_excerpt(excerpt_id, command.source_range.end)
728 .unwrap();
729 Crease::inline(start..end, placeholder, render_toggle, render_trailer)
730 }),
731 cx,
732 );
733
734 self.pending_slash_command_creases.extend(
735 updated
736 .iter()
737 .map(|command| command.source_range.clone())
738 .zip(crease_ids),
739 );
740 })
741 }
742 ContextEvent::InvokedSlashCommandChanged { command_id } => {
743 self.update_invoked_slash_command(*command_id, window, cx);
744 }
745 ContextEvent::SlashCommandOutputSectionAdded { section } => {
746 self.insert_slash_command_output_sections([section.clone()], false, window, cx);
747 }
748 ContextEvent::Operation(_) => {}
749 ContextEvent::ShowAssistError(error_message) => {
750 self.last_error = Some(AssistError::Message(error_message.clone()));
751 }
752 ContextEvent::ShowPaymentRequiredError => {
753 self.last_error = Some(AssistError::PaymentRequired);
754 }
755 }
756 }
757
758 fn update_invoked_slash_command(
759 &mut self,
760 command_id: InvokedSlashCommandId,
761 window: &mut Window,
762 cx: &mut Context<Self>,
763 ) {
764 if let Some(invoked_slash_command) =
765 self.context.read(cx).invoked_slash_command(&command_id)
766 {
767 if let InvokedSlashCommandStatus::Finished = invoked_slash_command.status {
768 let run_commands_in_ranges = invoked_slash_command
769 .run_commands_in_ranges
770 .iter()
771 .cloned()
772 .collect::<Vec<_>>();
773 for range in run_commands_in_ranges {
774 let commands = self.context.update(cx, |context, cx| {
775 context.reparse(cx);
776 context
777 .pending_commands_for_range(range.clone(), cx)
778 .to_vec()
779 });
780
781 for command in commands {
782 self.run_command(
783 command.source_range,
784 &command.name,
785 &command.arguments,
786 false,
787 self.workspace.clone(),
788 window,
789 cx,
790 );
791 }
792 }
793 }
794 }
795
796 self.editor.update(cx, |editor, cx| {
797 if let Some(invoked_slash_command) =
798 self.context.read(cx).invoked_slash_command(&command_id)
799 {
800 if let InvokedSlashCommandStatus::Finished = invoked_slash_command.status {
801 let buffer = editor.buffer().read(cx).snapshot(cx);
802 let (&excerpt_id, _buffer_id, _buffer_snapshot) =
803 buffer.as_singleton().unwrap();
804
805 let start = buffer
806 .anchor_in_excerpt(excerpt_id, invoked_slash_command.range.start)
807 .unwrap();
808 let end = buffer
809 .anchor_in_excerpt(excerpt_id, invoked_slash_command.range.end)
810 .unwrap();
811 editor.remove_folds_with_type(
812 &[start..end],
813 TypeId::of::<PendingSlashCommand>(),
814 false,
815 cx,
816 );
817
818 editor.remove_creases(
819 HashSet::from_iter(self.invoked_slash_command_creases.remove(&command_id)),
820 cx,
821 );
822 } else if let hash_map::Entry::Vacant(entry) =
823 self.invoked_slash_command_creases.entry(command_id)
824 {
825 let buffer = editor.buffer().read(cx).snapshot(cx);
826 let (&excerpt_id, _buffer_id, _buffer_snapshot) =
827 buffer.as_singleton().unwrap();
828 let context = self.context.downgrade();
829 let crease_start = buffer
830 .anchor_in_excerpt(excerpt_id, invoked_slash_command.range.start)
831 .unwrap();
832 let crease_end = buffer
833 .anchor_in_excerpt(excerpt_id, invoked_slash_command.range.end)
834 .unwrap();
835 let crease = Crease::inline(
836 crease_start..crease_end,
837 invoked_slash_command_fold_placeholder(command_id, context),
838 fold_toggle("invoked-slash-command"),
839 |_row, _folded, _window, _cx| Empty.into_any(),
840 );
841 let crease_ids = editor.insert_creases([crease.clone()], cx);
842 editor.fold_creases(vec![crease], false, window, cx);
843 entry.insert(crease_ids[0]);
844 } else {
845 cx.notify()
846 }
847 } else {
848 editor.remove_creases(
849 HashSet::from_iter(self.invoked_slash_command_creases.remove(&command_id)),
850 cx,
851 );
852 cx.notify();
853 };
854 });
855 }
856
857 fn insert_thought_process_output_sections(
858 &mut self,
859 sections: impl IntoIterator<
860 Item = (
861 ThoughtProcessOutputSection<language::Anchor>,
862 ThoughtProcessStatus,
863 ),
864 >,
865 window: &mut Window,
866 cx: &mut Context<Self>,
867 ) -> Vec<CreaseId> {
868 self.editor.update(cx, |editor, cx| {
869 let buffer = editor.buffer().read(cx).snapshot(cx);
870 let excerpt_id = *buffer.as_singleton().unwrap().0;
871 let mut buffer_rows_to_fold = BTreeSet::new();
872 let mut creases = Vec::new();
873 for (section, status) in sections {
874 let start = buffer
875 .anchor_in_excerpt(excerpt_id, section.range.start)
876 .unwrap();
877 let end = buffer
878 .anchor_in_excerpt(excerpt_id, section.range.end)
879 .unwrap();
880 let buffer_row = MultiBufferRow(start.to_point(&buffer).row);
881 buffer_rows_to_fold.insert(buffer_row);
882 creases.push(
883 Crease::inline(
884 start..end,
885 FoldPlaceholder {
886 render: render_thought_process_fold_icon_button(
887 cx.entity().downgrade(),
888 status,
889 ),
890 merge_adjacent: false,
891 ..Default::default()
892 },
893 render_slash_command_output_toggle,
894 |_, _, _, _| Empty.into_any_element(),
895 )
896 .with_metadata(CreaseMetadata {
897 icon_path: SharedString::from(IconName::Ai.path()),
898 label: "Thinking Process".into(),
899 }),
900 );
901 }
902
903 let creases = editor.insert_creases(creases, cx);
904
905 for buffer_row in buffer_rows_to_fold.into_iter().rev() {
906 editor.fold_at(buffer_row, window, cx);
907 }
908
909 creases
910 })
911 }
912
913 fn insert_slash_command_output_sections(
914 &mut self,
915 sections: impl IntoIterator<Item = SlashCommandOutputSection<language::Anchor>>,
916 expand_result: bool,
917 window: &mut Window,
918 cx: &mut Context<Self>,
919 ) {
920 self.editor.update(cx, |editor, cx| {
921 let buffer = editor.buffer().read(cx).snapshot(cx);
922 let excerpt_id = *buffer.as_singleton().unwrap().0;
923 let mut buffer_rows_to_fold = BTreeSet::new();
924 let mut creases = Vec::new();
925 for section in sections {
926 let start = buffer
927 .anchor_in_excerpt(excerpt_id, section.range.start)
928 .unwrap();
929 let end = buffer
930 .anchor_in_excerpt(excerpt_id, section.range.end)
931 .unwrap();
932 let buffer_row = MultiBufferRow(start.to_point(&buffer).row);
933 buffer_rows_to_fold.insert(buffer_row);
934 creases.push(
935 Crease::inline(
936 start..end,
937 FoldPlaceholder {
938 render: render_fold_icon_button(
939 cx.entity().downgrade(),
940 section.icon.path().into(),
941 section.label.clone(),
942 ),
943 merge_adjacent: false,
944 ..Default::default()
945 },
946 render_slash_command_output_toggle,
947 |_, _, _, _| Empty.into_any_element(),
948 )
949 .with_metadata(CreaseMetadata {
950 icon_path: section.icon.path().into(),
951 label: section.label,
952 }),
953 );
954 }
955
956 editor.insert_creases(creases, cx);
957
958 if expand_result {
959 buffer_rows_to_fold.clear();
960 }
961 for buffer_row in buffer_rows_to_fold.into_iter().rev() {
962 editor.fold_at(buffer_row, window, cx);
963 }
964 });
965 }
966
967 fn handle_editor_event(
968 &mut self,
969 _: &Entity<Editor>,
970 event: &EditorEvent,
971 window: &mut Window,
972 cx: &mut Context<Self>,
973 ) {
974 match event {
975 EditorEvent::ScrollPositionChanged { autoscroll, .. } => {
976 let cursor_scroll_position = self.cursor_scroll_position(window, cx);
977 if *autoscroll {
978 self.scroll_position = cursor_scroll_position;
979 } else if self.scroll_position != cursor_scroll_position {
980 self.scroll_position = None;
981 }
982 }
983 EditorEvent::SelectionsChanged { .. } => {
984 self.scroll_position = self.cursor_scroll_position(window, cx);
985 }
986 _ => {}
987 }
988 cx.emit(event.clone());
989 }
990
991 fn handle_editor_search_event(
992 &mut self,
993 _: &Entity<Editor>,
994 event: &SearchEvent,
995 _window: &mut Window,
996 cx: &mut Context<Self>,
997 ) {
998 cx.emit(event.clone());
999 }
1000
1001 fn cursor_scroll_position(
1002 &self,
1003 window: &mut Window,
1004 cx: &mut Context<Self>,
1005 ) -> Option<ScrollPosition> {
1006 self.editor.update(cx, |editor, cx| {
1007 let snapshot = editor.snapshot(window, cx);
1008 let cursor = editor.selections.newest_anchor().head();
1009 let cursor_row = cursor
1010 .to_display_point(&snapshot.display_snapshot)
1011 .row()
1012 .as_f32();
1013 let scroll_position = editor
1014 .scroll_manager
1015 .anchor()
1016 .scroll_position(&snapshot.display_snapshot);
1017
1018 let scroll_bottom = scroll_position.y + editor.visible_line_count().unwrap_or(0.);
1019 if (scroll_position.y..scroll_bottom).contains(&cursor_row) {
1020 Some(ScrollPosition {
1021 cursor,
1022 offset_before_cursor: point(scroll_position.x, cursor_row - scroll_position.y),
1023 })
1024 } else {
1025 None
1026 }
1027 })
1028 }
1029
1030 fn esc_kbd(cx: &App) -> Div {
1031 let colors = cx.theme().colors().clone();
1032
1033 h_flex()
1034 .items_center()
1035 .gap_1()
1036 .font(theme::ThemeSettings::get_global(cx).buffer_font.clone())
1037 .text_size(TextSize::XSmall.rems(cx))
1038 .text_color(colors.text_muted)
1039 .child("Press")
1040 .child(
1041 h_flex()
1042 .rounded_sm()
1043 .px_1()
1044 .mr_0p5()
1045 .border_1()
1046 .border_color(colors.border_variant.alpha(0.6))
1047 .bg(colors.element_background.alpha(0.6))
1048 .child("esc"),
1049 )
1050 .child("to cancel")
1051 }
1052
1053 fn update_message_headers(&mut self, cx: &mut Context<Self>) {
1054 self.editor.update(cx, |editor, cx| {
1055 let buffer = editor.buffer().read(cx).snapshot(cx);
1056
1057 let excerpt_id = *buffer.as_singleton().unwrap().0;
1058 let mut old_blocks = std::mem::take(&mut self.blocks);
1059 let mut blocks_to_remove: HashMap<_, _> = old_blocks
1060 .iter()
1061 .map(|(message_id, (_, block_id))| (*message_id, *block_id))
1062 .collect();
1063 let mut blocks_to_replace: HashMap<_, RenderBlock> = Default::default();
1064
1065 let render_block = |message: MessageMetadata| -> RenderBlock {
1066 Arc::new({
1067 let context = self.context.clone();
1068
1069 move |cx| {
1070 let message_id = MessageId(message.timestamp);
1071 let llm_loading = message.role == Role::Assistant
1072 && message.status == MessageStatus::Pending;
1073
1074 let (label, spinner, note) = match message.role {
1075 Role::User => (
1076 Label::new("You").color(Color::Default).into_any_element(),
1077 None,
1078 None,
1079 ),
1080 Role::Assistant => {
1081 let base_label = Label::new("Agent").color(Color::Info);
1082 let mut spinner = None;
1083 let mut note = None;
1084 let animated_label = if llm_loading {
1085 base_label
1086 .with_animation(
1087 "pulsating-label",
1088 Animation::new(Duration::from_secs(2))
1089 .repeat()
1090 .with_easing(pulsating_between(0.4, 0.8)),
1091 |label, delta| label.alpha(delta),
1092 )
1093 .into_any_element()
1094 } else {
1095 base_label.into_any_element()
1096 };
1097 if llm_loading {
1098 spinner = Some(
1099 Icon::new(IconName::ArrowCircle)
1100 .size(IconSize::XSmall)
1101 .color(Color::Info)
1102 .with_animation(
1103 "arrow-circle",
1104 Animation::new(Duration::from_secs(2)).repeat(),
1105 |icon, delta| {
1106 icon.transform(Transformation::rotate(
1107 percentage(delta),
1108 ))
1109 },
1110 )
1111 .into_any_element(),
1112 );
1113 note = Some(Self::esc_kbd(cx).into_any_element());
1114 }
1115 (animated_label, spinner, note)
1116 }
1117 Role::System => (
1118 Label::new("System")
1119 .color(Color::Warning)
1120 .into_any_element(),
1121 None,
1122 None,
1123 ),
1124 };
1125
1126 let sender = h_flex()
1127 .items_center()
1128 .gap_2p5()
1129 .child(
1130 ButtonLike::new("role")
1131 .style(ButtonStyle::Filled)
1132 .child(
1133 h_flex()
1134 .items_center()
1135 .gap_1p5()
1136 .child(label)
1137 .children(spinner),
1138 )
1139 .tooltip(|window, cx| {
1140 Tooltip::with_meta(
1141 "Toggle message role",
1142 None,
1143 "Available roles: You (User), Agent, System",
1144 window,
1145 cx,
1146 )
1147 })
1148 .on_click({
1149 let context = context.clone();
1150 move |_, _window, cx| {
1151 context.update(cx, |context, cx| {
1152 context.cycle_message_roles(
1153 HashSet::from_iter(Some(message_id)),
1154 cx,
1155 )
1156 })
1157 }
1158 }),
1159 )
1160 .children(note);
1161
1162 h_flex()
1163 .id(("message_header", message_id.as_u64()))
1164 .pl(cx.margins.gutter.full_width())
1165 .h_11()
1166 .w_full()
1167 .relative()
1168 .gap_1p5()
1169 .child(sender)
1170 .children(match &message.cache {
1171 Some(cache) if cache.is_final_anchor => match cache.status {
1172 CacheStatus::Cached => Some(
1173 div()
1174 .id("cached")
1175 .child(
1176 Icon::new(IconName::DatabaseZap)
1177 .size(IconSize::XSmall)
1178 .color(Color::Hint),
1179 )
1180 .tooltip(|window, cx| {
1181 Tooltip::with_meta(
1182 "Context Cached",
1183 None,
1184 "Large messages cached to optimize performance",
1185 window,
1186 cx,
1187 )
1188 })
1189 .into_any_element(),
1190 ),
1191 CacheStatus::Pending => Some(
1192 div()
1193 .child(
1194 Icon::new(IconName::Ellipsis)
1195 .size(IconSize::XSmall)
1196 .color(Color::Hint),
1197 )
1198 .into_any_element(),
1199 ),
1200 },
1201 _ => None,
1202 })
1203 .children(match &message.status {
1204 MessageStatus::Error(error) => Some(
1205 Button::new("show-error", "Error")
1206 .color(Color::Error)
1207 .selected_label_color(Color::Error)
1208 .selected_icon_color(Color::Error)
1209 .icon(IconName::XCircle)
1210 .icon_color(Color::Error)
1211 .icon_size(IconSize::XSmall)
1212 .icon_position(IconPosition::Start)
1213 .tooltip(Tooltip::text("View Details"))
1214 .on_click({
1215 let context = context.clone();
1216 let error = error.clone();
1217 move |_, _window, cx| {
1218 context.update(cx, |_, cx| {
1219 cx.emit(ContextEvent::ShowAssistError(
1220 error.clone(),
1221 ));
1222 });
1223 }
1224 })
1225 .into_any_element(),
1226 ),
1227 MessageStatus::Canceled => Some(
1228 h_flex()
1229 .gap_1()
1230 .items_center()
1231 .child(
1232 Icon::new(IconName::XCircle)
1233 .color(Color::Disabled)
1234 .size(IconSize::XSmall),
1235 )
1236 .child(
1237 Label::new("Canceled")
1238 .size(LabelSize::Small)
1239 .color(Color::Disabled),
1240 )
1241 .into_any_element(),
1242 ),
1243 _ => None,
1244 })
1245 .into_any_element()
1246 }
1247 })
1248 };
1249 let create_block_properties = |message: &Message| BlockProperties {
1250 height: Some(2),
1251 style: BlockStyle::Sticky,
1252 placement: BlockPlacement::Above(
1253 buffer
1254 .anchor_in_excerpt(excerpt_id, message.anchor_range.start)
1255 .unwrap(),
1256 ),
1257 priority: usize::MAX,
1258 render: render_block(MessageMetadata::from(message)),
1259 render_in_minimap: false,
1260 };
1261 let mut new_blocks = vec![];
1262 let mut block_index_to_message = vec![];
1263 for message in self.context.read(cx).messages(cx) {
1264 if let Some(_) = blocks_to_remove.remove(&message.id) {
1265 // This is an old message that we might modify.
1266 let Some((meta, block_id)) = old_blocks.get_mut(&message.id) else {
1267 debug_assert!(
1268 false,
1269 "old_blocks should contain a message_id we've just removed."
1270 );
1271 continue;
1272 };
1273 // Should we modify it?
1274 let message_meta = MessageMetadata::from(&message);
1275 if meta != &message_meta {
1276 blocks_to_replace.insert(*block_id, render_block(message_meta.clone()));
1277 *meta = message_meta;
1278 }
1279 } else {
1280 // This is a new message.
1281 new_blocks.push(create_block_properties(&message));
1282 block_index_to_message.push((message.id, MessageMetadata::from(&message)));
1283 }
1284 }
1285 editor.replace_blocks(blocks_to_replace, None, cx);
1286 editor.remove_blocks(blocks_to_remove.into_values().collect(), None, cx);
1287
1288 let ids = editor.insert_blocks(new_blocks, None, cx);
1289 old_blocks.extend(ids.into_iter().zip(block_index_to_message).map(
1290 |(block_id, (message_id, message_meta))| (message_id, (message_meta, block_id)),
1291 ));
1292 self.blocks = old_blocks;
1293 });
1294 }
1295
1296 /// Returns either the selected text, or the content of the Markdown code
1297 /// block surrounding the cursor.
1298 fn get_selection_or_code_block(
1299 context_editor_view: &Entity<TextThreadEditor>,
1300 cx: &mut Context<Workspace>,
1301 ) -> Option<(String, bool)> {
1302 const CODE_FENCE_DELIMITER: &'static str = "```";
1303
1304 let context_editor = context_editor_view.read(cx).editor.clone();
1305 context_editor.update(cx, |context_editor, cx| {
1306 if context_editor.selections.newest::<Point>(cx).is_empty() {
1307 let snapshot = context_editor.buffer().read(cx).snapshot(cx);
1308 let (_, _, snapshot) = snapshot.as_singleton()?;
1309
1310 let head = context_editor.selections.newest::<Point>(cx).head();
1311 let offset = snapshot.point_to_offset(head);
1312
1313 let surrounding_code_block_range = find_surrounding_code_block(snapshot, offset)?;
1314 let mut text = snapshot
1315 .text_for_range(surrounding_code_block_range)
1316 .collect::<String>();
1317
1318 // If there is no newline trailing the closing three-backticks, then
1319 // tree-sitter-md extends the range of the content node to include
1320 // the backticks.
1321 if text.ends_with(CODE_FENCE_DELIMITER) {
1322 text.drain((text.len() - CODE_FENCE_DELIMITER.len())..);
1323 }
1324
1325 (!text.is_empty()).then_some((text, true))
1326 } else {
1327 let selection = context_editor.selections.newest_adjusted(cx);
1328 let buffer = context_editor.buffer().read(cx).snapshot(cx);
1329 let selected_text = buffer.text_for_range(selection.range()).collect::<String>();
1330
1331 (!selected_text.is_empty()).then_some((selected_text, false))
1332 }
1333 })
1334 }
1335
1336 pub fn insert_selection(
1337 workspace: &mut Workspace,
1338 _: &InsertIntoEditor,
1339 window: &mut Window,
1340 cx: &mut Context<Workspace>,
1341 ) {
1342 let Some(agent_panel_delegate) = <dyn AgentPanelDelegate>::try_global(cx) else {
1343 return;
1344 };
1345 let Some(context_editor_view) =
1346 agent_panel_delegate.active_context_editor(workspace, window, cx)
1347 else {
1348 return;
1349 };
1350 let Some(active_editor_view) = workspace
1351 .active_item(cx)
1352 .and_then(|item| item.act_as::<Editor>(cx))
1353 else {
1354 return;
1355 };
1356
1357 if let Some((text, _)) = Self::get_selection_or_code_block(&context_editor_view, cx) {
1358 active_editor_view.update(cx, |editor, cx| {
1359 editor.insert(&text, window, cx);
1360 editor.focus_handle(cx).focus(window);
1361 })
1362 }
1363 }
1364
1365 pub fn copy_code(
1366 workspace: &mut Workspace,
1367 _: &CopyCode,
1368 window: &mut Window,
1369 cx: &mut Context<Workspace>,
1370 ) {
1371 let result = maybe!({
1372 let agent_panel_delegate = <dyn AgentPanelDelegate>::try_global(cx)?;
1373 let context_editor_view =
1374 agent_panel_delegate.active_context_editor(workspace, window, cx)?;
1375 Self::get_selection_or_code_block(&context_editor_view, cx)
1376 });
1377 let Some((text, is_code_block)) = result else {
1378 return;
1379 };
1380
1381 cx.write_to_clipboard(ClipboardItem::new_string(text));
1382
1383 struct CopyToClipboardToast;
1384 workspace.show_toast(
1385 Toast::new(
1386 NotificationId::unique::<CopyToClipboardToast>(),
1387 format!(
1388 "{} copied to clipboard.",
1389 if is_code_block {
1390 "Code block"
1391 } else {
1392 "Selection"
1393 }
1394 ),
1395 )
1396 .autohide(),
1397 cx,
1398 );
1399 }
1400
1401 pub fn handle_insert_dragged_files(
1402 workspace: &mut Workspace,
1403 action: &InsertDraggedFiles,
1404 window: &mut Window,
1405 cx: &mut Context<Workspace>,
1406 ) {
1407 let Some(agent_panel_delegate) = <dyn AgentPanelDelegate>::try_global(cx) else {
1408 return;
1409 };
1410 let Some(context_editor_view) =
1411 agent_panel_delegate.active_context_editor(workspace, window, cx)
1412 else {
1413 return;
1414 };
1415
1416 let project = context_editor_view.read(cx).project.clone();
1417
1418 let paths = match action {
1419 InsertDraggedFiles::ProjectPaths(paths) => Task::ready((paths.clone(), vec![])),
1420 InsertDraggedFiles::ExternalFiles(paths) => {
1421 let tasks = paths
1422 .clone()
1423 .into_iter()
1424 .map(|path| Workspace::project_path_for_path(project.clone(), &path, false, cx))
1425 .collect::<Vec<_>>();
1426
1427 cx.background_spawn(async move {
1428 let mut paths = vec![];
1429 let mut worktrees = vec![];
1430
1431 let opened_paths = futures::future::join_all(tasks).await;
1432
1433 for entry in opened_paths {
1434 if let Some((worktree, project_path)) = entry.log_err() {
1435 worktrees.push(worktree);
1436 paths.push(project_path);
1437 }
1438 }
1439
1440 (paths, worktrees)
1441 })
1442 }
1443 };
1444
1445 context_editor_view.update(cx, |_, cx| {
1446 cx.spawn_in(window, async move |this, cx| {
1447 let (paths, dragged_file_worktrees) = paths.await;
1448 this.update_in(cx, |this, window, cx| {
1449 this.insert_dragged_files(paths, dragged_file_worktrees, window, cx);
1450 })
1451 .ok();
1452 })
1453 .detach();
1454 })
1455 }
1456
1457 pub fn insert_dragged_files(
1458 &mut self,
1459 opened_paths: Vec<ProjectPath>,
1460 added_worktrees: Vec<Entity<Worktree>>,
1461 window: &mut Window,
1462 cx: &mut Context<Self>,
1463 ) {
1464 let mut file_slash_command_args = vec![];
1465 for project_path in opened_paths.into_iter() {
1466 let Some(worktree) = self
1467 .project
1468 .read(cx)
1469 .worktree_for_id(project_path.worktree_id, cx)
1470 else {
1471 continue;
1472 };
1473 let worktree_root_name = worktree.read(cx).root_name().to_string();
1474 let mut full_path = PathBuf::from(worktree_root_name.clone());
1475 full_path.push(&project_path.path);
1476 file_slash_command_args.push(full_path.to_string_lossy().to_string());
1477 }
1478
1479 let cmd_name = FileSlashCommand.name();
1480
1481 let file_argument = file_slash_command_args.join(" ");
1482
1483 self.editor.update(cx, |editor, cx| {
1484 editor.insert("\n", window, cx);
1485 editor.insert(&format!("/{} {}", cmd_name, file_argument), window, cx);
1486 });
1487 self.confirm_command(&ConfirmCommand, window, cx);
1488 self.dragged_file_worktrees.extend(added_worktrees);
1489 }
1490
1491 pub fn quote_selection(
1492 workspace: &mut Workspace,
1493 _: &QuoteSelection,
1494 window: &mut Window,
1495 cx: &mut Context<Workspace>,
1496 ) {
1497 let Some(agent_panel_delegate) = <dyn AgentPanelDelegate>::try_global(cx) else {
1498 return;
1499 };
1500
1501 let Some((selections, buffer)) = maybe!({
1502 let editor = workspace
1503 .active_item(cx)
1504 .and_then(|item| item.act_as::<Editor>(cx))?;
1505
1506 let buffer = editor.read(cx).buffer().clone();
1507 let snapshot = buffer.read(cx).snapshot(cx);
1508 let selections = editor.update(cx, |editor, cx| {
1509 editor
1510 .selections
1511 .all_adjusted(cx)
1512 .into_iter()
1513 .filter_map(|s| {
1514 (!s.is_empty())
1515 .then(|| snapshot.anchor_after(s.start)..snapshot.anchor_before(s.end))
1516 })
1517 .collect::<Vec<_>>()
1518 });
1519 Some((selections, buffer))
1520 }) else {
1521 return;
1522 };
1523
1524 if selections.is_empty() {
1525 return;
1526 }
1527
1528 agent_panel_delegate.quote_selection(workspace, selections, buffer, window, cx);
1529 }
1530
1531 pub fn quote_ranges(
1532 &mut self,
1533 ranges: Vec<Range<Point>>,
1534 snapshot: MultiBufferSnapshot,
1535 window: &mut Window,
1536 cx: &mut Context<Self>,
1537 ) {
1538 let creases = selections_creases(ranges, snapshot, cx);
1539
1540 self.editor.update(cx, |editor, cx| {
1541 editor.insert("\n", window, cx);
1542 for (text, crease_title) in creases {
1543 let point = editor.selections.newest::<Point>(cx).head();
1544 let start_row = MultiBufferRow(point.row);
1545
1546 editor.insert(&text, window, cx);
1547
1548 let snapshot = editor.buffer().read(cx).snapshot(cx);
1549 let anchor_before = snapshot.anchor_after(point);
1550 let anchor_after = editor
1551 .selections
1552 .newest_anchor()
1553 .head()
1554 .bias_left(&snapshot);
1555
1556 editor.insert("\n", window, cx);
1557
1558 let fold_placeholder =
1559 quote_selection_fold_placeholder(crease_title, cx.entity().downgrade());
1560 let crease = Crease::inline(
1561 anchor_before..anchor_after,
1562 fold_placeholder,
1563 render_quote_selection_output_toggle,
1564 |_, _, _, _| Empty.into_any(),
1565 );
1566 editor.insert_creases(vec![crease], cx);
1567 editor.fold_at(start_row, window, cx);
1568 }
1569 })
1570 }
1571
1572 fn copy(&mut self, _: &editor::actions::Copy, _window: &mut Window, cx: &mut Context<Self>) {
1573 if self.editor.read(cx).selections.count() == 1 {
1574 let (copied_text, metadata, _) = self.get_clipboard_contents(cx);
1575 cx.write_to_clipboard(ClipboardItem::new_string_with_json_metadata(
1576 copied_text,
1577 metadata,
1578 ));
1579 cx.stop_propagation();
1580 return;
1581 }
1582
1583 cx.propagate();
1584 }
1585
1586 fn cut(&mut self, _: &editor::actions::Cut, window: &mut Window, cx: &mut Context<Self>) {
1587 if self.editor.read(cx).selections.count() == 1 {
1588 let (copied_text, metadata, selections) = self.get_clipboard_contents(cx);
1589
1590 self.editor.update(cx, |editor, cx| {
1591 editor.transact(window, cx, |this, window, cx| {
1592 this.change_selections(Default::default(), window, cx, |s| {
1593 s.select(selections);
1594 });
1595 this.insert("", window, cx);
1596 cx.write_to_clipboard(ClipboardItem::new_string_with_json_metadata(
1597 copied_text,
1598 metadata,
1599 ));
1600 });
1601 });
1602
1603 cx.stop_propagation();
1604 return;
1605 }
1606
1607 cx.propagate();
1608 }
1609
1610 fn get_clipboard_contents(
1611 &mut self,
1612 cx: &mut Context<Self>,
1613 ) -> (String, CopyMetadata, Vec<text::Selection<usize>>) {
1614 let (mut selection, creases) = self.editor.update(cx, |editor, cx| {
1615 let mut selection = editor.selections.newest_adjusted(cx);
1616 let snapshot = editor.buffer().read(cx).snapshot(cx);
1617
1618 selection.goal = SelectionGoal::None;
1619
1620 let selection_start = snapshot.point_to_offset(selection.start);
1621
1622 (
1623 selection.map(|point| snapshot.point_to_offset(point)),
1624 editor.display_map.update(cx, |display_map, cx| {
1625 display_map
1626 .snapshot(cx)
1627 .crease_snapshot
1628 .creases_in_range(
1629 MultiBufferRow(selection.start.row)
1630 ..MultiBufferRow(selection.end.row + 1),
1631 &snapshot,
1632 )
1633 .filter_map(|crease| {
1634 if let Crease::Inline {
1635 range, metadata, ..
1636 } = &crease
1637 {
1638 let metadata = metadata.as_ref()?;
1639 let start = range
1640 .start
1641 .to_offset(&snapshot)
1642 .saturating_sub(selection_start);
1643 let end = range
1644 .end
1645 .to_offset(&snapshot)
1646 .saturating_sub(selection_start);
1647
1648 let range_relative_to_selection = start..end;
1649 if !range_relative_to_selection.is_empty() {
1650 return Some(SelectedCreaseMetadata {
1651 range_relative_to_selection,
1652 crease: metadata.clone(),
1653 });
1654 }
1655 }
1656 None
1657 })
1658 .collect::<Vec<_>>()
1659 }),
1660 )
1661 });
1662
1663 let context = self.context.read(cx);
1664
1665 let mut text = String::new();
1666
1667 // If selection is empty, we want to copy the entire line
1668 if selection.range().is_empty() {
1669 let snapshot = context.buffer().read(cx).snapshot();
1670 let point = snapshot.offset_to_point(selection.range().start);
1671 selection.start = snapshot.point_to_offset(Point::new(point.row, 0));
1672 selection.end = snapshot
1673 .point_to_offset(cmp::min(Point::new(point.row + 1, 0), snapshot.max_point()));
1674 for chunk in context.buffer().read(cx).text_for_range(selection.range()) {
1675 text.push_str(chunk);
1676 }
1677 } else {
1678 for message in context.messages(cx) {
1679 if message.offset_range.start >= selection.range().end {
1680 break;
1681 } else if message.offset_range.end >= selection.range().start {
1682 let range = cmp::max(message.offset_range.start, selection.range().start)
1683 ..cmp::min(message.offset_range.end, selection.range().end);
1684 if !range.is_empty() {
1685 for chunk in context.buffer().read(cx).text_for_range(range) {
1686 text.push_str(chunk);
1687 }
1688 if message.offset_range.end < selection.range().end {
1689 text.push('\n');
1690 }
1691 }
1692 }
1693 }
1694 }
1695 (text, CopyMetadata { creases }, vec![selection])
1696 }
1697
1698 fn paste(
1699 &mut self,
1700 action: &editor::actions::Paste,
1701 window: &mut Window,
1702 cx: &mut Context<Self>,
1703 ) {
1704 cx.stop_propagation();
1705
1706 let images = if let Some(item) = cx.read_from_clipboard() {
1707 item.into_entries()
1708 .filter_map(|entry| {
1709 if let ClipboardEntry::Image(image) = entry {
1710 Some(image)
1711 } else {
1712 None
1713 }
1714 })
1715 .collect()
1716 } else {
1717 Vec::new()
1718 };
1719
1720 let metadata = if let Some(item) = cx.read_from_clipboard() {
1721 item.entries().first().and_then(|entry| {
1722 if let ClipboardEntry::String(text) = entry {
1723 text.metadata_json::<CopyMetadata>()
1724 } else {
1725 None
1726 }
1727 })
1728 } else {
1729 None
1730 };
1731
1732 if images.is_empty() {
1733 self.editor.update(cx, |editor, cx| {
1734 let paste_position = editor.selections.newest::<usize>(cx).head();
1735 editor.paste(action, window, cx);
1736
1737 if let Some(metadata) = metadata {
1738 let buffer = editor.buffer().read(cx).snapshot(cx);
1739
1740 let mut buffer_rows_to_fold = BTreeSet::new();
1741 let weak_editor = cx.entity().downgrade();
1742 editor.insert_creases(
1743 metadata.creases.into_iter().map(|metadata| {
1744 let start = buffer.anchor_after(
1745 paste_position + metadata.range_relative_to_selection.start,
1746 );
1747 let end = buffer.anchor_before(
1748 paste_position + metadata.range_relative_to_selection.end,
1749 );
1750
1751 let buffer_row = MultiBufferRow(start.to_point(&buffer).row);
1752 buffer_rows_to_fold.insert(buffer_row);
1753 Crease::inline(
1754 start..end,
1755 FoldPlaceholder {
1756 render: render_fold_icon_button(
1757 weak_editor.clone(),
1758 metadata.crease.icon_path.clone(),
1759 metadata.crease.label.clone(),
1760 ),
1761 ..Default::default()
1762 },
1763 render_slash_command_output_toggle,
1764 |_, _, _, _| Empty.into_any(),
1765 )
1766 .with_metadata(metadata.crease.clone())
1767 }),
1768 cx,
1769 );
1770 for buffer_row in buffer_rows_to_fold.into_iter().rev() {
1771 editor.fold_at(buffer_row, window, cx);
1772 }
1773 }
1774 });
1775 } else {
1776 let mut image_positions = Vec::new();
1777 self.editor.update(cx, |editor, cx| {
1778 editor.transact(window, cx, |editor, _window, cx| {
1779 let edits = editor
1780 .selections
1781 .all::<usize>(cx)
1782 .into_iter()
1783 .map(|selection| (selection.start..selection.end, "\n"));
1784 editor.edit(edits, cx);
1785
1786 let snapshot = editor.buffer().read(cx).snapshot(cx);
1787 for selection in editor.selections.all::<usize>(cx) {
1788 image_positions.push(snapshot.anchor_before(selection.end));
1789 }
1790 });
1791 });
1792
1793 self.context.update(cx, |context, cx| {
1794 for image in images {
1795 let Some(render_image) = image.to_image_data(cx.svg_renderer()).log_err()
1796 else {
1797 continue;
1798 };
1799 let image_id = image.id();
1800 let image_task = LanguageModelImage::from_image(Arc::new(image), cx).shared();
1801
1802 for image_position in image_positions.iter() {
1803 context.insert_content(
1804 Content::Image {
1805 anchor: image_position.text_anchor,
1806 image_id,
1807 image: image_task.clone(),
1808 render_image: render_image.clone(),
1809 },
1810 cx,
1811 );
1812 }
1813 }
1814 });
1815 }
1816 }
1817
1818 fn update_image_blocks(&mut self, cx: &mut Context<Self>) {
1819 self.editor.update(cx, |editor, cx| {
1820 let buffer = editor.buffer().read(cx).snapshot(cx);
1821 let excerpt_id = *buffer.as_singleton().unwrap().0;
1822 let old_blocks = std::mem::take(&mut self.image_blocks);
1823 let new_blocks = self
1824 .context
1825 .read(cx)
1826 .contents(cx)
1827 .map(
1828 |Content::Image {
1829 anchor,
1830 render_image,
1831 ..
1832 }| (anchor, render_image),
1833 )
1834 .filter_map(|(anchor, render_image)| {
1835 const MAX_HEIGHT_IN_LINES: u32 = 8;
1836 let anchor = buffer.anchor_in_excerpt(excerpt_id, anchor).unwrap();
1837 let image = render_image.clone();
1838 anchor.is_valid(&buffer).then(|| BlockProperties {
1839 placement: BlockPlacement::Above(anchor),
1840 height: Some(MAX_HEIGHT_IN_LINES),
1841 style: BlockStyle::Sticky,
1842 render: Arc::new(move |cx| {
1843 let image_size = size_for_image(
1844 &image,
1845 size(
1846 cx.max_width - cx.margins.gutter.full_width(),
1847 MAX_HEIGHT_IN_LINES as f32 * cx.line_height,
1848 ),
1849 );
1850 h_flex()
1851 .pl(cx.margins.gutter.full_width())
1852 .child(
1853 img(image.clone())
1854 .object_fit(gpui::ObjectFit::ScaleDown)
1855 .w(image_size.width)
1856 .h(image_size.height),
1857 )
1858 .into_any_element()
1859 }),
1860 priority: 0,
1861 render_in_minimap: false,
1862 })
1863 })
1864 .collect::<Vec<_>>();
1865
1866 editor.remove_blocks(old_blocks, None, cx);
1867 let ids = editor.insert_blocks(new_blocks, None, cx);
1868 self.image_blocks = HashSet::from_iter(ids);
1869 });
1870 }
1871
1872 fn split(&mut self, _: &Split, _window: &mut Window, cx: &mut Context<Self>) {
1873 self.context.update(cx, |context, cx| {
1874 let selections = self.editor.read(cx).selections.disjoint_anchors();
1875 for selection in selections.as_ref() {
1876 let buffer = self.editor.read(cx).buffer().read(cx).snapshot(cx);
1877 let range = selection
1878 .map(|endpoint| endpoint.to_offset(&buffer))
1879 .range();
1880 context.split_message(range, cx);
1881 }
1882 });
1883 }
1884
1885 fn save(&mut self, _: &Save, _window: &mut Window, cx: &mut Context<Self>) {
1886 self.context.update(cx, |context, cx| {
1887 context.save(Some(Duration::from_millis(500)), self.fs.clone(), cx)
1888 });
1889 }
1890
1891 pub fn title(&self, cx: &App) -> SharedString {
1892 self.context.read(cx).summary().or_default()
1893 }
1894
1895 pub fn regenerate_summary(&mut self, cx: &mut Context<Self>) {
1896 self.context
1897 .update(cx, |context, cx| context.summarize(true, cx));
1898 }
1899
1900 fn render_notice(&self, cx: &mut Context<Self>) -> Option<AnyElement> {
1901 // This was previously gated behind the `zed-pro` feature flag. Since we
1902 // aren't planning to ship that right now, we're just hard-coding this
1903 // value to not show the nudge.
1904 let nudge = Some(false);
1905
1906 let model_registry = LanguageModelRegistry::read_global(cx);
1907
1908 if nudge.map_or(false, |value| value) {
1909 Some(
1910 h_flex()
1911 .p_3()
1912 .border_b_1()
1913 .border_color(cx.theme().colors().border_variant)
1914 .bg(cx.theme().colors().editor_background)
1915 .justify_between()
1916 .child(
1917 h_flex()
1918 .gap_3()
1919 .child(Icon::new(IconName::ZedAssistant).color(Color::Accent))
1920 .child(Label::new("Zed AI is here! Get started by signing in →")),
1921 )
1922 .child(
1923 Button::new("sign-in", "Sign in")
1924 .size(ButtonSize::Compact)
1925 .style(ButtonStyle::Filled)
1926 .on_click(cx.listener(|this, _event, _window, cx| {
1927 let client = this
1928 .workspace
1929 .read_with(cx, |workspace, _| workspace.client().clone())
1930 .log_err();
1931
1932 if let Some(client) = client {
1933 cx.spawn(async move |context_editor, cx| {
1934 match client.authenticate_and_connect(true, cx).await {
1935 util::ConnectionResult::Timeout => {
1936 log::error!("Authentication timeout")
1937 }
1938 util::ConnectionResult::ConnectionReset => {
1939 log::error!("Connection reset")
1940 }
1941 util::ConnectionResult::Result(r) => {
1942 if r.log_err().is_some() {
1943 context_editor
1944 .update(cx, |_, cx| cx.notify())
1945 .ok();
1946 }
1947 }
1948 }
1949 })
1950 .detach()
1951 }
1952 })),
1953 )
1954 .into_any_element(),
1955 )
1956 } else if let Some(configuration_error) =
1957 model_registry.configuration_error(model_registry.default_model(), cx)
1958 {
1959 Some(
1960 h_flex()
1961 .px_3()
1962 .py_2()
1963 .border_b_1()
1964 .border_color(cx.theme().colors().border_variant)
1965 .bg(cx.theme().colors().editor_background)
1966 .justify_between()
1967 .child(
1968 h_flex()
1969 .gap_3()
1970 .child(
1971 Icon::new(IconName::Warning)
1972 .size(IconSize::Small)
1973 .color(Color::Warning),
1974 )
1975 .child(Label::new(configuration_error.to_string())),
1976 )
1977 .child(
1978 Button::new("open-configuration", "Configure Providers")
1979 .size(ButtonSize::Compact)
1980 .icon(Some(IconName::SlidersVertical))
1981 .icon_size(IconSize::Small)
1982 .icon_position(IconPosition::Start)
1983 .style(ButtonStyle::Filled)
1984 .on_click({
1985 let focus_handle = self.focus_handle(cx).clone();
1986 move |_event, window, cx| {
1987 focus_handle.dispatch_action(
1988 &zed_actions::agent::OpenConfiguration,
1989 window,
1990 cx,
1991 );
1992 }
1993 }),
1994 )
1995 .into_any_element(),
1996 )
1997 } else {
1998 None
1999 }
2000 }
2001
2002 fn render_send_button(&self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
2003 let focus_handle = self.focus_handle(cx).clone();
2004
2005 let (style, tooltip) = match token_state(&self.context, cx) {
2006 Some(TokenState::NoTokensLeft { .. }) => (
2007 ButtonStyle::Tinted(TintColor::Error),
2008 Some(Tooltip::text("Token limit reached")(window, cx)),
2009 ),
2010 Some(TokenState::HasMoreTokens {
2011 over_warn_threshold,
2012 ..
2013 }) => {
2014 let (style, tooltip) = if over_warn_threshold {
2015 (
2016 ButtonStyle::Tinted(TintColor::Warning),
2017 Some(Tooltip::text("Token limit is close to exhaustion")(
2018 window, cx,
2019 )),
2020 )
2021 } else {
2022 (ButtonStyle::Filled, None)
2023 };
2024 (style, tooltip)
2025 }
2026 None => (ButtonStyle::Filled, None),
2027 };
2028
2029 Button::new("send_button", "Send")
2030 .label_size(LabelSize::Small)
2031 .disabled(self.sending_disabled(cx))
2032 .style(style)
2033 .when_some(tooltip, |button, tooltip| {
2034 button.tooltip(move |_, _| tooltip.clone())
2035 })
2036 .layer(ElevationIndex::ModalSurface)
2037 .key_binding(
2038 KeyBinding::for_action_in(&Assist, &focus_handle, window, cx)
2039 .map(|kb| kb.size(rems_from_px(12.))),
2040 )
2041 .on_click(move |_event, window, cx| {
2042 focus_handle.dispatch_action(&Assist, window, cx);
2043 })
2044 }
2045
2046 /// Whether or not we should allow messages to be sent.
2047 /// Will return false if the selected provided has a configuration error or
2048 /// if the user has not accepted the terms of service for this provider.
2049 fn sending_disabled(&self, cx: &mut Context<'_, TextThreadEditor>) -> bool {
2050 let model_registry = LanguageModelRegistry::read_global(cx);
2051 let Some(configuration_error) =
2052 model_registry.configuration_error(model_registry.default_model(), cx)
2053 else {
2054 return false;
2055 };
2056
2057 match configuration_error {
2058 ConfigurationError::NoProvider
2059 | ConfigurationError::ModelNotFound
2060 | ConfigurationError::ProviderNotAuthenticated(_) => true,
2061 ConfigurationError::ProviderPendingTermsAcceptance(_) => self.show_accept_terms,
2062 }
2063 }
2064
2065 fn render_inject_context_menu(&self, cx: &mut Context<Self>) -> impl IntoElement {
2066 slash_command_picker::SlashCommandSelector::new(
2067 self.slash_commands.clone(),
2068 cx.entity().downgrade(),
2069 IconButton::new("trigger", IconName::Plus)
2070 .icon_size(IconSize::Small)
2071 .icon_color(Color::Muted),
2072 move |window, cx| {
2073 Tooltip::with_meta(
2074 "Add Context",
2075 None,
2076 "Type / to insert via keyboard",
2077 window,
2078 cx,
2079 )
2080 },
2081 )
2082 }
2083
2084 fn render_burn_mode_toggle(&self, cx: &mut Context<Self>) -> Option<AnyElement> {
2085 let context = self.context().read(cx);
2086 let active_model = LanguageModelRegistry::read_global(cx)
2087 .default_model()
2088 .map(|default| default.model)?;
2089 if !active_model.supports_burn_mode() {
2090 return None;
2091 }
2092
2093 let active_completion_mode = context.completion_mode();
2094 let burn_mode_enabled = active_completion_mode == CompletionMode::Burn;
2095 let icon = if burn_mode_enabled {
2096 IconName::ZedBurnModeOn
2097 } else {
2098 IconName::ZedBurnMode
2099 };
2100
2101 Some(
2102 IconButton::new("burn-mode", icon)
2103 .icon_size(IconSize::Small)
2104 .icon_color(Color::Muted)
2105 .toggle_state(burn_mode_enabled)
2106 .selected_icon_color(Color::Error)
2107 .on_click(cx.listener(move |this, _event, _window, cx| {
2108 this.context().update(cx, |context, _cx| {
2109 context.set_completion_mode(match active_completion_mode {
2110 CompletionMode::Burn => CompletionMode::Normal,
2111 CompletionMode::Normal => CompletionMode::Burn,
2112 });
2113 });
2114 }))
2115 .tooltip(move |_window, cx| {
2116 cx.new(|_| BurnModeTooltip::new().selected(burn_mode_enabled))
2117 .into()
2118 })
2119 .into_any_element(),
2120 )
2121 }
2122
2123 fn render_language_model_selector(
2124 &self,
2125 window: &mut Window,
2126 cx: &mut Context<Self>,
2127 ) -> impl IntoElement {
2128 let active_model = LanguageModelRegistry::read_global(cx)
2129 .default_model()
2130 .map(|default| default.model);
2131 let model_name = match active_model {
2132 Some(model) => model.name().0,
2133 None => SharedString::from("No model selected"),
2134 };
2135
2136 let active_provider = LanguageModelRegistry::read_global(cx)
2137 .default_model()
2138 .map(|default| default.provider);
2139 let provider_icon = match active_provider {
2140 Some(provider) => provider.icon(),
2141 None => IconName::Ai,
2142 };
2143
2144 let focus_handle = self.editor().focus_handle(cx).clone();
2145
2146 PickerPopoverMenu::new(
2147 self.language_model_selector.clone(),
2148 ButtonLike::new("active-model")
2149 .style(ButtonStyle::Subtle)
2150 .child(
2151 h_flex()
2152 .gap_0p5()
2153 .child(
2154 Icon::new(provider_icon)
2155 .color(Color::Muted)
2156 .size(IconSize::XSmall),
2157 )
2158 .child(
2159 Label::new(model_name)
2160 .color(Color::Muted)
2161 .size(LabelSize::Small)
2162 .ml_0p5(),
2163 )
2164 .child(
2165 Icon::new(IconName::ChevronDown)
2166 .color(Color::Muted)
2167 .size(IconSize::XSmall),
2168 ),
2169 ),
2170 move |window, cx| {
2171 Tooltip::for_action_in(
2172 "Change Model",
2173 &ToggleModelSelector,
2174 &focus_handle,
2175 window,
2176 cx,
2177 )
2178 },
2179 gpui::Corner::BottomLeft,
2180 cx,
2181 )
2182 .with_handle(self.language_model_selector_menu_handle.clone())
2183 .render(window, cx)
2184 }
2185
2186 fn render_last_error(&self, cx: &mut Context<Self>) -> Option<AnyElement> {
2187 let last_error = self.last_error.as_ref()?;
2188
2189 Some(
2190 div()
2191 .absolute()
2192 .right_3()
2193 .bottom_12()
2194 .max_w_96()
2195 .py_2()
2196 .px_3()
2197 .elevation_2(cx)
2198 .occlude()
2199 .child(match last_error {
2200 AssistError::PaymentRequired => self.render_payment_required_error(cx),
2201 AssistError::Message(error_message) => {
2202 self.render_assist_error(error_message, cx)
2203 }
2204 })
2205 .into_any(),
2206 )
2207 }
2208
2209 fn render_payment_required_error(&self, cx: &mut Context<Self>) -> AnyElement {
2210 const ERROR_MESSAGE: &str = "Free tier exceeded. Subscribe and add payment to continue using Zed LLMs. You'll be billed at cost for tokens used.";
2211
2212 v_flex()
2213 .gap_0p5()
2214 .child(
2215 h_flex()
2216 .gap_1p5()
2217 .items_center()
2218 .child(Icon::new(IconName::XCircle).color(Color::Error))
2219 .child(Label::new("Free Usage Exceeded").weight(FontWeight::MEDIUM)),
2220 )
2221 .child(
2222 div()
2223 .id("error-message")
2224 .max_h_24()
2225 .overflow_y_scroll()
2226 .child(Label::new(ERROR_MESSAGE)),
2227 )
2228 .child(
2229 h_flex()
2230 .justify_end()
2231 .mt_1()
2232 .child(Button::new("subscribe", "Subscribe").on_click(cx.listener(
2233 |this, _, _window, cx| {
2234 this.last_error = None;
2235 cx.open_url(&zed_urls::account_url(cx));
2236 cx.notify();
2237 },
2238 )))
2239 .child(Button::new("dismiss", "Dismiss").on_click(cx.listener(
2240 |this, _, _window, cx| {
2241 this.last_error = None;
2242 cx.notify();
2243 },
2244 ))),
2245 )
2246 .into_any()
2247 }
2248
2249 fn render_assist_error(
2250 &self,
2251 error_message: &SharedString,
2252 cx: &mut Context<Self>,
2253 ) -> AnyElement {
2254 v_flex()
2255 .gap_0p5()
2256 .child(
2257 h_flex()
2258 .gap_1p5()
2259 .items_center()
2260 .child(Icon::new(IconName::XCircle).color(Color::Error))
2261 .child(
2262 Label::new("Error interacting with language model")
2263 .weight(FontWeight::MEDIUM),
2264 ),
2265 )
2266 .child(
2267 div()
2268 .id("error-message")
2269 .max_h_32()
2270 .overflow_y_scroll()
2271 .child(Label::new(error_message.clone())),
2272 )
2273 .child(
2274 h_flex()
2275 .justify_end()
2276 .mt_1()
2277 .child(Button::new("dismiss", "Dismiss").on_click(cx.listener(
2278 |this, _, _window, cx| {
2279 this.last_error = None;
2280 cx.notify();
2281 },
2282 ))),
2283 )
2284 .into_any()
2285 }
2286}
2287
2288/// Returns the contents of the *outermost* fenced code block that contains the given offset.
2289fn find_surrounding_code_block(snapshot: &BufferSnapshot, offset: usize) -> Option<Range<usize>> {
2290 const CODE_BLOCK_NODE: &'static str = "fenced_code_block";
2291 const CODE_BLOCK_CONTENT: &'static str = "code_fence_content";
2292
2293 let layer = snapshot.syntax_layers().next()?;
2294
2295 let root_node = layer.node();
2296 let mut cursor = root_node.walk();
2297
2298 // Go to the first child for the given offset
2299 while cursor.goto_first_child_for_byte(offset).is_some() {
2300 // If we're at the end of the node, go to the next one.
2301 // Example: if you have a fenced-code-block, and you're on the start of the line
2302 // right after the closing ```, you want to skip the fenced-code-block and
2303 // go to the next sibling.
2304 if cursor.node().end_byte() == offset {
2305 cursor.goto_next_sibling();
2306 }
2307
2308 if cursor.node().start_byte() > offset {
2309 break;
2310 }
2311
2312 // We found the fenced code block.
2313 if cursor.node().kind() == CODE_BLOCK_NODE {
2314 // Now we need to find the child node that contains the code.
2315 cursor.goto_first_child();
2316 loop {
2317 if cursor.node().kind() == CODE_BLOCK_CONTENT {
2318 return Some(cursor.node().byte_range());
2319 }
2320 if !cursor.goto_next_sibling() {
2321 break;
2322 }
2323 }
2324 }
2325 }
2326
2327 None
2328}
2329
2330fn render_thought_process_fold_icon_button(
2331 editor: WeakEntity<Editor>,
2332 status: ThoughtProcessStatus,
2333) -> Arc<dyn Send + Sync + Fn(FoldId, Range<Anchor>, &mut App) -> AnyElement> {
2334 Arc::new(move |fold_id, fold_range, _cx| {
2335 let editor = editor.clone();
2336
2337 let button = ButtonLike::new(fold_id).layer(ElevationIndex::ElevatedSurface);
2338 let button = match status {
2339 ThoughtProcessStatus::Pending => button
2340 .child(
2341 Icon::new(IconName::LightBulb)
2342 .size(IconSize::Small)
2343 .color(Color::Muted),
2344 )
2345 .child(
2346 Label::new("Thinking…").color(Color::Muted).with_animation(
2347 "pulsating-label",
2348 Animation::new(Duration::from_secs(2))
2349 .repeat()
2350 .with_easing(pulsating_between(0.4, 0.8)),
2351 |label, delta| label.alpha(delta),
2352 ),
2353 ),
2354 ThoughtProcessStatus::Completed => button
2355 .style(ButtonStyle::Filled)
2356 .child(Icon::new(IconName::LightBulb).size(IconSize::Small))
2357 .child(Label::new("Thought Process").single_line()),
2358 };
2359
2360 button
2361 .on_click(move |_, window, cx| {
2362 editor
2363 .update(cx, |editor, cx| {
2364 let buffer_start = fold_range
2365 .start
2366 .to_point(&editor.buffer().read(cx).read(cx));
2367 let buffer_row = MultiBufferRow(buffer_start.row);
2368 editor.unfold_at(buffer_row, window, cx);
2369 })
2370 .ok();
2371 })
2372 .into_any_element()
2373 })
2374}
2375
2376fn render_fold_icon_button(
2377 editor: WeakEntity<Editor>,
2378 icon_path: SharedString,
2379 label: SharedString,
2380) -> Arc<dyn Send + Sync + Fn(FoldId, Range<Anchor>, &mut App) -> AnyElement> {
2381 Arc::new(move |fold_id, fold_range, _cx| {
2382 let editor = editor.clone();
2383 ButtonLike::new(fold_id)
2384 .style(ButtonStyle::Filled)
2385 .layer(ElevationIndex::ElevatedSurface)
2386 .child(Icon::from_path(icon_path.clone()))
2387 .child(Label::new(label.clone()).single_line())
2388 .on_click(move |_, window, cx| {
2389 editor
2390 .update(cx, |editor, cx| {
2391 let buffer_start = fold_range
2392 .start
2393 .to_point(&editor.buffer().read(cx).read(cx));
2394 let buffer_row = MultiBufferRow(buffer_start.row);
2395 editor.unfold_at(buffer_row, window, cx);
2396 })
2397 .ok();
2398 })
2399 .into_any_element()
2400 })
2401}
2402
2403type ToggleFold = Arc<dyn Fn(bool, &mut Window, &mut App) + Send + Sync>;
2404
2405fn render_slash_command_output_toggle(
2406 row: MultiBufferRow,
2407 is_folded: bool,
2408 fold: ToggleFold,
2409 _window: &mut Window,
2410 _cx: &mut App,
2411) -> AnyElement {
2412 Disclosure::new(
2413 ("slash-command-output-fold-indicator", row.0 as u64),
2414 !is_folded,
2415 )
2416 .toggle_state(is_folded)
2417 .on_click(move |_e, window, cx| fold(!is_folded, window, cx))
2418 .into_any_element()
2419}
2420
2421pub fn fold_toggle(
2422 name: &'static str,
2423) -> impl Fn(
2424 MultiBufferRow,
2425 bool,
2426 Arc<dyn Fn(bool, &mut Window, &mut App) + Send + Sync>,
2427 &mut Window,
2428 &mut App,
2429) -> AnyElement {
2430 move |row, is_folded, fold, _window, _cx| {
2431 Disclosure::new((name, row.0 as u64), !is_folded)
2432 .toggle_state(is_folded)
2433 .on_click(move |_e, window, cx| fold(!is_folded, window, cx))
2434 .into_any_element()
2435 }
2436}
2437
2438fn quote_selection_fold_placeholder(title: String, editor: WeakEntity<Editor>) -> FoldPlaceholder {
2439 FoldPlaceholder {
2440 render: Arc::new({
2441 move |fold_id, fold_range, _cx| {
2442 let editor = editor.clone();
2443 ButtonLike::new(fold_id)
2444 .style(ButtonStyle::Filled)
2445 .layer(ElevationIndex::ElevatedSurface)
2446 .child(Icon::new(IconName::TextSnippet))
2447 .child(Label::new(title.clone()).single_line())
2448 .on_click(move |_, window, cx| {
2449 editor
2450 .update(cx, |editor, cx| {
2451 let buffer_start = fold_range
2452 .start
2453 .to_point(&editor.buffer().read(cx).read(cx));
2454 let buffer_row = MultiBufferRow(buffer_start.row);
2455 editor.unfold_at(buffer_row, window, cx);
2456 })
2457 .ok();
2458 })
2459 .into_any_element()
2460 }
2461 }),
2462 merge_adjacent: false,
2463 ..Default::default()
2464 }
2465}
2466
2467fn render_quote_selection_output_toggle(
2468 row: MultiBufferRow,
2469 is_folded: bool,
2470 fold: ToggleFold,
2471 _window: &mut Window,
2472 _cx: &mut App,
2473) -> AnyElement {
2474 Disclosure::new(("quote-selection-indicator", row.0 as u64), !is_folded)
2475 .toggle_state(is_folded)
2476 .on_click(move |_e, window, cx| fold(!is_folded, window, cx))
2477 .into_any_element()
2478}
2479
2480fn render_pending_slash_command_gutter_decoration(
2481 row: MultiBufferRow,
2482 status: &PendingSlashCommandStatus,
2483 confirm_command: Arc<dyn Fn(&mut Window, &mut App)>,
2484) -> AnyElement {
2485 let mut icon = IconButton::new(
2486 ("slash-command-gutter-decoration", row.0),
2487 ui::IconName::TriangleRight,
2488 )
2489 .on_click(move |_e, window, cx| confirm_command(window, cx))
2490 .icon_size(ui::IconSize::Small)
2491 .size(ui::ButtonSize::None);
2492
2493 match status {
2494 PendingSlashCommandStatus::Idle => {
2495 icon = icon.icon_color(Color::Muted);
2496 }
2497 PendingSlashCommandStatus::Running { .. } => {
2498 icon = icon.toggle_state(true);
2499 }
2500 PendingSlashCommandStatus::Error(_) => icon = icon.icon_color(Color::Error),
2501 }
2502
2503 icon.into_any_element()
2504}
2505
2506fn render_docs_slash_command_trailer(
2507 row: MultiBufferRow,
2508 command: ParsedSlashCommand,
2509 cx: &mut App,
2510) -> AnyElement {
2511 if command.arguments.is_empty() {
2512 return Empty.into_any();
2513 }
2514 let args = DocsSlashCommandArgs::parse(&command.arguments);
2515
2516 let Some(store) = args
2517 .provider()
2518 .and_then(|provider| IndexedDocsStore::try_global(provider, cx).ok())
2519 else {
2520 return Empty.into_any();
2521 };
2522
2523 let Some(package) = args.package() else {
2524 return Empty.into_any();
2525 };
2526
2527 let mut children = Vec::new();
2528
2529 if store.is_indexing(&package) {
2530 children.push(
2531 div()
2532 .id(("crates-being-indexed", row.0))
2533 .child(Icon::new(IconName::ArrowCircle).with_animation(
2534 "arrow-circle",
2535 Animation::new(Duration::from_secs(4)).repeat(),
2536 |icon, delta| icon.transform(Transformation::rotate(percentage(delta))),
2537 ))
2538 .tooltip({
2539 let package = package.clone();
2540 Tooltip::text(format!("Indexing {package}…"))
2541 })
2542 .into_any_element(),
2543 );
2544 }
2545
2546 if let Some(latest_error) = store.latest_error_for_package(&package) {
2547 children.push(
2548 div()
2549 .id(("latest-error", row.0))
2550 .child(
2551 Icon::new(IconName::Warning)
2552 .size(IconSize::Small)
2553 .color(Color::Warning),
2554 )
2555 .tooltip(Tooltip::text(format!("Failed to index: {latest_error}")))
2556 .into_any_element(),
2557 )
2558 }
2559
2560 let is_indexing = store.is_indexing(&package);
2561 let latest_error = store.latest_error_for_package(&package);
2562
2563 if !is_indexing && latest_error.is_none() {
2564 return Empty.into_any();
2565 }
2566
2567 h_flex().gap_2().children(children).into_any_element()
2568}
2569
2570#[derive(Debug, Clone, Serialize, Deserialize)]
2571struct CopyMetadata {
2572 creases: Vec<SelectedCreaseMetadata>,
2573}
2574
2575#[derive(Debug, Clone, Serialize, Deserialize)]
2576struct SelectedCreaseMetadata {
2577 range_relative_to_selection: Range<usize>,
2578 crease: CreaseMetadata,
2579}
2580
2581impl EventEmitter<EditorEvent> for TextThreadEditor {}
2582impl EventEmitter<SearchEvent> for TextThreadEditor {}
2583
2584impl Render for TextThreadEditor {
2585 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
2586 let provider = LanguageModelRegistry::read_global(cx)
2587 .default_model()
2588 .map(|default| default.provider);
2589
2590 let accept_terms = if self.show_accept_terms {
2591 provider.as_ref().and_then(|provider| {
2592 provider.render_accept_terms(LanguageModelProviderTosView::PromptEditorPopup, cx)
2593 })
2594 } else {
2595 None
2596 };
2597
2598 let language_model_selector = self.language_model_selector_menu_handle.clone();
2599 let burn_mode_toggle = self.render_burn_mode_toggle(cx);
2600
2601 v_flex()
2602 .key_context("ContextEditor")
2603 .capture_action(cx.listener(TextThreadEditor::cancel))
2604 .capture_action(cx.listener(TextThreadEditor::save))
2605 .capture_action(cx.listener(TextThreadEditor::copy))
2606 .capture_action(cx.listener(TextThreadEditor::cut))
2607 .capture_action(cx.listener(TextThreadEditor::paste))
2608 .capture_action(cx.listener(TextThreadEditor::cycle_message_role))
2609 .capture_action(cx.listener(TextThreadEditor::confirm_command))
2610 .on_action(cx.listener(TextThreadEditor::assist))
2611 .on_action(cx.listener(TextThreadEditor::split))
2612 .on_action(move |_: &ToggleModelSelector, window, cx| {
2613 language_model_selector.toggle(window, cx);
2614 })
2615 .size_full()
2616 .children(self.render_notice(cx))
2617 .child(
2618 div()
2619 .flex_grow()
2620 .bg(cx.theme().colors().editor_background)
2621 .child(self.editor.clone()),
2622 )
2623 .when_some(accept_terms, |this, element| {
2624 this.child(
2625 div()
2626 .absolute()
2627 .right_3()
2628 .bottom_12()
2629 .max_w_96()
2630 .py_2()
2631 .px_3()
2632 .elevation_2(cx)
2633 .bg(cx.theme().colors().surface_background)
2634 .occlude()
2635 .child(element),
2636 )
2637 })
2638 .children(self.render_last_error(cx))
2639 .child(
2640 h_flex()
2641 .relative()
2642 .py_2()
2643 .pl_1p5()
2644 .pr_2()
2645 .w_full()
2646 .justify_between()
2647 .border_t_1()
2648 .border_color(cx.theme().colors().border_variant)
2649 .bg(cx.theme().colors().editor_background)
2650 .child(
2651 h_flex()
2652 .gap_0p5()
2653 .child(self.render_inject_context_menu(cx))
2654 .when_some(burn_mode_toggle, |this, element| this.child(element)),
2655 )
2656 .child(
2657 h_flex()
2658 .gap_1()
2659 .child(self.render_language_model_selector(window, cx))
2660 .child(self.render_send_button(window, cx)),
2661 ),
2662 )
2663 }
2664}
2665
2666impl Focusable for TextThreadEditor {
2667 fn focus_handle(&self, cx: &App) -> FocusHandle {
2668 self.editor.focus_handle(cx)
2669 }
2670}
2671
2672impl Item for TextThreadEditor {
2673 type Event = editor::EditorEvent;
2674
2675 fn tab_content_text(&self, _detail: usize, cx: &App) -> SharedString {
2676 util::truncate_and_trailoff(&self.title(cx), MAX_TAB_TITLE_LEN).into()
2677 }
2678
2679 fn to_item_events(event: &Self::Event, mut f: impl FnMut(item::ItemEvent)) {
2680 match event {
2681 EditorEvent::Edited { .. } => {
2682 f(item::ItemEvent::Edit);
2683 }
2684 EditorEvent::TitleChanged => {
2685 f(item::ItemEvent::UpdateTab);
2686 }
2687 _ => {}
2688 }
2689 }
2690
2691 fn tab_tooltip_text(&self, cx: &App) -> Option<SharedString> {
2692 Some(self.title(cx).to_string().into())
2693 }
2694
2695 fn as_searchable(&self, handle: &Entity<Self>) -> Option<Box<dyn SearchableItemHandle>> {
2696 Some(Box::new(handle.clone()))
2697 }
2698
2699 fn set_nav_history(
2700 &mut self,
2701 nav_history: pane::ItemNavHistory,
2702 window: &mut Window,
2703 cx: &mut Context<Self>,
2704 ) {
2705 self.editor.update(cx, |editor, cx| {
2706 Item::set_nav_history(editor, nav_history, window, cx)
2707 })
2708 }
2709
2710 fn navigate(
2711 &mut self,
2712 data: Box<dyn std::any::Any>,
2713 window: &mut Window,
2714 cx: &mut Context<Self>,
2715 ) -> bool {
2716 self.editor
2717 .update(cx, |editor, cx| Item::navigate(editor, data, window, cx))
2718 }
2719
2720 fn deactivated(&mut self, window: &mut Window, cx: &mut Context<Self>) {
2721 self.editor
2722 .update(cx, |editor, cx| Item::deactivated(editor, window, cx))
2723 }
2724
2725 fn act_as_type<'a>(
2726 &'a self,
2727 type_id: TypeId,
2728 self_handle: &'a Entity<Self>,
2729 _: &'a App,
2730 ) -> Option<AnyView> {
2731 if type_id == TypeId::of::<Self>() {
2732 Some(self_handle.to_any())
2733 } else if type_id == TypeId::of::<Editor>() {
2734 Some(self.editor.to_any())
2735 } else {
2736 None
2737 }
2738 }
2739
2740 fn include_in_nav_history() -> bool {
2741 false
2742 }
2743}
2744
2745impl SearchableItem for TextThreadEditor {
2746 type Match = <Editor as SearchableItem>::Match;
2747
2748 fn clear_matches(&mut self, window: &mut Window, cx: &mut Context<Self>) {
2749 self.editor.update(cx, |editor, cx| {
2750 editor.clear_matches(window, cx);
2751 });
2752 }
2753
2754 fn update_matches(
2755 &mut self,
2756 matches: &[Self::Match],
2757 window: &mut Window,
2758 cx: &mut Context<Self>,
2759 ) {
2760 self.editor
2761 .update(cx, |editor, cx| editor.update_matches(matches, window, cx));
2762 }
2763
2764 fn query_suggestion(&mut self, window: &mut Window, cx: &mut Context<Self>) -> String {
2765 self.editor
2766 .update(cx, |editor, cx| editor.query_suggestion(window, cx))
2767 }
2768
2769 fn activate_match(
2770 &mut self,
2771 index: usize,
2772 matches: &[Self::Match],
2773 window: &mut Window,
2774 cx: &mut Context<Self>,
2775 ) {
2776 self.editor.update(cx, |editor, cx| {
2777 editor.activate_match(index, matches, window, cx);
2778 });
2779 }
2780
2781 fn select_matches(
2782 &mut self,
2783 matches: &[Self::Match],
2784 window: &mut Window,
2785 cx: &mut Context<Self>,
2786 ) {
2787 self.editor
2788 .update(cx, |editor, cx| editor.select_matches(matches, window, cx));
2789 }
2790
2791 fn replace(
2792 &mut self,
2793 identifier: &Self::Match,
2794 query: &project::search::SearchQuery,
2795 window: &mut Window,
2796 cx: &mut Context<Self>,
2797 ) {
2798 self.editor.update(cx, |editor, cx| {
2799 editor.replace(identifier, query, window, cx)
2800 });
2801 }
2802
2803 fn find_matches(
2804 &mut self,
2805 query: Arc<project::search::SearchQuery>,
2806 window: &mut Window,
2807 cx: &mut Context<Self>,
2808 ) -> Task<Vec<Self::Match>> {
2809 self.editor
2810 .update(cx, |editor, cx| editor.find_matches(query, window, cx))
2811 }
2812
2813 fn active_match_index(
2814 &mut self,
2815 direction: Direction,
2816 matches: &[Self::Match],
2817 window: &mut Window,
2818 cx: &mut Context<Self>,
2819 ) -> Option<usize> {
2820 self.editor.update(cx, |editor, cx| {
2821 editor.active_match_index(direction, matches, window, cx)
2822 })
2823 }
2824}
2825
2826impl FollowableItem for TextThreadEditor {
2827 fn remote_id(&self) -> Option<workspace::ViewId> {
2828 self.remote_id
2829 }
2830
2831 fn to_state_proto(&self, window: &Window, cx: &App) -> Option<proto::view::Variant> {
2832 let context = self.context.read(cx);
2833 Some(proto::view::Variant::ContextEditor(
2834 proto::view::ContextEditor {
2835 context_id: context.id().to_proto(),
2836 editor: if let Some(proto::view::Variant::Editor(proto)) =
2837 self.editor.read(cx).to_state_proto(window, cx)
2838 {
2839 Some(proto)
2840 } else {
2841 None
2842 },
2843 },
2844 ))
2845 }
2846
2847 fn from_state_proto(
2848 workspace: Entity<Workspace>,
2849 id: workspace::ViewId,
2850 state: &mut Option<proto::view::Variant>,
2851 window: &mut Window,
2852 cx: &mut App,
2853 ) -> Option<Task<Result<Entity<Self>>>> {
2854 let proto::view::Variant::ContextEditor(_) = state.as_ref()? else {
2855 return None;
2856 };
2857 let Some(proto::view::Variant::ContextEditor(state)) = state.take() else {
2858 unreachable!()
2859 };
2860
2861 let context_id = ContextId::from_proto(state.context_id);
2862 let editor_state = state.editor?;
2863
2864 let project = workspace.read(cx).project().clone();
2865 let agent_panel_delegate = <dyn AgentPanelDelegate>::try_global(cx)?;
2866
2867 let context_editor_task = workspace.update(cx, |workspace, cx| {
2868 agent_panel_delegate.open_remote_context(workspace, context_id, window, cx)
2869 });
2870
2871 Some(window.spawn(cx, async move |cx| {
2872 let context_editor = context_editor_task.await?;
2873 context_editor
2874 .update_in(cx, |context_editor, window, cx| {
2875 context_editor.remote_id = Some(id);
2876 context_editor.editor.update(cx, |editor, cx| {
2877 editor.apply_update_proto(
2878 &project,
2879 proto::update_view::Variant::Editor(proto::update_view::Editor {
2880 selections: editor_state.selections,
2881 pending_selection: editor_state.pending_selection,
2882 scroll_top_anchor: editor_state.scroll_top_anchor,
2883 scroll_x: editor_state.scroll_y,
2884 scroll_y: editor_state.scroll_y,
2885 ..Default::default()
2886 }),
2887 window,
2888 cx,
2889 )
2890 })
2891 })?
2892 .await?;
2893 Ok(context_editor)
2894 }))
2895 }
2896
2897 fn to_follow_event(event: &Self::Event) -> Option<item::FollowEvent> {
2898 Editor::to_follow_event(event)
2899 }
2900
2901 fn add_event_to_update_proto(
2902 &self,
2903 event: &Self::Event,
2904 update: &mut Option<proto::update_view::Variant>,
2905 window: &Window,
2906 cx: &App,
2907 ) -> bool {
2908 self.editor
2909 .read(cx)
2910 .add_event_to_update_proto(event, update, window, cx)
2911 }
2912
2913 fn apply_update_proto(
2914 &mut self,
2915 project: &Entity<Project>,
2916 message: proto::update_view::Variant,
2917 window: &mut Window,
2918 cx: &mut Context<Self>,
2919 ) -> Task<Result<()>> {
2920 self.editor.update(cx, |editor, cx| {
2921 editor.apply_update_proto(project, message, window, cx)
2922 })
2923 }
2924
2925 fn is_project_item(&self, _window: &Window, _cx: &App) -> bool {
2926 true
2927 }
2928
2929 fn set_leader_id(
2930 &mut self,
2931 leader_id: Option<CollaboratorId>,
2932 window: &mut Window,
2933 cx: &mut Context<Self>,
2934 ) {
2935 self.editor
2936 .update(cx, |editor, cx| editor.set_leader_id(leader_id, window, cx))
2937 }
2938
2939 fn dedup(&self, existing: &Self, _window: &Window, cx: &App) -> Option<item::Dedup> {
2940 if existing.context.read(cx).id() == self.context.read(cx).id() {
2941 Some(item::Dedup::KeepExisting)
2942 } else {
2943 None
2944 }
2945 }
2946}
2947
2948pub fn render_remaining_tokens(
2949 context_editor: &Entity<TextThreadEditor>,
2950 cx: &App,
2951) -> Option<impl IntoElement + use<>> {
2952 let context = &context_editor.read(cx).context;
2953
2954 let (token_count_color, token_count, max_token_count, tooltip) = match token_state(context, cx)?
2955 {
2956 TokenState::NoTokensLeft {
2957 max_token_count,
2958 token_count,
2959 } => (
2960 Color::Error,
2961 token_count,
2962 max_token_count,
2963 Some("Token Limit Reached"),
2964 ),
2965 TokenState::HasMoreTokens {
2966 max_token_count,
2967 token_count,
2968 over_warn_threshold,
2969 } => {
2970 let (color, tooltip) = if over_warn_threshold {
2971 (Color::Warning, Some("Token Limit is Close to Exhaustion"))
2972 } else {
2973 (Color::Muted, None)
2974 };
2975 (color, token_count, max_token_count, tooltip)
2976 }
2977 };
2978
2979 Some(
2980 h_flex()
2981 .id("token-count")
2982 .gap_0p5()
2983 .child(
2984 Label::new(humanize_token_count(token_count))
2985 .size(LabelSize::Small)
2986 .color(token_count_color),
2987 )
2988 .child(Label::new("/").size(LabelSize::Small).color(Color::Muted))
2989 .child(
2990 Label::new(humanize_token_count(max_token_count))
2991 .size(LabelSize::Small)
2992 .color(Color::Muted),
2993 )
2994 .when_some(tooltip, |element, tooltip| {
2995 element.tooltip(Tooltip::text(tooltip))
2996 }),
2997 )
2998}
2999
3000enum PendingSlashCommand {}
3001
3002fn invoked_slash_command_fold_placeholder(
3003 command_id: InvokedSlashCommandId,
3004 context: WeakEntity<AssistantContext>,
3005) -> FoldPlaceholder {
3006 FoldPlaceholder {
3007 constrain_width: false,
3008 merge_adjacent: false,
3009 render: Arc::new(move |fold_id, _, cx| {
3010 let Some(context) = context.upgrade() else {
3011 return Empty.into_any();
3012 };
3013
3014 let Some(command) = context.read(cx).invoked_slash_command(&command_id) else {
3015 return Empty.into_any();
3016 };
3017
3018 h_flex()
3019 .id(fold_id)
3020 .px_1()
3021 .ml_6()
3022 .gap_2()
3023 .bg(cx.theme().colors().surface_background)
3024 .rounded_sm()
3025 .child(Label::new(format!("/{}", command.name)))
3026 .map(|parent| match &command.status {
3027 InvokedSlashCommandStatus::Running(_) => {
3028 parent.child(Icon::new(IconName::ArrowCircle).with_animation(
3029 "arrow-circle",
3030 Animation::new(Duration::from_secs(4)).repeat(),
3031 |icon, delta| icon.transform(Transformation::rotate(percentage(delta))),
3032 ))
3033 }
3034 InvokedSlashCommandStatus::Error(message) => parent.child(
3035 Label::new(format!("error: {message}"))
3036 .single_line()
3037 .color(Color::Error),
3038 ),
3039 InvokedSlashCommandStatus::Finished => parent,
3040 })
3041 .into_any_element()
3042 }),
3043 type_tag: Some(TypeId::of::<PendingSlashCommand>()),
3044 }
3045}
3046
3047enum TokenState {
3048 NoTokensLeft {
3049 max_token_count: u64,
3050 token_count: u64,
3051 },
3052 HasMoreTokens {
3053 max_token_count: u64,
3054 token_count: u64,
3055 over_warn_threshold: bool,
3056 },
3057}
3058
3059fn token_state(context: &Entity<AssistantContext>, cx: &App) -> Option<TokenState> {
3060 const WARNING_TOKEN_THRESHOLD: f32 = 0.8;
3061
3062 let model = LanguageModelRegistry::read_global(cx)
3063 .default_model()?
3064 .model;
3065 let token_count = context.read(cx).token_count()?;
3066 let max_token_count = model.max_token_count_for_mode(context.read(cx).completion_mode().into());
3067 let token_state = if max_token_count.saturating_sub(token_count) == 0 {
3068 TokenState::NoTokensLeft {
3069 max_token_count,
3070 token_count,
3071 }
3072 } else {
3073 let over_warn_threshold =
3074 token_count as f32 / max_token_count as f32 >= WARNING_TOKEN_THRESHOLD;
3075 TokenState::HasMoreTokens {
3076 max_token_count,
3077 token_count,
3078 over_warn_threshold,
3079 }
3080 };
3081 Some(token_state)
3082}
3083
3084fn size_for_image(data: &RenderImage, max_size: Size<Pixels>) -> Size<Pixels> {
3085 let image_size = data
3086 .size(0)
3087 .map(|dimension| Pixels::from(u32::from(dimension)));
3088 let image_ratio = image_size.width / image_size.height;
3089 let bounds_ratio = max_size.width / max_size.height;
3090
3091 if image_size.width > max_size.width || image_size.height > max_size.height {
3092 if bounds_ratio > image_ratio {
3093 size(
3094 image_size.width * (max_size.height / image_size.height),
3095 max_size.height,
3096 )
3097 } else {
3098 size(
3099 max_size.width,
3100 image_size.height * (max_size.width / image_size.width),
3101 )
3102 }
3103 } else {
3104 size(image_size.width, image_size.height)
3105 }
3106}
3107
3108pub fn humanize_token_count(count: u64) -> String {
3109 match count {
3110 0..=999 => count.to_string(),
3111 1000..=9999 => {
3112 let thousands = count / 1000;
3113 let hundreds = (count % 1000 + 50) / 100;
3114 if hundreds == 0 {
3115 format!("{}k", thousands)
3116 } else if hundreds == 10 {
3117 format!("{}k", thousands + 1)
3118 } else {
3119 format!("{}.{}k", thousands, hundreds)
3120 }
3121 }
3122 1_000_000..=9_999_999 => {
3123 let millions = count / 1_000_000;
3124 let hundred_thousands = (count % 1_000_000 + 50_000) / 100_000;
3125 if hundred_thousands == 0 {
3126 format!("{}M", millions)
3127 } else if hundred_thousands == 10 {
3128 format!("{}M", millions + 1)
3129 } else {
3130 format!("{}.{}M", millions, hundred_thousands)
3131 }
3132 }
3133 10_000_000.. => format!("{}M", (count + 500_000) / 1_000_000),
3134 _ => format!("{}k", (count + 500) / 1000),
3135 }
3136}
3137
3138pub fn make_lsp_adapter_delegate(
3139 project: &Entity<Project>,
3140 cx: &mut App,
3141) -> Result<Option<Arc<dyn LspAdapterDelegate>>> {
3142 project.update(cx, |project, cx| {
3143 // TODO: Find the right worktree.
3144 let Some(worktree) = project.worktrees(cx).next() else {
3145 return Ok(None::<Arc<dyn LspAdapterDelegate>>);
3146 };
3147 let http_client = project.client().http_client();
3148 project.lsp_store().update(cx, |_, cx| {
3149 Ok(Some(LocalLspAdapterDelegate::new(
3150 project.languages().clone(),
3151 project.environment(),
3152 cx.weak_entity(),
3153 &worktree,
3154 http_client,
3155 project.fs().clone(),
3156 cx,
3157 ) as Arc<dyn LspAdapterDelegate>))
3158 })
3159 })
3160}
3161
3162#[cfg(test)]
3163mod tests {
3164 use super::*;
3165 use editor::SelectionEffects;
3166 use fs::FakeFs;
3167 use gpui::{App, TestAppContext, VisualTestContext};
3168 use indoc::indoc;
3169 use language::{Buffer, LanguageRegistry};
3170 use pretty_assertions::assert_eq;
3171 use prompt_store::PromptBuilder;
3172 use text::OffsetRangeExt;
3173 use unindent::Unindent;
3174 use util::path;
3175
3176 #[gpui::test]
3177 async fn test_copy_paste_whole_message(cx: &mut TestAppContext) {
3178 let (context, context_editor, mut cx) = setup_context_editor_text(vec![
3179 (Role::User, "What is the Zed editor?"),
3180 (
3181 Role::Assistant,
3182 "Zed is a modern, high-performance code editor designed from the ground up for speed and collaboration.",
3183 ),
3184 (Role::User, ""),
3185 ],cx).await;
3186
3187 // Select & Copy whole user message
3188 assert_copy_paste_context_editor(
3189 &context_editor,
3190 message_range(&context, 0, &mut cx),
3191 indoc! {"
3192 What is the Zed editor?
3193 Zed is a modern, high-performance code editor designed from the ground up for speed and collaboration.
3194 What is the Zed editor?
3195 "},
3196 &mut cx,
3197 );
3198
3199 // Select & Copy whole assistant message
3200 assert_copy_paste_context_editor(
3201 &context_editor,
3202 message_range(&context, 1, &mut cx),
3203 indoc! {"
3204 What is the Zed editor?
3205 Zed is a modern, high-performance code editor designed from the ground up for speed and collaboration.
3206 What is the Zed editor?
3207 Zed is a modern, high-performance code editor designed from the ground up for speed and collaboration.
3208 "},
3209 &mut cx,
3210 );
3211 }
3212
3213 #[gpui::test]
3214 async fn test_copy_paste_no_selection(cx: &mut TestAppContext) {
3215 let (context, context_editor, mut cx) = setup_context_editor_text(
3216 vec![
3217 (Role::User, "user1"),
3218 (Role::Assistant, "assistant1"),
3219 (Role::Assistant, "assistant2"),
3220 (Role::User, ""),
3221 ],
3222 cx,
3223 )
3224 .await;
3225
3226 // Copy and paste first assistant message
3227 let message_2_range = message_range(&context, 1, &mut cx);
3228 assert_copy_paste_context_editor(
3229 &context_editor,
3230 message_2_range.start..message_2_range.start,
3231 indoc! {"
3232 user1
3233 assistant1
3234 assistant2
3235 assistant1
3236 "},
3237 &mut cx,
3238 );
3239
3240 // Copy and cut second assistant message
3241 let message_3_range = message_range(&context, 2, &mut cx);
3242 assert_copy_paste_context_editor(
3243 &context_editor,
3244 message_3_range.start..message_3_range.start,
3245 indoc! {"
3246 user1
3247 assistant1
3248 assistant2
3249 assistant1
3250 assistant2
3251 "},
3252 &mut cx,
3253 );
3254 }
3255
3256 #[gpui::test]
3257 fn test_find_code_blocks(cx: &mut App) {
3258 let markdown = languages::language("markdown", tree_sitter_md::LANGUAGE.into());
3259
3260 let buffer = cx.new(|cx| {
3261 let text = r#"
3262 line 0
3263 line 1
3264 ```rust
3265 fn main() {}
3266 ```
3267 line 5
3268 line 6
3269 line 7
3270 ```go
3271 func main() {}
3272 ```
3273 line 11
3274 ```
3275 this is plain text code block
3276 ```
3277
3278 ```go
3279 func another() {}
3280 ```
3281 line 19
3282 "#
3283 .unindent();
3284 let mut buffer = Buffer::local(text, cx);
3285 buffer.set_language(Some(markdown.clone()), cx);
3286 buffer
3287 });
3288 let snapshot = buffer.read(cx).snapshot();
3289
3290 let code_blocks = vec![
3291 Point::new(3, 0)..Point::new(4, 0),
3292 Point::new(9, 0)..Point::new(10, 0),
3293 Point::new(13, 0)..Point::new(14, 0),
3294 Point::new(17, 0)..Point::new(18, 0),
3295 ]
3296 .into_iter()
3297 .map(|range| snapshot.point_to_offset(range.start)..snapshot.point_to_offset(range.end))
3298 .collect::<Vec<_>>();
3299
3300 let expected_results = vec![
3301 (0, None),
3302 (1, None),
3303 (2, Some(code_blocks[0].clone())),
3304 (3, Some(code_blocks[0].clone())),
3305 (4, Some(code_blocks[0].clone())),
3306 (5, None),
3307 (6, None),
3308 (7, None),
3309 (8, Some(code_blocks[1].clone())),
3310 (9, Some(code_blocks[1].clone())),
3311 (10, Some(code_blocks[1].clone())),
3312 (11, None),
3313 (12, Some(code_blocks[2].clone())),
3314 (13, Some(code_blocks[2].clone())),
3315 (14, Some(code_blocks[2].clone())),
3316 (15, None),
3317 (16, Some(code_blocks[3].clone())),
3318 (17, Some(code_blocks[3].clone())),
3319 (18, Some(code_blocks[3].clone())),
3320 (19, None),
3321 ];
3322
3323 for (row, expected) in expected_results {
3324 let offset = snapshot.point_to_offset(Point::new(row, 0));
3325 let range = find_surrounding_code_block(&snapshot, offset);
3326 assert_eq!(range, expected, "unexpected result on row {:?}", row);
3327 }
3328 }
3329
3330 async fn setup_context_editor_text(
3331 messages: Vec<(Role, &str)>,
3332 cx: &mut TestAppContext,
3333 ) -> (
3334 Entity<AssistantContext>,
3335 Entity<TextThreadEditor>,
3336 VisualTestContext,
3337 ) {
3338 cx.update(init_test);
3339
3340 let fs = FakeFs::new(cx.executor());
3341 let context = create_context_with_messages(messages, cx);
3342
3343 let project = Project::test(fs.clone(), [path!("/test").as_ref()], cx).await;
3344 let window = cx.add_window(|window, cx| Workspace::test_new(project.clone(), window, cx));
3345 let workspace = window.root(cx).unwrap();
3346 let mut cx = VisualTestContext::from_window(*window, cx);
3347
3348 let context_editor = window
3349 .update(&mut cx, |_, window, cx| {
3350 cx.new(|cx| {
3351 let editor = TextThreadEditor::for_context(
3352 context.clone(),
3353 fs,
3354 workspace.downgrade(),
3355 project,
3356 None,
3357 window,
3358 cx,
3359 );
3360 editor
3361 })
3362 })
3363 .unwrap();
3364
3365 (context, context_editor, cx)
3366 }
3367
3368 fn message_range(
3369 context: &Entity<AssistantContext>,
3370 message_ix: usize,
3371 cx: &mut TestAppContext,
3372 ) -> Range<usize> {
3373 context.update(cx, |context, cx| {
3374 context
3375 .messages(cx)
3376 .nth(message_ix)
3377 .unwrap()
3378 .anchor_range
3379 .to_offset(&context.buffer().read(cx).snapshot())
3380 })
3381 }
3382
3383 fn assert_copy_paste_context_editor<T: editor::ToOffset>(
3384 context_editor: &Entity<TextThreadEditor>,
3385 range: Range<T>,
3386 expected_text: &str,
3387 cx: &mut VisualTestContext,
3388 ) {
3389 context_editor.update_in(cx, |context_editor, window, cx| {
3390 context_editor.editor.update(cx, |editor, cx| {
3391 editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
3392 s.select_ranges([range])
3393 });
3394 });
3395
3396 context_editor.copy(&Default::default(), window, cx);
3397
3398 context_editor.editor.update(cx, |editor, cx| {
3399 editor.move_to_end(&Default::default(), window, cx);
3400 });
3401
3402 context_editor.paste(&Default::default(), window, cx);
3403
3404 context_editor.editor.update(cx, |editor, cx| {
3405 assert_eq!(editor.text(cx), expected_text);
3406 });
3407 });
3408 }
3409
3410 fn create_context_with_messages(
3411 mut messages: Vec<(Role, &str)>,
3412 cx: &mut TestAppContext,
3413 ) -> Entity<AssistantContext> {
3414 let registry = Arc::new(LanguageRegistry::test(cx.executor()));
3415 let prompt_builder = Arc::new(PromptBuilder::new(None).unwrap());
3416 cx.new(|cx| {
3417 let mut context = AssistantContext::local(
3418 registry,
3419 None,
3420 None,
3421 prompt_builder.clone(),
3422 Arc::new(SlashCommandWorkingSet::default()),
3423 cx,
3424 );
3425 let mut message_1 = context.messages(cx).next().unwrap();
3426 let (role, text) = messages.remove(0);
3427
3428 loop {
3429 if role == message_1.role {
3430 context.buffer().update(cx, |buffer, cx| {
3431 buffer.edit([(message_1.offset_range, text)], None, cx);
3432 });
3433 break;
3434 }
3435 let mut ids = HashSet::default();
3436 ids.insert(message_1.id);
3437 context.cycle_message_roles(ids, cx);
3438 message_1 = context.messages(cx).next().unwrap();
3439 }
3440
3441 let mut last_message_id = message_1.id;
3442 for (role, text) in messages {
3443 context.insert_message_after(last_message_id, role, MessageStatus::Done, cx);
3444 let message = context.messages(cx).last().unwrap();
3445 last_message_id = message.id;
3446 context.buffer().update(cx, |buffer, cx| {
3447 buffer.edit([(message.offset_range, text)], None, cx);
3448 })
3449 }
3450
3451 context
3452 })
3453 }
3454
3455 fn init_test(cx: &mut App) {
3456 let settings_store = SettingsStore::test(cx);
3457 prompt_store::init(cx);
3458 LanguageModelRegistry::test(cx);
3459 cx.set_global(settings_store);
3460 language::init(cx);
3461 agent_settings::init(cx);
3462 Project::init_settings(cx);
3463 theme::init(theme::LoadThemes::JustBase, cx);
3464 workspace::init_settings(cx);
3465 editor::init_settings(cx);
3466 }
3467}