agent_diff.rs

   1use crate::{Keep, KeepAll, OpenAgentDiff, Reject, RejectAll};
   2use acp_thread::{AcpThread, AcpThreadEvent};
   3use action_log::{ActionLogTelemetry, LastRejectUndo};
   4use agent_settings::AgentSettings;
   5use anyhow::Result;
   6use buffer_diff::DiffHunkStatus;
   7use collections::{HashMap, HashSet};
   8use editor::{
   9    Direction, Editor, EditorEvent, EditorSettings, MultiBuffer, MultiBufferSnapshot,
  10    SelectionEffects, ToPoint,
  11    actions::{GoToHunk, GoToPreviousHunk},
  12    multibuffer_context_lines,
  13    scroll::Autoscroll,
  14};
  15
  16use gpui::{
  17    Action, AnyElement, App, AppContext, Empty, Entity, EventEmitter, FocusHandle, Focusable,
  18    Global, SharedString, Subscription, Task, WeakEntity, Window, prelude::*,
  19};
  20
  21use language::{Buffer, Capability, OffsetRangeExt, Point};
  22use multi_buffer::PathKey;
  23use project::{Project, ProjectItem, ProjectPath};
  24use settings::{Settings, SettingsStore};
  25use std::{
  26    any::{Any, TypeId},
  27    collections::hash_map::Entry,
  28    ops::Range,
  29    sync::Arc,
  30};
  31use ui::{CommonAnimationExt, IconButtonShape, KeyBinding, Tooltip, prelude::*, vertical_divider};
  32use util::ResultExt;
  33use workspace::{
  34    Item, ItemHandle, ItemNavHistory, ToolbarItemEvent, ToolbarItemLocation, ToolbarItemView,
  35    Workspace,
  36    item::{ItemEvent, SaveOptions, TabContentParams},
  37    searchable::SearchableItemHandle,
  38};
  39use zed_actions::assistant::ToggleFocus;
  40
  41pub struct AgentDiffPane {
  42    multibuffer: Entity<MultiBuffer>,
  43    editor: Entity<Editor>,
  44    thread: Entity<AcpThread>,
  45    focus_handle: FocusHandle,
  46    workspace: WeakEntity<Workspace>,
  47    _subscriptions: Vec<Subscription>,
  48}
  49
  50impl AgentDiffPane {
  51    pub fn deploy(
  52        thread: Entity<AcpThread>,
  53        workspace: WeakEntity<Workspace>,
  54        window: &mut Window,
  55        cx: &mut App,
  56    ) -> Result<Entity<Self>> {
  57        workspace.update(cx, |workspace, cx| {
  58            Self::deploy_in_workspace(thread, workspace, window, cx)
  59        })
  60    }
  61
  62    pub fn deploy_in_workspace(
  63        thread: Entity<AcpThread>,
  64        workspace: &mut Workspace,
  65        window: &mut Window,
  66        cx: &mut Context<Workspace>,
  67    ) -> Entity<Self> {
  68        let existing_diff = workspace
  69            .items_of_type::<AgentDiffPane>(cx)
  70            .find(|diff| diff.read(cx).thread == thread);
  71
  72        if let Some(existing_diff) = existing_diff {
  73            workspace.activate_item(&existing_diff, true, true, window, cx);
  74            existing_diff
  75        } else {
  76            let agent_diff = cx
  77                .new(|cx| AgentDiffPane::new(thread.clone(), workspace.weak_handle(), window, cx));
  78            workspace.add_item_to_center(Box::new(agent_diff.clone()), window, cx);
  79            agent_diff
  80        }
  81    }
  82
  83    pub fn new(
  84        thread: Entity<AcpThread>,
  85        workspace: WeakEntity<Workspace>,
  86        window: &mut Window,
  87        cx: &mut Context<Self>,
  88    ) -> Self {
  89        let focus_handle = cx.focus_handle();
  90        let multibuffer = cx.new(|_| MultiBuffer::new(Capability::ReadWrite));
  91
  92        let project = thread.read(cx).project().clone();
  93        let editor = cx.new(|cx| {
  94            let mut editor =
  95                Editor::for_multibuffer(multibuffer.clone(), Some(project.clone()), window, cx);
  96            editor.disable_inline_diagnostics();
  97            editor.set_expand_all_diff_hunks(cx);
  98            editor
  99                .set_render_diff_hunk_controls(diff_hunk_controls(&thread, workspace.clone()), cx);
 100            editor.register_addon(AgentDiffAddon);
 101            editor.disable_mouse_wheel_zoom();
 102            editor
 103        });
 104
 105        let action_log = thread.read(cx).action_log().clone();
 106
 107        let mut this = Self {
 108            _subscriptions: vec![
 109                cx.observe_in(&action_log, window, |this, _action_log, window, cx| {
 110                    this.update_excerpts(window, cx)
 111                }),
 112                cx.subscribe(&thread, |this, _thread, event, cx| {
 113                    this.handle_acp_thread_event(event, cx)
 114                }),
 115            ],
 116            multibuffer,
 117            editor,
 118            thread,
 119            focus_handle,
 120            workspace,
 121        };
 122        this.update_excerpts(window, cx);
 123        this
 124    }
 125
 126    fn update_excerpts(&mut self, window: &mut Window, cx: &mut Context<Self>) {
 127        let changed_buffers = self
 128            .thread
 129            .read(cx)
 130            .action_log()
 131            .read(cx)
 132            .changed_buffers(cx);
 133
 134        // Sort edited files alphabetically for consistency with Git diff view
 135        let mut sorted_buffers: Vec<_> = changed_buffers.iter().collect();
 136        sorted_buffers.sort_by(|(buffer_a, _), (buffer_b, _)| {
 137            let path_a = buffer_a.read(cx).file().map(|f| f.path().clone());
 138            let path_b = buffer_b.read(cx).file().map(|f| f.path().clone());
 139            path_a.cmp(&path_b)
 140        });
 141
 142        let mut buffers_to_delete = self
 143            .multibuffer
 144            .read(cx)
 145            .snapshot(cx)
 146            .excerpts()
 147            .map(|excerpt| excerpt.context.start.buffer_id)
 148            .collect::<HashSet<_>>();
 149
 150        for (buffer, diff_handle) in sorted_buffers {
 151            if buffer.read(cx).file().is_none() {
 152                continue;
 153            }
 154
 155            let path_key = PathKey::for_buffer(&buffer, cx);
 156            buffers_to_delete.remove(&buffer.read(cx).remote_id());
 157
 158            let snapshot = buffer.read(cx).snapshot();
 159
 160            let diff_hunk_ranges = diff_handle
 161                .read(cx)
 162                .snapshot(cx)
 163                .hunks_intersecting_range(
 164                    language::Anchor::min_max_range_for_buffer(snapshot.remote_id()),
 165                    &snapshot,
 166                )
 167                .map(|diff_hunk| diff_hunk.buffer_range.to_point(&snapshot))
 168                .collect::<Vec<_>>();
 169
 170            let (was_empty, is_excerpt_newly_added) =
 171                self.multibuffer.update(cx, |multibuffer, cx| {
 172                    let was_empty = multibuffer.is_empty();
 173                    let is_excerpt_newly_added = multibuffer.update_excerpts_for_path(
 174                        path_key.clone(),
 175                        buffer.clone(),
 176                        diff_hunk_ranges,
 177                        multibuffer_context_lines(cx),
 178                        cx,
 179                    );
 180                    multibuffer.add_diff(diff_handle.clone(), cx);
 181                    (was_empty, is_excerpt_newly_added)
 182                });
 183
 184            self.editor.update(cx, |editor, cx| {
 185                if was_empty {
 186                    let first_hunk = editor
 187                        .diff_hunks_in_ranges(
 188                            &[editor::Anchor::Min..editor::Anchor::Max],
 189                            &self.multibuffer.read(cx).read(cx),
 190                        )
 191                        .next();
 192
 193                    if let Some(first_hunk) = first_hunk {
 194                        let first_hunk_start = first_hunk.multi_buffer_range.start;
 195                        editor.change_selections(Default::default(), window, cx, |selections| {
 196                            selections.select_anchor_ranges([first_hunk_start..first_hunk_start]);
 197                        })
 198                    }
 199                }
 200
 201                if is_excerpt_newly_added
 202                    && buffer
 203                        .read(cx)
 204                        .file()
 205                        .is_some_and(|file| file.disk_state().is_deleted())
 206                {
 207                    editor.fold_buffer(snapshot.text.remote_id(), cx)
 208                }
 209            });
 210        }
 211
 212        self.multibuffer.update(cx, |multibuffer, cx| {
 213            for buffer_id in buffers_to_delete {
 214                multibuffer.remove_excerpts_for_buffer(buffer_id, cx);
 215            }
 216        });
 217
 218        if self.multibuffer.read(cx).is_empty()
 219            && self
 220                .editor
 221                .read(cx)
 222                .focus_handle(cx)
 223                .contains_focused(window, cx)
 224        {
 225            self.focus_handle.focus(window, cx);
 226        } else if self.focus_handle.is_focused(window) && !self.multibuffer.read(cx).is_empty() {
 227            self.editor.update(cx, |editor, cx| {
 228                editor.focus_handle(cx).focus(window, cx);
 229            });
 230        }
 231    }
 232
 233    fn handle_acp_thread_event(&mut self, event: &AcpThreadEvent, cx: &mut Context<Self>) {
 234        if let AcpThreadEvent::TitleUpdated = event {
 235            cx.emit(EditorEvent::TitleChanged);
 236        }
 237    }
 238
 239    pub fn move_to_path(&self, path_key: PathKey, window: &mut Window, cx: &mut App) {
 240        if let Some(position) = self.multibuffer.read(cx).location_for_path(&path_key, cx) {
 241            self.editor.update(cx, |editor, cx| {
 242                let first_hunk = editor
 243                    .diff_hunks_in_ranges(
 244                        &[position..editor::Anchor::Max],
 245                        &self.multibuffer.read(cx).read(cx),
 246                    )
 247                    .next();
 248
 249                if let Some(first_hunk) = first_hunk {
 250                    let first_hunk_start = first_hunk.multi_buffer_range.start;
 251                    editor.change_selections(Default::default(), window, cx, |selections| {
 252                        selections.select_anchor_ranges([first_hunk_start..first_hunk_start]);
 253                    })
 254                }
 255            });
 256        }
 257    }
 258
 259    fn keep(&mut self, _: &Keep, window: &mut Window, cx: &mut Context<Self>) {
 260        self.editor.update(cx, |editor, cx| {
 261            let snapshot = editor.buffer().read(cx).snapshot(cx);
 262            keep_edits_in_selection(editor, &snapshot, &self.thread, window, cx);
 263        });
 264    }
 265
 266    fn reject(&mut self, _: &Reject, window: &mut Window, cx: &mut Context<Self>) {
 267        self.editor.update(cx, |editor, cx| {
 268            let snapshot = editor.buffer().read(cx).snapshot(cx);
 269            reject_edits_in_selection(
 270                editor,
 271                &snapshot,
 272                &self.thread,
 273                self.workspace.clone(),
 274                window,
 275                cx,
 276            );
 277        });
 278    }
 279
 280    fn reject_all(&mut self, _: &RejectAll, window: &mut Window, cx: &mut Context<Self>) {
 281        self.editor.update(cx, |editor, cx| {
 282            let snapshot = editor.buffer().read(cx).snapshot(cx);
 283            reject_edits_in_ranges(
 284                editor,
 285                &snapshot,
 286                &self.thread,
 287                vec![editor::Anchor::Min..editor::Anchor::Max],
 288                self.workspace.clone(),
 289                window,
 290                cx,
 291            );
 292        });
 293    }
 294
 295    fn keep_all(&mut self, _: &KeepAll, _window: &mut Window, cx: &mut Context<Self>) {
 296        let telemetry = ActionLogTelemetry::from(self.thread.read(cx));
 297        let action_log = self.thread.read(cx).action_log().clone();
 298        action_log.update(cx, |action_log, cx| {
 299            action_log.keep_all_edits(Some(telemetry), cx)
 300        });
 301    }
 302}
 303
 304fn keep_edits_in_selection(
 305    editor: &mut Editor,
 306    buffer_snapshot: &MultiBufferSnapshot,
 307    thread: &Entity<AcpThread>,
 308    window: &mut Window,
 309    cx: &mut Context<Editor>,
 310) {
 311    let ranges = editor
 312        .selections
 313        .disjoint_anchor_ranges()
 314        .collect::<Vec<_>>();
 315
 316    keep_edits_in_ranges(editor, buffer_snapshot, thread, ranges, window, cx)
 317}
 318
 319fn reject_edits_in_selection(
 320    editor: &mut Editor,
 321    buffer_snapshot: &MultiBufferSnapshot,
 322    thread: &Entity<AcpThread>,
 323    workspace: WeakEntity<Workspace>,
 324    window: &mut Window,
 325    cx: &mut Context<Editor>,
 326) {
 327    let ranges = editor
 328        .selections
 329        .disjoint_anchor_ranges()
 330        .collect::<Vec<_>>();
 331    reject_edits_in_ranges(
 332        editor,
 333        buffer_snapshot,
 334        thread,
 335        ranges,
 336        workspace,
 337        window,
 338        cx,
 339    )
 340}
 341
 342fn keep_edits_in_ranges(
 343    editor: &mut Editor,
 344    buffer_snapshot: &MultiBufferSnapshot,
 345    thread: &Entity<AcpThread>,
 346    ranges: Vec<Range<editor::Anchor>>,
 347    window: &mut Window,
 348    cx: &mut Context<Editor>,
 349) {
 350    let diff_hunks_in_ranges = editor
 351        .diff_hunks_in_ranges(&ranges, buffer_snapshot)
 352        .collect::<Vec<_>>();
 353
 354    update_editor_selection(editor, buffer_snapshot, &diff_hunks_in_ranges, window, cx);
 355
 356    let multibuffer = editor.buffer().clone();
 357    for hunk in &diff_hunks_in_ranges {
 358        let buffer = multibuffer.read(cx).buffer(hunk.buffer_id);
 359        if let Some(buffer) = buffer {
 360            let action_log = thread.read(cx).action_log().clone();
 361            let telemetry = ActionLogTelemetry::from(thread.read(cx));
 362            action_log.update(cx, |action_log, cx| {
 363                action_log.keep_edits_in_range(
 364                    buffer,
 365                    hunk.buffer_range.clone(),
 366                    Some(telemetry),
 367                    cx,
 368                )
 369            });
 370        }
 371    }
 372}
 373
 374fn reject_edits_in_ranges(
 375    editor: &mut Editor,
 376    buffer_snapshot: &MultiBufferSnapshot,
 377    thread: &Entity<AcpThread>,
 378    ranges: Vec<Range<editor::Anchor>>,
 379    workspace: WeakEntity<Workspace>,
 380    window: &mut Window,
 381    cx: &mut Context<Editor>,
 382) {
 383    let diff_hunks_in_ranges = editor
 384        .diff_hunks_in_ranges(&ranges, buffer_snapshot)
 385        .collect::<Vec<_>>();
 386
 387    update_editor_selection(editor, buffer_snapshot, &diff_hunks_in_ranges, window, cx);
 388
 389    let multibuffer = editor.buffer().clone();
 390
 391    let mut ranges_by_buffer = HashMap::default();
 392    for hunk in &diff_hunks_in_ranges {
 393        let buffer = multibuffer.read(cx).buffer(hunk.buffer_id);
 394        if let Some(buffer) = buffer {
 395            ranges_by_buffer
 396                .entry(buffer.clone())
 397                .or_insert_with(Vec::new)
 398                .push(hunk.buffer_range.clone());
 399        }
 400    }
 401
 402    let action_log = thread.read(cx).action_log().clone();
 403    let telemetry = ActionLogTelemetry::from(thread.read(cx));
 404    let mut undo_buffers = Vec::new();
 405
 406    for (buffer, ranges) in ranges_by_buffer {
 407        action_log
 408            .update(cx, |action_log, cx| {
 409                let (task, undo_info) =
 410                    action_log.reject_edits_in_ranges(buffer, ranges, Some(telemetry.clone()), cx);
 411                undo_buffers.extend(undo_info);
 412                task
 413            })
 414            .detach_and_log_err(cx);
 415    }
 416    if !undo_buffers.is_empty() {
 417        action_log.update(cx, |action_log, _cx| {
 418            action_log.set_last_reject_undo(LastRejectUndo {
 419                buffers: undo_buffers,
 420            });
 421        });
 422
 423        if let Some(workspace) = workspace.upgrade() {
 424            cx.defer(move |cx| {
 425                workspace.update(cx, |workspace, cx| {
 426                    crate::ui::show_undo_reject_toast(workspace, action_log, cx);
 427                });
 428            });
 429        }
 430    }
 431}
 432
 433fn update_editor_selection(
 434    editor: &mut Editor,
 435    buffer_snapshot: &MultiBufferSnapshot,
 436    diff_hunks: &[multi_buffer::MultiBufferDiffHunk],
 437    window: &mut Window,
 438    cx: &mut Context<Editor>,
 439) {
 440    let newest_cursor = editor
 441        .selections
 442        .newest::<Point>(&editor.display_snapshot(cx))
 443        .head();
 444
 445    if !diff_hunks.iter().any(|hunk| {
 446        hunk.row_range
 447            .contains(&multi_buffer::MultiBufferRow(newest_cursor.row))
 448    }) {
 449        return;
 450    }
 451
 452    let target_hunk = {
 453        diff_hunks
 454            .last()
 455            .and_then(|last_kept_hunk| {
 456                let last_kept_hunk_end = last_kept_hunk.multi_buffer_range.end;
 457                editor
 458                    .diff_hunks_in_ranges(
 459                        &[last_kept_hunk_end..editor::Anchor::Max],
 460                        buffer_snapshot,
 461                    )
 462                    .nth(1)
 463            })
 464            .or_else(|| {
 465                let first_kept_hunk = diff_hunks.first()?;
 466                let first_kept_hunk_start = first_kept_hunk.multi_buffer_range.start;
 467                editor
 468                    .diff_hunks_in_ranges(
 469                        &[editor::Anchor::Min..first_kept_hunk_start],
 470                        buffer_snapshot,
 471                    )
 472                    .next()
 473            })
 474    };
 475
 476    if let Some(target_hunk) = target_hunk {
 477        editor.change_selections(Default::default(), window, cx, |selections| {
 478            let next_hunk_start = target_hunk.multi_buffer_range.start;
 479            selections.select_anchor_ranges([next_hunk_start..next_hunk_start]);
 480        })
 481    }
 482}
 483
 484impl EventEmitter<EditorEvent> for AgentDiffPane {}
 485
 486impl Focusable for AgentDiffPane {
 487    fn focus_handle(&self, cx: &App) -> FocusHandle {
 488        if self.multibuffer.read(cx).is_empty() {
 489            self.focus_handle.clone()
 490        } else {
 491            self.editor.focus_handle(cx)
 492        }
 493    }
 494}
 495
 496impl Item for AgentDiffPane {
 497    type Event = EditorEvent;
 498
 499    fn tab_icon(&self, _window: &Window, _cx: &App) -> Option<Icon> {
 500        Some(Icon::new(IconName::ZedAssistant).color(Color::Muted))
 501    }
 502
 503    fn to_item_events(event: &EditorEvent, f: &mut dyn FnMut(ItemEvent)) {
 504        Editor::to_item_events(event, f)
 505    }
 506
 507    fn deactivated(&mut self, window: &mut Window, cx: &mut Context<Self>) {
 508        self.editor
 509            .update(cx, |editor, cx| editor.deactivated(window, cx));
 510    }
 511
 512    fn navigate(
 513        &mut self,
 514        data: Arc<dyn Any + Send>,
 515        window: &mut Window,
 516        cx: &mut Context<Self>,
 517    ) -> bool {
 518        self.editor
 519            .update(cx, |editor, cx| editor.navigate(data, window, cx))
 520    }
 521
 522    fn tab_tooltip_text(&self, _: &App) -> Option<SharedString> {
 523        Some("Agent Diff".into())
 524    }
 525
 526    fn tab_content(&self, params: TabContentParams, _window: &Window, cx: &App) -> AnyElement {
 527        let title = self.thread.read(cx).title();
 528        Label::new(if let Some(title) = title {
 529            format!("Review: {}", title)
 530        } else {
 531            "Review".to_string()
 532        })
 533        .color(if params.selected {
 534            Color::Default
 535        } else {
 536            Color::Muted
 537        })
 538        .into_any_element()
 539    }
 540
 541    fn telemetry_event_text(&self) -> Option<&'static str> {
 542        Some("Assistant Diff Opened")
 543    }
 544
 545    fn as_searchable(&self, _: &Entity<Self>, _: &App) -> Option<Box<dyn SearchableItemHandle>> {
 546        Some(Box::new(self.editor.clone()))
 547    }
 548
 549    fn for_each_project_item(
 550        &self,
 551        cx: &App,
 552        f: &mut dyn FnMut(gpui::EntityId, &dyn project::ProjectItem),
 553    ) {
 554        self.editor.for_each_project_item(cx, f)
 555    }
 556
 557    fn set_nav_history(
 558        &mut self,
 559        nav_history: ItemNavHistory,
 560        _: &mut Window,
 561        cx: &mut Context<Self>,
 562    ) {
 563        self.editor.update(cx, |editor, _| {
 564            editor.set_nav_history(Some(nav_history));
 565        });
 566    }
 567
 568    fn can_split(&self) -> bool {
 569        true
 570    }
 571
 572    fn clone_on_split(
 573        &self,
 574        _workspace_id: Option<workspace::WorkspaceId>,
 575        window: &mut Window,
 576        cx: &mut Context<Self>,
 577    ) -> Task<Option<Entity<Self>>>
 578    where
 579        Self: Sized,
 580    {
 581        Task::ready(Some(cx.new(|cx| {
 582            Self::new(self.thread.clone(), self.workspace.clone(), window, cx)
 583        })))
 584    }
 585
 586    fn is_dirty(&self, cx: &App) -> bool {
 587        self.multibuffer.read(cx).is_dirty(cx)
 588    }
 589
 590    fn has_conflict(&self, cx: &App) -> bool {
 591        self.multibuffer.read(cx).has_conflict(cx)
 592    }
 593
 594    fn can_save(&self, _: &App) -> bool {
 595        true
 596    }
 597
 598    fn save(
 599        &mut self,
 600        options: SaveOptions,
 601        project: Entity<Project>,
 602        window: &mut Window,
 603        cx: &mut Context<Self>,
 604    ) -> Task<Result<()>> {
 605        self.editor.save(options, project, window, cx)
 606    }
 607
 608    fn save_as(
 609        &mut self,
 610        _: Entity<Project>,
 611        _: ProjectPath,
 612        _window: &mut Window,
 613        _: &mut Context<Self>,
 614    ) -> Task<Result<()>> {
 615        unreachable!()
 616    }
 617
 618    fn reload(
 619        &mut self,
 620        project: Entity<Project>,
 621        window: &mut Window,
 622        cx: &mut Context<Self>,
 623    ) -> Task<Result<()>> {
 624        self.editor.reload(project, window, cx)
 625    }
 626
 627    fn act_as_type<'a>(
 628        &'a self,
 629        type_id: TypeId,
 630        self_handle: &'a Entity<Self>,
 631        _: &'a App,
 632    ) -> Option<gpui::AnyEntity> {
 633        if type_id == TypeId::of::<Self>() {
 634            Some(self_handle.clone().into())
 635        } else if type_id == TypeId::of::<Editor>() {
 636            Some(self.editor.clone().into())
 637        } else {
 638            None
 639        }
 640    }
 641
 642    fn added_to_workspace(
 643        &mut self,
 644        workspace: &mut Workspace,
 645        window: &mut Window,
 646        cx: &mut Context<Self>,
 647    ) {
 648        self.editor.update(cx, |editor, cx| {
 649            editor.added_to_workspace(workspace, window, cx)
 650        });
 651    }
 652
 653    fn tab_content_text(&self, _detail: usize, _cx: &App) -> SharedString {
 654        "Agent Diff".into()
 655    }
 656}
 657
 658impl Render for AgentDiffPane {
 659    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
 660        let is_empty = self.multibuffer.read(cx).is_empty();
 661        let focus_handle = &self.focus_handle;
 662
 663        div()
 664            .track_focus(focus_handle)
 665            .key_context(if is_empty { "EmptyPane" } else { "AgentDiff" })
 666            .on_action(cx.listener(Self::keep))
 667            .on_action(cx.listener(Self::reject))
 668            .on_action(cx.listener(Self::reject_all))
 669            .on_action(cx.listener(Self::keep_all))
 670            .bg(cx.theme().colors().editor_background)
 671            .flex()
 672            .items_center()
 673            .justify_center()
 674            .size_full()
 675            .when(is_empty, |el| {
 676                el.child(
 677                    v_flex()
 678                        .items_center()
 679                        .gap_2()
 680                        .child("No changes to review")
 681                        .child(
 682                            Button::new("continue-iterating", "Continue Iterating")
 683                                .style(ButtonStyle::Filled)
 684                                .start_icon(
 685                                    Icon::new(IconName::ForwardArrow)
 686                                        .size(IconSize::Small)
 687                                        .color(Color::Muted),
 688                                )
 689                                .full_width()
 690                                .key_binding(KeyBinding::for_action_in(
 691                                    &ToggleFocus,
 692                                    &focus_handle.clone(),
 693                                    cx,
 694                                ))
 695                                .on_click(|_event, window, cx| {
 696                                    window.dispatch_action(ToggleFocus.boxed_clone(), cx)
 697                                }),
 698                        ),
 699                )
 700            })
 701            .when(!is_empty, |el| el.child(self.editor.clone()))
 702    }
 703}
 704
 705fn diff_hunk_controls(
 706    thread: &Entity<AcpThread>,
 707    workspace: WeakEntity<Workspace>,
 708) -> editor::RenderDiffHunkControlsFn {
 709    let thread = thread.clone();
 710
 711    Arc::new(
 712        move |row, status, hunk_range, is_created_file, line_height, editor, _, cx| {
 713            {
 714                render_diff_hunk_controls(
 715                    row,
 716                    status,
 717                    hunk_range,
 718                    is_created_file,
 719                    line_height,
 720                    &thread,
 721                    editor,
 722                    workspace.clone(),
 723                    cx,
 724                )
 725            }
 726        },
 727    )
 728}
 729
 730fn render_diff_hunk_controls(
 731    row: u32,
 732    _status: &DiffHunkStatus,
 733    hunk_range: Range<editor::Anchor>,
 734    is_created_file: bool,
 735    line_height: Pixels,
 736    thread: &Entity<AcpThread>,
 737    editor: &Entity<Editor>,
 738    workspace: WeakEntity<Workspace>,
 739    cx: &mut App,
 740) -> AnyElement {
 741    let editor = editor.clone();
 742
 743    h_flex()
 744        .h(line_height)
 745        .mr_0p5()
 746        .gap_1()
 747        .px_0p5()
 748        .pb_1()
 749        .border_x_1()
 750        .border_b_1()
 751        .border_color(cx.theme().colors().border)
 752        .rounded_b_md()
 753        .bg(cx.theme().colors().editor_background)
 754        .gap_1()
 755        .block_mouse_except_scroll()
 756        .shadow_md()
 757        .children(vec![
 758            Button::new(("reject", row as u64), "Reject")
 759                .disabled(is_created_file)
 760                .key_binding(
 761                    KeyBinding::for_action_in(&Reject, &editor.read(cx).focus_handle(cx), cx)
 762                        .map(|kb| kb.size(rems_from_px(12.))),
 763                )
 764                .on_click({
 765                    let editor = editor.clone();
 766                    let thread = thread.clone();
 767                    move |_event, window, cx| {
 768                        editor.update(cx, |editor, cx| {
 769                            let snapshot = editor.buffer().read(cx).snapshot(cx);
 770                            reject_edits_in_ranges(
 771                                editor,
 772                                &snapshot,
 773                                &thread,
 774                                vec![hunk_range.start..hunk_range.start],
 775                                workspace.clone(),
 776                                window,
 777                                cx,
 778                            );
 779                        })
 780                    }
 781                }),
 782            Button::new(("keep", row as u64), "Keep")
 783                .key_binding(
 784                    KeyBinding::for_action_in(&Keep, &editor.read(cx).focus_handle(cx), cx)
 785                        .map(|kb| kb.size(rems_from_px(12.))),
 786                )
 787                .on_click({
 788                    let editor = editor.clone();
 789                    let thread = thread.clone();
 790                    move |_event, window, cx| {
 791                        editor.update(cx, |editor, cx| {
 792                            let snapshot = editor.buffer().read(cx).snapshot(cx);
 793                            keep_edits_in_ranges(
 794                                editor,
 795                                &snapshot,
 796                                &thread,
 797                                vec![hunk_range.start..hunk_range.start],
 798                                window,
 799                                cx,
 800                            );
 801                        });
 802                    }
 803                }),
 804        ])
 805        .when(
 806            !editor.read(cx).buffer().read(cx).all_diff_hunks_expanded(),
 807            |el| {
 808                el.child(
 809                    IconButton::new(("next-hunk", row as u64), IconName::ArrowDown)
 810                        .shape(IconButtonShape::Square)
 811                        .icon_size(IconSize::Small)
 812                        // .disabled(!has_multiple_hunks)
 813                        .tooltip({
 814                            let focus_handle = editor.focus_handle(cx);
 815                            move |_window, cx| {
 816                                Tooltip::for_action_in("Next Hunk", &GoToHunk, &focus_handle, cx)
 817                            }
 818                        })
 819                        .on_click({
 820                            let editor = editor.clone();
 821                            move |_event, window, cx| {
 822                                editor.update(cx, |editor, cx| {
 823                                    let snapshot = editor.snapshot(window, cx);
 824                                    let position =
 825                                        hunk_range.end.to_point(&snapshot.buffer_snapshot());
 826                                    editor.go_to_hunk_before_or_after_position(
 827                                        &snapshot,
 828                                        position,
 829                                        Direction::Next,
 830                                        true,
 831                                        window,
 832                                        cx,
 833                                    );
 834                                    editor.expand_selected_diff_hunks(cx);
 835                                });
 836                            }
 837                        }),
 838                )
 839                .child(
 840                    IconButton::new(("prev-hunk", row as u64), IconName::ArrowUp)
 841                        .shape(IconButtonShape::Square)
 842                        .icon_size(IconSize::Small)
 843                        // .disabled(!has_multiple_hunks)
 844                        .tooltip({
 845                            let focus_handle = editor.focus_handle(cx);
 846                            move |_window, cx| {
 847                                Tooltip::for_action_in(
 848                                    "Previous Hunk",
 849                                    &GoToPreviousHunk,
 850                                    &focus_handle,
 851                                    cx,
 852                                )
 853                            }
 854                        })
 855                        .on_click({
 856                            let editor = editor.clone();
 857                            move |_event, window, cx| {
 858                                editor.update(cx, |editor, cx| {
 859                                    let snapshot = editor.snapshot(window, cx);
 860                                    let point =
 861                                        hunk_range.start.to_point(&snapshot.buffer_snapshot());
 862                                    editor.go_to_hunk_before_or_after_position(
 863                                        &snapshot,
 864                                        point,
 865                                        Direction::Prev,
 866                                        true,
 867                                        window,
 868                                        cx,
 869                                    );
 870                                    editor.expand_selected_diff_hunks(cx);
 871                                });
 872                            }
 873                        }),
 874                )
 875            },
 876        )
 877        .into_any_element()
 878}
 879
 880struct AgentDiffAddon;
 881
 882impl editor::Addon for AgentDiffAddon {
 883    fn to_any(&self) -> &dyn std::any::Any {
 884        self
 885    }
 886
 887    fn extend_key_context(&self, key_context: &mut gpui::KeyContext, _: &App) {
 888        key_context.add("agent_diff");
 889    }
 890}
 891
 892pub struct AgentDiffToolbar {
 893    active_item: Option<AgentDiffToolbarItem>,
 894    _settings_subscription: Subscription,
 895}
 896
 897pub enum AgentDiffToolbarItem {
 898    Pane(WeakEntity<AgentDiffPane>),
 899    Editor {
 900        editor: WeakEntity<Editor>,
 901        state: EditorState,
 902        _diff_subscription: Subscription,
 903    },
 904}
 905
 906impl AgentDiffToolbar {
 907    pub fn new(cx: &mut Context<Self>) -> Self {
 908        Self {
 909            active_item: None,
 910            _settings_subscription: cx.observe_global::<SettingsStore>(Self::update_location),
 911        }
 912    }
 913
 914    fn dispatch_action(&self, action: &dyn Action, window: &mut Window, cx: &mut Context<Self>) {
 915        let Some(active_item) = self.active_item.as_ref() else {
 916            return;
 917        };
 918
 919        match active_item {
 920            AgentDiffToolbarItem::Pane(agent_diff) => {
 921                if let Some(agent_diff) = agent_diff.upgrade() {
 922                    agent_diff.focus_handle(cx).focus(window, cx);
 923                }
 924            }
 925            AgentDiffToolbarItem::Editor { editor, .. } => {
 926                if let Some(editor) = editor.upgrade() {
 927                    editor.read(cx).focus_handle(cx).focus(window, cx);
 928                }
 929            }
 930        }
 931
 932        let action = action.boxed_clone();
 933        cx.defer(move |cx| {
 934            cx.dispatch_action(action.as_ref());
 935        })
 936    }
 937
 938    fn handle_diff_notify(&mut self, agent_diff: Entity<AgentDiff>, cx: &mut Context<Self>) {
 939        let Some(AgentDiffToolbarItem::Editor { editor, state, .. }) = self.active_item.as_mut()
 940        else {
 941            return;
 942        };
 943
 944        *state = agent_diff.read(cx).editor_state(editor);
 945        self.update_location(cx);
 946        cx.notify();
 947    }
 948
 949    fn update_location(&mut self, cx: &mut Context<Self>) {
 950        let location = self.location(cx);
 951        cx.emit(ToolbarItemEvent::ChangeLocation(location));
 952    }
 953
 954    fn location(&self, cx: &App) -> ToolbarItemLocation {
 955        if !EditorSettings::get_global(cx).toolbar.agent_review {
 956            return ToolbarItemLocation::Hidden;
 957        }
 958
 959        match &self.active_item {
 960            None => ToolbarItemLocation::Hidden,
 961            Some(AgentDiffToolbarItem::Pane(_)) => ToolbarItemLocation::PrimaryRight,
 962            Some(AgentDiffToolbarItem::Editor { state, .. }) => match state {
 963                EditorState::Reviewing => ToolbarItemLocation::PrimaryRight,
 964                EditorState::Idle => ToolbarItemLocation::Hidden,
 965            },
 966        }
 967    }
 968}
 969
 970impl EventEmitter<ToolbarItemEvent> for AgentDiffToolbar {}
 971
 972impl ToolbarItemView for AgentDiffToolbar {
 973    fn set_active_pane_item(
 974        &mut self,
 975        active_pane_item: Option<&dyn ItemHandle>,
 976        _: &mut Window,
 977        cx: &mut Context<Self>,
 978    ) -> ToolbarItemLocation {
 979        if let Some(item) = active_pane_item {
 980            if let Some(pane) = item.act_as::<AgentDiffPane>(cx) {
 981                self.active_item = Some(AgentDiffToolbarItem::Pane(pane.downgrade()));
 982                return self.location(cx);
 983            }
 984
 985            if let Some(editor) = item.act_as::<Editor>(cx)
 986                && editor.read(cx).mode().is_full()
 987            {
 988                let agent_diff = AgentDiff::global(cx);
 989
 990                self.active_item = Some(AgentDiffToolbarItem::Editor {
 991                    editor: editor.downgrade(),
 992                    state: agent_diff.read(cx).editor_state(&editor.downgrade()),
 993                    _diff_subscription: cx.observe(&agent_diff, Self::handle_diff_notify),
 994                });
 995
 996                return self.location(cx);
 997            }
 998        }
 999
1000        self.active_item = None;
1001        self.location(cx)
1002    }
1003
1004    fn pane_focus_update(
1005        &mut self,
1006        _pane_focused: bool,
1007        _window: &mut Window,
1008        _cx: &mut Context<Self>,
1009    ) {
1010    }
1011}
1012
1013impl Render for AgentDiffToolbar {
1014    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
1015        let spinner_icon = div()
1016            .px_0p5()
1017            .id("generating")
1018            .tooltip(Tooltip::text("Generating Changes…"))
1019            .child(
1020                Icon::new(IconName::LoadCircle)
1021                    .size(IconSize::Small)
1022                    .color(Color::Accent)
1023                    .with_rotate_animation(3),
1024            )
1025            .into_any();
1026
1027        let Some(active_item) = self.active_item.as_ref() else {
1028            return Empty.into_any();
1029        };
1030
1031        match active_item {
1032            AgentDiffToolbarItem::Editor { editor, state, .. } => {
1033                let Some(editor) = editor.upgrade() else {
1034                    return Empty.into_any();
1035                };
1036
1037                let editor_focus_handle = editor.read(cx).focus_handle(cx);
1038
1039                let content = match state {
1040                    EditorState::Idle => return Empty.into_any(),
1041                    EditorState::Reviewing => vec![
1042                        h_flex()
1043                            .child(
1044                                IconButton::new("hunk-up", IconName::ArrowUp)
1045                                    .icon_size(IconSize::Small)
1046                                    .tooltip(Tooltip::for_action_title_in(
1047                                        "Previous Hunk",
1048                                        &GoToPreviousHunk,
1049                                        &editor_focus_handle,
1050                                    ))
1051                                    .on_click({
1052                                        let editor_focus_handle = editor_focus_handle.clone();
1053                                        move |_, window, cx| {
1054                                            editor_focus_handle.dispatch_action(
1055                                                &GoToPreviousHunk,
1056                                                window,
1057                                                cx,
1058                                            );
1059                                        }
1060                                    }),
1061                            )
1062                            .child(
1063                                IconButton::new("hunk-down", IconName::ArrowDown)
1064                                    .icon_size(IconSize::Small)
1065                                    .tooltip(Tooltip::for_action_title_in(
1066                                        "Next Hunk",
1067                                        &GoToHunk,
1068                                        &editor_focus_handle,
1069                                    ))
1070                                    .on_click({
1071                                        let editor_focus_handle = editor_focus_handle.clone();
1072                                        move |_, window, cx| {
1073                                            editor_focus_handle
1074                                                .dispatch_action(&GoToHunk, window, cx);
1075                                        }
1076                                    }),
1077                            )
1078                            .into_any_element(),
1079                        vertical_divider().into_any_element(),
1080                        h_flex()
1081                            .gap_0p5()
1082                            .child(
1083                                Button::new("reject-all", "Reject All")
1084                                    .key_binding({
1085                                        KeyBinding::for_action_in(
1086                                            &RejectAll,
1087                                            &editor_focus_handle,
1088                                            cx,
1089                                        )
1090                                        .map(|kb| kb.size(rems_from_px(12.)))
1091                                    })
1092                                    .on_click(cx.listener(|this, _, window, cx| {
1093                                        this.dispatch_action(&RejectAll, window, cx)
1094                                    })),
1095                            )
1096                            .child(
1097                                Button::new("keep-all", "Keep All")
1098                                    .key_binding({
1099                                        KeyBinding::for_action_in(
1100                                            &KeepAll,
1101                                            &editor_focus_handle,
1102                                            cx,
1103                                        )
1104                                        .map(|kb| kb.size(rems_from_px(12.)))
1105                                    })
1106                                    .on_click(cx.listener(|this, _, window, cx| {
1107                                        this.dispatch_action(&KeepAll, window, cx)
1108                                    })),
1109                            )
1110                            .into_any_element(),
1111                    ],
1112                };
1113
1114                h_flex()
1115                    .track_focus(&editor_focus_handle)
1116                    .size_full()
1117                    .px_1()
1118                    .mr_1()
1119                    .gap_1()
1120                    .children(content)
1121                    .child(vertical_divider())
1122                    .when_some(editor.read(cx).workspace(), |this, _workspace| {
1123                        this.child(
1124                            IconButton::new("review", IconName::ListTodo)
1125                                .icon_size(IconSize::Small)
1126                                .tooltip(Tooltip::for_action_title_in(
1127                                    "Review All Files",
1128                                    &OpenAgentDiff,
1129                                    &editor_focus_handle,
1130                                ))
1131                                .on_click({
1132                                    cx.listener(move |this, _, window, cx| {
1133                                        this.dispatch_action(&OpenAgentDiff, window, cx);
1134                                    })
1135                                }),
1136                        )
1137                    })
1138                    .child(vertical_divider())
1139                    .on_action({
1140                        let editor = editor.clone();
1141                        move |_action: &OpenAgentDiff, window, cx| {
1142                            AgentDiff::global(cx).update(cx, |agent_diff, cx| {
1143                                agent_diff.deploy_pane_from_editor(&editor, window, cx);
1144                            });
1145                        }
1146                    })
1147                    .into_any()
1148            }
1149            AgentDiffToolbarItem::Pane(agent_diff) => {
1150                let Some(agent_diff) = agent_diff.upgrade() else {
1151                    return Empty.into_any();
1152                };
1153
1154                let has_pending_edit_tool_use = agent_diff
1155                    .read(cx)
1156                    .thread
1157                    .read(cx)
1158                    .has_pending_edit_tool_calls();
1159
1160                if has_pending_edit_tool_use {
1161                    return div().px_2().child(spinner_icon).into_any();
1162                }
1163
1164                let is_empty = agent_diff.read(cx).multibuffer.read(cx).is_empty();
1165                if is_empty {
1166                    return Empty.into_any();
1167                }
1168
1169                let focus_handle = agent_diff.focus_handle(cx);
1170
1171                h_group_xl()
1172                    .my_neg_1()
1173                    .py_1()
1174                    .items_center()
1175                    .flex_wrap()
1176                    .child(
1177                        h_group_sm()
1178                            .child(
1179                                Button::new("reject-all", "Reject All")
1180                                    .key_binding({
1181                                        KeyBinding::for_action_in(&RejectAll, &focus_handle, cx)
1182                                            .map(|kb| kb.size(rems_from_px(12.)))
1183                                    })
1184                                    .on_click(cx.listener(|this, _, window, cx| {
1185                                        this.dispatch_action(&RejectAll, window, cx)
1186                                    })),
1187                            )
1188                            .child(
1189                                Button::new("keep-all", "Keep All")
1190                                    .key_binding({
1191                                        KeyBinding::for_action_in(&KeepAll, &focus_handle, cx)
1192                                            .map(|kb| kb.size(rems_from_px(12.)))
1193                                    })
1194                                    .on_click(cx.listener(|this, _, window, cx| {
1195                                        this.dispatch_action(&KeepAll, window, cx)
1196                                    })),
1197                            ),
1198                    )
1199                    .into_any()
1200            }
1201        }
1202    }
1203}
1204
1205#[derive(Default)]
1206pub struct AgentDiff {
1207    reviewing_editors: HashMap<WeakEntity<Editor>, EditorState>,
1208    workspace_threads: HashMap<WeakEntity<Workspace>, WorkspaceThread>,
1209}
1210
1211#[derive(Clone, Debug, PartialEq, Eq)]
1212pub enum EditorState {
1213    Idle,
1214    Reviewing,
1215}
1216
1217struct WorkspaceThread {
1218    thread: WeakEntity<AcpThread>,
1219    _thread_subscriptions: (Subscription, Subscription),
1220    singleton_editors: HashMap<WeakEntity<Buffer>, HashMap<WeakEntity<Editor>, Subscription>>,
1221    _settings_subscription: Subscription,
1222    _workspace_subscription: Option<Subscription>,
1223}
1224
1225struct AgentDiffGlobal(Entity<AgentDiff>);
1226
1227impl Global for AgentDiffGlobal {}
1228
1229impl AgentDiff {
1230    fn global(cx: &mut App) -> Entity<Self> {
1231        cx.try_global::<AgentDiffGlobal>()
1232            .map(|global| global.0.clone())
1233            .unwrap_or_else(|| {
1234                let entity = cx.new(|_cx| Self::default());
1235                let global = AgentDiffGlobal(entity.clone());
1236                cx.set_global(global);
1237                entity
1238            })
1239    }
1240
1241    pub fn set_active_thread(
1242        workspace: &WeakEntity<Workspace>,
1243        thread: Entity<AcpThread>,
1244        window: &mut Window,
1245        cx: &mut App,
1246    ) {
1247        Self::global(cx).update(cx, |this, cx| {
1248            this.register_active_thread_impl(workspace, thread, window, cx);
1249        });
1250    }
1251
1252    fn register_active_thread_impl(
1253        &mut self,
1254        workspace: &WeakEntity<Workspace>,
1255        thread: Entity<AcpThread>,
1256        window: &mut Window,
1257        cx: &mut Context<Self>,
1258    ) {
1259        let action_log = thread.read(cx).action_log().clone();
1260
1261        let action_log_subscription = cx.observe_in(&action_log, window, {
1262            let workspace = workspace.clone();
1263            move |this, _action_log, window, cx| {
1264                this.update_reviewing_editors(&workspace, window, cx);
1265            }
1266        });
1267
1268        let thread_subscription = cx.subscribe_in(&thread, window, {
1269            let workspace = workspace.clone();
1270            move |this, thread, event, window, cx| {
1271                this.handle_acp_thread_event(&workspace, thread, event, window, cx)
1272            }
1273        });
1274
1275        if let Some(workspace_thread) = self.workspace_threads.get_mut(workspace) {
1276            // replace thread and action log subscription, but keep editors
1277            workspace_thread.thread = thread.downgrade();
1278            workspace_thread._thread_subscriptions = (action_log_subscription, thread_subscription);
1279            self.update_reviewing_editors(workspace, window, cx);
1280            return;
1281        }
1282
1283        let settings_subscription = cx.observe_global_in::<SettingsStore>(window, {
1284            let workspace = workspace.clone();
1285            let mut was_active = AgentSettings::get_global(cx).single_file_review;
1286            move |this, window, cx| {
1287                let is_active = AgentSettings::get_global(cx).single_file_review;
1288                if was_active != is_active {
1289                    was_active = is_active;
1290                    this.update_reviewing_editors(&workspace, window, cx);
1291                }
1292            }
1293        });
1294
1295        let workspace_subscription = workspace
1296            .upgrade()
1297            .map(|workspace| cx.subscribe_in(&workspace, window, Self::handle_workspace_event));
1298
1299        self.workspace_threads.insert(
1300            workspace.clone(),
1301            WorkspaceThread {
1302                thread: thread.downgrade(),
1303                _thread_subscriptions: (action_log_subscription, thread_subscription),
1304                singleton_editors: HashMap::default(),
1305                _settings_subscription: settings_subscription,
1306                _workspace_subscription: workspace_subscription,
1307            },
1308        );
1309
1310        let workspace = workspace.clone();
1311        cx.defer_in(window, move |this, window, cx| {
1312            if let Some(workspace) = workspace.upgrade() {
1313                this.register_workspace(workspace, window, cx);
1314            }
1315        });
1316    }
1317
1318    fn register_workspace(
1319        &mut self,
1320        workspace: Entity<Workspace>,
1321        window: &mut Window,
1322        cx: &mut Context<Self>,
1323    ) {
1324        let agent_diff = cx.entity();
1325
1326        let editors = workspace.update(cx, |workspace, cx| {
1327            let agent_diff = agent_diff.clone();
1328
1329            Self::register_review_action::<Keep>(workspace, Self::keep, &agent_diff);
1330            Self::register_review_action::<Reject>(workspace, Self::reject, &agent_diff);
1331            Self::register_review_action::<KeepAll>(workspace, Self::keep_all, &agent_diff);
1332            Self::register_review_action::<RejectAll>(workspace, Self::reject_all, &agent_diff);
1333
1334            workspace.items_of_type(cx).collect::<Vec<_>>()
1335        });
1336
1337        let weak_workspace = workspace.downgrade();
1338
1339        for editor in editors {
1340            if let Some(buffer) = Self::full_editor_buffer(editor.read(cx), cx) {
1341                self.register_editor(weak_workspace.clone(), buffer, editor, window, cx);
1342            };
1343        }
1344
1345        self.update_reviewing_editors(&weak_workspace, window, cx);
1346    }
1347
1348    fn register_review_action<T: Action>(
1349        workspace: &mut Workspace,
1350        review: impl Fn(
1351            &Entity<Editor>,
1352            &Entity<AcpThread>,
1353            &WeakEntity<Workspace>,
1354            &mut Window,
1355            &mut App,
1356        ) -> PostReviewState
1357        + 'static,
1358        this: &Entity<AgentDiff>,
1359    ) {
1360        let this = this.clone();
1361        workspace.register_action(move |workspace, _: &T, window, cx| {
1362            let review = &review;
1363            let task = this.update(cx, |this, cx| {
1364                this.review_in_active_editor(workspace, review, window, cx)
1365            });
1366
1367            if let Some(task) = task {
1368                task.detach_and_log_err(cx);
1369            } else {
1370                cx.propagate();
1371            }
1372        });
1373    }
1374
1375    fn handle_acp_thread_event(
1376        &mut self,
1377        workspace: &WeakEntity<Workspace>,
1378        thread: &Entity<AcpThread>,
1379        event: &AcpThreadEvent,
1380        window: &mut Window,
1381        cx: &mut Context<Self>,
1382    ) {
1383        match event {
1384            AcpThreadEvent::NewEntry => {
1385                if thread
1386                    .read(cx)
1387                    .entries()
1388                    .last()
1389                    .is_some_and(|entry| entry.diffs().next().is_some())
1390                {
1391                    self.update_reviewing_editors(workspace, window, cx);
1392                }
1393            }
1394            AcpThreadEvent::EntryUpdated(ix) => {
1395                if thread
1396                    .read(cx)
1397                    .entries()
1398                    .get(*ix)
1399                    .is_some_and(|entry| entry.diffs().next().is_some())
1400                {
1401                    self.update_reviewing_editors(workspace, window, cx);
1402                }
1403            }
1404            AcpThreadEvent::Stopped(_) => {
1405                self.update_reviewing_editors(workspace, window, cx);
1406            }
1407            AcpThreadEvent::Error | AcpThreadEvent::LoadError(_) | AcpThreadEvent::Refusal => {
1408                self.update_reviewing_editors(workspace, window, cx);
1409            }
1410            AcpThreadEvent::TitleUpdated
1411            | AcpThreadEvent::TokenUsageUpdated
1412            | AcpThreadEvent::SubagentSpawned(_)
1413            | AcpThreadEvent::EntriesRemoved(_)
1414            | AcpThreadEvent::ToolAuthorizationRequested(_)
1415            | AcpThreadEvent::ToolAuthorizationReceived(_)
1416            | AcpThreadEvent::PromptCapabilitiesUpdated
1417            | AcpThreadEvent::AvailableCommandsUpdated(_)
1418            | AcpThreadEvent::Retry(_)
1419            | AcpThreadEvent::ModeUpdated(_)
1420            | AcpThreadEvent::ConfigOptionsUpdated(_)
1421            | AcpThreadEvent::WorkingDirectoriesUpdated
1422            | AcpThreadEvent::PromptUpdated => {}
1423        }
1424    }
1425
1426    fn handle_workspace_event(
1427        &mut self,
1428        workspace: &Entity<Workspace>,
1429        event: &workspace::Event,
1430        window: &mut Window,
1431        cx: &mut Context<Self>,
1432    ) {
1433        if let workspace::Event::ItemAdded { item } = event
1434            && let Some(editor) = item.downcast::<Editor>()
1435            && let Some(buffer) = Self::full_editor_buffer(editor.read(cx), cx)
1436        {
1437            self.register_editor(workspace.downgrade(), buffer, editor, window, cx);
1438        }
1439    }
1440
1441    fn full_editor_buffer(editor: &Editor, cx: &App) -> Option<WeakEntity<Buffer>> {
1442        if editor.mode().is_full() {
1443            editor
1444                .buffer()
1445                .read(cx)
1446                .as_singleton()
1447                .map(|buffer| buffer.downgrade())
1448        } else {
1449            None
1450        }
1451    }
1452
1453    fn register_editor(
1454        &mut self,
1455        workspace: WeakEntity<Workspace>,
1456        buffer: WeakEntity<Buffer>,
1457        editor: Entity<Editor>,
1458        window: &mut Window,
1459        cx: &mut Context<Self>,
1460    ) {
1461        let Some(workspace_thread) = self.workspace_threads.get_mut(&workspace) else {
1462            return;
1463        };
1464
1465        let weak_editor = editor.downgrade();
1466
1467        workspace_thread
1468            .singleton_editors
1469            .entry(buffer.clone())
1470            .or_default()
1471            .entry(weak_editor.clone())
1472            .or_insert_with(|| {
1473                let workspace = workspace.clone();
1474                cx.observe_release(&editor, move |this, _, _cx| {
1475                    let Some(active_thread) = this.workspace_threads.get_mut(&workspace) else {
1476                        return;
1477                    };
1478
1479                    if let Entry::Occupied(mut entry) =
1480                        active_thread.singleton_editors.entry(buffer)
1481                    {
1482                        let set = entry.get_mut();
1483                        set.remove(&weak_editor);
1484
1485                        if set.is_empty() {
1486                            entry.remove();
1487                        }
1488                    }
1489                })
1490            });
1491
1492        self.update_reviewing_editors(&workspace, window, cx);
1493    }
1494
1495    fn update_reviewing_editors(
1496        &mut self,
1497        workspace: &WeakEntity<Workspace>,
1498        window: &mut Window,
1499        cx: &mut Context<Self>,
1500    ) {
1501        if !AgentSettings::get_global(cx).single_file_review {
1502            for (editor, _) in self.reviewing_editors.drain() {
1503                editor
1504                    .update(cx, |editor, cx| {
1505                        editor.end_temporary_diff_override(cx);
1506                        editor.unregister_addon::<EditorAgentDiffAddon>();
1507                    })
1508                    .ok();
1509            }
1510            return;
1511        }
1512
1513        let Some(workspace_thread) = self.workspace_threads.get_mut(workspace) else {
1514            return;
1515        };
1516
1517        let Some(thread) = workspace_thread.thread.upgrade() else {
1518            return;
1519        };
1520
1521        let action_log = thread.read(cx).action_log();
1522        let changed_buffers = action_log.read(cx).changed_buffers(cx);
1523
1524        let mut unaffected = self.reviewing_editors.clone();
1525
1526        for (buffer, diff_handle) in changed_buffers {
1527            if buffer.read(cx).file().is_none() {
1528                continue;
1529            }
1530
1531            let Some(buffer_editors) = workspace_thread.singleton_editors.get(&buffer.downgrade())
1532            else {
1533                continue;
1534            };
1535
1536            for weak_editor in buffer_editors.keys() {
1537                let Some(editor) = weak_editor.upgrade() else {
1538                    continue;
1539                };
1540
1541                let multibuffer = editor.read(cx).buffer().clone();
1542                multibuffer.update(cx, |multibuffer, cx| {
1543                    multibuffer.add_diff(diff_handle.clone(), cx);
1544                });
1545
1546                let reviewing_state = EditorState::Reviewing;
1547
1548                let previous_state = self
1549                    .reviewing_editors
1550                    .insert(weak_editor.clone(), reviewing_state.clone());
1551
1552                if previous_state.is_none() {
1553                    editor.update(cx, |editor, cx| {
1554                        editor.start_temporary_diff_override();
1555                        editor.set_render_diff_hunk_controls(
1556                            diff_hunk_controls(&thread, workspace.clone()),
1557                            cx,
1558                        );
1559                        editor.set_expand_all_diff_hunks(cx);
1560                        editor.register_addon(EditorAgentDiffAddon);
1561                    });
1562                } else {
1563                    unaffected.remove(weak_editor);
1564                }
1565
1566                if reviewing_state == EditorState::Reviewing
1567                    && previous_state != Some(reviewing_state)
1568                {
1569                    // Jump to first hunk when we enter review mode
1570                    editor.update(cx, |editor, cx| {
1571                        let snapshot = multibuffer.read(cx).snapshot(cx);
1572                        if let Some(first_hunk) = snapshot.diff_hunks().next() {
1573                            let first_hunk_start = first_hunk.multi_buffer_range.start;
1574
1575                            editor.change_selections(
1576                                SelectionEffects::scroll(Autoscroll::center()),
1577                                window,
1578                                cx,
1579                                |selections| {
1580                                    selections.select_ranges([first_hunk_start..first_hunk_start])
1581                                },
1582                            );
1583                        }
1584                    });
1585                }
1586            }
1587        }
1588
1589        // Remove editors from this workspace that are no longer under review
1590        for (editor, _) in unaffected {
1591            // Note: We could avoid this check by storing `reviewing_editors` by Workspace,
1592            // but that would add another lookup in `AgentDiff::editor_state`
1593            // which gets called much more frequently.
1594            let in_workspace = editor
1595                .read_with(cx, |editor, _cx| editor.workspace())
1596                .ok()
1597                .flatten()
1598                .is_some_and(|editor_workspace| {
1599                    editor_workspace.entity_id() == workspace.entity_id()
1600                });
1601
1602            if in_workspace {
1603                editor
1604                    .update(cx, |editor, cx| {
1605                        editor.end_temporary_diff_override(cx);
1606                        editor.unregister_addon::<EditorAgentDiffAddon>();
1607                    })
1608                    .ok();
1609                self.reviewing_editors.remove(&editor);
1610            }
1611        }
1612
1613        cx.notify();
1614    }
1615
1616    fn editor_state(&self, editor: &WeakEntity<Editor>) -> EditorState {
1617        self.reviewing_editors
1618            .get(editor)
1619            .cloned()
1620            .unwrap_or(EditorState::Idle)
1621    }
1622
1623    fn deploy_pane_from_editor(&self, editor: &Entity<Editor>, window: &mut Window, cx: &mut App) {
1624        let Some(workspace) = editor.read(cx).workspace() else {
1625            return;
1626        };
1627
1628        let Some(WorkspaceThread { thread, .. }) =
1629            self.workspace_threads.get(&workspace.downgrade())
1630        else {
1631            return;
1632        };
1633
1634        let Some(thread) = thread.upgrade() else {
1635            return;
1636        };
1637
1638        AgentDiffPane::deploy(thread, workspace.downgrade(), window, cx).log_err();
1639    }
1640
1641    fn keep_all(
1642        editor: &Entity<Editor>,
1643        thread: &Entity<AcpThread>,
1644        _workspace: &WeakEntity<Workspace>,
1645        window: &mut Window,
1646        cx: &mut App,
1647    ) -> PostReviewState {
1648        editor.update(cx, |editor, cx| {
1649            let snapshot = editor.buffer().read(cx).snapshot(cx);
1650            keep_edits_in_ranges(
1651                editor,
1652                &snapshot,
1653                thread,
1654                vec![editor::Anchor::Min..editor::Anchor::Max],
1655                window,
1656                cx,
1657            );
1658        });
1659        PostReviewState::AllReviewed
1660    }
1661
1662    fn reject_all(
1663        editor: &Entity<Editor>,
1664        thread: &Entity<AcpThread>,
1665        workspace: &WeakEntity<Workspace>,
1666        window: &mut Window,
1667        cx: &mut App,
1668    ) -> PostReviewState {
1669        editor.update(cx, |editor, cx| {
1670            let snapshot = editor.buffer().read(cx).snapshot(cx);
1671            reject_edits_in_ranges(
1672                editor,
1673                &snapshot,
1674                thread,
1675                vec![editor::Anchor::Min..editor::Anchor::Max],
1676                workspace.clone(),
1677                window,
1678                cx,
1679            );
1680        });
1681        PostReviewState::AllReviewed
1682    }
1683
1684    fn keep(
1685        editor: &Entity<Editor>,
1686        thread: &Entity<AcpThread>,
1687        _workspace: &WeakEntity<Workspace>,
1688        window: &mut Window,
1689        cx: &mut App,
1690    ) -> PostReviewState {
1691        editor.update(cx, |editor, cx| {
1692            let snapshot = editor.buffer().read(cx).snapshot(cx);
1693            keep_edits_in_selection(editor, &snapshot, thread, window, cx);
1694            Self::post_review_state(&snapshot)
1695        })
1696    }
1697
1698    fn reject(
1699        editor: &Entity<Editor>,
1700        thread: &Entity<AcpThread>,
1701        workspace: &WeakEntity<Workspace>,
1702        window: &mut Window,
1703        cx: &mut App,
1704    ) -> PostReviewState {
1705        editor.update(cx, |editor, cx| {
1706            let snapshot = editor.buffer().read(cx).snapshot(cx);
1707            reject_edits_in_selection(editor, &snapshot, thread, workspace.clone(), window, cx);
1708            Self::post_review_state(&snapshot)
1709        })
1710    }
1711
1712    fn post_review_state(snapshot: &MultiBufferSnapshot) -> PostReviewState {
1713        for (i, _) in snapshot.diff_hunks().enumerate() {
1714            if i > 0 {
1715                return PostReviewState::Pending;
1716            }
1717        }
1718        PostReviewState::AllReviewed
1719    }
1720
1721    fn review_in_active_editor(
1722        &mut self,
1723        workspace: &mut Workspace,
1724        review: impl Fn(
1725            &Entity<Editor>,
1726            &Entity<AcpThread>,
1727            &WeakEntity<Workspace>,
1728            &mut Window,
1729            &mut App,
1730        ) -> PostReviewState,
1731        window: &mut Window,
1732        cx: &mut Context<Self>,
1733    ) -> Option<Task<Result<()>>> {
1734        let active_item = workspace.active_item(cx)?;
1735        let editor = active_item.act_as::<Editor>(cx)?;
1736
1737        if !matches!(
1738            self.editor_state(&editor.downgrade()),
1739            EditorState::Reviewing
1740        ) {
1741            return None;
1742        }
1743
1744        let WorkspaceThread { thread, .. } =
1745            self.workspace_threads.get(&workspace.weak_handle())?;
1746
1747        let thread = thread.upgrade()?;
1748
1749        let review_result = review(&editor, &thread, &workspace.weak_handle(), window, cx);
1750
1751        if matches!(review_result, PostReviewState::AllReviewed)
1752            && let Some(curr_buffer) = editor.read(cx).buffer().read(cx).as_singleton()
1753        {
1754            let changed_buffers = thread.read(cx).action_log().read(cx).changed_buffers(cx);
1755
1756            let mut keys = changed_buffers.keys();
1757            keys.find(|k| *k == &curr_buffer);
1758            let next_project_path = keys
1759                .next()
1760                .filter(|k| *k != &curr_buffer)
1761                .and_then(|after| after.read(cx).project_path(cx));
1762
1763            if let Some(path) = next_project_path {
1764                let task = workspace.open_path(path, None, true, window, cx);
1765                let task = cx.spawn(async move |_, _cx| task.await.map(|_| ()));
1766                return Some(task);
1767            }
1768        }
1769
1770        Some(Task::ready(Ok(())))
1771    }
1772}
1773
1774enum PostReviewState {
1775    AllReviewed,
1776    Pending,
1777}
1778
1779pub struct EditorAgentDiffAddon;
1780
1781impl editor::Addon for EditorAgentDiffAddon {
1782    fn to_any(&self) -> &dyn std::any::Any {
1783        self
1784    }
1785
1786    fn extend_key_context(&self, key_context: &mut gpui::KeyContext, _: &App) {
1787        key_context.add("agent_diff");
1788        key_context.add("editor_agent_diff");
1789    }
1790}
1791
1792#[cfg(test)]
1793mod tests {
1794    use super::*;
1795    use crate::Keep;
1796    use acp_thread::AgentConnection as _;
1797    use agent_settings::AgentSettings;
1798    use editor::EditorSettings;
1799    use gpui::{TestAppContext, UpdateGlobal, VisualTestContext};
1800    use project::{FakeFs, Project};
1801    use serde_json::json;
1802    use settings::SettingsStore;
1803    use std::{path::Path, rc::Rc};
1804    use util::path;
1805    use workspace::{MultiWorkspace, PathList};
1806
1807    #[gpui::test]
1808    async fn test_multibuffer_agent_diff(cx: &mut TestAppContext) {
1809        cx.update(|cx| {
1810            let settings_store = SettingsStore::test(cx);
1811            cx.set_global(settings_store);
1812            prompt_store::init(cx);
1813            theme_settings::init(theme::LoadThemes::JustBase, cx);
1814            language_model::init(cx);
1815        });
1816
1817        let fs = FakeFs::new(cx.executor());
1818        fs.insert_tree(
1819            path!("/test"),
1820            json!({"file1": "abc\ndef\nghi\njkl\nmno\npqr\nstu\nvwx\nyz"}),
1821        )
1822        .await;
1823        let project = Project::test(fs, [path!("/test").as_ref()], cx).await;
1824        let buffer_path = project
1825            .read_with(cx, |project, cx| {
1826                project.find_project_path("test/file1", cx)
1827            })
1828            .unwrap();
1829
1830        let connection = Rc::new(acp_thread::StubAgentConnection::new());
1831        let thread = cx
1832            .update(|cx| {
1833                connection.clone().new_session(
1834                    project.clone(),
1835                    PathList::new(&[Path::new(path!("/test"))]),
1836                    cx,
1837                )
1838            })
1839            .await
1840            .unwrap();
1841
1842        let action_log = cx.read(|cx| thread.read(cx).action_log().clone());
1843
1844        let (multi_workspace, cx) =
1845            cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
1846        let workspace = multi_workspace.read_with(cx, |mw, _| mw.workspace().clone());
1847        let agent_diff = cx.new_window_entity(|window, cx| {
1848            AgentDiffPane::new(thread.clone(), workspace.downgrade(), window, cx)
1849        });
1850        let editor = agent_diff.read_with(cx, |diff, _cx| diff.editor.clone());
1851
1852        let buffer = project
1853            .update(cx, |project, cx| project.open_buffer(buffer_path, cx))
1854            .await
1855            .unwrap();
1856        cx.update(|_, cx| {
1857            action_log.update(cx, |log, cx| log.buffer_read(buffer.clone(), cx));
1858            buffer.update(cx, |buffer, cx| {
1859                buffer
1860                    .edit(
1861                        [
1862                            (Point::new(1, 1)..Point::new(1, 2), "E"),
1863                            (Point::new(3, 2)..Point::new(3, 3), "L"),
1864                            (Point::new(5, 0)..Point::new(5, 1), "P"),
1865                            (Point::new(7, 1)..Point::new(7, 2), "W"),
1866                        ],
1867                        None,
1868                        cx,
1869                    )
1870                    .unwrap()
1871            });
1872            action_log.update(cx, |log, cx| log.buffer_edited(buffer.clone(), cx));
1873        });
1874        cx.run_until_parked();
1875
1876        // When opening the assistant diff, the cursor is positioned on the first hunk.
1877        assert_eq!(
1878            editor.read_with(cx, |editor, cx| editor.text(cx)),
1879            "abc\ndef\ndEf\nghi\njkl\njkL\nmno\npqr\nPqr\nstu\nvwx\nvWx\nyz"
1880        );
1881        assert_eq!(
1882            editor
1883                .update(cx, |editor, cx| editor
1884                    .selections
1885                    .newest::<Point>(&editor.display_snapshot(cx)))
1886                .range(),
1887            Point::new(1, 0)..Point::new(1, 0)
1888        );
1889
1890        // After keeping a hunk, the cursor should be positioned on the second hunk.
1891        agent_diff.update_in(cx, |diff, window, cx| diff.keep(&Keep, window, cx));
1892        cx.run_until_parked();
1893        assert_eq!(
1894            editor.read_with(cx, |editor, cx| editor.text(cx)),
1895            "abc\ndEf\nghi\njkl\njkL\nmno\npqr\nPqr\nstu\nvwx\nvWx\nyz"
1896        );
1897        assert_eq!(
1898            editor
1899                .update(cx, |editor, cx| editor
1900                    .selections
1901                    .newest::<Point>(&editor.display_snapshot(cx)))
1902                .range(),
1903            Point::new(3, 0)..Point::new(3, 0)
1904        );
1905
1906        // Rejecting a hunk also moves the cursor to the next hunk, possibly cycling if it's at the end.
1907        editor.update_in(cx, |editor, window, cx| {
1908            editor.change_selections(SelectionEffects::no_scroll(), window, cx, |selections| {
1909                selections.select_ranges([Point::new(10, 0)..Point::new(10, 0)])
1910            });
1911        });
1912        agent_diff.update_in(cx, |diff, window, cx| {
1913            diff.reject(&crate::Reject, window, cx)
1914        });
1915        cx.run_until_parked();
1916        assert_eq!(
1917            editor.read_with(cx, |editor, cx| editor.text(cx)),
1918            "abc\ndEf\nghi\njkl\njkL\nmno\npqr\nPqr\nstu\nvwx\nyz"
1919        );
1920        assert_eq!(
1921            editor
1922                .update(cx, |editor, cx| editor
1923                    .selections
1924                    .newest::<Point>(&editor.display_snapshot(cx)))
1925                .range(),
1926            Point::new(3, 0)..Point::new(3, 0)
1927        );
1928
1929        // Keeping a range that doesn't intersect the current selection doesn't move it.
1930        agent_diff.update_in(cx, |_diff, window, cx| {
1931            let position = editor
1932                .read(cx)
1933                .buffer()
1934                .read(cx)
1935                .read(cx)
1936                .anchor_before(Point::new(7, 0));
1937            editor.update(cx, |editor, cx| {
1938                let snapshot = editor.buffer().read(cx).snapshot(cx);
1939                keep_edits_in_ranges(
1940                    editor,
1941                    &snapshot,
1942                    &thread,
1943                    vec![position..position],
1944                    window,
1945                    cx,
1946                )
1947            });
1948        });
1949        cx.run_until_parked();
1950        assert_eq!(
1951            editor.read_with(cx, |editor, cx| editor.text(cx)),
1952            "abc\ndEf\nghi\njkl\njkL\nmno\nPqr\nstu\nvwx\nyz"
1953        );
1954        assert_eq!(
1955            editor
1956                .update(cx, |editor, cx| editor
1957                    .selections
1958                    .newest::<Point>(&editor.display_snapshot(cx)))
1959                .range(),
1960            Point::new(3, 0)..Point::new(3, 0)
1961        );
1962    }
1963
1964    #[gpui::test]
1965    async fn test_single_file_review_diff(cx: &mut TestAppContext) {
1966        cx.update(|cx| {
1967            let settings_store = SettingsStore::test(cx);
1968            cx.set_global(settings_store);
1969            prompt_store::init(cx);
1970            theme_settings::init(theme::LoadThemes::JustBase, cx);
1971            language_model::init(cx);
1972            workspace::register_project_item::<Editor>(cx);
1973        });
1974
1975        cx.update(|cx| {
1976            SettingsStore::update_global(cx, |store, _cx| {
1977                let mut agent_settings = store.get::<AgentSettings>(None).clone();
1978                agent_settings.single_file_review = true;
1979                store.override_global(agent_settings);
1980            });
1981        });
1982
1983        let fs = FakeFs::new(cx.executor());
1984        fs.insert_tree(
1985            path!("/test"),
1986            json!({"file1": "abc\ndef\nghi\njkl\nmno\npqr\nstu\nvwx\nyz"}),
1987        )
1988        .await;
1989        fs.insert_tree(path!("/test"), json!({"file2": "abc\ndef\nghi"}))
1990            .await;
1991
1992        let project = Project::test(fs, [path!("/test").as_ref()], cx).await;
1993        let buffer_path1 = project
1994            .read_with(cx, |project, cx| {
1995                project.find_project_path("test/file1", cx)
1996            })
1997            .unwrap();
1998        let buffer_path2 = project
1999            .read_with(cx, |project, cx| {
2000                project.find_project_path("test/file2", cx)
2001            })
2002            .unwrap();
2003
2004        let (multi_workspace, cx) =
2005            cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
2006        let workspace = multi_workspace.read_with(cx, |mw, _| mw.workspace().clone());
2007
2008        // Add the diff toolbar to the active pane
2009        let diff_toolbar = cx.new_window_entity(|_, cx| AgentDiffToolbar::new(cx));
2010
2011        workspace.update_in(cx, {
2012            let diff_toolbar = diff_toolbar.clone();
2013
2014            move |workspace, window, cx| {
2015                workspace.active_pane().update(cx, |pane, cx| {
2016                    pane.toolbar().update(cx, |toolbar, cx| {
2017                        toolbar.add_item(diff_toolbar, window, cx);
2018                    });
2019                })
2020            }
2021        });
2022
2023        let connection = Rc::new(acp_thread::StubAgentConnection::new());
2024        let thread = cx
2025            .update(|_, cx| {
2026                connection.clone().new_session(
2027                    project.clone(),
2028                    PathList::new(&[Path::new(path!("/test"))]),
2029                    cx,
2030                )
2031            })
2032            .await
2033            .unwrap();
2034        let action_log = thread.read_with(cx, |thread, _| thread.action_log().clone());
2035
2036        // Set the active thread
2037        cx.update(|window, cx| {
2038            AgentDiff::set_active_thread(&workspace.downgrade(), thread.clone(), window, cx)
2039        });
2040
2041        let buffer1 = project
2042            .update(cx, |project, cx| {
2043                project.open_buffer(buffer_path1.clone(), cx)
2044            })
2045            .await
2046            .unwrap();
2047        let buffer2 = project
2048            .update(cx, |project, cx| {
2049                project.open_buffer(buffer_path2.clone(), cx)
2050            })
2051            .await
2052            .unwrap();
2053
2054        // Open an editor for buffer1
2055        let editor1 = cx.new_window_entity(|window, cx| {
2056            Editor::for_buffer(buffer1.clone(), Some(project.clone()), window, cx)
2057        });
2058
2059        workspace.update_in(cx, |workspace, window, cx| {
2060            workspace.add_item_to_active_pane(Box::new(editor1.clone()), None, true, window, cx);
2061        });
2062        cx.run_until_parked();
2063
2064        // Toolbar knows about the current editor, but it's hidden since there are no changes yet
2065        assert!(diff_toolbar.read_with(cx, |toolbar, _cx| matches!(
2066            toolbar.active_item,
2067            Some(AgentDiffToolbarItem::Editor {
2068                state: EditorState::Idle,
2069                ..
2070            })
2071        )));
2072        assert_eq!(
2073            diff_toolbar.read_with(cx, |toolbar, cx| toolbar.location(cx)),
2074            ToolbarItemLocation::Hidden
2075        );
2076
2077        // Make changes
2078        cx.update(|_, cx| {
2079            action_log.update(cx, |log, cx| log.buffer_read(buffer1.clone(), cx));
2080            buffer1.update(cx, |buffer, cx| {
2081                buffer
2082                    .edit(
2083                        [
2084                            (Point::new(1, 1)..Point::new(1, 2), "E"),
2085                            (Point::new(3, 2)..Point::new(3, 3), "L"),
2086                            (Point::new(5, 0)..Point::new(5, 1), "P"),
2087                            (Point::new(7, 1)..Point::new(7, 2), "W"),
2088                        ],
2089                        None,
2090                        cx,
2091                    )
2092                    .unwrap()
2093            });
2094            action_log.update(cx, |log, cx| log.buffer_edited(buffer1.clone(), cx));
2095
2096            action_log.update(cx, |log, cx| log.buffer_read(buffer2.clone(), cx));
2097            buffer2.update(cx, |buffer, cx| {
2098                buffer
2099                    .edit(
2100                        [
2101                            (Point::new(0, 0)..Point::new(0, 1), "A"),
2102                            (Point::new(2, 1)..Point::new(2, 2), "H"),
2103                        ],
2104                        None,
2105                        cx,
2106                    )
2107                    .unwrap();
2108            });
2109            action_log.update(cx, |log, cx| log.buffer_edited(buffer2.clone(), cx));
2110        });
2111        cx.run_until_parked();
2112
2113        // The already opened editor displays the diff and the cursor is at the first hunk
2114        assert_eq!(
2115            editor1.read_with(cx, |editor, cx| editor.text(cx)),
2116            "abc\ndef\ndEf\nghi\njkl\njkL\nmno\npqr\nPqr\nstu\nvwx\nvWx\nyz"
2117        );
2118        assert_eq!(
2119            editor1
2120                .update(cx, |editor, cx| editor
2121                    .selections
2122                    .newest::<Point>(&editor.display_snapshot(cx)))
2123                .range(),
2124            Point::new(1, 0)..Point::new(1, 0)
2125        );
2126
2127        // The toolbar is displayed in the right state
2128        assert_eq!(
2129            diff_toolbar.read_with(cx, |toolbar, cx| toolbar.location(cx)),
2130            ToolbarItemLocation::PrimaryRight
2131        );
2132        assert!(diff_toolbar.read_with(cx, |toolbar, _cx| matches!(
2133            toolbar.active_item,
2134            Some(AgentDiffToolbarItem::Editor {
2135                state: EditorState::Reviewing,
2136                ..
2137            })
2138        )));
2139
2140        // The toolbar respects its setting
2141        override_toolbar_agent_review_setting(false, cx);
2142        assert_eq!(
2143            diff_toolbar.read_with(cx, |toolbar, cx| toolbar.location(cx)),
2144            ToolbarItemLocation::Hidden
2145        );
2146        override_toolbar_agent_review_setting(true, cx);
2147        assert_eq!(
2148            diff_toolbar.read_with(cx, |toolbar, cx| toolbar.location(cx)),
2149            ToolbarItemLocation::PrimaryRight
2150        );
2151
2152        // After keeping a hunk, the cursor should be positioned on the second hunk.
2153        workspace.update(cx, |_, cx| {
2154            cx.dispatch_action(&Keep);
2155        });
2156        cx.run_until_parked();
2157        assert_eq!(
2158            editor1.read_with(cx, |editor, cx| editor.text(cx)),
2159            "abc\ndEf\nghi\njkl\njkL\nmno\npqr\nPqr\nstu\nvwx\nvWx\nyz"
2160        );
2161        assert_eq!(
2162            editor1
2163                .update(cx, |editor, cx| editor
2164                    .selections
2165                    .newest::<Point>(&editor.display_snapshot(cx)))
2166                .range(),
2167            Point::new(3, 0)..Point::new(3, 0)
2168        );
2169
2170        // Rejecting a hunk also moves the cursor to the next hunk, possibly cycling if it's at the end.
2171        editor1.update_in(cx, |editor, window, cx| {
2172            editor.change_selections(SelectionEffects::no_scroll(), window, cx, |selections| {
2173                selections.select_ranges([Point::new(10, 0)..Point::new(10, 0)])
2174            });
2175        });
2176        workspace.update(cx, |_, cx| {
2177            cx.dispatch_action(&Reject);
2178        });
2179        cx.run_until_parked();
2180        assert_eq!(
2181            editor1.read_with(cx, |editor, cx| editor.text(cx)),
2182            "abc\ndEf\nghi\njkl\njkL\nmno\npqr\nPqr\nstu\nvwx\nyz"
2183        );
2184        assert_eq!(
2185            editor1
2186                .update(cx, |editor, cx| editor
2187                    .selections
2188                    .newest::<Point>(&editor.display_snapshot(cx)))
2189                .range(),
2190            Point::new(3, 0)..Point::new(3, 0)
2191        );
2192
2193        // Keeping a range that doesn't intersect the current selection doesn't move it.
2194        editor1.update_in(cx, |editor, window, cx| {
2195            let buffer = editor.buffer().read(cx);
2196            let position = buffer.read(cx).anchor_before(Point::new(7, 0));
2197            let snapshot = buffer.snapshot(cx);
2198            keep_edits_in_ranges(
2199                editor,
2200                &snapshot,
2201                &thread,
2202                vec![position..position],
2203                window,
2204                cx,
2205            )
2206        });
2207        cx.run_until_parked();
2208        assert_eq!(
2209            editor1.read_with(cx, |editor, cx| editor.text(cx)),
2210            "abc\ndEf\nghi\njkl\njkL\nmno\nPqr\nstu\nvwx\nyz"
2211        );
2212        assert_eq!(
2213            editor1
2214                .update(cx, |editor, cx| editor
2215                    .selections
2216                    .newest::<Point>(&editor.display_snapshot(cx)))
2217                .range(),
2218            Point::new(3, 0)..Point::new(3, 0)
2219        );
2220
2221        // Reviewing the last change opens the next changed buffer
2222        workspace
2223            .update_in(cx, |workspace, window, cx| {
2224                AgentDiff::global(cx).update(cx, |agent_diff, cx| {
2225                    agent_diff.review_in_active_editor(workspace, AgentDiff::keep, window, cx)
2226                })
2227            })
2228            .unwrap()
2229            .await
2230            .unwrap();
2231
2232        cx.run_until_parked();
2233
2234        let editor2 = workspace.update(cx, |workspace, cx| {
2235            workspace.active_item_as::<Editor>(cx).unwrap()
2236        });
2237
2238        let editor2_path = editor2
2239            .read_with(cx, |editor, cx| editor.project_path(cx))
2240            .unwrap();
2241        assert_eq!(editor2_path, buffer_path2);
2242
2243        assert_eq!(
2244            editor2.read_with(cx, |editor, cx| editor.text(cx)),
2245            "abc\nAbc\ndef\nghi\ngHi"
2246        );
2247        assert_eq!(
2248            editor2
2249                .update(cx, |editor, cx| editor
2250                    .selections
2251                    .newest::<Point>(&editor.display_snapshot(cx)))
2252                .range(),
2253            Point::new(0, 0)..Point::new(0, 0)
2254        );
2255
2256        // Editor 1 toolbar is hidden since all changes have been reviewed
2257        workspace.update_in(cx, |workspace, window, cx| {
2258            workspace.activate_item(&editor1, true, true, window, cx)
2259        });
2260
2261        assert!(diff_toolbar.read_with(cx, |toolbar, _cx| matches!(
2262            toolbar.active_item,
2263            Some(AgentDiffToolbarItem::Editor {
2264                state: EditorState::Idle,
2265                ..
2266            })
2267        )));
2268        assert_eq!(
2269            diff_toolbar.read_with(cx, |toolbar, cx| toolbar.location(cx)),
2270            ToolbarItemLocation::Hidden
2271        );
2272    }
2273
2274    fn override_toolbar_agent_review_setting(active: bool, cx: &mut VisualTestContext) {
2275        cx.update(|_window, cx| {
2276            SettingsStore::update_global(cx, |store, _cx| {
2277                let mut editor_settings = store.get::<EditorSettings>(None).clone();
2278                editor_settings.toolbar.agent_review = active;
2279                store.override_global(editor_settings);
2280            })
2281        });
2282        cx.run_until_parked();
2283    }
2284}