agent_diff.rs

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