text_thread_editor.rs

   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                &sections,
 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            };
1260            let mut new_blocks = vec![];
1261            let mut block_index_to_message = vec![];
1262            for message in self.context.read(cx).messages(cx) {
1263                if let Some(_) = blocks_to_remove.remove(&message.id) {
1264                    // This is an old message that we might modify.
1265                    let Some((meta, block_id)) = old_blocks.get_mut(&message.id) else {
1266                        debug_assert!(
1267                            false,
1268                            "old_blocks should contain a message_id we've just removed."
1269                        );
1270                        continue;
1271                    };
1272                    // Should we modify it?
1273                    let message_meta = MessageMetadata::from(&message);
1274                    if meta != &message_meta {
1275                        blocks_to_replace.insert(*block_id, render_block(message_meta.clone()));
1276                        *meta = message_meta;
1277                    }
1278                } else {
1279                    // This is a new message.
1280                    new_blocks.push(create_block_properties(&message));
1281                    block_index_to_message.push((message.id, MessageMetadata::from(&message)));
1282                }
1283            }
1284            editor.replace_blocks(blocks_to_replace, None, cx);
1285            editor.remove_blocks(blocks_to_remove.into_values().collect(), None, cx);
1286
1287            let ids = editor.insert_blocks(new_blocks, None, cx);
1288            old_blocks.extend(ids.into_iter().zip(block_index_to_message).map(
1289                |(block_id, (message_id, message_meta))| (message_id, (message_meta, block_id)),
1290            ));
1291            self.blocks = old_blocks;
1292        });
1293    }
1294
1295    /// Returns either the selected text, or the content of the Markdown code
1296    /// block surrounding the cursor.
1297    fn get_selection_or_code_block(
1298        context_editor_view: &Entity<TextThreadEditor>,
1299        cx: &mut Context<Workspace>,
1300    ) -> Option<(String, bool)> {
1301        const CODE_FENCE_DELIMITER: &'static str = "```";
1302
1303        let context_editor = context_editor_view.read(cx).editor.clone();
1304        context_editor.update(cx, |context_editor, cx| {
1305            if context_editor.selections.newest::<Point>(cx).is_empty() {
1306                let snapshot = context_editor.buffer().read(cx).snapshot(cx);
1307                let (_, _, snapshot) = snapshot.as_singleton()?;
1308
1309                let head = context_editor.selections.newest::<Point>(cx).head();
1310                let offset = snapshot.point_to_offset(head);
1311
1312                let surrounding_code_block_range = find_surrounding_code_block(snapshot, offset)?;
1313                let mut text = snapshot
1314                    .text_for_range(surrounding_code_block_range)
1315                    .collect::<String>();
1316
1317                // If there is no newline trailing the closing three-backticks, then
1318                // tree-sitter-md extends the range of the content node to include
1319                // the backticks.
1320                if text.ends_with(CODE_FENCE_DELIMITER) {
1321                    text.drain((text.len() - CODE_FENCE_DELIMITER.len())..);
1322                }
1323
1324                (!text.is_empty()).then_some((text, true))
1325            } else {
1326                let selection = context_editor.selections.newest_adjusted(cx);
1327                let buffer = context_editor.buffer().read(cx).snapshot(cx);
1328                let selected_text = buffer.text_for_range(selection.range()).collect::<String>();
1329
1330                (!selected_text.is_empty()).then_some((selected_text, false))
1331            }
1332        })
1333    }
1334
1335    pub fn insert_selection(
1336        workspace: &mut Workspace,
1337        _: &InsertIntoEditor,
1338        window: &mut Window,
1339        cx: &mut Context<Workspace>,
1340    ) {
1341        let Some(agent_panel_delegate) = <dyn AgentPanelDelegate>::try_global(cx) else {
1342            return;
1343        };
1344        let Some(context_editor_view) =
1345            agent_panel_delegate.active_context_editor(workspace, window, cx)
1346        else {
1347            return;
1348        };
1349        let Some(active_editor_view) = workspace
1350            .active_item(cx)
1351            .and_then(|item| item.act_as::<Editor>(cx))
1352        else {
1353            return;
1354        };
1355
1356        if let Some((text, _)) = Self::get_selection_or_code_block(&context_editor_view, cx) {
1357            active_editor_view.update(cx, |editor, cx| {
1358                editor.insert(&text, window, cx);
1359                editor.focus_handle(cx).focus(window);
1360            })
1361        }
1362    }
1363
1364    pub fn copy_code(
1365        workspace: &mut Workspace,
1366        _: &CopyCode,
1367        window: &mut Window,
1368        cx: &mut Context<Workspace>,
1369    ) {
1370        let result = maybe!({
1371            let agent_panel_delegate = <dyn AgentPanelDelegate>::try_global(cx)?;
1372            let context_editor_view =
1373                agent_panel_delegate.active_context_editor(workspace, window, cx)?;
1374            Self::get_selection_or_code_block(&context_editor_view, cx)
1375        });
1376        let Some((text, is_code_block)) = result else {
1377            return;
1378        };
1379
1380        cx.write_to_clipboard(ClipboardItem::new_string(text));
1381
1382        struct CopyToClipboardToast;
1383        workspace.show_toast(
1384            Toast::new(
1385                NotificationId::unique::<CopyToClipboardToast>(),
1386                format!(
1387                    "{} copied to clipboard.",
1388                    if is_code_block {
1389                        "Code block"
1390                    } else {
1391                        "Selection"
1392                    }
1393                ),
1394            )
1395            .autohide(),
1396            cx,
1397        );
1398    }
1399
1400    pub fn handle_insert_dragged_files(
1401        workspace: &mut Workspace,
1402        action: &InsertDraggedFiles,
1403        window: &mut Window,
1404        cx: &mut Context<Workspace>,
1405    ) {
1406        let Some(agent_panel_delegate) = <dyn AgentPanelDelegate>::try_global(cx) else {
1407            return;
1408        };
1409        let Some(context_editor_view) =
1410            agent_panel_delegate.active_context_editor(workspace, window, cx)
1411        else {
1412            return;
1413        };
1414
1415        let project = context_editor_view.read(cx).project.clone();
1416
1417        let paths = match action {
1418            InsertDraggedFiles::ProjectPaths(paths) => Task::ready((paths.clone(), vec![])),
1419            InsertDraggedFiles::ExternalFiles(paths) => {
1420                let tasks = paths
1421                    .clone()
1422                    .into_iter()
1423                    .map(|path| Workspace::project_path_for_path(project.clone(), &path, false, cx))
1424                    .collect::<Vec<_>>();
1425
1426                cx.background_spawn(async move {
1427                    let mut paths = vec![];
1428                    let mut worktrees = vec![];
1429
1430                    let opened_paths = futures::future::join_all(tasks).await;
1431
1432                    for entry in opened_paths {
1433                        if let Some((worktree, project_path)) = entry.log_err() {
1434                            worktrees.push(worktree);
1435                            paths.push(project_path);
1436                        }
1437                    }
1438
1439                    (paths, worktrees)
1440                })
1441            }
1442        };
1443
1444        context_editor_view.update(cx, |_, cx| {
1445            cx.spawn_in(window, async move |this, cx| {
1446                let (paths, dragged_file_worktrees) = paths.await;
1447                this.update_in(cx, |this, window, cx| {
1448                    this.insert_dragged_files(paths, dragged_file_worktrees, window, cx);
1449                })
1450                .ok();
1451            })
1452            .detach();
1453        })
1454    }
1455
1456    pub fn insert_dragged_files(
1457        &mut self,
1458        opened_paths: Vec<ProjectPath>,
1459        added_worktrees: Vec<Entity<Worktree>>,
1460        window: &mut Window,
1461        cx: &mut Context<Self>,
1462    ) {
1463        let mut file_slash_command_args = vec![];
1464        for project_path in opened_paths.into_iter() {
1465            let Some(worktree) = self
1466                .project
1467                .read(cx)
1468                .worktree_for_id(project_path.worktree_id, cx)
1469            else {
1470                continue;
1471            };
1472            let worktree_root_name = worktree.read(cx).root_name().to_string();
1473            let mut full_path = PathBuf::from(worktree_root_name.clone());
1474            full_path.push(&project_path.path);
1475            file_slash_command_args.push(full_path.to_string_lossy().to_string());
1476        }
1477
1478        let cmd_name = FileSlashCommand.name();
1479
1480        let file_argument = file_slash_command_args.join(" ");
1481
1482        self.editor.update(cx, |editor, cx| {
1483            editor.insert("\n", window, cx);
1484            editor.insert(&format!("/{} {}", cmd_name, file_argument), window, cx);
1485        });
1486        self.confirm_command(&ConfirmCommand, window, cx);
1487        self.dragged_file_worktrees.extend(added_worktrees);
1488    }
1489
1490    pub fn quote_selection(
1491        workspace: &mut Workspace,
1492        _: &QuoteSelection,
1493        window: &mut Window,
1494        cx: &mut Context<Workspace>,
1495    ) {
1496        let Some(agent_panel_delegate) = <dyn AgentPanelDelegate>::try_global(cx) else {
1497            return;
1498        };
1499
1500        let Some((selections, buffer)) = maybe!({
1501            let editor = workspace
1502                .active_item(cx)
1503                .and_then(|item| item.act_as::<Editor>(cx))?;
1504
1505            let buffer = editor.read(cx).buffer().clone();
1506            let snapshot = buffer.read(cx).snapshot(cx);
1507            let selections = editor.update(cx, |editor, cx| {
1508                editor
1509                    .selections
1510                    .all_adjusted(cx)
1511                    .into_iter()
1512                    .filter_map(|s| {
1513                        (!s.is_empty())
1514                            .then(|| snapshot.anchor_after(s.start)..snapshot.anchor_before(s.end))
1515                    })
1516                    .collect::<Vec<_>>()
1517            });
1518            Some((selections, buffer))
1519        }) else {
1520            return;
1521        };
1522
1523        if selections.is_empty() {
1524            return;
1525        }
1526
1527        agent_panel_delegate.quote_selection(workspace, selections, buffer, window, cx);
1528    }
1529
1530    pub fn quote_ranges(
1531        &mut self,
1532        ranges: Vec<Range<Point>>,
1533        snapshot: MultiBufferSnapshot,
1534        window: &mut Window,
1535        cx: &mut Context<Self>,
1536    ) {
1537        let creases = selections_creases(ranges, snapshot, cx);
1538
1539        self.editor.update(cx, |editor, cx| {
1540            editor.insert("\n", window, cx);
1541            for (text, crease_title) in creases {
1542                let point = editor.selections.newest::<Point>(cx).head();
1543                let start_row = MultiBufferRow(point.row);
1544
1545                editor.insert(&text, window, cx);
1546
1547                let snapshot = editor.buffer().read(cx).snapshot(cx);
1548                let anchor_before = snapshot.anchor_after(point);
1549                let anchor_after = editor
1550                    .selections
1551                    .newest_anchor()
1552                    .head()
1553                    .bias_left(&snapshot);
1554
1555                editor.insert("\n", window, cx);
1556
1557                let fold_placeholder =
1558                    quote_selection_fold_placeholder(crease_title, cx.entity().downgrade());
1559                let crease = Crease::inline(
1560                    anchor_before..anchor_after,
1561                    fold_placeholder,
1562                    render_quote_selection_output_toggle,
1563                    |_, _, _, _| Empty.into_any(),
1564                );
1565                editor.insert_creases(vec![crease], cx);
1566                editor.fold_at(start_row, window, cx);
1567            }
1568        })
1569    }
1570
1571    fn copy(&mut self, _: &editor::actions::Copy, _window: &mut Window, cx: &mut Context<Self>) {
1572        if self.editor.read(cx).selections.count() == 1 {
1573            let (copied_text, metadata, _) = self.get_clipboard_contents(cx);
1574            cx.write_to_clipboard(ClipboardItem::new_string_with_json_metadata(
1575                copied_text,
1576                metadata,
1577            ));
1578            cx.stop_propagation();
1579            return;
1580        }
1581
1582        cx.propagate();
1583    }
1584
1585    fn cut(&mut self, _: &editor::actions::Cut, window: &mut Window, cx: &mut Context<Self>) {
1586        if self.editor.read(cx).selections.count() == 1 {
1587            let (copied_text, metadata, selections) = self.get_clipboard_contents(cx);
1588
1589            self.editor.update(cx, |editor, cx| {
1590                editor.transact(window, cx, |this, window, cx| {
1591                    this.change_selections(Default::default(), window, cx, |s| {
1592                        s.select(selections);
1593                    });
1594                    this.insert("", window, cx);
1595                    cx.write_to_clipboard(ClipboardItem::new_string_with_json_metadata(
1596                        copied_text,
1597                        metadata,
1598                    ));
1599                });
1600            });
1601
1602            cx.stop_propagation();
1603            return;
1604        }
1605
1606        cx.propagate();
1607    }
1608
1609    fn get_clipboard_contents(
1610        &mut self,
1611        cx: &mut Context<Self>,
1612    ) -> (String, CopyMetadata, Vec<text::Selection<usize>>) {
1613        let (mut selection, creases) = self.editor.update(cx, |editor, cx| {
1614            let mut selection = editor.selections.newest_adjusted(cx);
1615            let snapshot = editor.buffer().read(cx).snapshot(cx);
1616
1617            selection.goal = SelectionGoal::None;
1618
1619            let selection_start = snapshot.point_to_offset(selection.start);
1620
1621            (
1622                selection.map(|point| snapshot.point_to_offset(point)),
1623                editor.display_map.update(cx, |display_map, cx| {
1624                    display_map
1625                        .snapshot(cx)
1626                        .crease_snapshot
1627                        .creases_in_range(
1628                            MultiBufferRow(selection.start.row)
1629                                ..MultiBufferRow(selection.end.row + 1),
1630                            &snapshot,
1631                        )
1632                        .filter_map(|crease| {
1633                            if let Crease::Inline {
1634                                range, metadata, ..
1635                            } = &crease
1636                            {
1637                                let metadata = metadata.as_ref()?;
1638                                let start = range
1639                                    .start
1640                                    .to_offset(&snapshot)
1641                                    .saturating_sub(selection_start);
1642                                let end = range
1643                                    .end
1644                                    .to_offset(&snapshot)
1645                                    .saturating_sub(selection_start);
1646
1647                                let range_relative_to_selection = start..end;
1648                                if !range_relative_to_selection.is_empty() {
1649                                    return Some(SelectedCreaseMetadata {
1650                                        range_relative_to_selection,
1651                                        crease: metadata.clone(),
1652                                    });
1653                                }
1654                            }
1655                            None
1656                        })
1657                        .collect::<Vec<_>>()
1658                }),
1659            )
1660        });
1661
1662        let context = self.context.read(cx);
1663
1664        let mut text = String::new();
1665
1666        // If selection is empty, we want to copy the entire line
1667        if selection.range().is_empty() {
1668            let snapshot = context.buffer().read(cx).snapshot();
1669            let point = snapshot.offset_to_point(selection.range().start);
1670            selection.start = snapshot.point_to_offset(Point::new(point.row, 0));
1671            selection.end = snapshot
1672                .point_to_offset(cmp::min(Point::new(point.row + 1, 0), snapshot.max_point()));
1673            for chunk in context.buffer().read(cx).text_for_range(selection.range()) {
1674                text.push_str(chunk);
1675            }
1676        } else {
1677            for message in context.messages(cx) {
1678                if message.offset_range.start >= selection.range().end {
1679                    break;
1680                } else if message.offset_range.end >= selection.range().start {
1681                    let range = cmp::max(message.offset_range.start, selection.range().start)
1682                        ..cmp::min(message.offset_range.end, selection.range().end);
1683                    if !range.is_empty() {
1684                        for chunk in context.buffer().read(cx).text_for_range(range) {
1685                            text.push_str(chunk);
1686                        }
1687                        if message.offset_range.end < selection.range().end {
1688                            text.push('\n');
1689                        }
1690                    }
1691                }
1692            }
1693        }
1694        (text, CopyMetadata { creases }, vec![selection])
1695    }
1696
1697    fn paste(
1698        &mut self,
1699        action: &editor::actions::Paste,
1700        window: &mut Window,
1701        cx: &mut Context<Self>,
1702    ) {
1703        cx.stop_propagation();
1704
1705        let images = if let Some(item) = cx.read_from_clipboard() {
1706            item.into_entries()
1707                .filter_map(|entry| {
1708                    if let ClipboardEntry::Image(image) = entry {
1709                        Some(image)
1710                    } else {
1711                        None
1712                    }
1713                })
1714                .collect()
1715        } else {
1716            Vec::new()
1717        };
1718
1719        let metadata = if let Some(item) = cx.read_from_clipboard() {
1720            item.entries().first().and_then(|entry| {
1721                if let ClipboardEntry::String(text) = entry {
1722                    text.metadata_json::<CopyMetadata>()
1723                } else {
1724                    None
1725                }
1726            })
1727        } else {
1728            None
1729        };
1730
1731        if images.is_empty() {
1732            self.editor.update(cx, |editor, cx| {
1733                let paste_position = editor.selections.newest::<usize>(cx).head();
1734                editor.paste(action, window, cx);
1735
1736                if let Some(metadata) = metadata {
1737                    let buffer = editor.buffer().read(cx).snapshot(cx);
1738
1739                    let mut buffer_rows_to_fold = BTreeSet::new();
1740                    let weak_editor = cx.entity().downgrade();
1741                    editor.insert_creases(
1742                        metadata.creases.into_iter().map(|metadata| {
1743                            let start = buffer.anchor_after(
1744                                paste_position + metadata.range_relative_to_selection.start,
1745                            );
1746                            let end = buffer.anchor_before(
1747                                paste_position + metadata.range_relative_to_selection.end,
1748                            );
1749
1750                            let buffer_row = MultiBufferRow(start.to_point(&buffer).row);
1751                            buffer_rows_to_fold.insert(buffer_row);
1752                            Crease::inline(
1753                                start..end,
1754                                FoldPlaceholder {
1755                                    render: render_fold_icon_button(
1756                                        weak_editor.clone(),
1757                                        metadata.crease.icon_path.clone(),
1758                                        metadata.crease.label.clone(),
1759                                    ),
1760                                    ..Default::default()
1761                                },
1762                                render_slash_command_output_toggle,
1763                                |_, _, _, _| Empty.into_any(),
1764                            )
1765                            .with_metadata(metadata.crease.clone())
1766                        }),
1767                        cx,
1768                    );
1769                    for buffer_row in buffer_rows_to_fold.into_iter().rev() {
1770                        editor.fold_at(buffer_row, window, cx);
1771                    }
1772                }
1773            });
1774        } else {
1775            let mut image_positions = Vec::new();
1776            self.editor.update(cx, |editor, cx| {
1777                editor.transact(window, cx, |editor, _window, cx| {
1778                    let edits = editor
1779                        .selections
1780                        .all::<usize>(cx)
1781                        .into_iter()
1782                        .map(|selection| (selection.start..selection.end, "\n"));
1783                    editor.edit(edits, cx);
1784
1785                    let snapshot = editor.buffer().read(cx).snapshot(cx);
1786                    for selection in editor.selections.all::<usize>(cx) {
1787                        image_positions.push(snapshot.anchor_before(selection.end));
1788                    }
1789                });
1790            });
1791
1792            self.context.update(cx, |context, cx| {
1793                for image in images {
1794                    let Some(render_image) = image.to_image_data(cx.svg_renderer()).log_err()
1795                    else {
1796                        continue;
1797                    };
1798                    let image_id = image.id();
1799                    let image_task = LanguageModelImage::from_image(Arc::new(image), cx).shared();
1800
1801                    for image_position in image_positions.iter() {
1802                        context.insert_content(
1803                            Content::Image {
1804                                anchor: image_position.text_anchor,
1805                                image_id,
1806                                image: image_task.clone(),
1807                                render_image: render_image.clone(),
1808                            },
1809                            cx,
1810                        );
1811                    }
1812                }
1813            });
1814        }
1815    }
1816
1817    fn update_image_blocks(&mut self, cx: &mut Context<Self>) {
1818        self.editor.update(cx, |editor, cx| {
1819            let buffer = editor.buffer().read(cx).snapshot(cx);
1820            let excerpt_id = *buffer.as_singleton().unwrap().0;
1821            let old_blocks = std::mem::take(&mut self.image_blocks);
1822            let new_blocks = self
1823                .context
1824                .read(cx)
1825                .contents(cx)
1826                .map(
1827                    |Content::Image {
1828                         anchor,
1829                         render_image,
1830                         ..
1831                     }| (anchor, render_image),
1832                )
1833                .filter_map(|(anchor, render_image)| {
1834                    const MAX_HEIGHT_IN_LINES: u32 = 8;
1835                    let anchor = buffer.anchor_in_excerpt(excerpt_id, anchor).unwrap();
1836                    let image = render_image.clone();
1837                    anchor.is_valid(&buffer).then(|| BlockProperties {
1838                        placement: BlockPlacement::Above(anchor),
1839                        height: Some(MAX_HEIGHT_IN_LINES),
1840                        style: BlockStyle::Sticky,
1841                        render: Arc::new(move |cx| {
1842                            let image_size = size_for_image(
1843                                &image,
1844                                size(
1845                                    cx.max_width - cx.margins.gutter.full_width(),
1846                                    MAX_HEIGHT_IN_LINES as f32 * cx.line_height,
1847                                ),
1848                            );
1849                            h_flex()
1850                                .pl(cx.margins.gutter.full_width())
1851                                .child(
1852                                    img(image.clone())
1853                                        .object_fit(gpui::ObjectFit::ScaleDown)
1854                                        .w(image_size.width)
1855                                        .h(image_size.height),
1856                                )
1857                                .into_any_element()
1858                        }),
1859                        priority: 0,
1860                    })
1861                })
1862                .collect::<Vec<_>>();
1863
1864            editor.remove_blocks(old_blocks, None, cx);
1865            let ids = editor.insert_blocks(new_blocks, None, cx);
1866            self.image_blocks = HashSet::from_iter(ids);
1867        });
1868    }
1869
1870    fn split(&mut self, _: &Split, _window: &mut Window, cx: &mut Context<Self>) {
1871        self.context.update(cx, |context, cx| {
1872            let selections = self.editor.read(cx).selections.disjoint_anchors();
1873            for selection in selections.as_ref() {
1874                let buffer = self.editor.read(cx).buffer().read(cx).snapshot(cx);
1875                let range = selection
1876                    .map(|endpoint| endpoint.to_offset(&buffer))
1877                    .range();
1878                context.split_message(range, cx);
1879            }
1880        });
1881    }
1882
1883    fn save(&mut self, _: &Save, _window: &mut Window, cx: &mut Context<Self>) {
1884        self.context.update(cx, |context, cx| {
1885            context.save(Some(Duration::from_millis(500)), self.fs.clone(), cx)
1886        });
1887    }
1888
1889    pub fn title(&self, cx: &App) -> SharedString {
1890        self.context.read(cx).summary().or_default()
1891    }
1892
1893    pub fn regenerate_summary(&mut self, cx: &mut Context<Self>) {
1894        self.context
1895            .update(cx, |context, cx| context.summarize(true, cx));
1896    }
1897
1898    fn render_notice(&self, cx: &mut Context<Self>) -> Option<AnyElement> {
1899        // This was previously gated behind the `zed-pro` feature flag. Since we
1900        // aren't planning to ship that right now, we're just hard-coding this
1901        // value to not show the nudge.
1902        let nudge = Some(false);
1903
1904        let model_registry = LanguageModelRegistry::read_global(cx);
1905
1906        if nudge.map_or(false, |value| value) {
1907            Some(
1908                h_flex()
1909                    .p_3()
1910                    .border_b_1()
1911                    .border_color(cx.theme().colors().border_variant)
1912                    .bg(cx.theme().colors().editor_background)
1913                    .justify_between()
1914                    .child(
1915                        h_flex()
1916                            .gap_3()
1917                            .child(Icon::new(IconName::ZedAssistant).color(Color::Accent))
1918                            .child(Label::new("Zed AI is here! Get started by signing in →")),
1919                    )
1920                    .child(
1921                        Button::new("sign-in", "Sign in")
1922                            .size(ButtonSize::Compact)
1923                            .style(ButtonStyle::Filled)
1924                            .on_click(cx.listener(|this, _event, _window, cx| {
1925                                let client = this
1926                                    .workspace
1927                                    .read_with(cx, |workspace, _| workspace.client().clone())
1928                                    .log_err();
1929
1930                                if let Some(client) = client {
1931                                    cx.spawn(async move |context_editor, cx| {
1932                                        match client.authenticate_and_connect(true, cx).await {
1933                                            util::ConnectionResult::Timeout => {
1934                                                log::error!("Authentication timeout")
1935                                            }
1936                                            util::ConnectionResult::ConnectionReset => {
1937                                                log::error!("Connection reset")
1938                                            }
1939                                            util::ConnectionResult::Result(r) => {
1940                                                if r.log_err().is_some() {
1941                                                    context_editor
1942                                                        .update(cx, |_, cx| cx.notify())
1943                                                        .ok();
1944                                                }
1945                                            }
1946                                        }
1947                                    })
1948                                    .detach()
1949                                }
1950                            })),
1951                    )
1952                    .into_any_element(),
1953            )
1954        } else if let Some(configuration_error) =
1955            model_registry.configuration_error(model_registry.default_model(), cx)
1956        {
1957            Some(
1958                h_flex()
1959                    .px_3()
1960                    .py_2()
1961                    .border_b_1()
1962                    .border_color(cx.theme().colors().border_variant)
1963                    .bg(cx.theme().colors().editor_background)
1964                    .justify_between()
1965                    .child(
1966                        h_flex()
1967                            .gap_3()
1968                            .child(
1969                                Icon::new(IconName::Warning)
1970                                    .size(IconSize::Small)
1971                                    .color(Color::Warning),
1972                            )
1973                            .child(Label::new(configuration_error.to_string())),
1974                    )
1975                    .child(
1976                        Button::new("open-configuration", "Configure Providers")
1977                            .size(ButtonSize::Compact)
1978                            .icon(Some(IconName::SlidersVertical))
1979                            .icon_size(IconSize::Small)
1980                            .icon_position(IconPosition::Start)
1981                            .style(ButtonStyle::Filled)
1982                            .on_click({
1983                                let focus_handle = self.focus_handle(cx).clone();
1984                                move |_event, window, cx| {
1985                                    focus_handle.dispatch_action(
1986                                        &zed_actions::agent::OpenConfiguration,
1987                                        window,
1988                                        cx,
1989                                    );
1990                                }
1991                            }),
1992                    )
1993                    .into_any_element(),
1994            )
1995        } else {
1996            None
1997        }
1998    }
1999
2000    fn render_send_button(&self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
2001        let focus_handle = self.focus_handle(cx).clone();
2002
2003        let (style, tooltip) = match token_state(&self.context, cx) {
2004            Some(TokenState::NoTokensLeft { .. }) => (
2005                ButtonStyle::Tinted(TintColor::Error),
2006                Some(Tooltip::text("Token limit reached")(window, cx)),
2007            ),
2008            Some(TokenState::HasMoreTokens {
2009                over_warn_threshold,
2010                ..
2011            }) => {
2012                let (style, tooltip) = if over_warn_threshold {
2013                    (
2014                        ButtonStyle::Tinted(TintColor::Warning),
2015                        Some(Tooltip::text("Token limit is close to exhaustion")(
2016                            window, cx,
2017                        )),
2018                    )
2019                } else {
2020                    (ButtonStyle::Filled, None)
2021                };
2022                (style, tooltip)
2023            }
2024            None => (ButtonStyle::Filled, None),
2025        };
2026
2027        Button::new("send_button", "Send")
2028            .label_size(LabelSize::Small)
2029            .disabled(self.sending_disabled(cx))
2030            .style(style)
2031            .when_some(tooltip, |button, tooltip| {
2032                button.tooltip(move |_, _| tooltip.clone())
2033            })
2034            .layer(ElevationIndex::ModalSurface)
2035            .key_binding(
2036                KeyBinding::for_action_in(&Assist, &focus_handle, window, cx)
2037                    .map(|kb| kb.size(rems_from_px(12.))),
2038            )
2039            .on_click(move |_event, window, cx| {
2040                focus_handle.dispatch_action(&Assist, window, cx);
2041            })
2042    }
2043
2044    /// Whether or not we should allow messages to be sent.
2045    /// Will return false if the selected provided has a configuration error or
2046    /// if the user has not accepted the terms of service for this provider.
2047    fn sending_disabled(&self, cx: &mut Context<'_, TextThreadEditor>) -> bool {
2048        let model_registry = LanguageModelRegistry::read_global(cx);
2049        let Some(configuration_error) =
2050            model_registry.configuration_error(model_registry.default_model(), cx)
2051        else {
2052            return false;
2053        };
2054
2055        match configuration_error {
2056            ConfigurationError::NoProvider
2057            | ConfigurationError::ModelNotFound
2058            | ConfigurationError::ProviderNotAuthenticated(_) => true,
2059            ConfigurationError::ProviderPendingTermsAcceptance(_) => self.show_accept_terms,
2060        }
2061    }
2062
2063    fn render_inject_context_menu(&self, cx: &mut Context<Self>) -> impl IntoElement {
2064        slash_command_picker::SlashCommandSelector::new(
2065            self.slash_commands.clone(),
2066            cx.entity().downgrade(),
2067            IconButton::new("trigger", IconName::Plus)
2068                .icon_size(IconSize::Small)
2069                .icon_color(Color::Muted),
2070            move |window, cx| {
2071                Tooltip::with_meta(
2072                    "Add Context",
2073                    None,
2074                    "Type / to insert via keyboard",
2075                    window,
2076                    cx,
2077                )
2078            },
2079        )
2080    }
2081
2082    fn render_burn_mode_toggle(&self, cx: &mut Context<Self>) -> Option<AnyElement> {
2083        let context = self.context().read(cx);
2084        let active_model = LanguageModelRegistry::read_global(cx)
2085            .default_model()
2086            .map(|default| default.model)?;
2087        if !active_model.supports_burn_mode() {
2088            return None;
2089        }
2090
2091        let active_completion_mode = context.completion_mode();
2092        let burn_mode_enabled = active_completion_mode == CompletionMode::Burn;
2093        let icon = if burn_mode_enabled {
2094            IconName::ZedBurnModeOn
2095        } else {
2096            IconName::ZedBurnMode
2097        };
2098
2099        Some(
2100            IconButton::new("burn-mode", icon)
2101                .icon_size(IconSize::Small)
2102                .icon_color(Color::Muted)
2103                .toggle_state(burn_mode_enabled)
2104                .selected_icon_color(Color::Error)
2105                .on_click(cx.listener(move |this, _event, _window, cx| {
2106                    this.context().update(cx, |context, _cx| {
2107                        context.set_completion_mode(match active_completion_mode {
2108                            CompletionMode::Burn => CompletionMode::Normal,
2109                            CompletionMode::Normal => CompletionMode::Burn,
2110                        });
2111                    });
2112                }))
2113                .tooltip(move |_window, cx| {
2114                    cx.new(|_| BurnModeTooltip::new().selected(burn_mode_enabled))
2115                        .into()
2116                })
2117                .into_any_element(),
2118        )
2119    }
2120
2121    fn render_language_model_selector(
2122        &self,
2123        window: &mut Window,
2124        cx: &mut Context<Self>,
2125    ) -> impl IntoElement {
2126        let active_model = LanguageModelRegistry::read_global(cx)
2127            .default_model()
2128            .map(|default| default.model);
2129        let model_name = match active_model {
2130            Some(model) => model.name().0,
2131            None => SharedString::from("No model selected"),
2132        };
2133
2134        let active_provider = LanguageModelRegistry::read_global(cx)
2135            .default_model()
2136            .map(|default| default.provider);
2137        let provider_icon = match active_provider {
2138            Some(provider) => provider.icon(),
2139            None => IconName::Ai,
2140        };
2141
2142        let focus_handle = self.editor().focus_handle(cx).clone();
2143
2144        PickerPopoverMenu::new(
2145            self.language_model_selector.clone(),
2146            ButtonLike::new("active-model")
2147                .style(ButtonStyle::Subtle)
2148                .child(
2149                    h_flex()
2150                        .gap_0p5()
2151                        .child(
2152                            Icon::new(provider_icon)
2153                                .color(Color::Muted)
2154                                .size(IconSize::XSmall),
2155                        )
2156                        .child(
2157                            Label::new(model_name)
2158                                .color(Color::Muted)
2159                                .size(LabelSize::Small)
2160                                .ml_0p5(),
2161                        )
2162                        .child(
2163                            Icon::new(IconName::ChevronDown)
2164                                .color(Color::Muted)
2165                                .size(IconSize::XSmall),
2166                        ),
2167                ),
2168            move |window, cx| {
2169                Tooltip::for_action_in(
2170                    "Change Model",
2171                    &ToggleModelSelector,
2172                    &focus_handle,
2173                    window,
2174                    cx,
2175                )
2176            },
2177            gpui::Corner::BottomLeft,
2178            cx,
2179        )
2180        .with_handle(self.language_model_selector_menu_handle.clone())
2181        .render(window, cx)
2182    }
2183
2184    fn render_last_error(&self, cx: &mut Context<Self>) -> Option<AnyElement> {
2185        let last_error = self.last_error.as_ref()?;
2186
2187        Some(
2188            div()
2189                .absolute()
2190                .right_3()
2191                .bottom_12()
2192                .max_w_96()
2193                .py_2()
2194                .px_3()
2195                .elevation_2(cx)
2196                .occlude()
2197                .child(match last_error {
2198                    AssistError::PaymentRequired => self.render_payment_required_error(cx),
2199                    AssistError::Message(error_message) => {
2200                        self.render_assist_error(error_message, cx)
2201                    }
2202                })
2203                .into_any(),
2204        )
2205    }
2206
2207    fn render_payment_required_error(&self, cx: &mut Context<Self>) -> AnyElement {
2208        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.";
2209
2210        v_flex()
2211            .gap_0p5()
2212            .child(
2213                h_flex()
2214                    .gap_1p5()
2215                    .items_center()
2216                    .child(Icon::new(IconName::XCircle).color(Color::Error))
2217                    .child(Label::new("Free Usage Exceeded").weight(FontWeight::MEDIUM)),
2218            )
2219            .child(
2220                div()
2221                    .id("error-message")
2222                    .max_h_24()
2223                    .overflow_y_scroll()
2224                    .child(Label::new(ERROR_MESSAGE)),
2225            )
2226            .child(
2227                h_flex()
2228                    .justify_end()
2229                    .mt_1()
2230                    .child(Button::new("subscribe", "Subscribe").on_click(cx.listener(
2231                        |this, _, _window, cx| {
2232                            this.last_error = None;
2233                            cx.open_url(&zed_urls::account_url(cx));
2234                            cx.notify();
2235                        },
2236                    )))
2237                    .child(Button::new("dismiss", "Dismiss").on_click(cx.listener(
2238                        |this, _, _window, cx| {
2239                            this.last_error = None;
2240                            cx.notify();
2241                        },
2242                    ))),
2243            )
2244            .into_any()
2245    }
2246
2247    fn render_assist_error(
2248        &self,
2249        error_message: &SharedString,
2250        cx: &mut Context<Self>,
2251    ) -> AnyElement {
2252        v_flex()
2253            .gap_0p5()
2254            .child(
2255                h_flex()
2256                    .gap_1p5()
2257                    .items_center()
2258                    .child(Icon::new(IconName::XCircle).color(Color::Error))
2259                    .child(
2260                        Label::new("Error interacting with language model")
2261                            .weight(FontWeight::MEDIUM),
2262                    ),
2263            )
2264            .child(
2265                div()
2266                    .id("error-message")
2267                    .max_h_32()
2268                    .overflow_y_scroll()
2269                    .child(Label::new(error_message.clone())),
2270            )
2271            .child(
2272                h_flex()
2273                    .justify_end()
2274                    .mt_1()
2275                    .child(Button::new("dismiss", "Dismiss").on_click(cx.listener(
2276                        |this, _, _window, cx| {
2277                            this.last_error = None;
2278                            cx.notify();
2279                        },
2280                    ))),
2281            )
2282            .into_any()
2283    }
2284}
2285
2286/// Returns the contents of the *outermost* fenced code block that contains the given offset.
2287fn find_surrounding_code_block(snapshot: &BufferSnapshot, offset: usize) -> Option<Range<usize>> {
2288    const CODE_BLOCK_NODE: &'static str = "fenced_code_block";
2289    const CODE_BLOCK_CONTENT: &'static str = "code_fence_content";
2290
2291    let layer = snapshot.syntax_layers().next()?;
2292
2293    let root_node = layer.node();
2294    let mut cursor = root_node.walk();
2295
2296    // Go to the first child for the given offset
2297    while cursor.goto_first_child_for_byte(offset).is_some() {
2298        // If we're at the end of the node, go to the next one.
2299        // Example: if you have a fenced-code-block, and you're on the start of the line
2300        // right after the closing ```, you want to skip the fenced-code-block and
2301        // go to the next sibling.
2302        if cursor.node().end_byte() == offset {
2303            cursor.goto_next_sibling();
2304        }
2305
2306        if cursor.node().start_byte() > offset {
2307            break;
2308        }
2309
2310        // We found the fenced code block.
2311        if cursor.node().kind() == CODE_BLOCK_NODE {
2312            // Now we need to find the child node that contains the code.
2313            cursor.goto_first_child();
2314            loop {
2315                if cursor.node().kind() == CODE_BLOCK_CONTENT {
2316                    return Some(cursor.node().byte_range());
2317                }
2318                if !cursor.goto_next_sibling() {
2319                    break;
2320                }
2321            }
2322        }
2323    }
2324
2325    None
2326}
2327
2328fn render_thought_process_fold_icon_button(
2329    editor: WeakEntity<Editor>,
2330    status: ThoughtProcessStatus,
2331) -> Arc<dyn Send + Sync + Fn(FoldId, Range<Anchor>, &mut App) -> AnyElement> {
2332    Arc::new(move |fold_id, fold_range, _cx| {
2333        let editor = editor.clone();
2334
2335        let button = ButtonLike::new(fold_id).layer(ElevationIndex::ElevatedSurface);
2336        let button = match status {
2337            ThoughtProcessStatus::Pending => button
2338                .child(
2339                    Icon::new(IconName::LightBulb)
2340                        .size(IconSize::Small)
2341                        .color(Color::Muted),
2342                )
2343                .child(
2344                    Label::new("Thinking…").color(Color::Muted).with_animation(
2345                        "pulsating-label",
2346                        Animation::new(Duration::from_secs(2))
2347                            .repeat()
2348                            .with_easing(pulsating_between(0.4, 0.8)),
2349                        |label, delta| label.alpha(delta),
2350                    ),
2351                ),
2352            ThoughtProcessStatus::Completed => button
2353                .style(ButtonStyle::Filled)
2354                .child(Icon::new(IconName::LightBulb).size(IconSize::Small))
2355                .child(Label::new("Thought Process").single_line()),
2356        };
2357
2358        button
2359            .on_click(move |_, window, cx| {
2360                editor
2361                    .update(cx, |editor, cx| {
2362                        let buffer_start = fold_range
2363                            .start
2364                            .to_point(&editor.buffer().read(cx).read(cx));
2365                        let buffer_row = MultiBufferRow(buffer_start.row);
2366                        editor.unfold_at(buffer_row, window, cx);
2367                    })
2368                    .ok();
2369            })
2370            .into_any_element()
2371    })
2372}
2373
2374fn render_fold_icon_button(
2375    editor: WeakEntity<Editor>,
2376    icon_path: SharedString,
2377    label: SharedString,
2378) -> Arc<dyn Send + Sync + Fn(FoldId, Range<Anchor>, &mut App) -> AnyElement> {
2379    Arc::new(move |fold_id, fold_range, _cx| {
2380        let editor = editor.clone();
2381        ButtonLike::new(fold_id)
2382            .style(ButtonStyle::Filled)
2383            .layer(ElevationIndex::ElevatedSurface)
2384            .child(Icon::from_path(icon_path.clone()))
2385            .child(Label::new(label.clone()).single_line())
2386            .on_click(move |_, window, cx| {
2387                editor
2388                    .update(cx, |editor, cx| {
2389                        let buffer_start = fold_range
2390                            .start
2391                            .to_point(&editor.buffer().read(cx).read(cx));
2392                        let buffer_row = MultiBufferRow(buffer_start.row);
2393                        editor.unfold_at(buffer_row, window, cx);
2394                    })
2395                    .ok();
2396            })
2397            .into_any_element()
2398    })
2399}
2400
2401type ToggleFold = Arc<dyn Fn(bool, &mut Window, &mut App) + Send + Sync>;
2402
2403fn render_slash_command_output_toggle(
2404    row: MultiBufferRow,
2405    is_folded: bool,
2406    fold: ToggleFold,
2407    _window: &mut Window,
2408    _cx: &mut App,
2409) -> AnyElement {
2410    Disclosure::new(
2411        ("slash-command-output-fold-indicator", row.0 as u64),
2412        !is_folded,
2413    )
2414    .toggle_state(is_folded)
2415    .on_click(move |_e, window, cx| fold(!is_folded, window, cx))
2416    .into_any_element()
2417}
2418
2419pub fn fold_toggle(
2420    name: &'static str,
2421) -> impl Fn(
2422    MultiBufferRow,
2423    bool,
2424    Arc<dyn Fn(bool, &mut Window, &mut App) + Send + Sync>,
2425    &mut Window,
2426    &mut App,
2427) -> AnyElement {
2428    move |row, is_folded, fold, _window, _cx| {
2429        Disclosure::new((name, row.0 as u64), !is_folded)
2430            .toggle_state(is_folded)
2431            .on_click(move |_e, window, cx| fold(!is_folded, window, cx))
2432            .into_any_element()
2433    }
2434}
2435
2436fn quote_selection_fold_placeholder(title: String, editor: WeakEntity<Editor>) -> FoldPlaceholder {
2437    FoldPlaceholder {
2438        render: Arc::new({
2439            move |fold_id, fold_range, _cx| {
2440                let editor = editor.clone();
2441                ButtonLike::new(fold_id)
2442                    .style(ButtonStyle::Filled)
2443                    .layer(ElevationIndex::ElevatedSurface)
2444                    .child(Icon::new(IconName::TextSnippet))
2445                    .child(Label::new(title.clone()).single_line())
2446                    .on_click(move |_, window, cx| {
2447                        editor
2448                            .update(cx, |editor, cx| {
2449                                let buffer_start = fold_range
2450                                    .start
2451                                    .to_point(&editor.buffer().read(cx).read(cx));
2452                                let buffer_row = MultiBufferRow(buffer_start.row);
2453                                editor.unfold_at(buffer_row, window, cx);
2454                            })
2455                            .ok();
2456                    })
2457                    .into_any_element()
2458            }
2459        }),
2460        merge_adjacent: false,
2461        ..Default::default()
2462    }
2463}
2464
2465fn render_quote_selection_output_toggle(
2466    row: MultiBufferRow,
2467    is_folded: bool,
2468    fold: ToggleFold,
2469    _window: &mut Window,
2470    _cx: &mut App,
2471) -> AnyElement {
2472    Disclosure::new(("quote-selection-indicator", row.0 as u64), !is_folded)
2473        .toggle_state(is_folded)
2474        .on_click(move |_e, window, cx| fold(!is_folded, window, cx))
2475        .into_any_element()
2476}
2477
2478fn render_pending_slash_command_gutter_decoration(
2479    row: MultiBufferRow,
2480    status: &PendingSlashCommandStatus,
2481    confirm_command: Arc<dyn Fn(&mut Window, &mut App)>,
2482) -> AnyElement {
2483    let mut icon = IconButton::new(
2484        ("slash-command-gutter-decoration", row.0),
2485        ui::IconName::TriangleRight,
2486    )
2487    .on_click(move |_e, window, cx| confirm_command(window, cx))
2488    .icon_size(ui::IconSize::Small)
2489    .size(ui::ButtonSize::None);
2490
2491    match status {
2492        PendingSlashCommandStatus::Idle => {
2493            icon = icon.icon_color(Color::Muted);
2494        }
2495        PendingSlashCommandStatus::Running { .. } => {
2496            icon = icon.toggle_state(true);
2497        }
2498        PendingSlashCommandStatus::Error(_) => icon = icon.icon_color(Color::Error),
2499    }
2500
2501    icon.into_any_element()
2502}
2503
2504fn render_docs_slash_command_trailer(
2505    row: MultiBufferRow,
2506    command: ParsedSlashCommand,
2507    cx: &mut App,
2508) -> AnyElement {
2509    if command.arguments.is_empty() {
2510        return Empty.into_any();
2511    }
2512    let args = DocsSlashCommandArgs::parse(&command.arguments);
2513
2514    let Some(store) = args
2515        .provider()
2516        .and_then(|provider| IndexedDocsStore::try_global(provider, cx).ok())
2517    else {
2518        return Empty.into_any();
2519    };
2520
2521    let Some(package) = args.package() else {
2522        return Empty.into_any();
2523    };
2524
2525    let mut children = Vec::new();
2526
2527    if store.is_indexing(&package) {
2528        children.push(
2529            div()
2530                .id(("crates-being-indexed", row.0))
2531                .child(Icon::new(IconName::ArrowCircle).with_animation(
2532                    "arrow-circle",
2533                    Animation::new(Duration::from_secs(4)).repeat(),
2534                    |icon, delta| icon.transform(Transformation::rotate(percentage(delta))),
2535                ))
2536                .tooltip({
2537                    let package = package.clone();
2538                    Tooltip::text(format!("Indexing {package}"))
2539                })
2540                .into_any_element(),
2541        );
2542    }
2543
2544    if let Some(latest_error) = store.latest_error_for_package(&package) {
2545        children.push(
2546            div()
2547                .id(("latest-error", row.0))
2548                .child(
2549                    Icon::new(IconName::Warning)
2550                        .size(IconSize::Small)
2551                        .color(Color::Warning),
2552                )
2553                .tooltip(Tooltip::text(format!("Failed to index: {latest_error}")))
2554                .into_any_element(),
2555        )
2556    }
2557
2558    let is_indexing = store.is_indexing(&package);
2559    let latest_error = store.latest_error_for_package(&package);
2560
2561    if !is_indexing && latest_error.is_none() {
2562        return Empty.into_any();
2563    }
2564
2565    h_flex().gap_2().children(children).into_any_element()
2566}
2567
2568#[derive(Debug, Clone, Serialize, Deserialize)]
2569struct CopyMetadata {
2570    creases: Vec<SelectedCreaseMetadata>,
2571}
2572
2573#[derive(Debug, Clone, Serialize, Deserialize)]
2574struct SelectedCreaseMetadata {
2575    range_relative_to_selection: Range<usize>,
2576    crease: CreaseMetadata,
2577}
2578
2579impl EventEmitter<EditorEvent> for TextThreadEditor {}
2580impl EventEmitter<SearchEvent> for TextThreadEditor {}
2581
2582impl Render for TextThreadEditor {
2583    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
2584        let provider = LanguageModelRegistry::read_global(cx)
2585            .default_model()
2586            .map(|default| default.provider);
2587
2588        let accept_terms = if self.show_accept_terms {
2589            provider.as_ref().and_then(|provider| {
2590                provider.render_accept_terms(LanguageModelProviderTosView::PromptEditorPopup, cx)
2591            })
2592        } else {
2593            None
2594        };
2595
2596        let language_model_selector = self.language_model_selector_menu_handle.clone();
2597        let burn_mode_toggle = self.render_burn_mode_toggle(cx);
2598
2599        v_flex()
2600            .key_context("ContextEditor")
2601            .capture_action(cx.listener(TextThreadEditor::cancel))
2602            .capture_action(cx.listener(TextThreadEditor::save))
2603            .capture_action(cx.listener(TextThreadEditor::copy))
2604            .capture_action(cx.listener(TextThreadEditor::cut))
2605            .capture_action(cx.listener(TextThreadEditor::paste))
2606            .capture_action(cx.listener(TextThreadEditor::cycle_message_role))
2607            .capture_action(cx.listener(TextThreadEditor::confirm_command))
2608            .on_action(cx.listener(TextThreadEditor::assist))
2609            .on_action(cx.listener(TextThreadEditor::split))
2610            .on_action(move |_: &ToggleModelSelector, window, cx| {
2611                language_model_selector.toggle(window, cx);
2612            })
2613            .size_full()
2614            .children(self.render_notice(cx))
2615            .child(
2616                div()
2617                    .flex_grow()
2618                    .bg(cx.theme().colors().editor_background)
2619                    .child(self.editor.clone()),
2620            )
2621            .when_some(accept_terms, |this, element| {
2622                this.child(
2623                    div()
2624                        .absolute()
2625                        .right_3()
2626                        .bottom_12()
2627                        .max_w_96()
2628                        .py_2()
2629                        .px_3()
2630                        .elevation_2(cx)
2631                        .bg(cx.theme().colors().surface_background)
2632                        .occlude()
2633                        .child(element),
2634                )
2635            })
2636            .children(self.render_last_error(cx))
2637            .child(
2638                h_flex()
2639                    .relative()
2640                    .py_2()
2641                    .pl_1p5()
2642                    .pr_2()
2643                    .w_full()
2644                    .justify_between()
2645                    .border_t_1()
2646                    .border_color(cx.theme().colors().border_variant)
2647                    .bg(cx.theme().colors().editor_background)
2648                    .child(
2649                        h_flex()
2650                            .gap_0p5()
2651                            .child(self.render_inject_context_menu(cx))
2652                            .when_some(burn_mode_toggle, |this, element| this.child(element)),
2653                    )
2654                    .child(
2655                        h_flex()
2656                            .gap_1()
2657                            .child(self.render_language_model_selector(window, cx))
2658                            .child(self.render_send_button(window, cx)),
2659                    ),
2660            )
2661    }
2662}
2663
2664impl Focusable for TextThreadEditor {
2665    fn focus_handle(&self, cx: &App) -> FocusHandle {
2666        self.editor.focus_handle(cx)
2667    }
2668}
2669
2670impl Item for TextThreadEditor {
2671    type Event = editor::EditorEvent;
2672
2673    fn tab_content_text(&self, _detail: usize, cx: &App) -> SharedString {
2674        util::truncate_and_trailoff(&self.title(cx), MAX_TAB_TITLE_LEN).into()
2675    }
2676
2677    fn to_item_events(event: &Self::Event, mut f: impl FnMut(item::ItemEvent)) {
2678        match event {
2679            EditorEvent::Edited { .. } => {
2680                f(item::ItemEvent::Edit);
2681            }
2682            EditorEvent::TitleChanged => {
2683                f(item::ItemEvent::UpdateTab);
2684            }
2685            _ => {}
2686        }
2687    }
2688
2689    fn tab_tooltip_text(&self, cx: &App) -> Option<SharedString> {
2690        Some(self.title(cx).to_string().into())
2691    }
2692
2693    fn as_searchable(&self, handle: &Entity<Self>) -> Option<Box<dyn SearchableItemHandle>> {
2694        Some(Box::new(handle.clone()))
2695    }
2696
2697    fn set_nav_history(
2698        &mut self,
2699        nav_history: pane::ItemNavHistory,
2700        window: &mut Window,
2701        cx: &mut Context<Self>,
2702    ) {
2703        self.editor.update(cx, |editor, cx| {
2704            Item::set_nav_history(editor, nav_history, window, cx)
2705        })
2706    }
2707
2708    fn navigate(
2709        &mut self,
2710        data: Box<dyn std::any::Any>,
2711        window: &mut Window,
2712        cx: &mut Context<Self>,
2713    ) -> bool {
2714        self.editor
2715            .update(cx, |editor, cx| Item::navigate(editor, data, window, cx))
2716    }
2717
2718    fn deactivated(&mut self, window: &mut Window, cx: &mut Context<Self>) {
2719        self.editor
2720            .update(cx, |editor, cx| Item::deactivated(editor, window, cx))
2721    }
2722
2723    fn act_as_type<'a>(
2724        &'a self,
2725        type_id: TypeId,
2726        self_handle: &'a Entity<Self>,
2727        _: &'a App,
2728    ) -> Option<AnyView> {
2729        if type_id == TypeId::of::<Self>() {
2730            Some(self_handle.to_any())
2731        } else if type_id == TypeId::of::<Editor>() {
2732            Some(self.editor.to_any())
2733        } else {
2734            None
2735        }
2736    }
2737
2738    fn include_in_nav_history() -> bool {
2739        false
2740    }
2741}
2742
2743impl SearchableItem for TextThreadEditor {
2744    type Match = <Editor as SearchableItem>::Match;
2745
2746    fn clear_matches(&mut self, window: &mut Window, cx: &mut Context<Self>) {
2747        self.editor.update(cx, |editor, cx| {
2748            editor.clear_matches(window, cx);
2749        });
2750    }
2751
2752    fn update_matches(
2753        &mut self,
2754        matches: &[Self::Match],
2755        window: &mut Window,
2756        cx: &mut Context<Self>,
2757    ) {
2758        self.editor
2759            .update(cx, |editor, cx| editor.update_matches(matches, window, cx));
2760    }
2761
2762    fn query_suggestion(&mut self, window: &mut Window, cx: &mut Context<Self>) -> String {
2763        self.editor
2764            .update(cx, |editor, cx| editor.query_suggestion(window, cx))
2765    }
2766
2767    fn activate_match(
2768        &mut self,
2769        index: usize,
2770        matches: &[Self::Match],
2771        window: &mut Window,
2772        cx: &mut Context<Self>,
2773    ) {
2774        self.editor.update(cx, |editor, cx| {
2775            editor.activate_match(index, matches, window, cx);
2776        });
2777    }
2778
2779    fn select_matches(
2780        &mut self,
2781        matches: &[Self::Match],
2782        window: &mut Window,
2783        cx: &mut Context<Self>,
2784    ) {
2785        self.editor
2786            .update(cx, |editor, cx| editor.select_matches(matches, window, cx));
2787    }
2788
2789    fn replace(
2790        &mut self,
2791        identifier: &Self::Match,
2792        query: &project::search::SearchQuery,
2793        window: &mut Window,
2794        cx: &mut Context<Self>,
2795    ) {
2796        self.editor.update(cx, |editor, cx| {
2797            editor.replace(identifier, query, window, cx)
2798        });
2799    }
2800
2801    fn find_matches(
2802        &mut self,
2803        query: Arc<project::search::SearchQuery>,
2804        window: &mut Window,
2805        cx: &mut Context<Self>,
2806    ) -> Task<Vec<Self::Match>> {
2807        self.editor
2808            .update(cx, |editor, cx| editor.find_matches(query, window, cx))
2809    }
2810
2811    fn active_match_index(
2812        &mut self,
2813        direction: Direction,
2814        matches: &[Self::Match],
2815        window: &mut Window,
2816        cx: &mut Context<Self>,
2817    ) -> Option<usize> {
2818        self.editor.update(cx, |editor, cx| {
2819            editor.active_match_index(direction, matches, window, cx)
2820        })
2821    }
2822}
2823
2824impl FollowableItem for TextThreadEditor {
2825    fn remote_id(&self) -> Option<workspace::ViewId> {
2826        self.remote_id
2827    }
2828
2829    fn to_state_proto(&self, window: &Window, cx: &App) -> Option<proto::view::Variant> {
2830        let context = self.context.read(cx);
2831        Some(proto::view::Variant::ContextEditor(
2832            proto::view::ContextEditor {
2833                context_id: context.id().to_proto(),
2834                editor: if let Some(proto::view::Variant::Editor(proto)) =
2835                    self.editor.read(cx).to_state_proto(window, cx)
2836                {
2837                    Some(proto)
2838                } else {
2839                    None
2840                },
2841            },
2842        ))
2843    }
2844
2845    fn from_state_proto(
2846        workspace: Entity<Workspace>,
2847        id: workspace::ViewId,
2848        state: &mut Option<proto::view::Variant>,
2849        window: &mut Window,
2850        cx: &mut App,
2851    ) -> Option<Task<Result<Entity<Self>>>> {
2852        let proto::view::Variant::ContextEditor(_) = state.as_ref()? else {
2853            return None;
2854        };
2855        let Some(proto::view::Variant::ContextEditor(state)) = state.take() else {
2856            unreachable!()
2857        };
2858
2859        let context_id = ContextId::from_proto(state.context_id);
2860        let editor_state = state.editor?;
2861
2862        let project = workspace.read(cx).project().clone();
2863        let agent_panel_delegate = <dyn AgentPanelDelegate>::try_global(cx)?;
2864
2865        let context_editor_task = workspace.update(cx, |workspace, cx| {
2866            agent_panel_delegate.open_remote_context(workspace, context_id, window, cx)
2867        });
2868
2869        Some(window.spawn(cx, async move |cx| {
2870            let context_editor = context_editor_task.await?;
2871            context_editor
2872                .update_in(cx, |context_editor, window, cx| {
2873                    context_editor.remote_id = Some(id);
2874                    context_editor.editor.update(cx, |editor, cx| {
2875                        editor.apply_update_proto(
2876                            &project,
2877                            proto::update_view::Variant::Editor(proto::update_view::Editor {
2878                                selections: editor_state.selections,
2879                                pending_selection: editor_state.pending_selection,
2880                                scroll_top_anchor: editor_state.scroll_top_anchor,
2881                                scroll_x: editor_state.scroll_y,
2882                                scroll_y: editor_state.scroll_y,
2883                                ..Default::default()
2884                            }),
2885                            window,
2886                            cx,
2887                        )
2888                    })
2889                })?
2890                .await?;
2891            Ok(context_editor)
2892        }))
2893    }
2894
2895    fn to_follow_event(event: &Self::Event) -> Option<item::FollowEvent> {
2896        Editor::to_follow_event(event)
2897    }
2898
2899    fn add_event_to_update_proto(
2900        &self,
2901        event: &Self::Event,
2902        update: &mut Option<proto::update_view::Variant>,
2903        window: &Window,
2904        cx: &App,
2905    ) -> bool {
2906        self.editor
2907            .read(cx)
2908            .add_event_to_update_proto(event, update, window, cx)
2909    }
2910
2911    fn apply_update_proto(
2912        &mut self,
2913        project: &Entity<Project>,
2914        message: proto::update_view::Variant,
2915        window: &mut Window,
2916        cx: &mut Context<Self>,
2917    ) -> Task<Result<()>> {
2918        self.editor.update(cx, |editor, cx| {
2919            editor.apply_update_proto(project, message, window, cx)
2920        })
2921    }
2922
2923    fn is_project_item(&self, _window: &Window, _cx: &App) -> bool {
2924        true
2925    }
2926
2927    fn set_leader_id(
2928        &mut self,
2929        leader_id: Option<CollaboratorId>,
2930        window: &mut Window,
2931        cx: &mut Context<Self>,
2932    ) {
2933        self.editor
2934            .update(cx, |editor, cx| editor.set_leader_id(leader_id, window, cx))
2935    }
2936
2937    fn dedup(&self, existing: &Self, _window: &Window, cx: &App) -> Option<item::Dedup> {
2938        if existing.context.read(cx).id() == self.context.read(cx).id() {
2939            Some(item::Dedup::KeepExisting)
2940        } else {
2941            None
2942        }
2943    }
2944}
2945
2946pub fn render_remaining_tokens(
2947    context_editor: &Entity<TextThreadEditor>,
2948    cx: &App,
2949) -> Option<impl IntoElement + use<>> {
2950    let context = &context_editor.read(cx).context;
2951
2952    let (token_count_color, token_count, max_token_count, tooltip) = match token_state(context, cx)?
2953    {
2954        TokenState::NoTokensLeft {
2955            max_token_count,
2956            token_count,
2957        } => (
2958            Color::Error,
2959            token_count,
2960            max_token_count,
2961            Some("Token Limit Reached"),
2962        ),
2963        TokenState::HasMoreTokens {
2964            max_token_count,
2965            token_count,
2966            over_warn_threshold,
2967        } => {
2968            let (color, tooltip) = if over_warn_threshold {
2969                (Color::Warning, Some("Token Limit is Close to Exhaustion"))
2970            } else {
2971                (Color::Muted, None)
2972            };
2973            (color, token_count, max_token_count, tooltip)
2974        }
2975    };
2976
2977    Some(
2978        h_flex()
2979            .id("token-count")
2980            .gap_0p5()
2981            .child(
2982                Label::new(humanize_token_count(token_count))
2983                    .size(LabelSize::Small)
2984                    .color(token_count_color),
2985            )
2986            .child(Label::new("/").size(LabelSize::Small).color(Color::Muted))
2987            .child(
2988                Label::new(humanize_token_count(max_token_count))
2989                    .size(LabelSize::Small)
2990                    .color(Color::Muted),
2991            )
2992            .when_some(tooltip, |element, tooltip| {
2993                element.tooltip(Tooltip::text(tooltip))
2994            }),
2995    )
2996}
2997
2998enum PendingSlashCommand {}
2999
3000fn invoked_slash_command_fold_placeholder(
3001    command_id: InvokedSlashCommandId,
3002    context: WeakEntity<AssistantContext>,
3003) -> FoldPlaceholder {
3004    FoldPlaceholder {
3005        constrain_width: false,
3006        merge_adjacent: false,
3007        render: Arc::new(move |fold_id, _, cx| {
3008            let Some(context) = context.upgrade() else {
3009                return Empty.into_any();
3010            };
3011
3012            let Some(command) = context.read(cx).invoked_slash_command(&command_id) else {
3013                return Empty.into_any();
3014            };
3015
3016            h_flex()
3017                .id(fold_id)
3018                .px_1()
3019                .ml_6()
3020                .gap_2()
3021                .bg(cx.theme().colors().surface_background)
3022                .rounded_sm()
3023                .child(Label::new(format!("/{}", command.name)))
3024                .map(|parent| match &command.status {
3025                    InvokedSlashCommandStatus::Running(_) => {
3026                        parent.child(Icon::new(IconName::ArrowCircle).with_animation(
3027                            "arrow-circle",
3028                            Animation::new(Duration::from_secs(4)).repeat(),
3029                            |icon, delta| icon.transform(Transformation::rotate(percentage(delta))),
3030                        ))
3031                    }
3032                    InvokedSlashCommandStatus::Error(message) => parent.child(
3033                        Label::new(format!("error: {message}"))
3034                            .single_line()
3035                            .color(Color::Error),
3036                    ),
3037                    InvokedSlashCommandStatus::Finished => parent,
3038                })
3039                .into_any_element()
3040        }),
3041        type_tag: Some(TypeId::of::<PendingSlashCommand>()),
3042    }
3043}
3044
3045enum TokenState {
3046    NoTokensLeft {
3047        max_token_count: u64,
3048        token_count: u64,
3049    },
3050    HasMoreTokens {
3051        max_token_count: u64,
3052        token_count: u64,
3053        over_warn_threshold: bool,
3054    },
3055}
3056
3057fn token_state(context: &Entity<AssistantContext>, cx: &App) -> Option<TokenState> {
3058    const WARNING_TOKEN_THRESHOLD: f32 = 0.8;
3059
3060    let model = LanguageModelRegistry::read_global(cx)
3061        .default_model()?
3062        .model;
3063    let token_count = context.read(cx).token_count()?;
3064    let max_token_count = model.max_token_count_for_mode(context.read(cx).completion_mode().into());
3065    let token_state = if max_token_count.saturating_sub(token_count) == 0 {
3066        TokenState::NoTokensLeft {
3067            max_token_count,
3068            token_count,
3069        }
3070    } else {
3071        let over_warn_threshold =
3072            token_count as f32 / max_token_count as f32 >= WARNING_TOKEN_THRESHOLD;
3073        TokenState::HasMoreTokens {
3074            max_token_count,
3075            token_count,
3076            over_warn_threshold,
3077        }
3078    };
3079    Some(token_state)
3080}
3081
3082fn size_for_image(data: &RenderImage, max_size: Size<Pixels>) -> Size<Pixels> {
3083    let image_size = data
3084        .size(0)
3085        .map(|dimension| Pixels::from(u32::from(dimension)));
3086    let image_ratio = image_size.width / image_size.height;
3087    let bounds_ratio = max_size.width / max_size.height;
3088
3089    if image_size.width > max_size.width || image_size.height > max_size.height {
3090        if bounds_ratio > image_ratio {
3091            size(
3092                image_size.width * (max_size.height / image_size.height),
3093                max_size.height,
3094            )
3095        } else {
3096            size(
3097                max_size.width,
3098                image_size.height * (max_size.width / image_size.width),
3099            )
3100        }
3101    } else {
3102        size(image_size.width, image_size.height)
3103    }
3104}
3105
3106pub fn humanize_token_count(count: u64) -> String {
3107    match count {
3108        0..=999 => count.to_string(),
3109        1000..=9999 => {
3110            let thousands = count / 1000;
3111            let hundreds = (count % 1000 + 50) / 100;
3112            if hundreds == 0 {
3113                format!("{}k", thousands)
3114            } else if hundreds == 10 {
3115                format!("{}k", thousands + 1)
3116            } else {
3117                format!("{}.{}k", thousands, hundreds)
3118            }
3119        }
3120        1_000_000..=9_999_999 => {
3121            let millions = count / 1_000_000;
3122            let hundred_thousands = (count % 1_000_000 + 50_000) / 100_000;
3123            if hundred_thousands == 0 {
3124                format!("{}M", millions)
3125            } else if hundred_thousands == 10 {
3126                format!("{}M", millions + 1)
3127            } else {
3128                format!("{}.{}M", millions, hundred_thousands)
3129            }
3130        }
3131        10_000_000.. => format!("{}M", (count + 500_000) / 1_000_000),
3132        _ => format!("{}k", (count + 500) / 1000),
3133    }
3134}
3135
3136pub fn make_lsp_adapter_delegate(
3137    project: &Entity<Project>,
3138    cx: &mut App,
3139) -> Result<Option<Arc<dyn LspAdapterDelegate>>> {
3140    project.update(cx, |project, cx| {
3141        // TODO: Find the right worktree.
3142        let Some(worktree) = project.worktrees(cx).next() else {
3143            return Ok(None::<Arc<dyn LspAdapterDelegate>>);
3144        };
3145        let http_client = project.client().http_client();
3146        project.lsp_store().update(cx, |_, cx| {
3147            Ok(Some(LocalLspAdapterDelegate::new(
3148                project.languages().clone(),
3149                project.environment(),
3150                cx.weak_entity(),
3151                &worktree,
3152                http_client,
3153                project.fs().clone(),
3154                cx,
3155            ) as Arc<dyn LspAdapterDelegate>))
3156        })
3157    })
3158}
3159
3160#[cfg(test)]
3161mod tests {
3162    use super::*;
3163    use editor::SelectionEffects;
3164    use fs::FakeFs;
3165    use gpui::{App, TestAppContext, VisualTestContext};
3166    use indoc::indoc;
3167    use language::{Buffer, LanguageRegistry};
3168    use pretty_assertions::assert_eq;
3169    use prompt_store::PromptBuilder;
3170    use text::OffsetRangeExt;
3171    use unindent::Unindent;
3172    use util::path;
3173
3174    #[gpui::test]
3175    async fn test_copy_paste_whole_message(cx: &mut TestAppContext) {
3176        let (context, context_editor, mut cx) = setup_context_editor_text(vec![
3177            (Role::User, "What is the Zed editor?"),
3178            (
3179                Role::Assistant,
3180                "Zed is a modern, high-performance code editor designed from the ground up for speed and collaboration.",
3181            ),
3182            (Role::User, ""),
3183        ],cx).await;
3184
3185        // Select & Copy whole user message
3186        assert_copy_paste_context_editor(
3187            &context_editor,
3188            message_range(&context, 0, &mut cx),
3189            indoc! {"
3190                What is the Zed editor?
3191                Zed is a modern, high-performance code editor designed from the ground up for speed and collaboration.
3192                What is the Zed editor?
3193            "},
3194            &mut cx,
3195        );
3196
3197        // Select & Copy whole assistant message
3198        assert_copy_paste_context_editor(
3199            &context_editor,
3200            message_range(&context, 1, &mut cx),
3201            indoc! {"
3202                What is the Zed editor?
3203                Zed is a modern, high-performance code editor designed from the ground up for speed and collaboration.
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            "},
3207            &mut cx,
3208        );
3209    }
3210
3211    #[gpui::test]
3212    async fn test_copy_paste_no_selection(cx: &mut TestAppContext) {
3213        let (context, context_editor, mut cx) = setup_context_editor_text(
3214            vec![
3215                (Role::User, "user1"),
3216                (Role::Assistant, "assistant1"),
3217                (Role::Assistant, "assistant2"),
3218                (Role::User, ""),
3219            ],
3220            cx,
3221        )
3222        .await;
3223
3224        // Copy and paste first assistant message
3225        let message_2_range = message_range(&context, 1, &mut cx);
3226        assert_copy_paste_context_editor(
3227            &context_editor,
3228            message_2_range.start..message_2_range.start,
3229            indoc! {"
3230                user1
3231                assistant1
3232                assistant2
3233                assistant1
3234            "},
3235            &mut cx,
3236        );
3237
3238        // Copy and cut second assistant message
3239        let message_3_range = message_range(&context, 2, &mut cx);
3240        assert_copy_paste_context_editor(
3241            &context_editor,
3242            message_3_range.start..message_3_range.start,
3243            indoc! {"
3244                user1
3245                assistant1
3246                assistant2
3247                assistant1
3248                assistant2
3249            "},
3250            &mut cx,
3251        );
3252    }
3253
3254    #[gpui::test]
3255    fn test_find_code_blocks(cx: &mut App) {
3256        let markdown = languages::language("markdown", tree_sitter_md::LANGUAGE.into());
3257
3258        let buffer = cx.new(|cx| {
3259            let text = r#"
3260                line 0
3261                line 1
3262                ```rust
3263                fn main() {}
3264                ```
3265                line 5
3266                line 6
3267                line 7
3268                ```go
3269                func main() {}
3270                ```
3271                line 11
3272                ```
3273                this is plain text code block
3274                ```
3275
3276                ```go
3277                func another() {}
3278                ```
3279                line 19
3280            "#
3281            .unindent();
3282            let mut buffer = Buffer::local(text, cx);
3283            buffer.set_language(Some(markdown.clone()), cx);
3284            buffer
3285        });
3286        let snapshot = buffer.read(cx).snapshot();
3287
3288        let code_blocks = vec![
3289            Point::new(3, 0)..Point::new(4, 0),
3290            Point::new(9, 0)..Point::new(10, 0),
3291            Point::new(13, 0)..Point::new(14, 0),
3292            Point::new(17, 0)..Point::new(18, 0),
3293        ]
3294        .into_iter()
3295        .map(|range| snapshot.point_to_offset(range.start)..snapshot.point_to_offset(range.end))
3296        .collect::<Vec<_>>();
3297
3298        let expected_results = vec![
3299            (0, None),
3300            (1, None),
3301            (2, Some(code_blocks[0].clone())),
3302            (3, Some(code_blocks[0].clone())),
3303            (4, Some(code_blocks[0].clone())),
3304            (5, None),
3305            (6, None),
3306            (7, None),
3307            (8, Some(code_blocks[1].clone())),
3308            (9, Some(code_blocks[1].clone())),
3309            (10, Some(code_blocks[1].clone())),
3310            (11, None),
3311            (12, Some(code_blocks[2].clone())),
3312            (13, Some(code_blocks[2].clone())),
3313            (14, Some(code_blocks[2].clone())),
3314            (15, None),
3315            (16, Some(code_blocks[3].clone())),
3316            (17, Some(code_blocks[3].clone())),
3317            (18, Some(code_blocks[3].clone())),
3318            (19, None),
3319        ];
3320
3321        for (row, expected) in expected_results {
3322            let offset = snapshot.point_to_offset(Point::new(row, 0));
3323            let range = find_surrounding_code_block(&snapshot, offset);
3324            assert_eq!(range, expected, "unexpected result on row {:?}", row);
3325        }
3326    }
3327
3328    async fn setup_context_editor_text(
3329        messages: Vec<(Role, &str)>,
3330        cx: &mut TestAppContext,
3331    ) -> (
3332        Entity<AssistantContext>,
3333        Entity<TextThreadEditor>,
3334        VisualTestContext,
3335    ) {
3336        cx.update(init_test);
3337
3338        let fs = FakeFs::new(cx.executor());
3339        let context = create_context_with_messages(messages, cx);
3340
3341        let project = Project::test(fs.clone(), [path!("/test").as_ref()], cx).await;
3342        let window = cx.add_window(|window, cx| Workspace::test_new(project.clone(), window, cx));
3343        let workspace = window.root(cx).unwrap();
3344        let mut cx = VisualTestContext::from_window(*window, cx);
3345
3346        let context_editor = window
3347            .update(&mut cx, |_, window, cx| {
3348                cx.new(|cx| {
3349                    let editor = TextThreadEditor::for_context(
3350                        context.clone(),
3351                        fs,
3352                        workspace.downgrade(),
3353                        project,
3354                        None,
3355                        window,
3356                        cx,
3357                    );
3358                    editor
3359                })
3360            })
3361            .unwrap();
3362
3363        (context, context_editor, cx)
3364    }
3365
3366    fn message_range(
3367        context: &Entity<AssistantContext>,
3368        message_ix: usize,
3369        cx: &mut TestAppContext,
3370    ) -> Range<usize> {
3371        context.update(cx, |context, cx| {
3372            context
3373                .messages(cx)
3374                .nth(message_ix)
3375                .unwrap()
3376                .anchor_range
3377                .to_offset(&context.buffer().read(cx).snapshot())
3378        })
3379    }
3380
3381    fn assert_copy_paste_context_editor<T: editor::ToOffset>(
3382        context_editor: &Entity<TextThreadEditor>,
3383        range: Range<T>,
3384        expected_text: &str,
3385        cx: &mut VisualTestContext,
3386    ) {
3387        context_editor.update_in(cx, |context_editor, window, cx| {
3388            context_editor.editor.update(cx, |editor, cx| {
3389                editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
3390                    s.select_ranges([range])
3391                });
3392            });
3393
3394            context_editor.copy(&Default::default(), window, cx);
3395
3396            context_editor.editor.update(cx, |editor, cx| {
3397                editor.move_to_end(&Default::default(), window, cx);
3398            });
3399
3400            context_editor.paste(&Default::default(), window, cx);
3401
3402            context_editor.editor.update(cx, |editor, cx| {
3403                assert_eq!(editor.text(cx), expected_text);
3404            });
3405        });
3406    }
3407
3408    fn create_context_with_messages(
3409        mut messages: Vec<(Role, &str)>,
3410        cx: &mut TestAppContext,
3411    ) -> Entity<AssistantContext> {
3412        let registry = Arc::new(LanguageRegistry::test(cx.executor()));
3413        let prompt_builder = Arc::new(PromptBuilder::new(None).unwrap());
3414        cx.new(|cx| {
3415            let mut context = AssistantContext::local(
3416                registry,
3417                None,
3418                None,
3419                prompt_builder.clone(),
3420                Arc::new(SlashCommandWorkingSet::default()),
3421                cx,
3422            );
3423            let mut message_1 = context.messages(cx).next().unwrap();
3424            let (role, text) = messages.remove(0);
3425
3426            loop {
3427                if role == message_1.role {
3428                    context.buffer().update(cx, |buffer, cx| {
3429                        buffer.edit([(message_1.offset_range, text)], None, cx);
3430                    });
3431                    break;
3432                }
3433                let mut ids = HashSet::default();
3434                ids.insert(message_1.id);
3435                context.cycle_message_roles(ids, cx);
3436                message_1 = context.messages(cx).next().unwrap();
3437            }
3438
3439            let mut last_message_id = message_1.id;
3440            for (role, text) in messages {
3441                context.insert_message_after(last_message_id, role, MessageStatus::Done, cx);
3442                let message = context.messages(cx).last().unwrap();
3443                last_message_id = message.id;
3444                context.buffer().update(cx, |buffer, cx| {
3445                    buffer.edit([(message.offset_range, text)], None, cx);
3446                })
3447            }
3448
3449            context
3450        })
3451    }
3452
3453    fn init_test(cx: &mut App) {
3454        let settings_store = SettingsStore::test(cx);
3455        prompt_store::init(cx);
3456        LanguageModelRegistry::test(cx);
3457        cx.set_global(settings_store);
3458        language::init(cx);
3459        agent_settings::init(cx);
3460        Project::init_settings(cx);
3461        theme::init(theme::LoadThemes::JustBase, cx);
3462        workspace::init_settings(cx);
3463        editor::init_settings(cx);
3464    }
3465}