inline_assistant.rs

   1use crate::{
   2    Assistant, AssistantPanel, AssistantPanelEvent, CycleNextInlineAssist,
   3    CyclePreviousInlineAssist,
   4};
   5use anyhow::{Context as _, Result, anyhow};
   6use assistant_context_editor::{RequestType, humanize_token_count};
   7use assistant_settings::AssistantSettings;
   8use client::{ErrorExt, telemetry::Telemetry};
   9use collections::{HashMap, HashSet, VecDeque, hash_map};
  10use editor::{
  11    Anchor, AnchorRangeExt, CodeActionProvider, Editor, EditorElement, EditorEvent, EditorMode,
  12    EditorStyle, ExcerptId, ExcerptRange, GutterDimensions, MultiBuffer, MultiBufferSnapshot,
  13    ToOffset as _, ToPoint,
  14    actions::{MoveDown, MoveUp, SelectAll},
  15    display_map::{
  16        BlockContext, BlockPlacement, BlockProperties, BlockStyle, CustomBlockId, RenderBlock,
  17        ToDisplayPoint,
  18    },
  19};
  20use feature_flags::{
  21    Assistant2FeatureFlag, FeatureFlagAppExt as _, FeatureFlagViewExt as _, ZedProFeatureFlag,
  22};
  23use fs::Fs;
  24use futures::{
  25    SinkExt, Stream, StreamExt, TryStreamExt as _,
  26    channel::mpsc,
  27    future::{BoxFuture, LocalBoxFuture},
  28    join,
  29};
  30use gpui::{
  31    AnyElement, App, ClickEvent, Context, CursorStyle, Entity, EventEmitter, FocusHandle,
  32    Focusable, FontWeight, Global, HighlightStyle, Subscription, Task, TextStyle, UpdateGlobal,
  33    WeakEntity, Window, anchored, deferred, point,
  34};
  35use language::{Buffer, IndentKind, Point, Selection, TransactionId, line_diff};
  36use language_model::{
  37    ConfiguredModel, LanguageModel, LanguageModelRegistry, LanguageModelRequest,
  38    LanguageModelRequestMessage, LanguageModelTextStream, Role, report_assistant_event,
  39};
  40use language_model_selector::{LanguageModelSelector, LanguageModelSelectorPopoverMenu};
  41use multi_buffer::MultiBufferRow;
  42use parking_lot::Mutex;
  43use project::{CodeAction, LspAction, ProjectTransaction};
  44use prompt_store::PromptBuilder;
  45use rope::Rope;
  46use settings::{Settings, SettingsStore, update_settings_file};
  47use smol::future::FutureExt;
  48use std::{
  49    cmp,
  50    future::{self, Future},
  51    iter, mem,
  52    ops::{Range, RangeInclusive},
  53    pin::Pin,
  54    rc::Rc,
  55    sync::Arc,
  56    task::{self, Poll},
  57    time::{Duration, Instant},
  58};
  59use streaming_diff::{CharOperation, LineDiff, LineOperation, StreamingDiff};
  60use telemetry_events::{AssistantEventData, AssistantKind, AssistantPhase};
  61use terminal_view::terminal_panel::TerminalPanel;
  62use text::{OffsetRangeExt, ToPoint as _};
  63use theme::ThemeSettings;
  64use ui::{
  65    CheckboxWithLabel, IconButtonShape, KeyBinding, Popover, Tooltip, prelude::*, text_for_action,
  66};
  67use util::{RangeExt, ResultExt};
  68use workspace::{ItemHandle, Toast, Workspace, notifications::NotificationId};
  69
  70pub fn init(
  71    fs: Arc<dyn Fs>,
  72    prompt_builder: Arc<PromptBuilder>,
  73    telemetry: Arc<Telemetry>,
  74    cx: &mut App,
  75) {
  76    cx.set_global(InlineAssistant::new(fs, prompt_builder, telemetry));
  77    cx.observe_new(|_, window, cx| {
  78        let Some(window) = window else {
  79            return;
  80        };
  81        let workspace = cx.entity().clone();
  82        InlineAssistant::update_global(cx, |inline_assistant, cx| {
  83            inline_assistant.register_workspace(&workspace, window, cx)
  84        });
  85
  86        cx.observe_flag::<Assistant2FeatureFlag, _>(window, {
  87            |is_assistant2_enabled, _workspace, _window, cx| {
  88                InlineAssistant::update_global(cx, |inline_assistant, _cx| {
  89                    inline_assistant.is_assistant2_enabled = is_assistant2_enabled;
  90                });
  91            }
  92        })
  93        .detach();
  94    })
  95    .detach();
  96}
  97
  98const PROMPT_HISTORY_MAX_LEN: usize = 20;
  99
 100pub struct InlineAssistant {
 101    next_assist_id: InlineAssistId,
 102    next_assist_group_id: InlineAssistGroupId,
 103    assists: HashMap<InlineAssistId, InlineAssist>,
 104    assists_by_editor: HashMap<WeakEntity<Editor>, EditorInlineAssists>,
 105    assist_groups: HashMap<InlineAssistGroupId, InlineAssistGroup>,
 106    confirmed_assists: HashMap<InlineAssistId, Entity<CodegenAlternative>>,
 107    prompt_history: VecDeque<String>,
 108    prompt_builder: Arc<PromptBuilder>,
 109    telemetry: Arc<Telemetry>,
 110    fs: Arc<dyn Fs>,
 111    is_assistant2_enabled: bool,
 112}
 113
 114impl Global for InlineAssistant {}
 115
 116impl InlineAssistant {
 117    pub fn new(
 118        fs: Arc<dyn Fs>,
 119        prompt_builder: Arc<PromptBuilder>,
 120        telemetry: Arc<Telemetry>,
 121    ) -> Self {
 122        Self {
 123            next_assist_id: InlineAssistId::default(),
 124            next_assist_group_id: InlineAssistGroupId::default(),
 125            assists: HashMap::default(),
 126            assists_by_editor: HashMap::default(),
 127            assist_groups: HashMap::default(),
 128            confirmed_assists: HashMap::default(),
 129            prompt_history: VecDeque::default(),
 130            prompt_builder,
 131            telemetry,
 132            fs,
 133            is_assistant2_enabled: false,
 134        }
 135    }
 136
 137    pub fn register_workspace(
 138        &mut self,
 139        workspace: &Entity<Workspace>,
 140        window: &mut Window,
 141        cx: &mut App,
 142    ) {
 143        window
 144            .subscribe(workspace, cx, |workspace, event, window, cx| {
 145                Self::update_global(cx, |this, cx| {
 146                    this.handle_workspace_event(workspace, event, window, cx)
 147                });
 148            })
 149            .detach();
 150
 151        let workspace = workspace.downgrade();
 152        cx.observe_global::<SettingsStore>(move |cx| {
 153            let Some(workspace) = workspace.upgrade() else {
 154                return;
 155            };
 156            let Some(terminal_panel) = workspace.read(cx).panel::<TerminalPanel>(cx) else {
 157                return;
 158            };
 159            let enabled = AssistantSettings::get_global(cx).enabled;
 160            terminal_panel.update(cx, |terminal_panel, cx| {
 161                terminal_panel.set_assistant_enabled(enabled, cx)
 162            });
 163        })
 164        .detach();
 165    }
 166
 167    fn handle_workspace_event(
 168        &mut self,
 169        workspace: Entity<Workspace>,
 170        event: &workspace::Event,
 171        window: &mut Window,
 172        cx: &mut App,
 173    ) {
 174        match event {
 175            workspace::Event::UserSavedItem { item, .. } => {
 176                // When the user manually saves an editor, automatically accepts all finished transformations.
 177                if let Some(editor) = item.upgrade().and_then(|item| item.act_as::<Editor>(cx)) {
 178                    if let Some(editor_assists) = self.assists_by_editor.get(&editor.downgrade()) {
 179                        for assist_id in editor_assists.assist_ids.clone() {
 180                            let assist = &self.assists[&assist_id];
 181                            if let CodegenStatus::Done = assist.codegen.read(cx).status(cx) {
 182                                self.finish_assist(assist_id, false, window, cx)
 183                            }
 184                        }
 185                    }
 186                }
 187            }
 188            workspace::Event::ItemAdded { item } => {
 189                self.register_workspace_item(&workspace, item.as_ref(), window, cx);
 190            }
 191            _ => (),
 192        }
 193    }
 194
 195    fn register_workspace_item(
 196        &mut self,
 197        workspace: &Entity<Workspace>,
 198        item: &dyn ItemHandle,
 199        window: &mut Window,
 200        cx: &mut App,
 201    ) {
 202        let is_assistant2_enabled = self.is_assistant2_enabled;
 203
 204        if let Some(editor) = item.act_as::<Editor>(cx) {
 205            editor.update(cx, |editor, cx| {
 206                if is_assistant2_enabled {
 207                    editor.remove_code_action_provider(
 208                        ASSISTANT_CODE_ACTION_PROVIDER_ID.into(),
 209                        window,
 210                        cx,
 211                    );
 212                } else {
 213                    editor.add_code_action_provider(
 214                        Rc::new(AssistantCodeActionProvider {
 215                            editor: cx.entity().downgrade(),
 216                            workspace: workspace.downgrade(),
 217                        }),
 218                        window,
 219                        cx,
 220                    );
 221                }
 222            });
 223        }
 224    }
 225
 226    pub fn assist(
 227        &mut self,
 228        editor: &Entity<Editor>,
 229        workspace: Option<WeakEntity<Workspace>>,
 230        assistant_panel: Option<&Entity<AssistantPanel>>,
 231        initial_prompt: Option<String>,
 232        window: &mut Window,
 233        cx: &mut App,
 234    ) {
 235        let (snapshot, initial_selections) = editor.update(cx, |editor, cx| {
 236            (
 237                editor.snapshot(window, cx),
 238                editor.selections.all::<Point>(cx),
 239            )
 240        });
 241
 242        let mut selections = Vec::<Selection<Point>>::new();
 243        let mut newest_selection = None;
 244        for mut selection in initial_selections {
 245            if selection.end > selection.start {
 246                selection.start.column = 0;
 247                // If the selection ends at the start of the line, we don't want to include it.
 248                if selection.end.column == 0 {
 249                    selection.end.row -= 1;
 250                }
 251                selection.end.column = snapshot
 252                    .buffer_snapshot
 253                    .line_len(MultiBufferRow(selection.end.row));
 254            } else if let Some(fold) =
 255                snapshot.crease_for_buffer_row(MultiBufferRow(selection.end.row))
 256            {
 257                selection.start = fold.range().start;
 258                selection.end = fold.range().end;
 259                if MultiBufferRow(selection.end.row) < snapshot.buffer_snapshot.max_row() {
 260                    let chars = snapshot
 261                        .buffer_snapshot
 262                        .chars_at(Point::new(selection.end.row + 1, 0));
 263
 264                    for c in chars {
 265                        if c == '\n' {
 266                            break;
 267                        }
 268                        if c.is_whitespace() {
 269                            continue;
 270                        }
 271                        if snapshot
 272                            .language_at(selection.end)
 273                            .is_some_and(|language| language.config().brackets.is_closing_brace(c))
 274                        {
 275                            selection.end.row += 1;
 276                            selection.end.column = snapshot
 277                                .buffer_snapshot
 278                                .line_len(MultiBufferRow(selection.end.row));
 279                        }
 280                    }
 281                }
 282            }
 283
 284            if let Some(prev_selection) = selections.last_mut() {
 285                if selection.start <= prev_selection.end {
 286                    prev_selection.end = selection.end;
 287                    continue;
 288                }
 289            }
 290
 291            let latest_selection = newest_selection.get_or_insert_with(|| selection.clone());
 292            if selection.id > latest_selection.id {
 293                *latest_selection = selection.clone();
 294            }
 295            selections.push(selection);
 296        }
 297        let snapshot = &snapshot.buffer_snapshot;
 298        let newest_selection = newest_selection.unwrap();
 299
 300        let mut codegen_ranges = Vec::new();
 301        for (buffer, buffer_range, excerpt_id) in
 302            snapshot.ranges_to_buffer_ranges(selections.iter().map(|selection| {
 303                snapshot.anchor_before(selection.start)..snapshot.anchor_after(selection.end)
 304            }))
 305        {
 306            let start = buffer.anchor_before(buffer_range.start);
 307            let end = buffer.anchor_after(buffer_range.end);
 308
 309            codegen_ranges.push(Anchor::range_in_buffer(
 310                excerpt_id,
 311                buffer.remote_id(),
 312                start..end,
 313            ));
 314
 315            if let Some(ConfiguredModel { model, .. }) =
 316                LanguageModelRegistry::read_global(cx).default_model()
 317            {
 318                self.telemetry.report_assistant_event(AssistantEventData {
 319                    conversation_id: None,
 320                    kind: AssistantKind::Inline,
 321                    phase: AssistantPhase::Invoked,
 322                    message_id: None,
 323                    model: model.telemetry_id(),
 324                    model_provider: model.provider_id().to_string(),
 325                    response_latency: None,
 326                    error_message: None,
 327                    language_name: buffer.language().map(|language| language.name().to_proto()),
 328                });
 329            }
 330        }
 331
 332        let assist_group_id = self.next_assist_group_id.post_inc();
 333        let prompt_buffer = cx.new(|cx| Buffer::local(initial_prompt.unwrap_or_default(), cx));
 334        let prompt_buffer = cx.new(|cx| MultiBuffer::singleton(prompt_buffer, cx));
 335
 336        let mut assists = Vec::new();
 337        let mut assist_to_focus = None;
 338        for range in codegen_ranges {
 339            let assist_id = self.next_assist_id.post_inc();
 340            let codegen = cx.new(|cx| {
 341                Codegen::new(
 342                    editor.read(cx).buffer().clone(),
 343                    range.clone(),
 344                    None,
 345                    self.telemetry.clone(),
 346                    self.prompt_builder.clone(),
 347                    cx,
 348                )
 349            });
 350
 351            let gutter_dimensions = Arc::new(Mutex::new(GutterDimensions::default()));
 352            let prompt_editor = cx.new(|cx| {
 353                PromptEditor::new(
 354                    assist_id,
 355                    gutter_dimensions.clone(),
 356                    self.prompt_history.clone(),
 357                    prompt_buffer.clone(),
 358                    codegen.clone(),
 359                    editor,
 360                    assistant_panel,
 361                    workspace.clone(),
 362                    self.fs.clone(),
 363                    window,
 364                    cx,
 365                )
 366            });
 367
 368            if assist_to_focus.is_none() {
 369                let focus_assist = if newest_selection.reversed {
 370                    range.start.to_point(&snapshot) == newest_selection.start
 371                } else {
 372                    range.end.to_point(&snapshot) == newest_selection.end
 373                };
 374                if focus_assist {
 375                    assist_to_focus = Some(assist_id);
 376                }
 377            }
 378
 379            let [prompt_block_id, end_block_id] =
 380                self.insert_assist_blocks(editor, &range, &prompt_editor, cx);
 381
 382            assists.push((
 383                assist_id,
 384                range,
 385                prompt_editor,
 386                prompt_block_id,
 387                end_block_id,
 388            ));
 389        }
 390
 391        let editor_assists = self
 392            .assists_by_editor
 393            .entry(editor.downgrade())
 394            .or_insert_with(|| EditorInlineAssists::new(&editor, window, cx));
 395        let mut assist_group = InlineAssistGroup::new();
 396        for (assist_id, range, prompt_editor, prompt_block_id, end_block_id) in assists {
 397            self.assists.insert(
 398                assist_id,
 399                InlineAssist::new(
 400                    assist_id,
 401                    assist_group_id,
 402                    assistant_panel.is_some(),
 403                    editor,
 404                    &prompt_editor,
 405                    prompt_block_id,
 406                    end_block_id,
 407                    range,
 408                    prompt_editor.read(cx).codegen.clone(),
 409                    workspace.clone(),
 410                    window,
 411                    cx,
 412                ),
 413            );
 414            assist_group.assist_ids.push(assist_id);
 415            editor_assists.assist_ids.push(assist_id);
 416        }
 417        self.assist_groups.insert(assist_group_id, assist_group);
 418
 419        if let Some(assist_id) = assist_to_focus {
 420            self.focus_assist(assist_id, window, cx);
 421        }
 422    }
 423
 424    pub fn suggest_assist(
 425        &mut self,
 426        editor: &Entity<Editor>,
 427        mut range: Range<Anchor>,
 428        initial_prompt: String,
 429        initial_transaction_id: Option<TransactionId>,
 430        focus: bool,
 431        workspace: Option<WeakEntity<Workspace>>,
 432        assistant_panel: Option<&Entity<AssistantPanel>>,
 433        window: &mut Window,
 434        cx: &mut App,
 435    ) -> InlineAssistId {
 436        let assist_group_id = self.next_assist_group_id.post_inc();
 437        let prompt_buffer = cx.new(|cx| Buffer::local(&initial_prompt, cx));
 438        let prompt_buffer = cx.new(|cx| MultiBuffer::singleton(prompt_buffer, cx));
 439
 440        let assist_id = self.next_assist_id.post_inc();
 441
 442        let buffer = editor.read(cx).buffer().clone();
 443        {
 444            let snapshot = buffer.read(cx).read(cx);
 445            range.start = range.start.bias_left(&snapshot);
 446            range.end = range.end.bias_right(&snapshot);
 447        }
 448
 449        let codegen = cx.new(|cx| {
 450            Codegen::new(
 451                editor.read(cx).buffer().clone(),
 452                range.clone(),
 453                initial_transaction_id,
 454                self.telemetry.clone(),
 455                self.prompt_builder.clone(),
 456                cx,
 457            )
 458        });
 459
 460        let gutter_dimensions = Arc::new(Mutex::new(GutterDimensions::default()));
 461        let prompt_editor = cx.new(|cx| {
 462            PromptEditor::new(
 463                assist_id,
 464                gutter_dimensions.clone(),
 465                self.prompt_history.clone(),
 466                prompt_buffer.clone(),
 467                codegen.clone(),
 468                editor,
 469                assistant_panel,
 470                workspace.clone(),
 471                self.fs.clone(),
 472                window,
 473                cx,
 474            )
 475        });
 476
 477        let [prompt_block_id, end_block_id] =
 478            self.insert_assist_blocks(editor, &range, &prompt_editor, cx);
 479
 480        let editor_assists = self
 481            .assists_by_editor
 482            .entry(editor.downgrade())
 483            .or_insert_with(|| EditorInlineAssists::new(&editor, window, cx));
 484
 485        let mut assist_group = InlineAssistGroup::new();
 486        self.assists.insert(
 487            assist_id,
 488            InlineAssist::new(
 489                assist_id,
 490                assist_group_id,
 491                assistant_panel.is_some(),
 492                editor,
 493                &prompt_editor,
 494                prompt_block_id,
 495                end_block_id,
 496                range,
 497                prompt_editor.read(cx).codegen.clone(),
 498                workspace.clone(),
 499                window,
 500                cx,
 501            ),
 502        );
 503        assist_group.assist_ids.push(assist_id);
 504        editor_assists.assist_ids.push(assist_id);
 505        self.assist_groups.insert(assist_group_id, assist_group);
 506
 507        if focus {
 508            self.focus_assist(assist_id, window, cx);
 509        }
 510
 511        assist_id
 512    }
 513
 514    fn insert_assist_blocks(
 515        &self,
 516        editor: &Entity<Editor>,
 517        range: &Range<Anchor>,
 518        prompt_editor: &Entity<PromptEditor>,
 519        cx: &mut App,
 520    ) -> [CustomBlockId; 2] {
 521        let prompt_editor_height = prompt_editor.update(cx, |prompt_editor, cx| {
 522            prompt_editor
 523                .editor
 524                .update(cx, |editor, cx| editor.max_point(cx).row().0 + 1 + 2)
 525        });
 526        let assist_blocks = vec![
 527            BlockProperties {
 528                style: BlockStyle::Sticky,
 529                placement: BlockPlacement::Above(range.start),
 530                height: Some(prompt_editor_height),
 531                render: build_assist_editor_renderer(prompt_editor),
 532                priority: 0,
 533            },
 534            BlockProperties {
 535                style: BlockStyle::Sticky,
 536                placement: BlockPlacement::Below(range.end),
 537                height: None,
 538                render: Arc::new(|cx| {
 539                    v_flex()
 540                        .h_full()
 541                        .w_full()
 542                        .border_t_1()
 543                        .border_color(cx.theme().status().info_border)
 544                        .into_any_element()
 545                }),
 546                priority: 0,
 547            },
 548        ];
 549
 550        editor.update(cx, |editor, cx| {
 551            let block_ids = editor.insert_blocks(assist_blocks, None, cx);
 552            [block_ids[0], block_ids[1]]
 553        })
 554    }
 555
 556    fn handle_prompt_editor_focus_in(&mut self, assist_id: InlineAssistId, cx: &mut App) {
 557        let assist = &self.assists[&assist_id];
 558        let Some(decorations) = assist.decorations.as_ref() else {
 559            return;
 560        };
 561        let assist_group = self.assist_groups.get_mut(&assist.group_id).unwrap();
 562        let editor_assists = self.assists_by_editor.get_mut(&assist.editor).unwrap();
 563
 564        assist_group.active_assist_id = Some(assist_id);
 565        if assist_group.linked {
 566            for assist_id in &assist_group.assist_ids {
 567                if let Some(decorations) = self.assists[assist_id].decorations.as_ref() {
 568                    decorations.prompt_editor.update(cx, |prompt_editor, cx| {
 569                        prompt_editor.set_show_cursor_when_unfocused(true, cx)
 570                    });
 571                }
 572            }
 573        }
 574
 575        assist
 576            .editor
 577            .update(cx, |editor, cx| {
 578                let scroll_top = editor.scroll_position(cx).y;
 579                let scroll_bottom = scroll_top + editor.visible_line_count().unwrap_or(0.);
 580                let prompt_row = editor
 581                    .row_for_block(decorations.prompt_block_id, cx)
 582                    .unwrap()
 583                    .0 as f32;
 584
 585                if (scroll_top..scroll_bottom).contains(&prompt_row) {
 586                    editor_assists.scroll_lock = Some(InlineAssistScrollLock {
 587                        assist_id,
 588                        distance_from_top: prompt_row - scroll_top,
 589                    });
 590                } else {
 591                    editor_assists.scroll_lock = None;
 592                }
 593            })
 594            .ok();
 595    }
 596
 597    fn handle_prompt_editor_focus_out(&mut self, assist_id: InlineAssistId, cx: &mut App) {
 598        let assist = &self.assists[&assist_id];
 599        let assist_group = self.assist_groups.get_mut(&assist.group_id).unwrap();
 600        if assist_group.active_assist_id == Some(assist_id) {
 601            assist_group.active_assist_id = None;
 602            if assist_group.linked {
 603                for assist_id in &assist_group.assist_ids {
 604                    if let Some(decorations) = self.assists[assist_id].decorations.as_ref() {
 605                        decorations.prompt_editor.update(cx, |prompt_editor, cx| {
 606                            prompt_editor.set_show_cursor_when_unfocused(false, cx)
 607                        });
 608                    }
 609                }
 610            }
 611        }
 612    }
 613
 614    fn handle_prompt_editor_event(
 615        &mut self,
 616        prompt_editor: Entity<PromptEditor>,
 617        event: &PromptEditorEvent,
 618        window: &mut Window,
 619        cx: &mut App,
 620    ) {
 621        let assist_id = prompt_editor.read(cx).id;
 622        match event {
 623            PromptEditorEvent::StartRequested => {
 624                self.start_assist(assist_id, window, cx);
 625            }
 626            PromptEditorEvent::StopRequested => {
 627                self.stop_assist(assist_id, cx);
 628            }
 629            PromptEditorEvent::ConfirmRequested => {
 630                self.finish_assist(assist_id, false, window, cx);
 631            }
 632            PromptEditorEvent::CancelRequested => {
 633                self.finish_assist(assist_id, true, window, cx);
 634            }
 635            PromptEditorEvent::DismissRequested => {
 636                self.dismiss_assist(assist_id, window, cx);
 637            }
 638        }
 639    }
 640
 641    fn handle_editor_newline(&mut self, editor: Entity<Editor>, window: &mut Window, cx: &mut App) {
 642        let Some(editor_assists) = self.assists_by_editor.get(&editor.downgrade()) else {
 643            return;
 644        };
 645
 646        if editor.read(cx).selections.count() == 1 {
 647            let (selection, buffer) = editor.update(cx, |editor, cx| {
 648                (
 649                    editor.selections.newest::<usize>(cx),
 650                    editor.buffer().read(cx).snapshot(cx),
 651                )
 652            });
 653            for assist_id in &editor_assists.assist_ids {
 654                let assist = &self.assists[assist_id];
 655                let assist_range = assist.range.to_offset(&buffer);
 656                if assist_range.contains(&selection.start) && assist_range.contains(&selection.end)
 657                {
 658                    if matches!(assist.codegen.read(cx).status(cx), CodegenStatus::Pending) {
 659                        self.dismiss_assist(*assist_id, window, cx);
 660                    } else {
 661                        self.finish_assist(*assist_id, false, window, cx);
 662                    }
 663
 664                    return;
 665                }
 666            }
 667        }
 668
 669        cx.propagate();
 670    }
 671
 672    fn handle_editor_cancel(&mut self, editor: Entity<Editor>, window: &mut Window, cx: &mut App) {
 673        let Some(editor_assists) = self.assists_by_editor.get(&editor.downgrade()) else {
 674            return;
 675        };
 676
 677        if editor.read(cx).selections.count() == 1 {
 678            let (selection, buffer) = editor.update(cx, |editor, cx| {
 679                (
 680                    editor.selections.newest::<usize>(cx),
 681                    editor.buffer().read(cx).snapshot(cx),
 682                )
 683            });
 684            let mut closest_assist_fallback = None;
 685            for assist_id in &editor_assists.assist_ids {
 686                let assist = &self.assists[assist_id];
 687                let assist_range = assist.range.to_offset(&buffer);
 688                if assist.decorations.is_some() {
 689                    if assist_range.contains(&selection.start)
 690                        && assist_range.contains(&selection.end)
 691                    {
 692                        self.focus_assist(*assist_id, window, cx);
 693                        return;
 694                    } else {
 695                        let distance_from_selection = assist_range
 696                            .start
 697                            .abs_diff(selection.start)
 698                            .min(assist_range.start.abs_diff(selection.end))
 699                            + assist_range
 700                                .end
 701                                .abs_diff(selection.start)
 702                                .min(assist_range.end.abs_diff(selection.end));
 703                        match closest_assist_fallback {
 704                            Some((_, old_distance)) => {
 705                                if distance_from_selection < old_distance {
 706                                    closest_assist_fallback =
 707                                        Some((assist_id, distance_from_selection));
 708                                }
 709                            }
 710                            None => {
 711                                closest_assist_fallback = Some((assist_id, distance_from_selection))
 712                            }
 713                        }
 714                    }
 715                }
 716            }
 717
 718            if let Some((&assist_id, _)) = closest_assist_fallback {
 719                self.focus_assist(assist_id, window, cx);
 720            }
 721        }
 722
 723        cx.propagate();
 724    }
 725
 726    fn handle_editor_release(
 727        &mut self,
 728        editor: WeakEntity<Editor>,
 729        window: &mut Window,
 730        cx: &mut App,
 731    ) {
 732        if let Some(editor_assists) = self.assists_by_editor.get_mut(&editor) {
 733            for assist_id in editor_assists.assist_ids.clone() {
 734                self.finish_assist(assist_id, true, window, cx);
 735            }
 736        }
 737    }
 738
 739    fn handle_editor_change(&mut self, editor: Entity<Editor>, window: &mut Window, cx: &mut App) {
 740        let Some(editor_assists) = self.assists_by_editor.get(&editor.downgrade()) else {
 741            return;
 742        };
 743        let Some(scroll_lock) = editor_assists.scroll_lock.as_ref() else {
 744            return;
 745        };
 746        let assist = &self.assists[&scroll_lock.assist_id];
 747        let Some(decorations) = assist.decorations.as_ref() else {
 748            return;
 749        };
 750
 751        editor.update(cx, |editor, cx| {
 752            let scroll_position = editor.scroll_position(cx);
 753            let target_scroll_top = editor
 754                .row_for_block(decorations.prompt_block_id, cx)
 755                .unwrap()
 756                .0 as f32
 757                - scroll_lock.distance_from_top;
 758            if target_scroll_top != scroll_position.y {
 759                editor.set_scroll_position(point(scroll_position.x, target_scroll_top), window, cx);
 760            }
 761        });
 762    }
 763
 764    fn handle_editor_event(
 765        &mut self,
 766        editor: Entity<Editor>,
 767        event: &EditorEvent,
 768        window: &mut Window,
 769        cx: &mut App,
 770    ) {
 771        let Some(editor_assists) = self.assists_by_editor.get_mut(&editor.downgrade()) else {
 772            return;
 773        };
 774
 775        match event {
 776            EditorEvent::Edited { transaction_id } => {
 777                let buffer = editor.read(cx).buffer().read(cx);
 778                let edited_ranges =
 779                    buffer.edited_ranges_for_transaction::<usize>(*transaction_id, cx);
 780                let snapshot = buffer.snapshot(cx);
 781
 782                for assist_id in editor_assists.assist_ids.clone() {
 783                    let assist = &self.assists[&assist_id];
 784                    if matches!(
 785                        assist.codegen.read(cx).status(cx),
 786                        CodegenStatus::Error(_) | CodegenStatus::Done
 787                    ) {
 788                        let assist_range = assist.range.to_offset(&snapshot);
 789                        if edited_ranges
 790                            .iter()
 791                            .any(|range| range.overlaps(&assist_range))
 792                        {
 793                            self.finish_assist(assist_id, false, window, cx);
 794                        }
 795                    }
 796                }
 797            }
 798            EditorEvent::ScrollPositionChanged { .. } => {
 799                if let Some(scroll_lock) = editor_assists.scroll_lock.as_ref() {
 800                    let assist = &self.assists[&scroll_lock.assist_id];
 801                    if let Some(decorations) = assist.decorations.as_ref() {
 802                        let distance_from_top = editor.update(cx, |editor, cx| {
 803                            let scroll_top = editor.scroll_position(cx).y;
 804                            let prompt_row = editor
 805                                .row_for_block(decorations.prompt_block_id, cx)
 806                                .unwrap()
 807                                .0 as f32;
 808                            prompt_row - scroll_top
 809                        });
 810
 811                        if distance_from_top != scroll_lock.distance_from_top {
 812                            editor_assists.scroll_lock = None;
 813                        }
 814                    }
 815                }
 816            }
 817            EditorEvent::SelectionsChanged { .. } => {
 818                for assist_id in editor_assists.assist_ids.clone() {
 819                    let assist = &self.assists[&assist_id];
 820                    if let Some(decorations) = assist.decorations.as_ref() {
 821                        if decorations
 822                            .prompt_editor
 823                            .focus_handle(cx)
 824                            .is_focused(window)
 825                        {
 826                            return;
 827                        }
 828                    }
 829                }
 830
 831                editor_assists.scroll_lock = None;
 832            }
 833            _ => {}
 834        }
 835    }
 836
 837    pub fn finish_assist(
 838        &mut self,
 839        assist_id: InlineAssistId,
 840        undo: bool,
 841        window: &mut Window,
 842        cx: &mut App,
 843    ) {
 844        if let Some(assist) = self.assists.get(&assist_id) {
 845            let assist_group_id = assist.group_id;
 846            if self.assist_groups[&assist_group_id].linked {
 847                for assist_id in self.unlink_assist_group(assist_group_id, window, cx) {
 848                    self.finish_assist(assist_id, undo, window, cx);
 849                }
 850                return;
 851            }
 852        }
 853
 854        self.dismiss_assist(assist_id, window, cx);
 855
 856        if let Some(assist) = self.assists.remove(&assist_id) {
 857            if let hash_map::Entry::Occupied(mut entry) = self.assist_groups.entry(assist.group_id)
 858            {
 859                entry.get_mut().assist_ids.retain(|id| *id != assist_id);
 860                if entry.get().assist_ids.is_empty() {
 861                    entry.remove();
 862                }
 863            }
 864
 865            if let hash_map::Entry::Occupied(mut entry) =
 866                self.assists_by_editor.entry(assist.editor.clone())
 867            {
 868                entry.get_mut().assist_ids.retain(|id| *id != assist_id);
 869                if entry.get().assist_ids.is_empty() {
 870                    entry.remove();
 871                    if let Some(editor) = assist.editor.upgrade() {
 872                        self.update_editor_highlights(&editor, cx);
 873                    }
 874                } else {
 875                    entry.get().highlight_updates.send(()).ok();
 876                }
 877            }
 878
 879            let active_alternative = assist.codegen.read(cx).active_alternative().clone();
 880            let message_id = active_alternative.read(cx).message_id.clone();
 881
 882            if let Some(ConfiguredModel { model, .. }) =
 883                LanguageModelRegistry::read_global(cx).default_model()
 884            {
 885                let language_name = assist.editor.upgrade().and_then(|editor| {
 886                    let multibuffer = editor.read(cx).buffer().read(cx);
 887                    let multibuffer_snapshot = multibuffer.snapshot(cx);
 888                    let ranges = multibuffer_snapshot.range_to_buffer_ranges(assist.range.clone());
 889                    ranges
 890                        .first()
 891                        .and_then(|(buffer, _, _)| buffer.language())
 892                        .map(|language| language.name())
 893                });
 894                report_assistant_event(
 895                    AssistantEventData {
 896                        conversation_id: None,
 897                        kind: AssistantKind::Inline,
 898                        message_id,
 899                        phase: if undo {
 900                            AssistantPhase::Rejected
 901                        } else {
 902                            AssistantPhase::Accepted
 903                        },
 904                        model: model.telemetry_id(),
 905                        model_provider: model.provider_id().to_string(),
 906                        response_latency: None,
 907                        error_message: None,
 908                        language_name: language_name.map(|name| name.to_proto()),
 909                    },
 910                    Some(self.telemetry.clone()),
 911                    cx.http_client(),
 912                    model.api_key(cx),
 913                    cx.background_executor(),
 914                );
 915            }
 916
 917            if undo {
 918                assist.codegen.update(cx, |codegen, cx| codegen.undo(cx));
 919            } else {
 920                self.confirmed_assists.insert(assist_id, active_alternative);
 921            }
 922        }
 923    }
 924
 925    fn dismiss_assist(
 926        &mut self,
 927        assist_id: InlineAssistId,
 928        window: &mut Window,
 929        cx: &mut App,
 930    ) -> bool {
 931        let Some(assist) = self.assists.get_mut(&assist_id) else {
 932            return false;
 933        };
 934        let Some(editor) = assist.editor.upgrade() else {
 935            return false;
 936        };
 937        let Some(decorations) = assist.decorations.take() else {
 938            return false;
 939        };
 940
 941        editor.update(cx, |editor, cx| {
 942            let mut to_remove = decorations.removed_line_block_ids;
 943            to_remove.insert(decorations.prompt_block_id);
 944            to_remove.insert(decorations.end_block_id);
 945            editor.remove_blocks(to_remove, None, cx);
 946        });
 947
 948        if decorations
 949            .prompt_editor
 950            .focus_handle(cx)
 951            .contains_focused(window, cx)
 952        {
 953            self.focus_next_assist(assist_id, window, cx);
 954        }
 955
 956        if let Some(editor_assists) = self.assists_by_editor.get_mut(&editor.downgrade()) {
 957            if editor_assists
 958                .scroll_lock
 959                .as_ref()
 960                .map_or(false, |lock| lock.assist_id == assist_id)
 961            {
 962                editor_assists.scroll_lock = None;
 963            }
 964            editor_assists.highlight_updates.send(()).ok();
 965        }
 966
 967        true
 968    }
 969
 970    fn focus_next_assist(&mut self, assist_id: InlineAssistId, window: &mut Window, cx: &mut App) {
 971        let Some(assist) = self.assists.get(&assist_id) else {
 972            return;
 973        };
 974
 975        let assist_group = &self.assist_groups[&assist.group_id];
 976        let assist_ix = assist_group
 977            .assist_ids
 978            .iter()
 979            .position(|id| *id == assist_id)
 980            .unwrap();
 981        let assist_ids = assist_group
 982            .assist_ids
 983            .iter()
 984            .skip(assist_ix + 1)
 985            .chain(assist_group.assist_ids.iter().take(assist_ix));
 986
 987        for assist_id in assist_ids {
 988            let assist = &self.assists[assist_id];
 989            if assist.decorations.is_some() {
 990                self.focus_assist(*assist_id, window, cx);
 991                return;
 992            }
 993        }
 994
 995        assist
 996            .editor
 997            .update(cx, |editor, cx| window.focus(&editor.focus_handle(cx)))
 998            .ok();
 999    }
1000
1001    fn focus_assist(&mut self, assist_id: InlineAssistId, window: &mut Window, cx: &mut App) {
1002        let Some(assist) = self.assists.get(&assist_id) else {
1003            return;
1004        };
1005
1006        if let Some(decorations) = assist.decorations.as_ref() {
1007            decorations.prompt_editor.update(cx, |prompt_editor, cx| {
1008                prompt_editor.editor.update(cx, |editor, cx| {
1009                    window.focus(&editor.focus_handle(cx));
1010                    editor.select_all(&SelectAll, window, cx);
1011                })
1012            });
1013        }
1014
1015        self.scroll_to_assist(assist_id, window, cx);
1016    }
1017
1018    pub fn scroll_to_assist(
1019        &mut self,
1020        assist_id: InlineAssistId,
1021        window: &mut Window,
1022        cx: &mut App,
1023    ) {
1024        let Some(assist) = self.assists.get(&assist_id) else {
1025            return;
1026        };
1027        let Some(editor) = assist.editor.upgrade() else {
1028            return;
1029        };
1030
1031        let position = assist.range.start;
1032        editor.update(cx, |editor, cx| {
1033            editor.change_selections(None, window, cx, |selections| {
1034                selections.select_anchor_ranges([position..position])
1035            });
1036
1037            let mut scroll_target_top;
1038            let mut scroll_target_bottom;
1039            if let Some(decorations) = assist.decorations.as_ref() {
1040                scroll_target_top = editor
1041                    .row_for_block(decorations.prompt_block_id, cx)
1042                    .unwrap()
1043                    .0 as f32;
1044                scroll_target_bottom = editor
1045                    .row_for_block(decorations.end_block_id, cx)
1046                    .unwrap()
1047                    .0 as f32;
1048            } else {
1049                let snapshot = editor.snapshot(window, cx);
1050                let start_row = assist
1051                    .range
1052                    .start
1053                    .to_display_point(&snapshot.display_snapshot)
1054                    .row();
1055                scroll_target_top = start_row.0 as f32;
1056                scroll_target_bottom = scroll_target_top + 1.;
1057            }
1058            scroll_target_top -= editor.vertical_scroll_margin() as f32;
1059            scroll_target_bottom += editor.vertical_scroll_margin() as f32;
1060
1061            let height_in_lines = editor.visible_line_count().unwrap_or(0.);
1062            let scroll_top = editor.scroll_position(cx).y;
1063            let scroll_bottom = scroll_top + height_in_lines;
1064
1065            if scroll_target_top < scroll_top {
1066                editor.set_scroll_position(point(0., scroll_target_top), window, cx);
1067            } else if scroll_target_bottom > scroll_bottom {
1068                if (scroll_target_bottom - scroll_target_top) <= height_in_lines {
1069                    editor.set_scroll_position(
1070                        point(0., scroll_target_bottom - height_in_lines),
1071                        window,
1072                        cx,
1073                    );
1074                } else {
1075                    editor.set_scroll_position(point(0., scroll_target_top), window, cx);
1076                }
1077            }
1078        });
1079    }
1080
1081    fn unlink_assist_group(
1082        &mut self,
1083        assist_group_id: InlineAssistGroupId,
1084        window: &mut Window,
1085        cx: &mut App,
1086    ) -> Vec<InlineAssistId> {
1087        let assist_group = self.assist_groups.get_mut(&assist_group_id).unwrap();
1088        assist_group.linked = false;
1089        for assist_id in &assist_group.assist_ids {
1090            let assist = self.assists.get_mut(assist_id).unwrap();
1091            if let Some(editor_decorations) = assist.decorations.as_ref() {
1092                editor_decorations
1093                    .prompt_editor
1094                    .update(cx, |prompt_editor, cx| prompt_editor.unlink(window, cx));
1095            }
1096        }
1097        assist_group.assist_ids.clone()
1098    }
1099
1100    pub fn start_assist(&mut self, assist_id: InlineAssistId, window: &mut Window, cx: &mut App) {
1101        let assist = if let Some(assist) = self.assists.get_mut(&assist_id) {
1102            assist
1103        } else {
1104            return;
1105        };
1106
1107        let assist_group_id = assist.group_id;
1108        if self.assist_groups[&assist_group_id].linked {
1109            for assist_id in self.unlink_assist_group(assist_group_id, window, cx) {
1110                self.start_assist(assist_id, window, cx);
1111            }
1112            return;
1113        }
1114
1115        let Some(user_prompt) = assist.user_prompt(cx) else {
1116            return;
1117        };
1118
1119        self.prompt_history.retain(|prompt| *prompt != user_prompt);
1120        self.prompt_history.push_back(user_prompt.clone());
1121        if self.prompt_history.len() > PROMPT_HISTORY_MAX_LEN {
1122            self.prompt_history.pop_front();
1123        }
1124
1125        let assistant_panel_context = assist.assistant_panel_context(cx);
1126
1127        assist
1128            .codegen
1129            .update(cx, |codegen, cx| {
1130                codegen.start(user_prompt, assistant_panel_context, cx)
1131            })
1132            .log_err();
1133    }
1134
1135    pub fn stop_assist(&mut self, assist_id: InlineAssistId, cx: &mut App) {
1136        let assist = if let Some(assist) = self.assists.get_mut(&assist_id) {
1137            assist
1138        } else {
1139            return;
1140        };
1141
1142        assist.codegen.update(cx, |codegen, cx| codegen.stop(cx));
1143    }
1144
1145    fn update_editor_highlights(&self, editor: &Entity<Editor>, cx: &mut App) {
1146        let mut gutter_pending_ranges = Vec::new();
1147        let mut gutter_transformed_ranges = Vec::new();
1148        let mut foreground_ranges = Vec::new();
1149        let mut inserted_row_ranges = Vec::new();
1150        let empty_assist_ids = Vec::new();
1151        let assist_ids = self
1152            .assists_by_editor
1153            .get(&editor.downgrade())
1154            .map_or(&empty_assist_ids, |editor_assists| {
1155                &editor_assists.assist_ids
1156            });
1157
1158        for assist_id in assist_ids {
1159            if let Some(assist) = self.assists.get(assist_id) {
1160                let codegen = assist.codegen.read(cx);
1161                let buffer = codegen.buffer(cx).read(cx).read(cx);
1162                foreground_ranges.extend(codegen.last_equal_ranges(cx).iter().cloned());
1163
1164                let pending_range =
1165                    codegen.edit_position(cx).unwrap_or(assist.range.start)..assist.range.end;
1166                if pending_range.end.to_offset(&buffer) > pending_range.start.to_offset(&buffer) {
1167                    gutter_pending_ranges.push(pending_range);
1168                }
1169
1170                if let Some(edit_position) = codegen.edit_position(cx) {
1171                    let edited_range = assist.range.start..edit_position;
1172                    if edited_range.end.to_offset(&buffer) > edited_range.start.to_offset(&buffer) {
1173                        gutter_transformed_ranges.push(edited_range);
1174                    }
1175                }
1176
1177                if assist.decorations.is_some() {
1178                    inserted_row_ranges
1179                        .extend(codegen.diff(cx).inserted_row_ranges.iter().cloned());
1180                }
1181            }
1182        }
1183
1184        let snapshot = editor.read(cx).buffer().read(cx).snapshot(cx);
1185        merge_ranges(&mut foreground_ranges, &snapshot);
1186        merge_ranges(&mut gutter_pending_ranges, &snapshot);
1187        merge_ranges(&mut gutter_transformed_ranges, &snapshot);
1188        editor.update(cx, |editor, cx| {
1189            enum GutterPendingRange {}
1190            if gutter_pending_ranges.is_empty() {
1191                editor.clear_gutter_highlights::<GutterPendingRange>(cx);
1192            } else {
1193                editor.highlight_gutter::<GutterPendingRange>(
1194                    &gutter_pending_ranges,
1195                    |cx| cx.theme().status().info_background,
1196                    cx,
1197                )
1198            }
1199
1200            enum GutterTransformedRange {}
1201            if gutter_transformed_ranges.is_empty() {
1202                editor.clear_gutter_highlights::<GutterTransformedRange>(cx);
1203            } else {
1204                editor.highlight_gutter::<GutterTransformedRange>(
1205                    &gutter_transformed_ranges,
1206                    |cx| cx.theme().status().info,
1207                    cx,
1208                )
1209            }
1210
1211            if foreground_ranges.is_empty() {
1212                editor.clear_highlights::<InlineAssist>(cx);
1213            } else {
1214                editor.highlight_text::<InlineAssist>(
1215                    foreground_ranges,
1216                    HighlightStyle {
1217                        fade_out: Some(0.6),
1218                        ..Default::default()
1219                    },
1220                    cx,
1221                );
1222            }
1223
1224            editor.clear_row_highlights::<InlineAssist>();
1225            for row_range in inserted_row_ranges {
1226                editor.highlight_rows::<InlineAssist>(
1227                    row_range,
1228                    cx.theme().status().info_background,
1229                    Default::default(),
1230                    cx,
1231                );
1232            }
1233        });
1234    }
1235
1236    fn update_editor_blocks(
1237        &mut self,
1238        editor: &Entity<Editor>,
1239        assist_id: InlineAssistId,
1240        window: &mut Window,
1241        cx: &mut App,
1242    ) {
1243        let Some(assist) = self.assists.get_mut(&assist_id) else {
1244            return;
1245        };
1246        let Some(decorations) = assist.decorations.as_mut() else {
1247            return;
1248        };
1249
1250        let codegen = assist.codegen.read(cx);
1251        let old_snapshot = codegen.snapshot(cx);
1252        let old_buffer = codegen.old_buffer(cx);
1253        let deleted_row_ranges = codegen.diff(cx).deleted_row_ranges.clone();
1254
1255        editor.update(cx, |editor, cx| {
1256            let old_blocks = mem::take(&mut decorations.removed_line_block_ids);
1257            editor.remove_blocks(old_blocks, None, cx);
1258
1259            let mut new_blocks = Vec::new();
1260            for (new_row, old_row_range) in deleted_row_ranges {
1261                let (_, buffer_start) = old_snapshot
1262                    .point_to_buffer_offset(Point::new(*old_row_range.start(), 0))
1263                    .unwrap();
1264                let (_, buffer_end) = old_snapshot
1265                    .point_to_buffer_offset(Point::new(
1266                        *old_row_range.end(),
1267                        old_snapshot.line_len(MultiBufferRow(*old_row_range.end())),
1268                    ))
1269                    .unwrap();
1270
1271                let deleted_lines_editor = cx.new(|cx| {
1272                    let multi_buffer =
1273                        cx.new(|_| MultiBuffer::without_headers(language::Capability::ReadOnly));
1274                    multi_buffer.update(cx, |multi_buffer, cx| {
1275                        multi_buffer.push_excerpts(
1276                            old_buffer.clone(),
1277                            Some(ExcerptRange::new(buffer_start..buffer_end)),
1278                            cx,
1279                        );
1280                    });
1281
1282                    enum DeletedLines {}
1283                    let mut editor = Editor::for_multibuffer(multi_buffer, None, window, cx);
1284                    editor.set_soft_wrap_mode(language::language_settings::SoftWrap::None, cx);
1285                    editor.set_show_wrap_guides(false, cx);
1286                    editor.set_show_gutter(false, cx);
1287                    editor.scroll_manager.set_forbid_vertical_scroll(true);
1288                    editor.set_show_scrollbars(false, cx);
1289                    editor.set_read_only(true);
1290                    editor.set_show_edit_predictions(Some(false), window, cx);
1291                    editor.highlight_rows::<DeletedLines>(
1292                        Anchor::min()..Anchor::max(),
1293                        cx.theme().status().deleted_background,
1294                        Default::default(),
1295                        cx,
1296                    );
1297                    editor
1298                });
1299
1300                let height =
1301                    deleted_lines_editor.update(cx, |editor, cx| editor.max_point(cx).row().0 + 1);
1302                new_blocks.push(BlockProperties {
1303                    placement: BlockPlacement::Above(new_row),
1304                    height: Some(height),
1305                    style: BlockStyle::Flex,
1306                    render: Arc::new(move |cx| {
1307                        div()
1308                            .block_mouse_down()
1309                            .bg(cx.theme().status().deleted_background)
1310                            .size_full()
1311                            .h(height as f32 * cx.window.line_height())
1312                            .pl(cx.gutter_dimensions.full_width())
1313                            .child(deleted_lines_editor.clone())
1314                            .into_any_element()
1315                    }),
1316                    priority: 0,
1317                });
1318            }
1319
1320            decorations.removed_line_block_ids = editor
1321                .insert_blocks(new_blocks, None, cx)
1322                .into_iter()
1323                .collect();
1324        })
1325    }
1326}
1327
1328struct EditorInlineAssists {
1329    assist_ids: Vec<InlineAssistId>,
1330    scroll_lock: Option<InlineAssistScrollLock>,
1331    highlight_updates: async_watch::Sender<()>,
1332    _update_highlights: Task<Result<()>>,
1333    _subscriptions: Vec<gpui::Subscription>,
1334}
1335
1336struct InlineAssistScrollLock {
1337    assist_id: InlineAssistId,
1338    distance_from_top: f32,
1339}
1340
1341impl EditorInlineAssists {
1342    fn new(editor: &Entity<Editor>, window: &mut Window, cx: &mut App) -> Self {
1343        let (highlight_updates_tx, mut highlight_updates_rx) = async_watch::channel(());
1344        Self {
1345            assist_ids: Vec::new(),
1346            scroll_lock: None,
1347            highlight_updates: highlight_updates_tx,
1348            _update_highlights: cx.spawn({
1349                let editor = editor.downgrade();
1350                async move |cx| {
1351                    while let Ok(()) = highlight_updates_rx.changed().await {
1352                        let editor = editor.upgrade().context("editor was dropped")?;
1353                        cx.update_global(|assistant: &mut InlineAssistant, cx| {
1354                            assistant.update_editor_highlights(&editor, cx);
1355                        })?;
1356                    }
1357                    Ok(())
1358                }
1359            }),
1360            _subscriptions: vec![
1361                cx.observe_release_in(editor, window, {
1362                    let editor = editor.downgrade();
1363                    |_, window, cx| {
1364                        InlineAssistant::update_global(cx, |this, cx| {
1365                            this.handle_editor_release(editor, window, cx);
1366                        })
1367                    }
1368                }),
1369                window.observe(editor, cx, move |editor, window, cx| {
1370                    InlineAssistant::update_global(cx, |this, cx| {
1371                        this.handle_editor_change(editor, window, cx)
1372                    })
1373                }),
1374                window.subscribe(editor, cx, move |editor, event, window, cx| {
1375                    InlineAssistant::update_global(cx, |this, cx| {
1376                        this.handle_editor_event(editor, event, window, cx)
1377                    })
1378                }),
1379                editor.update(cx, |editor, cx| {
1380                    let editor_handle = cx.entity().downgrade();
1381                    editor.register_action(move |_: &editor::actions::Newline, window, cx| {
1382                        InlineAssistant::update_global(cx, |this, cx| {
1383                            if let Some(editor) = editor_handle.upgrade() {
1384                                this.handle_editor_newline(editor, window, cx)
1385                            }
1386                        })
1387                    })
1388                }),
1389                editor.update(cx, |editor, cx| {
1390                    let editor_handle = cx.entity().downgrade();
1391                    editor.register_action(move |_: &editor::actions::Cancel, window, cx| {
1392                        InlineAssistant::update_global(cx, |this, cx| {
1393                            if let Some(editor) = editor_handle.upgrade() {
1394                                this.handle_editor_cancel(editor, window, cx)
1395                            }
1396                        })
1397                    })
1398                }),
1399            ],
1400        }
1401    }
1402}
1403
1404struct InlineAssistGroup {
1405    assist_ids: Vec<InlineAssistId>,
1406    linked: bool,
1407    active_assist_id: Option<InlineAssistId>,
1408}
1409
1410impl InlineAssistGroup {
1411    fn new() -> Self {
1412        Self {
1413            assist_ids: Vec::new(),
1414            linked: true,
1415            active_assist_id: None,
1416        }
1417    }
1418}
1419
1420fn build_assist_editor_renderer(editor: &Entity<PromptEditor>) -> RenderBlock {
1421    let editor = editor.clone();
1422    Arc::new(move |cx: &mut BlockContext| {
1423        *editor.read(cx).gutter_dimensions.lock() = *cx.gutter_dimensions;
1424        editor.clone().into_any_element()
1425    })
1426}
1427
1428#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, Hash)]
1429pub struct InlineAssistId(usize);
1430
1431impl InlineAssistId {
1432    fn post_inc(&mut self) -> InlineAssistId {
1433        let id = *self;
1434        self.0 += 1;
1435        id
1436    }
1437}
1438
1439#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, Hash)]
1440struct InlineAssistGroupId(usize);
1441
1442impl InlineAssistGroupId {
1443    fn post_inc(&mut self) -> InlineAssistGroupId {
1444        let id = *self;
1445        self.0 += 1;
1446        id
1447    }
1448}
1449
1450enum PromptEditorEvent {
1451    StartRequested,
1452    StopRequested,
1453    ConfirmRequested,
1454    CancelRequested,
1455    DismissRequested,
1456}
1457
1458struct PromptEditor {
1459    id: InlineAssistId,
1460    editor: Entity<Editor>,
1461    language_model_selector: Entity<LanguageModelSelector>,
1462    edited_since_done: bool,
1463    gutter_dimensions: Arc<Mutex<GutterDimensions>>,
1464    prompt_history: VecDeque<String>,
1465    prompt_history_ix: Option<usize>,
1466    pending_prompt: String,
1467    codegen: Entity<Codegen>,
1468    _codegen_subscription: Subscription,
1469    editor_subscriptions: Vec<Subscription>,
1470    pending_token_count: Task<Result<()>>,
1471    token_counts: Option<TokenCounts>,
1472    _token_count_subscriptions: Vec<Subscription>,
1473    workspace: Option<WeakEntity<Workspace>>,
1474    show_rate_limit_notice: bool,
1475}
1476
1477#[derive(Copy, Clone)]
1478pub struct TokenCounts {
1479    total: usize,
1480    assistant_panel: usize,
1481}
1482
1483impl EventEmitter<PromptEditorEvent> for PromptEditor {}
1484
1485impl Render for PromptEditor {
1486    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
1487        let gutter_dimensions = *self.gutter_dimensions.lock();
1488        let codegen = self.codegen.read(cx);
1489
1490        let mut buttons = Vec::new();
1491        if codegen.alternative_count(cx) > 1 {
1492            buttons.push(self.render_cycle_controls(cx));
1493        }
1494
1495        let status = codegen.status(cx);
1496        buttons.extend(match status {
1497            CodegenStatus::Idle => {
1498                vec![
1499                    IconButton::new("cancel", IconName::Close)
1500                        .icon_color(Color::Muted)
1501                        .shape(IconButtonShape::Square)
1502                        .tooltip(|window, cx| {
1503                            Tooltip::for_action("Cancel Assist", &menu::Cancel, window, cx)
1504                        })
1505                        .on_click(
1506                            cx.listener(|_, _, _, cx| cx.emit(PromptEditorEvent::CancelRequested)),
1507                        )
1508                        .into_any_element(),
1509                    IconButton::new("start", IconName::SparkleAlt)
1510                        .icon_color(Color::Muted)
1511                        .shape(IconButtonShape::Square)
1512                        .tooltip(|window, cx| {
1513                            Tooltip::for_action("Transform", &menu::Confirm, window, cx)
1514                        })
1515                        .on_click(
1516                            cx.listener(|_, _, _, cx| cx.emit(PromptEditorEvent::StartRequested)),
1517                        )
1518                        .into_any_element(),
1519                ]
1520            }
1521            CodegenStatus::Pending => {
1522                vec![
1523                    IconButton::new("cancel", IconName::Close)
1524                        .icon_color(Color::Muted)
1525                        .shape(IconButtonShape::Square)
1526                        .tooltip(Tooltip::text("Cancel Assist"))
1527                        .on_click(
1528                            cx.listener(|_, _, _, cx| cx.emit(PromptEditorEvent::CancelRequested)),
1529                        )
1530                        .into_any_element(),
1531                    IconButton::new("stop", IconName::Stop)
1532                        .icon_color(Color::Error)
1533                        .shape(IconButtonShape::Square)
1534                        .tooltip(|window, cx| {
1535                            Tooltip::with_meta(
1536                                "Interrupt Transformation",
1537                                Some(&menu::Cancel),
1538                                "Changes won't be discarded",
1539                                window,
1540                                cx,
1541                            )
1542                        })
1543                        .on_click(
1544                            cx.listener(|_, _, _, cx| cx.emit(PromptEditorEvent::StopRequested)),
1545                        )
1546                        .into_any_element(),
1547                ]
1548            }
1549            CodegenStatus::Error(_) | CodegenStatus::Done => {
1550                let must_rerun =
1551                    self.edited_since_done || matches!(status, CodegenStatus::Error(_));
1552                // when accept button isn't visible, then restart maps to confirm
1553                // when accept button is visible, then restart must be mapped to an alternate keyboard shortcut
1554                let restart_key: &dyn gpui::Action = if must_rerun {
1555                    &menu::Confirm
1556                } else {
1557                    &menu::Restart
1558                };
1559                vec![
1560                    IconButton::new("cancel", IconName::Close)
1561                        .icon_color(Color::Muted)
1562                        .shape(IconButtonShape::Square)
1563                        .tooltip(|window, cx| {
1564                            Tooltip::for_action("Cancel Assist", &menu::Cancel, window, cx)
1565                        })
1566                        .on_click(
1567                            cx.listener(|_, _, _, cx| cx.emit(PromptEditorEvent::CancelRequested)),
1568                        )
1569                        .into_any_element(),
1570                    IconButton::new("restart", IconName::RotateCw)
1571                        .icon_color(Color::Muted)
1572                        .shape(IconButtonShape::Square)
1573                        .tooltip(|window, cx| {
1574                            Tooltip::with_meta(
1575                                "Regenerate Transformation",
1576                                Some(restart_key),
1577                                "Current change will be discarded",
1578                                window,
1579                                cx,
1580                            )
1581                        })
1582                        .on_click(cx.listener(|_, _, _, cx| {
1583                            cx.emit(PromptEditorEvent::StartRequested);
1584                        }))
1585                        .into_any_element(),
1586                    if !must_rerun {
1587                        IconButton::new("confirm", IconName::Check)
1588                            .icon_color(Color::Info)
1589                            .shape(IconButtonShape::Square)
1590                            .tooltip(|window, cx| {
1591                                Tooltip::for_action("Confirm Assist", &menu::Confirm, window, cx)
1592                            })
1593                            .on_click(cx.listener(|_, _, _, cx| {
1594                                cx.emit(PromptEditorEvent::ConfirmRequested);
1595                            }))
1596                            .into_any_element()
1597                    } else {
1598                        div().into_any_element()
1599                    },
1600                ]
1601            }
1602        });
1603
1604        h_flex()
1605            .key_context("PromptEditor")
1606            .bg(cx.theme().colors().editor_background)
1607            .block_mouse_down()
1608            .cursor(CursorStyle::Arrow)
1609            .border_y_1()
1610            .border_color(cx.theme().status().info_border)
1611            .size_full()
1612            .py(window.line_height() / 2.5)
1613            .on_action(cx.listener(Self::confirm))
1614            .on_action(cx.listener(Self::cancel))
1615            .on_action(cx.listener(Self::restart))
1616            .on_action(cx.listener(Self::move_up))
1617            .on_action(cx.listener(Self::move_down))
1618            .capture_action(cx.listener(Self::cycle_prev))
1619            .capture_action(cx.listener(Self::cycle_next))
1620            .child(
1621                h_flex()
1622                    .w(gutter_dimensions.full_width() + (gutter_dimensions.margin / 2.0))
1623                    .justify_center()
1624                    .gap_2()
1625                    .child(LanguageModelSelectorPopoverMenu::new(
1626                        self.language_model_selector.clone(),
1627                        IconButton::new("context", IconName::SettingsAlt)
1628                            .shape(IconButtonShape::Square)
1629                            .icon_size(IconSize::Small)
1630                            .icon_color(Color::Muted),
1631                        move |window, cx| {
1632                            Tooltip::with_meta(
1633                                format!(
1634                                    "Using {}",
1635                                    LanguageModelRegistry::read_global(cx)
1636                                        .default_model()
1637                                        .map(|default| default.model.name().0)
1638                                        .unwrap_or_else(|| "No model selected".into()),
1639                                ),
1640                                None,
1641                                "Change Model",
1642                                window,
1643                                cx,
1644                            )
1645                        },
1646                        gpui::Corner::TopRight,
1647                    ))
1648                    .map(|el| {
1649                        let CodegenStatus::Error(error) = self.codegen.read(cx).status(cx) else {
1650                            return el;
1651                        };
1652
1653                        let error_message = SharedString::from(error.to_string());
1654                        if error.error_code() == proto::ErrorCode::RateLimitExceeded
1655                            && cx.has_flag::<ZedProFeatureFlag>()
1656                        {
1657                            el.child(
1658                                v_flex()
1659                                    .child(
1660                                        IconButton::new("rate-limit-error", IconName::XCircle)
1661                                            .toggle_state(self.show_rate_limit_notice)
1662                                            .shape(IconButtonShape::Square)
1663                                            .icon_size(IconSize::Small)
1664                                            .on_click(cx.listener(Self::toggle_rate_limit_notice)),
1665                                    )
1666                                    .children(self.show_rate_limit_notice.then(|| {
1667                                        deferred(
1668                                            anchored()
1669                                                .position_mode(gpui::AnchoredPositionMode::Local)
1670                                                .position(point(px(0.), px(24.)))
1671                                                .anchor(gpui::Corner::TopLeft)
1672                                                .child(self.render_rate_limit_notice(cx)),
1673                                        )
1674                                    })),
1675                            )
1676                        } else {
1677                            el.child(
1678                                div()
1679                                    .id("error")
1680                                    .tooltip(Tooltip::text(error_message))
1681                                    .child(
1682                                        Icon::new(IconName::XCircle)
1683                                            .size(IconSize::Small)
1684                                            .color(Color::Error),
1685                                    ),
1686                            )
1687                        }
1688                    }),
1689            )
1690            .child(div().flex_1().child(self.render_prompt_editor(cx)))
1691            .child(
1692                h_flex()
1693                    .gap_2()
1694                    .pr_6()
1695                    .children(self.render_token_count(cx))
1696                    .children(buttons),
1697            )
1698    }
1699}
1700
1701impl Focusable for PromptEditor {
1702    fn focus_handle(&self, cx: &App) -> FocusHandle {
1703        self.editor.focus_handle(cx)
1704    }
1705}
1706
1707impl PromptEditor {
1708    const MAX_LINES: u8 = 8;
1709
1710    fn new(
1711        id: InlineAssistId,
1712        gutter_dimensions: Arc<Mutex<GutterDimensions>>,
1713        prompt_history: VecDeque<String>,
1714        prompt_buffer: Entity<MultiBuffer>,
1715        codegen: Entity<Codegen>,
1716        parent_editor: &Entity<Editor>,
1717        assistant_panel: Option<&Entity<AssistantPanel>>,
1718        workspace: Option<WeakEntity<Workspace>>,
1719        fs: Arc<dyn Fs>,
1720        window: &mut Window,
1721        cx: &mut Context<Self>,
1722    ) -> Self {
1723        let prompt_editor = cx.new(|cx| {
1724            let mut editor = Editor::new(
1725                EditorMode::AutoHeight {
1726                    max_lines: Self::MAX_LINES as usize,
1727                },
1728                prompt_buffer,
1729                None,
1730                window,
1731                cx,
1732            );
1733            editor.set_soft_wrap_mode(language::language_settings::SoftWrap::EditorWidth, cx);
1734            // Since the prompt editors for all inline assistants are linked,
1735            // always show the cursor (even when it isn't focused) because
1736            // typing in one will make what you typed appear in all of them.
1737            editor.set_show_cursor_when_unfocused(true, cx);
1738            editor.set_placeholder_text(Self::placeholder_text(codegen.read(cx), window, cx), cx);
1739            editor
1740        });
1741
1742        let mut token_count_subscriptions = Vec::new();
1743        token_count_subscriptions.push(cx.subscribe_in(
1744            parent_editor,
1745            window,
1746            Self::handle_parent_editor_event,
1747        ));
1748        if let Some(assistant_panel) = assistant_panel {
1749            token_count_subscriptions.push(cx.subscribe_in(
1750                assistant_panel,
1751                window,
1752                Self::handle_assistant_panel_event,
1753            ));
1754        }
1755
1756        let mut this = Self {
1757            id,
1758            editor: prompt_editor,
1759            language_model_selector: cx.new(|cx| {
1760                let fs = fs.clone();
1761                LanguageModelSelector::new(
1762                    |cx| LanguageModelRegistry::read_global(cx).default_model(),
1763                    move |model, cx| {
1764                        update_settings_file::<AssistantSettings>(
1765                            fs.clone(),
1766                            cx,
1767                            move |settings, _| settings.set_model(model.clone()),
1768                        );
1769                    },
1770                    window,
1771                    cx,
1772                )
1773            }),
1774            edited_since_done: false,
1775            gutter_dimensions,
1776            prompt_history,
1777            prompt_history_ix: None,
1778            pending_prompt: String::new(),
1779            _codegen_subscription: cx.observe(&codegen, Self::handle_codegen_changed),
1780            editor_subscriptions: Vec::new(),
1781            codegen,
1782            pending_token_count: Task::ready(Ok(())),
1783            token_counts: None,
1784            _token_count_subscriptions: token_count_subscriptions,
1785            workspace,
1786            show_rate_limit_notice: false,
1787        };
1788        this.count_tokens(cx);
1789        this.subscribe_to_editor(window, cx);
1790        this
1791    }
1792
1793    fn subscribe_to_editor(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1794        self.editor_subscriptions.clear();
1795        self.editor_subscriptions.push(cx.subscribe_in(
1796            &self.editor,
1797            window,
1798            Self::handle_prompt_editor_events,
1799        ));
1800    }
1801
1802    fn set_show_cursor_when_unfocused(
1803        &mut self,
1804        show_cursor_when_unfocused: bool,
1805        cx: &mut Context<Self>,
1806    ) {
1807        self.editor.update(cx, |editor, cx| {
1808            editor.set_show_cursor_when_unfocused(show_cursor_when_unfocused, cx)
1809        });
1810    }
1811
1812    fn unlink(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1813        let prompt = self.prompt(cx);
1814        let focus = self.editor.focus_handle(cx).contains_focused(window, cx);
1815        self.editor = cx.new(|cx| {
1816            let mut editor = Editor::auto_height(Self::MAX_LINES as usize, window, cx);
1817            editor.set_soft_wrap_mode(language::language_settings::SoftWrap::EditorWidth, cx);
1818            editor.set_placeholder_text("Add a prompt…", cx);
1819            editor.set_text(prompt, window, cx);
1820            if focus {
1821                window.focus(&editor.focus_handle(cx));
1822            }
1823            editor
1824        });
1825        self.subscribe_to_editor(window, cx);
1826    }
1827
1828    fn placeholder_text(codegen: &Codegen, window: &Window, cx: &App) -> String {
1829        let context_keybinding = text_for_action(&zed_actions::assistant::ToggleFocus, window, cx)
1830            .map(|keybinding| format!("{keybinding} for context"))
1831            .unwrap_or_default();
1832
1833        let action = if codegen.is_insertion {
1834            "Generate"
1835        } else {
1836            "Transform"
1837        };
1838
1839        format!("{action}{context_keybinding} • ↓↑ for history")
1840    }
1841
1842    fn prompt(&self, cx: &App) -> String {
1843        self.editor.read(cx).text(cx)
1844    }
1845
1846    fn toggle_rate_limit_notice(
1847        &mut self,
1848        _: &ClickEvent,
1849        window: &mut Window,
1850        cx: &mut Context<Self>,
1851    ) {
1852        self.show_rate_limit_notice = !self.show_rate_limit_notice;
1853        if self.show_rate_limit_notice {
1854            window.focus(&self.editor.focus_handle(cx));
1855        }
1856        cx.notify();
1857    }
1858
1859    fn handle_parent_editor_event(
1860        &mut self,
1861        _: &Entity<Editor>,
1862        event: &EditorEvent,
1863        _: &mut Window,
1864        cx: &mut Context<Self>,
1865    ) {
1866        if let EditorEvent::BufferEdited { .. } = event {
1867            self.count_tokens(cx);
1868        }
1869    }
1870
1871    fn handle_assistant_panel_event(
1872        &mut self,
1873        _: &Entity<AssistantPanel>,
1874        event: &AssistantPanelEvent,
1875        _: &mut Window,
1876        cx: &mut Context<Self>,
1877    ) {
1878        let AssistantPanelEvent::ContextEdited { .. } = event;
1879        self.count_tokens(cx);
1880    }
1881
1882    fn count_tokens(&mut self, cx: &mut Context<Self>) {
1883        let assist_id = self.id;
1884        self.pending_token_count = cx.spawn(async move |this, cx| {
1885            cx.background_executor().timer(Duration::from_secs(1)).await;
1886            let token_count = cx
1887                .update_global(|inline_assistant: &mut InlineAssistant, cx| {
1888                    let assist = inline_assistant
1889                        .assists
1890                        .get(&assist_id)
1891                        .context("assist not found")?;
1892                    anyhow::Ok(assist.count_tokens(cx))
1893                })??
1894                .await?;
1895
1896            this.update(cx, |this, cx| {
1897                this.token_counts = Some(token_count);
1898                cx.notify();
1899            })
1900        })
1901    }
1902
1903    fn handle_prompt_editor_events(
1904        &mut self,
1905        _: &Entity<Editor>,
1906        event: &EditorEvent,
1907        window: &mut Window,
1908        cx: &mut Context<Self>,
1909    ) {
1910        match event {
1911            EditorEvent::Edited { .. } => {
1912                if let Some(workspace) = window.root::<Workspace>().flatten() {
1913                    workspace.update(cx, |workspace, cx| {
1914                        let is_via_ssh = workspace
1915                            .project()
1916                            .update(cx, |project, _| project.is_via_ssh());
1917
1918                        workspace
1919                            .client()
1920                            .telemetry()
1921                            .log_edit_event("inline assist", is_via_ssh);
1922                    });
1923                }
1924                let prompt = self.editor.read(cx).text(cx);
1925                if self
1926                    .prompt_history_ix
1927                    .map_or(true, |ix| self.prompt_history[ix] != prompt)
1928                {
1929                    self.prompt_history_ix.take();
1930                    self.pending_prompt = prompt;
1931                }
1932
1933                self.edited_since_done = true;
1934                cx.notify();
1935            }
1936            EditorEvent::BufferEdited => {
1937                self.count_tokens(cx);
1938            }
1939            EditorEvent::Blurred => {
1940                if self.show_rate_limit_notice {
1941                    self.show_rate_limit_notice = false;
1942                    cx.notify();
1943                }
1944            }
1945            _ => {}
1946        }
1947    }
1948
1949    fn handle_codegen_changed(&mut self, _: Entity<Codegen>, cx: &mut Context<Self>) {
1950        match self.codegen.read(cx).status(cx) {
1951            CodegenStatus::Idle => {
1952                self.editor
1953                    .update(cx, |editor, _| editor.set_read_only(false));
1954            }
1955            CodegenStatus::Pending => {
1956                self.editor
1957                    .update(cx, |editor, _| editor.set_read_only(true));
1958            }
1959            CodegenStatus::Done => {
1960                self.edited_since_done = false;
1961                self.editor
1962                    .update(cx, |editor, _| editor.set_read_only(false));
1963            }
1964            CodegenStatus::Error(error) => {
1965                if cx.has_flag::<ZedProFeatureFlag>()
1966                    && error.error_code() == proto::ErrorCode::RateLimitExceeded
1967                    && !dismissed_rate_limit_notice()
1968                {
1969                    self.show_rate_limit_notice = true;
1970                    cx.notify();
1971                }
1972
1973                self.edited_since_done = false;
1974                self.editor
1975                    .update(cx, |editor, _| editor.set_read_only(false));
1976            }
1977        }
1978    }
1979
1980    fn restart(&mut self, _: &menu::Restart, _window: &mut Window, cx: &mut Context<Self>) {
1981        cx.emit(PromptEditorEvent::StartRequested);
1982    }
1983
1984    fn cancel(
1985        &mut self,
1986        _: &editor::actions::Cancel,
1987        _window: &mut Window,
1988        cx: &mut Context<Self>,
1989    ) {
1990        match self.codegen.read(cx).status(cx) {
1991            CodegenStatus::Idle | CodegenStatus::Done | CodegenStatus::Error(_) => {
1992                cx.emit(PromptEditorEvent::CancelRequested);
1993            }
1994            CodegenStatus::Pending => {
1995                cx.emit(PromptEditorEvent::StopRequested);
1996            }
1997        }
1998    }
1999
2000    fn confirm(&mut self, _: &menu::Confirm, _window: &mut Window, cx: &mut Context<Self>) {
2001        match self.codegen.read(cx).status(cx) {
2002            CodegenStatus::Idle => {
2003                cx.emit(PromptEditorEvent::StartRequested);
2004            }
2005            CodegenStatus::Pending => {
2006                cx.emit(PromptEditorEvent::DismissRequested);
2007            }
2008            CodegenStatus::Done => {
2009                if self.edited_since_done {
2010                    cx.emit(PromptEditorEvent::StartRequested);
2011                } else {
2012                    cx.emit(PromptEditorEvent::ConfirmRequested);
2013                }
2014            }
2015            CodegenStatus::Error(_) => {
2016                cx.emit(PromptEditorEvent::StartRequested);
2017            }
2018        }
2019    }
2020
2021    fn move_up(&mut self, _: &MoveUp, window: &mut Window, cx: &mut Context<Self>) {
2022        if let Some(ix) = self.prompt_history_ix {
2023            if ix > 0 {
2024                self.prompt_history_ix = Some(ix - 1);
2025                let prompt = self.prompt_history[ix - 1].as_str();
2026                self.editor.update(cx, |editor, cx| {
2027                    editor.set_text(prompt, window, cx);
2028                    editor.move_to_beginning(&Default::default(), window, cx);
2029                });
2030            }
2031        } else if !self.prompt_history.is_empty() {
2032            self.prompt_history_ix = Some(self.prompt_history.len() - 1);
2033            let prompt = self.prompt_history[self.prompt_history.len() - 1].as_str();
2034            self.editor.update(cx, |editor, cx| {
2035                editor.set_text(prompt, window, cx);
2036                editor.move_to_beginning(&Default::default(), window, cx);
2037            });
2038        }
2039    }
2040
2041    fn move_down(&mut self, _: &MoveDown, window: &mut Window, cx: &mut Context<Self>) {
2042        if let Some(ix) = self.prompt_history_ix {
2043            if ix < self.prompt_history.len() - 1 {
2044                self.prompt_history_ix = Some(ix + 1);
2045                let prompt = self.prompt_history[ix + 1].as_str();
2046                self.editor.update(cx, |editor, cx| {
2047                    editor.set_text(prompt, window, cx);
2048                    editor.move_to_end(&Default::default(), window, cx)
2049                });
2050            } else {
2051                self.prompt_history_ix = None;
2052                let prompt = self.pending_prompt.as_str();
2053                self.editor.update(cx, |editor, cx| {
2054                    editor.set_text(prompt, window, cx);
2055                    editor.move_to_end(&Default::default(), window, cx)
2056                });
2057            }
2058        }
2059    }
2060
2061    fn cycle_prev(
2062        &mut self,
2063        _: &CyclePreviousInlineAssist,
2064        _: &mut Window,
2065        cx: &mut Context<Self>,
2066    ) {
2067        self.codegen
2068            .update(cx, |codegen, cx| codegen.cycle_prev(cx));
2069    }
2070
2071    fn cycle_next(&mut self, _: &CycleNextInlineAssist, _: &mut Window, cx: &mut Context<Self>) {
2072        self.codegen
2073            .update(cx, |codegen, cx| codegen.cycle_next(cx));
2074    }
2075
2076    fn render_cycle_controls(&self, cx: &Context<Self>) -> AnyElement {
2077        let codegen = self.codegen.read(cx);
2078        let disabled = matches!(codegen.status(cx), CodegenStatus::Idle);
2079
2080        let model_registry = LanguageModelRegistry::read_global(cx);
2081        let default_model = model_registry.default_model().map(|default| default.model);
2082        let alternative_models = model_registry.inline_alternative_models();
2083
2084        let get_model_name = |index: usize| -> String {
2085            let name = |model: &Arc<dyn LanguageModel>| model.name().0.to_string();
2086
2087            match index {
2088                0 => default_model.as_ref().map_or_else(String::new, name),
2089                index if index <= alternative_models.len() => alternative_models
2090                    .get(index - 1)
2091                    .map_or_else(String::new, name),
2092                _ => String::new(),
2093            }
2094        };
2095
2096        let total_models = alternative_models.len() + 1;
2097
2098        if total_models <= 1 {
2099            return div().into_any_element();
2100        }
2101
2102        let current_index = codegen.active_alternative;
2103        let prev_index = (current_index + total_models - 1) % total_models;
2104        let next_index = (current_index + 1) % total_models;
2105
2106        let prev_model_name = get_model_name(prev_index);
2107        let next_model_name = get_model_name(next_index);
2108
2109        h_flex()
2110            .child(
2111                IconButton::new("previous", IconName::ChevronLeft)
2112                    .icon_color(Color::Muted)
2113                    .disabled(disabled || current_index == 0)
2114                    .shape(IconButtonShape::Square)
2115                    .tooltip({
2116                        let focus_handle = self.editor.focus_handle(cx);
2117                        move |window, cx| {
2118                            cx.new(|cx| {
2119                                let mut tooltip = Tooltip::new("Previous Alternative").key_binding(
2120                                    KeyBinding::for_action_in(
2121                                        &CyclePreviousInlineAssist,
2122                                        &focus_handle,
2123                                        window,
2124                                        cx,
2125                                    ),
2126                                );
2127                                if !disabled && current_index != 0 {
2128                                    tooltip = tooltip.meta(prev_model_name.clone());
2129                                }
2130                                tooltip
2131                            })
2132                            .into()
2133                        }
2134                    })
2135                    .on_click(cx.listener(|this, _, _, cx| {
2136                        this.codegen
2137                            .update(cx, |codegen, cx| codegen.cycle_prev(cx))
2138                    })),
2139            )
2140            .child(
2141                Label::new(format!(
2142                    "{}/{}",
2143                    codegen.active_alternative + 1,
2144                    codegen.alternative_count(cx)
2145                ))
2146                .size(LabelSize::Small)
2147                .color(if disabled {
2148                    Color::Disabled
2149                } else {
2150                    Color::Muted
2151                }),
2152            )
2153            .child(
2154                IconButton::new("next", IconName::ChevronRight)
2155                    .icon_color(Color::Muted)
2156                    .disabled(disabled || current_index == total_models - 1)
2157                    .shape(IconButtonShape::Square)
2158                    .tooltip({
2159                        let focus_handle = self.editor.focus_handle(cx);
2160                        move |window, cx| {
2161                            cx.new(|cx| {
2162                                let mut tooltip = Tooltip::new("Next Alternative").key_binding(
2163                                    KeyBinding::for_action_in(
2164                                        &CycleNextInlineAssist,
2165                                        &focus_handle,
2166                                        window,
2167                                        cx,
2168                                    ),
2169                                );
2170                                if !disabled && current_index != total_models - 1 {
2171                                    tooltip = tooltip.meta(next_model_name.clone());
2172                                }
2173                                tooltip
2174                            })
2175                            .into()
2176                        }
2177                    })
2178                    .on_click(cx.listener(|this, _, _, cx| {
2179                        this.codegen
2180                            .update(cx, |codegen, cx| codegen.cycle_next(cx))
2181                    })),
2182            )
2183            .into_any_element()
2184    }
2185
2186    fn render_token_count(&self, cx: &mut Context<Self>) -> Option<impl IntoElement> {
2187        let model = LanguageModelRegistry::read_global(cx)
2188            .default_model()?
2189            .model;
2190        let token_counts = self.token_counts?;
2191        let max_token_count = model.max_token_count();
2192
2193        let remaining_tokens = max_token_count as isize - token_counts.total as isize;
2194        let token_count_color = if remaining_tokens <= 0 {
2195            Color::Error
2196        } else if token_counts.total as f32 / max_token_count as f32 >= 0.8 {
2197            Color::Warning
2198        } else {
2199            Color::Muted
2200        };
2201
2202        let mut token_count = h_flex()
2203            .id("token_count")
2204            .gap_0p5()
2205            .child(
2206                Label::new(humanize_token_count(token_counts.total))
2207                    .size(LabelSize::Small)
2208                    .color(token_count_color),
2209            )
2210            .child(Label::new("/").size(LabelSize::Small).color(Color::Muted))
2211            .child(
2212                Label::new(humanize_token_count(max_token_count))
2213                    .size(LabelSize::Small)
2214                    .color(Color::Muted),
2215            );
2216        if let Some(workspace) = self.workspace.clone() {
2217            token_count = token_count
2218                .tooltip(move |window, cx| {
2219                    Tooltip::with_meta(
2220                        format!(
2221                            "Tokens Used ({} from the Assistant Panel)",
2222                            humanize_token_count(token_counts.assistant_panel)
2223                        ),
2224                        None,
2225                        "Click to open the Assistant Panel",
2226                        window,
2227                        cx,
2228                    )
2229                })
2230                .cursor_pointer()
2231                .on_mouse_down(gpui::MouseButton::Left, |_, _, cx| cx.stop_propagation())
2232                .on_click(move |_, window, cx| {
2233                    cx.stop_propagation();
2234                    workspace
2235                        .update(cx, |workspace, cx| {
2236                            workspace.focus_panel::<AssistantPanel>(window, cx)
2237                        })
2238                        .ok();
2239                });
2240        } else {
2241            token_count = token_count
2242                .cursor_default()
2243                .tooltip(Tooltip::text("Tokens used"));
2244        }
2245
2246        Some(token_count)
2247    }
2248
2249    fn render_prompt_editor(&self, cx: &mut Context<Self>) -> impl IntoElement {
2250        let settings = ThemeSettings::get_global(cx);
2251        let text_style = TextStyle {
2252            color: if self.editor.read(cx).read_only(cx) {
2253                cx.theme().colors().text_disabled
2254            } else {
2255                cx.theme().colors().text
2256            },
2257            font_family: settings.buffer_font.family.clone(),
2258            font_fallbacks: settings.buffer_font.fallbacks.clone(),
2259            font_size: settings.buffer_font_size(cx).into(),
2260            font_weight: settings.buffer_font.weight,
2261            line_height: relative(settings.buffer_line_height.value()),
2262            ..Default::default()
2263        };
2264        EditorElement::new(
2265            &self.editor,
2266            EditorStyle {
2267                background: cx.theme().colors().editor_background,
2268                local_player: cx.theme().players().local(),
2269                text: text_style,
2270                ..Default::default()
2271            },
2272        )
2273    }
2274
2275    fn render_rate_limit_notice(&self, cx: &mut Context<Self>) -> impl IntoElement {
2276        Popover::new().child(
2277            v_flex()
2278                .occlude()
2279                .p_2()
2280                .child(
2281                    Label::new("Out of Tokens")
2282                        .size(LabelSize::Small)
2283                        .weight(FontWeight::BOLD),
2284                )
2285                .child(Label::new(
2286                    "Try Zed Pro for higher limits, a wider range of models, and more.",
2287                ))
2288                .child(
2289                    h_flex()
2290                        .justify_between()
2291                        .child(CheckboxWithLabel::new(
2292                            "dont-show-again",
2293                            Label::new("Don't show again"),
2294                            if dismissed_rate_limit_notice() {
2295                                ui::ToggleState::Selected
2296                            } else {
2297                                ui::ToggleState::Unselected
2298                            },
2299                            |selection, _, cx| {
2300                                let is_dismissed = match selection {
2301                                    ui::ToggleState::Unselected => false,
2302                                    ui::ToggleState::Indeterminate => return,
2303                                    ui::ToggleState::Selected => true,
2304                                };
2305
2306                                set_rate_limit_notice_dismissed(is_dismissed, cx)
2307                            },
2308                        ))
2309                        .child(
2310                            h_flex()
2311                                .gap_2()
2312                                .child(
2313                                    Button::new("dismiss", "Dismiss")
2314                                        .style(ButtonStyle::Transparent)
2315                                        .on_click(cx.listener(Self::toggle_rate_limit_notice)),
2316                                )
2317                                .child(Button::new("more-info", "More Info").on_click(
2318                                    |_event, window, cx| {
2319                                        window.dispatch_action(
2320                                            Box::new(zed_actions::OpenAccountSettings),
2321                                            cx,
2322                                        )
2323                                    },
2324                                )),
2325                        ),
2326                ),
2327        )
2328    }
2329}
2330
2331const DISMISSED_RATE_LIMIT_NOTICE_KEY: &str = "dismissed-rate-limit-notice";
2332
2333fn dismissed_rate_limit_notice() -> bool {
2334    db::kvp::KEY_VALUE_STORE
2335        .read_kvp(DISMISSED_RATE_LIMIT_NOTICE_KEY)
2336        .log_err()
2337        .map_or(false, |s| s.is_some())
2338}
2339
2340fn set_rate_limit_notice_dismissed(is_dismissed: bool, cx: &mut App) {
2341    db::write_and_log(cx, move || async move {
2342        if is_dismissed {
2343            db::kvp::KEY_VALUE_STORE
2344                .write_kvp(DISMISSED_RATE_LIMIT_NOTICE_KEY.into(), "1".into())
2345                .await
2346        } else {
2347            db::kvp::KEY_VALUE_STORE
2348                .delete_kvp(DISMISSED_RATE_LIMIT_NOTICE_KEY.into())
2349                .await
2350        }
2351    })
2352}
2353
2354struct InlineAssist {
2355    group_id: InlineAssistGroupId,
2356    range: Range<Anchor>,
2357    editor: WeakEntity<Editor>,
2358    decorations: Option<InlineAssistDecorations>,
2359    codegen: Entity<Codegen>,
2360    _subscriptions: Vec<Subscription>,
2361    workspace: Option<WeakEntity<Workspace>>,
2362    include_context: bool,
2363}
2364
2365impl InlineAssist {
2366    fn new(
2367        assist_id: InlineAssistId,
2368        group_id: InlineAssistGroupId,
2369        include_context: bool,
2370        editor: &Entity<Editor>,
2371        prompt_editor: &Entity<PromptEditor>,
2372        prompt_block_id: CustomBlockId,
2373        end_block_id: CustomBlockId,
2374        range: Range<Anchor>,
2375        codegen: Entity<Codegen>,
2376        workspace: Option<WeakEntity<Workspace>>,
2377        window: &mut Window,
2378        cx: &mut App,
2379    ) -> Self {
2380        let prompt_editor_focus_handle = prompt_editor.focus_handle(cx);
2381        InlineAssist {
2382            group_id,
2383            include_context,
2384            editor: editor.downgrade(),
2385            decorations: Some(InlineAssistDecorations {
2386                prompt_block_id,
2387                prompt_editor: prompt_editor.clone(),
2388                removed_line_block_ids: HashSet::default(),
2389                end_block_id,
2390            }),
2391            range,
2392            codegen: codegen.clone(),
2393            workspace: workspace.clone(),
2394            _subscriptions: vec![
2395                window.on_focus_in(&prompt_editor_focus_handle, cx, move |_, cx| {
2396                    InlineAssistant::update_global(cx, |this, cx| {
2397                        this.handle_prompt_editor_focus_in(assist_id, cx)
2398                    })
2399                }),
2400                window.on_focus_out(&prompt_editor_focus_handle, cx, move |_, _, cx| {
2401                    InlineAssistant::update_global(cx, |this, cx| {
2402                        this.handle_prompt_editor_focus_out(assist_id, cx)
2403                    })
2404                }),
2405                window.subscribe(
2406                    prompt_editor,
2407                    cx,
2408                    move |prompt_editor, event, window, cx| {
2409                        InlineAssistant::update_global(cx, |this, cx| {
2410                            this.handle_prompt_editor_event(prompt_editor, event, window, cx)
2411                        })
2412                    },
2413                ),
2414                window.observe(&codegen, cx, {
2415                    let editor = editor.downgrade();
2416                    move |_, window, cx| {
2417                        if let Some(editor) = editor.upgrade() {
2418                            InlineAssistant::update_global(cx, |this, cx| {
2419                                if let Some(editor_assists) =
2420                                    this.assists_by_editor.get(&editor.downgrade())
2421                                {
2422                                    editor_assists.highlight_updates.send(()).ok();
2423                                }
2424
2425                                this.update_editor_blocks(&editor, assist_id, window, cx);
2426                            })
2427                        }
2428                    }
2429                }),
2430                window.subscribe(&codegen, cx, move |codegen, event, window, cx| {
2431                    InlineAssistant::update_global(cx, |this, cx| match event {
2432                        CodegenEvent::Undone => this.finish_assist(assist_id, false, window, cx),
2433                        CodegenEvent::Finished => {
2434                            let assist = if let Some(assist) = this.assists.get(&assist_id) {
2435                                assist
2436                            } else {
2437                                return;
2438                            };
2439
2440                            if let CodegenStatus::Error(error) = codegen.read(cx).status(cx) {
2441                                if assist.decorations.is_none() {
2442                                    if let Some(workspace) = assist
2443                                        .workspace
2444                                        .as_ref()
2445                                        .and_then(|workspace| workspace.upgrade())
2446                                    {
2447                                        let error = format!("Inline assistant error: {}", error);
2448                                        workspace.update(cx, |workspace, cx| {
2449                                            struct InlineAssistantError;
2450
2451                                            let id =
2452                                                NotificationId::composite::<InlineAssistantError>(
2453                                                    assist_id.0,
2454                                                );
2455
2456                                            workspace.show_toast(Toast::new(id, error), cx);
2457                                        })
2458                                    }
2459                                }
2460                            }
2461
2462                            if assist.decorations.is_none() {
2463                                this.finish_assist(assist_id, false, window, cx);
2464                            }
2465                        }
2466                    })
2467                }),
2468            ],
2469        }
2470    }
2471
2472    fn user_prompt(&self, cx: &App) -> Option<String> {
2473        let decorations = self.decorations.as_ref()?;
2474        Some(decorations.prompt_editor.read(cx).prompt(cx))
2475    }
2476
2477    fn assistant_panel_context(&self, cx: &mut App) -> Option<LanguageModelRequest> {
2478        if self.include_context {
2479            let workspace = self.workspace.as_ref()?;
2480            let workspace = workspace.upgrade()?.read(cx);
2481            let assistant_panel = workspace.panel::<AssistantPanel>(cx)?;
2482            Some(
2483                assistant_panel
2484                    .read(cx)
2485                    .active_context(cx)?
2486                    .read(cx)
2487                    .to_completion_request(None, RequestType::Chat, cx),
2488            )
2489        } else {
2490            None
2491        }
2492    }
2493
2494    pub fn count_tokens(&self, cx: &mut App) -> BoxFuture<'static, Result<TokenCounts>> {
2495        let Some(user_prompt) = self.user_prompt(cx) else {
2496            return future::ready(Err(anyhow!("no user prompt"))).boxed();
2497        };
2498        let assistant_panel_context = self.assistant_panel_context(cx);
2499        self.codegen
2500            .read(cx)
2501            .count_tokens(user_prompt, assistant_panel_context, cx)
2502    }
2503}
2504
2505struct InlineAssistDecorations {
2506    prompt_block_id: CustomBlockId,
2507    prompt_editor: Entity<PromptEditor>,
2508    removed_line_block_ids: HashSet<CustomBlockId>,
2509    end_block_id: CustomBlockId,
2510}
2511
2512#[derive(Copy, Clone, Debug)]
2513pub enum CodegenEvent {
2514    Finished,
2515    Undone,
2516}
2517
2518pub struct Codegen {
2519    alternatives: Vec<Entity<CodegenAlternative>>,
2520    active_alternative: usize,
2521    seen_alternatives: HashSet<usize>,
2522    subscriptions: Vec<Subscription>,
2523    buffer: Entity<MultiBuffer>,
2524    range: Range<Anchor>,
2525    initial_transaction_id: Option<TransactionId>,
2526    telemetry: Arc<Telemetry>,
2527    builder: Arc<PromptBuilder>,
2528    is_insertion: bool,
2529}
2530
2531impl Codegen {
2532    pub fn new(
2533        buffer: Entity<MultiBuffer>,
2534        range: Range<Anchor>,
2535        initial_transaction_id: Option<TransactionId>,
2536        telemetry: Arc<Telemetry>,
2537        builder: Arc<PromptBuilder>,
2538        cx: &mut Context<Self>,
2539    ) -> Self {
2540        let codegen = cx.new(|cx| {
2541            CodegenAlternative::new(
2542                buffer.clone(),
2543                range.clone(),
2544                false,
2545                Some(telemetry.clone()),
2546                builder.clone(),
2547                cx,
2548            )
2549        });
2550        let mut this = Self {
2551            is_insertion: range.to_offset(&buffer.read(cx).snapshot(cx)).is_empty(),
2552            alternatives: vec![codegen],
2553            active_alternative: 0,
2554            seen_alternatives: HashSet::default(),
2555            subscriptions: Vec::new(),
2556            buffer,
2557            range,
2558            initial_transaction_id,
2559            telemetry,
2560            builder,
2561        };
2562        this.activate(0, cx);
2563        this
2564    }
2565
2566    fn subscribe_to_alternative(&mut self, cx: &mut Context<Self>) {
2567        let codegen = self.active_alternative().clone();
2568        self.subscriptions.clear();
2569        self.subscriptions
2570            .push(cx.observe(&codegen, |_, _, cx| cx.notify()));
2571        self.subscriptions
2572            .push(cx.subscribe(&codegen, |_, _, event, cx| cx.emit(*event)));
2573    }
2574
2575    fn active_alternative(&self) -> &Entity<CodegenAlternative> {
2576        &self.alternatives[self.active_alternative]
2577    }
2578
2579    fn status<'a>(&self, cx: &'a App) -> &'a CodegenStatus {
2580        &self.active_alternative().read(cx).status
2581    }
2582
2583    fn alternative_count(&self, cx: &App) -> usize {
2584        LanguageModelRegistry::read_global(cx)
2585            .inline_alternative_models()
2586            .len()
2587            + 1
2588    }
2589
2590    pub fn cycle_prev(&mut self, cx: &mut Context<Self>) {
2591        let next_active_ix = if self.active_alternative == 0 {
2592            self.alternatives.len() - 1
2593        } else {
2594            self.active_alternative - 1
2595        };
2596        self.activate(next_active_ix, cx);
2597    }
2598
2599    pub fn cycle_next(&mut self, cx: &mut Context<Self>) {
2600        let next_active_ix = (self.active_alternative + 1) % self.alternatives.len();
2601        self.activate(next_active_ix, cx);
2602    }
2603
2604    fn activate(&mut self, index: usize, cx: &mut Context<Self>) {
2605        self.active_alternative()
2606            .update(cx, |codegen, cx| codegen.set_active(false, cx));
2607        self.seen_alternatives.insert(index);
2608        self.active_alternative = index;
2609        self.active_alternative()
2610            .update(cx, |codegen, cx| codegen.set_active(true, cx));
2611        self.subscribe_to_alternative(cx);
2612        cx.notify();
2613    }
2614
2615    pub fn start(
2616        &mut self,
2617        user_prompt: String,
2618        assistant_panel_context: Option<LanguageModelRequest>,
2619        cx: &mut Context<Self>,
2620    ) -> Result<()> {
2621        let alternative_models = LanguageModelRegistry::read_global(cx)
2622            .inline_alternative_models()
2623            .to_vec();
2624
2625        self.active_alternative()
2626            .update(cx, |alternative, cx| alternative.undo(cx));
2627        self.activate(0, cx);
2628        self.alternatives.truncate(1);
2629
2630        for _ in 0..alternative_models.len() {
2631            self.alternatives.push(cx.new(|cx| {
2632                CodegenAlternative::new(
2633                    self.buffer.clone(),
2634                    self.range.clone(),
2635                    false,
2636                    Some(self.telemetry.clone()),
2637                    self.builder.clone(),
2638                    cx,
2639                )
2640            }));
2641        }
2642
2643        let primary_model = LanguageModelRegistry::read_global(cx)
2644            .default_model()
2645            .context("no active model")?
2646            .model;
2647
2648        for (model, alternative) in iter::once(primary_model)
2649            .chain(alternative_models)
2650            .zip(&self.alternatives)
2651        {
2652            alternative.update(cx, |alternative, cx| {
2653                alternative.start(
2654                    user_prompt.clone(),
2655                    assistant_panel_context.clone(),
2656                    model.clone(),
2657                    cx,
2658                )
2659            })?;
2660        }
2661
2662        Ok(())
2663    }
2664
2665    pub fn stop(&mut self, cx: &mut Context<Self>) {
2666        for codegen in &self.alternatives {
2667            codegen.update(cx, |codegen, cx| codegen.stop(cx));
2668        }
2669    }
2670
2671    pub fn undo(&mut self, cx: &mut Context<Self>) {
2672        self.active_alternative()
2673            .update(cx, |codegen, cx| codegen.undo(cx));
2674
2675        self.buffer.update(cx, |buffer, cx| {
2676            if let Some(transaction_id) = self.initial_transaction_id.take() {
2677                buffer.undo_transaction(transaction_id, cx);
2678                buffer.refresh_preview(cx);
2679            }
2680        });
2681    }
2682
2683    pub fn count_tokens(
2684        &self,
2685        user_prompt: String,
2686        assistant_panel_context: Option<LanguageModelRequest>,
2687        cx: &App,
2688    ) -> BoxFuture<'static, Result<TokenCounts>> {
2689        self.active_alternative()
2690            .read(cx)
2691            .count_tokens(user_prompt, assistant_panel_context, cx)
2692    }
2693
2694    pub fn buffer(&self, cx: &App) -> Entity<MultiBuffer> {
2695        self.active_alternative().read(cx).buffer.clone()
2696    }
2697
2698    pub fn old_buffer(&self, cx: &App) -> Entity<Buffer> {
2699        self.active_alternative().read(cx).old_buffer.clone()
2700    }
2701
2702    pub fn snapshot(&self, cx: &App) -> MultiBufferSnapshot {
2703        self.active_alternative().read(cx).snapshot.clone()
2704    }
2705
2706    pub fn edit_position(&self, cx: &App) -> Option<Anchor> {
2707        self.active_alternative().read(cx).edit_position
2708    }
2709
2710    fn diff<'a>(&self, cx: &'a App) -> &'a Diff {
2711        &self.active_alternative().read(cx).diff
2712    }
2713
2714    pub fn last_equal_ranges<'a>(&self, cx: &'a App) -> &'a [Range<Anchor>] {
2715        self.active_alternative().read(cx).last_equal_ranges()
2716    }
2717}
2718
2719impl EventEmitter<CodegenEvent> for Codegen {}
2720
2721pub struct CodegenAlternative {
2722    buffer: Entity<MultiBuffer>,
2723    old_buffer: Entity<Buffer>,
2724    snapshot: MultiBufferSnapshot,
2725    edit_position: Option<Anchor>,
2726    range: Range<Anchor>,
2727    last_equal_ranges: Vec<Range<Anchor>>,
2728    transformation_transaction_id: Option<TransactionId>,
2729    status: CodegenStatus,
2730    generation: Task<()>,
2731    diff: Diff,
2732    telemetry: Option<Arc<Telemetry>>,
2733    _subscription: gpui::Subscription,
2734    builder: Arc<PromptBuilder>,
2735    active: bool,
2736    edits: Vec<(Range<Anchor>, String)>,
2737    line_operations: Vec<LineOperation>,
2738    request: Option<LanguageModelRequest>,
2739    elapsed_time: Option<f64>,
2740    completion: Option<String>,
2741    message_id: Option<String>,
2742}
2743
2744enum CodegenStatus {
2745    Idle,
2746    Pending,
2747    Done,
2748    Error(anyhow::Error),
2749}
2750
2751#[derive(Default)]
2752struct Diff {
2753    deleted_row_ranges: Vec<(Anchor, RangeInclusive<u32>)>,
2754    inserted_row_ranges: Vec<Range<Anchor>>,
2755}
2756
2757impl Diff {
2758    fn is_empty(&self) -> bool {
2759        self.deleted_row_ranges.is_empty() && self.inserted_row_ranges.is_empty()
2760    }
2761}
2762
2763impl EventEmitter<CodegenEvent> for CodegenAlternative {}
2764
2765impl CodegenAlternative {
2766    pub fn new(
2767        multi_buffer: Entity<MultiBuffer>,
2768        range: Range<Anchor>,
2769        active: bool,
2770        telemetry: Option<Arc<Telemetry>>,
2771        builder: Arc<PromptBuilder>,
2772        cx: &mut Context<Self>,
2773    ) -> Self {
2774        let snapshot = multi_buffer.read(cx).snapshot(cx);
2775
2776        let (buffer, _, _) = snapshot
2777            .range_to_buffer_ranges(range.clone())
2778            .pop()
2779            .unwrap();
2780        let old_buffer = cx.new(|cx| {
2781            let text = buffer.as_rope().clone();
2782            let line_ending = buffer.line_ending();
2783            let language = buffer.language().cloned();
2784            let language_registry = multi_buffer
2785                .read(cx)
2786                .buffer(buffer.remote_id())
2787                .unwrap()
2788                .read(cx)
2789                .language_registry();
2790
2791            let mut buffer = Buffer::local_normalized(text, line_ending, cx);
2792            buffer.set_language(language, cx);
2793            if let Some(language_registry) = language_registry {
2794                buffer.set_language_registry(language_registry)
2795            }
2796            buffer
2797        });
2798
2799        Self {
2800            buffer: multi_buffer.clone(),
2801            old_buffer,
2802            edit_position: None,
2803            message_id: None,
2804            snapshot,
2805            last_equal_ranges: Default::default(),
2806            transformation_transaction_id: None,
2807            status: CodegenStatus::Idle,
2808            generation: Task::ready(()),
2809            diff: Diff::default(),
2810            telemetry,
2811            _subscription: cx.subscribe(&multi_buffer, Self::handle_buffer_event),
2812            builder,
2813            active,
2814            edits: Vec::new(),
2815            line_operations: Vec::new(),
2816            range,
2817            request: None,
2818            elapsed_time: None,
2819            completion: None,
2820        }
2821    }
2822
2823    fn set_active(&mut self, active: bool, cx: &mut Context<Self>) {
2824        if active != self.active {
2825            self.active = active;
2826
2827            if self.active {
2828                let edits = self.edits.clone();
2829                self.apply_edits(edits, cx);
2830                if matches!(self.status, CodegenStatus::Pending) {
2831                    let line_operations = self.line_operations.clone();
2832                    self.reapply_line_based_diff(line_operations, cx);
2833                } else {
2834                    self.reapply_batch_diff(cx).detach();
2835                }
2836            } else if let Some(transaction_id) = self.transformation_transaction_id.take() {
2837                self.buffer.update(cx, |buffer, cx| {
2838                    buffer.undo_transaction(transaction_id, cx);
2839                    buffer.forget_transaction(transaction_id, cx);
2840                });
2841            }
2842        }
2843    }
2844
2845    fn handle_buffer_event(
2846        &mut self,
2847        _buffer: Entity<MultiBuffer>,
2848        event: &multi_buffer::Event,
2849        cx: &mut Context<Self>,
2850    ) {
2851        if let multi_buffer::Event::TransactionUndone { transaction_id } = event {
2852            if self.transformation_transaction_id == Some(*transaction_id) {
2853                self.transformation_transaction_id = None;
2854                self.generation = Task::ready(());
2855                cx.emit(CodegenEvent::Undone);
2856            }
2857        }
2858    }
2859
2860    pub fn last_equal_ranges(&self) -> &[Range<Anchor>] {
2861        &self.last_equal_ranges
2862    }
2863
2864    pub fn count_tokens(
2865        &self,
2866        user_prompt: String,
2867        assistant_panel_context: Option<LanguageModelRequest>,
2868        cx: &App,
2869    ) -> BoxFuture<'static, Result<TokenCounts>> {
2870        if let Some(ConfiguredModel { model, .. }) =
2871            LanguageModelRegistry::read_global(cx).inline_assistant_model()
2872        {
2873            let request =
2874                self.build_request(&model, user_prompt, assistant_panel_context.clone(), cx);
2875            match request {
2876                Ok(request) => {
2877                    let total_count = model.count_tokens(request.clone(), cx);
2878                    let assistant_panel_count = assistant_panel_context
2879                        .map(|context| model.count_tokens(context, cx))
2880                        .unwrap_or_else(|| future::ready(Ok(0)).boxed());
2881
2882                    async move {
2883                        Ok(TokenCounts {
2884                            total: total_count.await?,
2885                            assistant_panel: assistant_panel_count.await?,
2886                        })
2887                    }
2888                    .boxed()
2889                }
2890                Err(error) => futures::future::ready(Err(error)).boxed(),
2891            }
2892        } else {
2893            future::ready(Err(anyhow!("no active model"))).boxed()
2894        }
2895    }
2896
2897    pub fn start(
2898        &mut self,
2899        user_prompt: String,
2900        assistant_panel_context: Option<LanguageModelRequest>,
2901        model: Arc<dyn LanguageModel>,
2902        cx: &mut Context<Self>,
2903    ) -> Result<()> {
2904        if let Some(transformation_transaction_id) = self.transformation_transaction_id.take() {
2905            self.buffer.update(cx, |buffer, cx| {
2906                buffer.undo_transaction(transformation_transaction_id, cx);
2907            });
2908        }
2909
2910        self.edit_position = Some(self.range.start.bias_right(&self.snapshot));
2911
2912        let api_key = model.api_key(cx);
2913        let telemetry_id = model.telemetry_id();
2914        let provider_id = model.provider_id();
2915        let stream: LocalBoxFuture<Result<LanguageModelTextStream>> =
2916            if user_prompt.trim().to_lowercase() == "delete" {
2917                async { Ok(LanguageModelTextStream::default()) }.boxed_local()
2918            } else {
2919                let request =
2920                    self.build_request(&model, user_prompt, assistant_panel_context, cx)?;
2921                self.request = Some(request.clone());
2922
2923                cx.spawn(async move |_, cx| model.stream_completion_text(request, &cx).await)
2924                    .boxed_local()
2925            };
2926        self.handle_stream(telemetry_id, provider_id.to_string(), api_key, stream, cx);
2927        Ok(())
2928    }
2929
2930    fn build_request(
2931        &self,
2932        model: &Arc<dyn LanguageModel>,
2933        user_prompt: String,
2934        assistant_panel_context: Option<LanguageModelRequest>,
2935        cx: &App,
2936    ) -> Result<LanguageModelRequest> {
2937        let buffer = self.buffer.read(cx).snapshot(cx);
2938        let language = buffer.language_at(self.range.start);
2939        let language_name = if let Some(language) = language.as_ref() {
2940            if Arc::ptr_eq(language, &language::PLAIN_TEXT) {
2941                None
2942            } else {
2943                Some(language.name())
2944            }
2945        } else {
2946            None
2947        };
2948
2949        let language_name = language_name.as_ref();
2950        let start = buffer.point_to_buffer_offset(self.range.start);
2951        let end = buffer.point_to_buffer_offset(self.range.end);
2952        let (buffer, range) = if let Some((start, end)) = start.zip(end) {
2953            let (start_buffer, start_buffer_offset) = start;
2954            let (end_buffer, end_buffer_offset) = end;
2955            if start_buffer.remote_id() == end_buffer.remote_id() {
2956                (start_buffer.clone(), start_buffer_offset..end_buffer_offset)
2957            } else {
2958                return Err(anyhow::anyhow!("invalid transformation range"));
2959            }
2960        } else {
2961            return Err(anyhow::anyhow!("invalid transformation range"));
2962        };
2963
2964        let prompt = self
2965            .builder
2966            .generate_inline_transformation_prompt(user_prompt, language_name, buffer, range)
2967            .map_err(|e| anyhow::anyhow!("Failed to generate content prompt: {}", e))?;
2968
2969        let mut messages = Vec::new();
2970        if let Some(context_request) = assistant_panel_context {
2971            messages = context_request.messages;
2972        }
2973
2974        messages.push(LanguageModelRequestMessage {
2975            role: Role::User,
2976            content: vec![prompt.into()],
2977            cache: false,
2978        });
2979
2980        Ok(LanguageModelRequest {
2981            thread_id: None,
2982            prompt_id: None,
2983            mode: None,
2984            messages,
2985            tools: Vec::new(),
2986            stop: Vec::new(),
2987            temperature: AssistantSettings::temperature_for_model(&model, cx),
2988        })
2989    }
2990
2991    pub fn handle_stream(
2992        &mut self,
2993        model_telemetry_id: String,
2994        model_provider_id: String,
2995        model_api_key: Option<String>,
2996        stream: impl 'static + Future<Output = Result<LanguageModelTextStream>>,
2997        cx: &mut Context<Self>,
2998    ) {
2999        let start_time = Instant::now();
3000        let snapshot = self.snapshot.clone();
3001        let selected_text = snapshot
3002            .text_for_range(self.range.start..self.range.end)
3003            .collect::<Rope>();
3004
3005        let selection_start = self.range.start.to_point(&snapshot);
3006
3007        // Start with the indentation of the first line in the selection
3008        let mut suggested_line_indent = snapshot
3009            .suggested_indents(selection_start.row..=selection_start.row, cx)
3010            .into_values()
3011            .next()
3012            .unwrap_or_else(|| snapshot.indent_size_for_line(MultiBufferRow(selection_start.row)));
3013
3014        // If the first line in the selection does not have indentation, check the following lines
3015        if suggested_line_indent.len == 0 && suggested_line_indent.kind == IndentKind::Space {
3016            for row in selection_start.row..=self.range.end.to_point(&snapshot).row {
3017                let line_indent = snapshot.indent_size_for_line(MultiBufferRow(row));
3018                // Prefer tabs if a line in the selection uses tabs as indentation
3019                if line_indent.kind == IndentKind::Tab {
3020                    suggested_line_indent.kind = IndentKind::Tab;
3021                    break;
3022                }
3023            }
3024        }
3025
3026        let http_client = cx.http_client();
3027        let telemetry = self.telemetry.clone();
3028        let language_name = {
3029            let multibuffer = self.buffer.read(cx);
3030            let snapshot = multibuffer.snapshot(cx);
3031            let ranges = snapshot.range_to_buffer_ranges(self.range.clone());
3032            ranges
3033                .first()
3034                .and_then(|(buffer, _, _)| buffer.language())
3035                .map(|language| language.name())
3036        };
3037
3038        self.diff = Diff::default();
3039        self.status = CodegenStatus::Pending;
3040        let mut edit_start = self.range.start.to_offset(&snapshot);
3041        let completion = Arc::new(Mutex::new(String::new()));
3042        let completion_clone = completion.clone();
3043
3044        self.generation = cx.spawn(async move |codegen, cx| {
3045            let stream = stream.await;
3046            let message_id = stream
3047                .as_ref()
3048                .ok()
3049                .and_then(|stream| stream.message_id.clone());
3050            let generate = async {
3051                let (mut diff_tx, mut diff_rx) = mpsc::channel(1);
3052                let executor = cx.background_executor().clone();
3053                let message_id = message_id.clone();
3054                let line_based_stream_diff: Task<anyhow::Result<()>> =
3055                    cx.background_spawn(async move {
3056                        let mut response_latency = None;
3057                        let request_start = Instant::now();
3058                        let diff = async {
3059                            let chunks =
3060                                StripInvalidSpans::new(stream?.stream.map_err(|e| e.into()));
3061                            futures::pin_mut!(chunks);
3062                            let mut diff = StreamingDiff::new(selected_text.to_string());
3063                            let mut line_diff = LineDiff::default();
3064
3065                            let mut new_text = String::new();
3066                            let mut base_indent = None;
3067                            let mut line_indent = None;
3068                            let mut first_line = true;
3069
3070                            while let Some(chunk) = chunks.next().await {
3071                                if response_latency.is_none() {
3072                                    response_latency = Some(request_start.elapsed());
3073                                }
3074                                let chunk = chunk?;
3075                                completion_clone.lock().push_str(&chunk);
3076
3077                                let mut lines = chunk.split('\n').peekable();
3078                                while let Some(line) = lines.next() {
3079                                    new_text.push_str(line);
3080                                    if line_indent.is_none() {
3081                                        if let Some(non_whitespace_ch_ix) =
3082                                            new_text.find(|ch: char| !ch.is_whitespace())
3083                                        {
3084                                            line_indent = Some(non_whitespace_ch_ix);
3085                                            base_indent = base_indent.or(line_indent);
3086
3087                                            let line_indent = line_indent.unwrap();
3088                                            let base_indent = base_indent.unwrap();
3089                                            let indent_delta =
3090                                                line_indent as i32 - base_indent as i32;
3091                                            let mut corrected_indent_len = cmp::max(
3092                                                0,
3093                                                suggested_line_indent.len as i32 + indent_delta,
3094                                            )
3095                                                as usize;
3096                                            if first_line {
3097                                                corrected_indent_len = corrected_indent_len
3098                                                    .saturating_sub(
3099                                                        selection_start.column as usize,
3100                                                    );
3101                                            }
3102
3103                                            let indent_char = suggested_line_indent.char();
3104                                            let mut indent_buffer = [0; 4];
3105                                            let indent_str =
3106                                                indent_char.encode_utf8(&mut indent_buffer);
3107                                            new_text.replace_range(
3108                                                ..line_indent,
3109                                                &indent_str.repeat(corrected_indent_len),
3110                                            );
3111                                        }
3112                                    }
3113
3114                                    if line_indent.is_some() {
3115                                        let char_ops = diff.push_new(&new_text);
3116                                        line_diff.push_char_operations(&char_ops, &selected_text);
3117                                        diff_tx
3118                                            .send((char_ops, line_diff.line_operations()))
3119                                            .await?;
3120                                        new_text.clear();
3121                                    }
3122
3123                                    if lines.peek().is_some() {
3124                                        let char_ops = diff.push_new("\n");
3125                                        line_diff.push_char_operations(&char_ops, &selected_text);
3126                                        diff_tx
3127                                            .send((char_ops, line_diff.line_operations()))
3128                                            .await?;
3129                                        if line_indent.is_none() {
3130                                            // Don't write out the leading indentation in empty lines on the next line
3131                                            // This is the case where the above if statement didn't clear the buffer
3132                                            new_text.clear();
3133                                        }
3134                                        line_indent = None;
3135                                        first_line = false;
3136                                    }
3137                                }
3138                            }
3139
3140                            let mut char_ops = diff.push_new(&new_text);
3141                            char_ops.extend(diff.finish());
3142                            line_diff.push_char_operations(&char_ops, &selected_text);
3143                            line_diff.finish(&selected_text);
3144                            diff_tx
3145                                .send((char_ops, line_diff.line_operations()))
3146                                .await?;
3147
3148                            anyhow::Ok(())
3149                        };
3150
3151                        let result = diff.await;
3152
3153                        let error_message = result.as_ref().err().map(|error| error.to_string());
3154                        report_assistant_event(
3155                            AssistantEventData {
3156                                conversation_id: None,
3157                                message_id,
3158                                kind: AssistantKind::Inline,
3159                                phase: AssistantPhase::Response,
3160                                model: model_telemetry_id,
3161                                model_provider: model_provider_id.to_string(),
3162                                response_latency,
3163                                error_message,
3164                                language_name: language_name.map(|name| name.to_proto()),
3165                            },
3166                            telemetry,
3167                            http_client,
3168                            model_api_key,
3169                            &executor,
3170                        );
3171
3172                        result?;
3173                        Ok(())
3174                    });
3175
3176                while let Some((char_ops, line_ops)) = diff_rx.next().await {
3177                    codegen.update(cx, |codegen, cx| {
3178                        codegen.last_equal_ranges.clear();
3179
3180                        let edits = char_ops
3181                            .into_iter()
3182                            .filter_map(|operation| match operation {
3183                                CharOperation::Insert { text } => {
3184                                    let edit_start = snapshot.anchor_after(edit_start);
3185                                    Some((edit_start..edit_start, text))
3186                                }
3187                                CharOperation::Delete { bytes } => {
3188                                    let edit_end = edit_start + bytes;
3189                                    let edit_range = snapshot.anchor_after(edit_start)
3190                                        ..snapshot.anchor_before(edit_end);
3191                                    edit_start = edit_end;
3192                                    Some((edit_range, String::new()))
3193                                }
3194                                CharOperation::Keep { bytes } => {
3195                                    let edit_end = edit_start + bytes;
3196                                    let edit_range = snapshot.anchor_after(edit_start)
3197                                        ..snapshot.anchor_before(edit_end);
3198                                    edit_start = edit_end;
3199                                    codegen.last_equal_ranges.push(edit_range);
3200                                    None
3201                                }
3202                            })
3203                            .collect::<Vec<_>>();
3204
3205                        if codegen.active {
3206                            codegen.apply_edits(edits.iter().cloned(), cx);
3207                            codegen.reapply_line_based_diff(line_ops.iter().cloned(), cx);
3208                        }
3209                        codegen.edits.extend(edits);
3210                        codegen.line_operations = line_ops;
3211                        codegen.edit_position = Some(snapshot.anchor_after(edit_start));
3212
3213                        cx.notify();
3214                    })?;
3215                }
3216
3217                // Streaming stopped and we have the new text in the buffer, and a line-based diff applied for the whole new buffer.
3218                // That diff is not what a regular diff is and might look unexpected, ergo apply a regular diff.
3219                // It's fine to apply even if the rest of the line diffing fails, as no more hunks are coming through `diff_rx`.
3220                let batch_diff_task =
3221                    codegen.update(cx, |codegen, cx| codegen.reapply_batch_diff(cx))?;
3222                let (line_based_stream_diff, ()) = join!(line_based_stream_diff, batch_diff_task);
3223                line_based_stream_diff?;
3224
3225                anyhow::Ok(())
3226            };
3227
3228            let result = generate.await;
3229            let elapsed_time = start_time.elapsed().as_secs_f64();
3230
3231            codegen
3232                .update(cx, |this, cx| {
3233                    this.message_id = message_id;
3234                    this.last_equal_ranges.clear();
3235                    if let Err(error) = result {
3236                        this.status = CodegenStatus::Error(error);
3237                    } else {
3238                        this.status = CodegenStatus::Done;
3239                    }
3240                    this.elapsed_time = Some(elapsed_time);
3241                    this.completion = Some(completion.lock().clone());
3242                    cx.emit(CodegenEvent::Finished);
3243                    cx.notify();
3244                })
3245                .ok();
3246        });
3247        cx.notify();
3248    }
3249
3250    pub fn stop(&mut self, cx: &mut Context<Self>) {
3251        self.last_equal_ranges.clear();
3252        if self.diff.is_empty() {
3253            self.status = CodegenStatus::Idle;
3254        } else {
3255            self.status = CodegenStatus::Done;
3256        }
3257        self.generation = Task::ready(());
3258        cx.emit(CodegenEvent::Finished);
3259        cx.notify();
3260    }
3261
3262    pub fn undo(&mut self, cx: &mut Context<Self>) {
3263        self.buffer.update(cx, |buffer, cx| {
3264            if let Some(transaction_id) = self.transformation_transaction_id.take() {
3265                buffer.undo_transaction(transaction_id, cx);
3266                buffer.refresh_preview(cx);
3267            }
3268        });
3269    }
3270
3271    fn apply_edits(
3272        &mut self,
3273        edits: impl IntoIterator<Item = (Range<Anchor>, String)>,
3274        cx: &mut Context<CodegenAlternative>,
3275    ) {
3276        let transaction = self.buffer.update(cx, |buffer, cx| {
3277            // Avoid grouping assistant edits with user edits.
3278            buffer.finalize_last_transaction(cx);
3279            buffer.start_transaction(cx);
3280            buffer.edit(edits, None, cx);
3281            buffer.end_transaction(cx)
3282        });
3283
3284        if let Some(transaction) = transaction {
3285            if let Some(first_transaction) = self.transformation_transaction_id {
3286                // Group all assistant edits into the first transaction.
3287                self.buffer.update(cx, |buffer, cx| {
3288                    buffer.merge_transactions(transaction, first_transaction, cx)
3289                });
3290            } else {
3291                self.transformation_transaction_id = Some(transaction);
3292                self.buffer
3293                    .update(cx, |buffer, cx| buffer.finalize_last_transaction(cx));
3294            }
3295        }
3296    }
3297
3298    fn reapply_line_based_diff(
3299        &mut self,
3300        line_operations: impl IntoIterator<Item = LineOperation>,
3301        cx: &mut Context<Self>,
3302    ) {
3303        let old_snapshot = self.snapshot.clone();
3304        let old_range = self.range.to_point(&old_snapshot);
3305        let new_snapshot = self.buffer.read(cx).snapshot(cx);
3306        let new_range = self.range.to_point(&new_snapshot);
3307
3308        let mut old_row = old_range.start.row;
3309        let mut new_row = new_range.start.row;
3310
3311        self.diff.deleted_row_ranges.clear();
3312        self.diff.inserted_row_ranges.clear();
3313        for operation in line_operations {
3314            match operation {
3315                LineOperation::Keep { lines } => {
3316                    old_row += lines;
3317                    new_row += lines;
3318                }
3319                LineOperation::Delete { lines } => {
3320                    let old_end_row = old_row + lines - 1;
3321                    let new_row = new_snapshot.anchor_before(Point::new(new_row, 0));
3322
3323                    if let Some((_, last_deleted_row_range)) =
3324                        self.diff.deleted_row_ranges.last_mut()
3325                    {
3326                        if *last_deleted_row_range.end() + 1 == old_row {
3327                            *last_deleted_row_range = *last_deleted_row_range.start()..=old_end_row;
3328                        } else {
3329                            self.diff
3330                                .deleted_row_ranges
3331                                .push((new_row, old_row..=old_end_row));
3332                        }
3333                    } else {
3334                        self.diff
3335                            .deleted_row_ranges
3336                            .push((new_row, old_row..=old_end_row));
3337                    }
3338
3339                    old_row += lines;
3340                }
3341                LineOperation::Insert { lines } => {
3342                    let new_end_row = new_row + lines - 1;
3343                    let start = new_snapshot.anchor_before(Point::new(new_row, 0));
3344                    let end = new_snapshot.anchor_before(Point::new(
3345                        new_end_row,
3346                        new_snapshot.line_len(MultiBufferRow(new_end_row)),
3347                    ));
3348                    self.diff.inserted_row_ranges.push(start..end);
3349                    new_row += lines;
3350                }
3351            }
3352
3353            cx.notify();
3354        }
3355    }
3356
3357    fn reapply_batch_diff(&mut self, cx: &mut Context<Self>) -> Task<()> {
3358        let old_snapshot = self.snapshot.clone();
3359        let old_range = self.range.to_point(&old_snapshot);
3360        let new_snapshot = self.buffer.read(cx).snapshot(cx);
3361        let new_range = self.range.to_point(&new_snapshot);
3362
3363        cx.spawn(async move |codegen, cx| {
3364            let (deleted_row_ranges, inserted_row_ranges) = cx
3365                .background_spawn(async move {
3366                    let old_text = old_snapshot
3367                        .text_for_range(
3368                            Point::new(old_range.start.row, 0)
3369                                ..Point::new(
3370                                    old_range.end.row,
3371                                    old_snapshot.line_len(MultiBufferRow(old_range.end.row)),
3372                                ),
3373                        )
3374                        .collect::<String>();
3375                    let new_text = new_snapshot
3376                        .text_for_range(
3377                            Point::new(new_range.start.row, 0)
3378                                ..Point::new(
3379                                    new_range.end.row,
3380                                    new_snapshot.line_len(MultiBufferRow(new_range.end.row)),
3381                                ),
3382                        )
3383                        .collect::<String>();
3384
3385                    let old_start_row = old_range.start.row;
3386                    let new_start_row = new_range.start.row;
3387                    let mut deleted_row_ranges: Vec<(Anchor, RangeInclusive<u32>)> = Vec::new();
3388                    let mut inserted_row_ranges = Vec::new();
3389                    for (old_rows, new_rows) in line_diff(&old_text, &new_text) {
3390                        let old_rows = old_start_row + old_rows.start..old_start_row + old_rows.end;
3391                        let new_rows = new_start_row + new_rows.start..new_start_row + new_rows.end;
3392                        if !old_rows.is_empty() {
3393                            deleted_row_ranges.push((
3394                                new_snapshot.anchor_before(Point::new(new_rows.start, 0)),
3395                                old_rows.start..=old_rows.end - 1,
3396                            ));
3397                        }
3398                        if !new_rows.is_empty() {
3399                            let start = new_snapshot.anchor_before(Point::new(new_rows.start, 0));
3400                            let new_end_row = new_rows.end - 1;
3401                            let end = new_snapshot.anchor_before(Point::new(
3402                                new_end_row,
3403                                new_snapshot.line_len(MultiBufferRow(new_end_row)),
3404                            ));
3405                            inserted_row_ranges.push(start..end);
3406                        }
3407                    }
3408                    (deleted_row_ranges, inserted_row_ranges)
3409                })
3410                .await;
3411
3412            codegen
3413                .update(cx, |codegen, cx| {
3414                    codegen.diff.deleted_row_ranges = deleted_row_ranges;
3415                    codegen.diff.inserted_row_ranges = inserted_row_ranges;
3416                    cx.notify();
3417                })
3418                .ok();
3419        })
3420    }
3421}
3422
3423struct StripInvalidSpans<T> {
3424    stream: T,
3425    stream_done: bool,
3426    buffer: String,
3427    first_line: bool,
3428    line_end: bool,
3429    starts_with_code_block: bool,
3430}
3431
3432impl<T> StripInvalidSpans<T>
3433where
3434    T: Stream<Item = Result<String>>,
3435{
3436    fn new(stream: T) -> Self {
3437        Self {
3438            stream,
3439            stream_done: false,
3440            buffer: String::new(),
3441            first_line: true,
3442            line_end: false,
3443            starts_with_code_block: false,
3444        }
3445    }
3446}
3447
3448impl<T> Stream for StripInvalidSpans<T>
3449where
3450    T: Stream<Item = Result<String>>,
3451{
3452    type Item = Result<String>;
3453
3454    fn poll_next(self: Pin<&mut Self>, cx: &mut task::Context) -> Poll<Option<Self::Item>> {
3455        const CODE_BLOCK_DELIMITER: &str = "```";
3456        const CURSOR_SPAN: &str = "<|CURSOR|>";
3457
3458        let this = unsafe { self.get_unchecked_mut() };
3459        loop {
3460            if !this.stream_done {
3461                let mut stream = unsafe { Pin::new_unchecked(&mut this.stream) };
3462                match stream.as_mut().poll_next(cx) {
3463                    Poll::Ready(Some(Ok(chunk))) => {
3464                        this.buffer.push_str(&chunk);
3465                    }
3466                    Poll::Ready(Some(Err(error))) => return Poll::Ready(Some(Err(error))),
3467                    Poll::Ready(None) => {
3468                        this.stream_done = true;
3469                    }
3470                    Poll::Pending => return Poll::Pending,
3471                }
3472            }
3473
3474            let mut chunk = String::new();
3475            let mut consumed = 0;
3476            if !this.buffer.is_empty() {
3477                let mut lines = this.buffer.split('\n').enumerate().peekable();
3478                while let Some((line_ix, line)) = lines.next() {
3479                    if line_ix > 0 {
3480                        this.first_line = false;
3481                    }
3482
3483                    if this.first_line {
3484                        let trimmed_line = line.trim();
3485                        if lines.peek().is_some() {
3486                            if trimmed_line.starts_with(CODE_BLOCK_DELIMITER) {
3487                                consumed += line.len() + 1;
3488                                this.starts_with_code_block = true;
3489                                continue;
3490                            }
3491                        } else if trimmed_line.is_empty()
3492                            || prefixes(CODE_BLOCK_DELIMITER)
3493                                .any(|prefix| trimmed_line.starts_with(prefix))
3494                        {
3495                            break;
3496                        }
3497                    }
3498
3499                    let line_without_cursor = line.replace(CURSOR_SPAN, "");
3500                    if lines.peek().is_some() {
3501                        if this.line_end {
3502                            chunk.push('\n');
3503                        }
3504
3505                        chunk.push_str(&line_without_cursor);
3506                        this.line_end = true;
3507                        consumed += line.len() + 1;
3508                    } else if this.stream_done {
3509                        if !this.starts_with_code_block
3510                            || !line_without_cursor.trim().ends_with(CODE_BLOCK_DELIMITER)
3511                        {
3512                            if this.line_end {
3513                                chunk.push('\n');
3514                            }
3515
3516                            chunk.push_str(&line);
3517                        }
3518
3519                        consumed += line.len();
3520                    } else {
3521                        let trimmed_line = line.trim();
3522                        if trimmed_line.is_empty()
3523                            || prefixes(CURSOR_SPAN).any(|prefix| trimmed_line.ends_with(prefix))
3524                            || prefixes(CODE_BLOCK_DELIMITER)
3525                                .any(|prefix| trimmed_line.ends_with(prefix))
3526                        {
3527                            break;
3528                        } else {
3529                            if this.line_end {
3530                                chunk.push('\n');
3531                                this.line_end = false;
3532                            }
3533
3534                            chunk.push_str(&line_without_cursor);
3535                            consumed += line.len();
3536                        }
3537                    }
3538                }
3539            }
3540
3541            this.buffer = this.buffer.split_off(consumed);
3542            if !chunk.is_empty() {
3543                return Poll::Ready(Some(Ok(chunk)));
3544            } else if this.stream_done {
3545                return Poll::Ready(None);
3546            }
3547        }
3548    }
3549}
3550
3551struct AssistantCodeActionProvider {
3552    editor: WeakEntity<Editor>,
3553    workspace: WeakEntity<Workspace>,
3554}
3555
3556const ASSISTANT_CODE_ACTION_PROVIDER_ID: &str = "assistant";
3557
3558impl CodeActionProvider for AssistantCodeActionProvider {
3559    fn id(&self) -> Arc<str> {
3560        ASSISTANT_CODE_ACTION_PROVIDER_ID.into()
3561    }
3562
3563    fn code_actions(
3564        &self,
3565        buffer: &Entity<Buffer>,
3566        range: Range<text::Anchor>,
3567        _: &mut Window,
3568        cx: &mut App,
3569    ) -> Task<Result<Vec<CodeAction>>> {
3570        if !Assistant::enabled(cx) {
3571            return Task::ready(Ok(Vec::new()));
3572        }
3573
3574        let snapshot = buffer.read(cx).snapshot();
3575        let mut range = range.to_point(&snapshot);
3576
3577        // Expand the range to line boundaries.
3578        range.start.column = 0;
3579        range.end.column = snapshot.line_len(range.end.row);
3580
3581        let mut has_diagnostics = false;
3582        for diagnostic in snapshot.diagnostics_in_range::<_, Point>(range.clone(), false) {
3583            range.start = cmp::min(range.start, diagnostic.range.start);
3584            range.end = cmp::max(range.end, diagnostic.range.end);
3585            has_diagnostics = true;
3586        }
3587        if has_diagnostics {
3588            if let Some(symbols_containing_start) = snapshot.symbols_containing(range.start, None) {
3589                if let Some(symbol) = symbols_containing_start.last() {
3590                    range.start = cmp::min(range.start, symbol.range.start.to_point(&snapshot));
3591                    range.end = cmp::max(range.end, symbol.range.end.to_point(&snapshot));
3592                }
3593            }
3594
3595            if let Some(symbols_containing_end) = snapshot.symbols_containing(range.end, None) {
3596                if let Some(symbol) = symbols_containing_end.last() {
3597                    range.start = cmp::min(range.start, symbol.range.start.to_point(&snapshot));
3598                    range.end = cmp::max(range.end, symbol.range.end.to_point(&snapshot));
3599                }
3600            }
3601
3602            Task::ready(Ok(vec![CodeAction {
3603                server_id: language::LanguageServerId(0),
3604                range: snapshot.anchor_before(range.start)..snapshot.anchor_after(range.end),
3605                lsp_action: LspAction::Action(Box::new(lsp::CodeAction {
3606                    title: "Fix with Assistant".into(),
3607                    ..Default::default()
3608                })),
3609                resolved: true,
3610            }]))
3611        } else {
3612            Task::ready(Ok(Vec::new()))
3613        }
3614    }
3615
3616    fn apply_code_action(
3617        &self,
3618        buffer: Entity<Buffer>,
3619        action: CodeAction,
3620        excerpt_id: ExcerptId,
3621        _push_to_history: bool,
3622        window: &mut Window,
3623        cx: &mut App,
3624    ) -> Task<Result<ProjectTransaction>> {
3625        let editor = self.editor.clone();
3626        let workspace = self.workspace.clone();
3627        window.spawn(cx, async move |cx| {
3628            let editor = editor.upgrade().context("editor was released")?;
3629            let range = editor
3630                .update(cx, |editor, cx| {
3631                    editor.buffer().update(cx, |multibuffer, cx| {
3632                        let buffer = buffer.read(cx);
3633                        let multibuffer_snapshot = multibuffer.read(cx);
3634
3635                        let old_context_range =
3636                            multibuffer_snapshot.context_range_for_excerpt(excerpt_id)?;
3637                        let mut new_context_range = old_context_range.clone();
3638                        if action
3639                            .range
3640                            .start
3641                            .cmp(&old_context_range.start, buffer)
3642                            .is_lt()
3643                        {
3644                            new_context_range.start = action.range.start;
3645                        }
3646                        if action.range.end.cmp(&old_context_range.end, buffer).is_gt() {
3647                            new_context_range.end = action.range.end;
3648                        }
3649                        drop(multibuffer_snapshot);
3650
3651                        if new_context_range != old_context_range {
3652                            multibuffer.resize_excerpt(excerpt_id, new_context_range, cx);
3653                        }
3654
3655                        let multibuffer_snapshot = multibuffer.read(cx);
3656                        Some(
3657                            multibuffer_snapshot
3658                                .anchor_in_excerpt(excerpt_id, action.range.start)?
3659                                ..multibuffer_snapshot
3660                                    .anchor_in_excerpt(excerpt_id, action.range.end)?,
3661                        )
3662                    })
3663                })?
3664                .context("invalid range")?;
3665            let assistant_panel = workspace.update(cx, |workspace, cx| {
3666                workspace
3667                    .panel::<AssistantPanel>(cx)
3668                    .context("assistant panel was released")
3669            })??;
3670
3671            cx.update_global(|assistant: &mut InlineAssistant, window, cx| {
3672                let assist_id = assistant.suggest_assist(
3673                    &editor,
3674                    range,
3675                    "Fix Diagnostics".into(),
3676                    None,
3677                    true,
3678                    Some(workspace),
3679                    Some(&assistant_panel),
3680                    window,
3681                    cx,
3682                );
3683                assistant.start_assist(assist_id, window, cx);
3684            })?;
3685
3686            Ok(ProjectTransaction::default())
3687        })
3688    }
3689}
3690
3691fn prefixes(text: &str) -> impl Iterator<Item = &str> {
3692    (0..text.len() - 1).map(|ix| &text[..ix + 1])
3693}
3694
3695fn merge_ranges(ranges: &mut Vec<Range<Anchor>>, buffer: &MultiBufferSnapshot) {
3696    ranges.sort_unstable_by(|a, b| {
3697        a.start
3698            .cmp(&b.start, buffer)
3699            .then_with(|| b.end.cmp(&a.end, buffer))
3700    });
3701
3702    let mut ix = 0;
3703    while ix + 1 < ranges.len() {
3704        let b = ranges[ix + 1].clone();
3705        let a = &mut ranges[ix];
3706        if a.end.cmp(&b.start, buffer).is_gt() {
3707            if a.end.cmp(&b.end, buffer).is_lt() {
3708                a.end = b.end;
3709            }
3710            ranges.remove(ix + 1);
3711        } else {
3712            ix += 1;
3713        }
3714    }
3715}
3716
3717#[cfg(test)]
3718mod tests {
3719    use super::*;
3720    use futures::stream::{self};
3721    use gpui::TestAppContext;
3722    use indoc::indoc;
3723    use language::{
3724        Buffer, Language, LanguageConfig, LanguageMatcher, Point, language_settings,
3725        tree_sitter_rust,
3726    };
3727    use language_model::{LanguageModelRegistry, TokenUsage};
3728    use rand::prelude::*;
3729    use serde::Serialize;
3730    use settings::SettingsStore;
3731    use std::{future, sync::Arc};
3732
3733    #[derive(Serialize)]
3734    pub struct DummyCompletionRequest {
3735        pub name: String,
3736    }
3737
3738    #[gpui::test(iterations = 10)]
3739    async fn test_transform_autoindent(cx: &mut TestAppContext, mut rng: StdRng) {
3740        cx.set_global(cx.update(SettingsStore::test));
3741        cx.update(language_model::LanguageModelRegistry::test);
3742        cx.update(language_settings::init);
3743
3744        let text = indoc! {"
3745            fn main() {
3746                let x = 0;
3747                for _ in 0..10 {
3748                    x += 1;
3749                }
3750            }
3751        "};
3752        let buffer = cx.new(|cx| Buffer::local(text, cx).with_language(Arc::new(rust_lang()), cx));
3753        let buffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx));
3754        let range = buffer.read_with(cx, |buffer, cx| {
3755            let snapshot = buffer.snapshot(cx);
3756            snapshot.anchor_before(Point::new(1, 0))..snapshot.anchor_after(Point::new(4, 5))
3757        });
3758        let prompt_builder = Arc::new(PromptBuilder::new(None).unwrap());
3759        let codegen = cx.new(|cx| {
3760            CodegenAlternative::new(
3761                buffer.clone(),
3762                range.clone(),
3763                true,
3764                None,
3765                prompt_builder,
3766                cx,
3767            )
3768        });
3769
3770        let chunks_tx = simulate_response_stream(codegen.clone(), cx);
3771
3772        let mut new_text = concat!(
3773            "       let mut x = 0;\n",
3774            "       while x < 10 {\n",
3775            "           x += 1;\n",
3776            "       }",
3777        );
3778        while !new_text.is_empty() {
3779            let max_len = cmp::min(new_text.len(), 10);
3780            let len = rng.gen_range(1..=max_len);
3781            let (chunk, suffix) = new_text.split_at(len);
3782            chunks_tx.unbounded_send(chunk.to_string()).unwrap();
3783            new_text = suffix;
3784            cx.background_executor.run_until_parked();
3785        }
3786        drop(chunks_tx);
3787        cx.background_executor.run_until_parked();
3788
3789        assert_eq!(
3790            buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()),
3791            indoc! {"
3792                fn main() {
3793                    let mut x = 0;
3794                    while x < 10 {
3795                        x += 1;
3796                    }
3797                }
3798            "}
3799        );
3800    }
3801
3802    #[gpui::test(iterations = 10)]
3803    async fn test_autoindent_when_generating_past_indentation(
3804        cx: &mut TestAppContext,
3805        mut rng: StdRng,
3806    ) {
3807        cx.set_global(cx.update(SettingsStore::test));
3808        cx.update(language_settings::init);
3809
3810        let text = indoc! {"
3811            fn main() {
3812                le
3813            }
3814        "};
3815        let buffer = cx.new(|cx| Buffer::local(text, cx).with_language(Arc::new(rust_lang()), cx));
3816        let buffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx));
3817        let range = buffer.read_with(cx, |buffer, cx| {
3818            let snapshot = buffer.snapshot(cx);
3819            snapshot.anchor_before(Point::new(1, 6))..snapshot.anchor_after(Point::new(1, 6))
3820        });
3821        let prompt_builder = Arc::new(PromptBuilder::new(None).unwrap());
3822        let codegen = cx.new(|cx| {
3823            CodegenAlternative::new(
3824                buffer.clone(),
3825                range.clone(),
3826                true,
3827                None,
3828                prompt_builder,
3829                cx,
3830            )
3831        });
3832
3833        let chunks_tx = simulate_response_stream(codegen.clone(), cx);
3834
3835        cx.background_executor.run_until_parked();
3836
3837        let mut new_text = concat!(
3838            "t mut x = 0;\n",
3839            "while x < 10 {\n",
3840            "    x += 1;\n",
3841            "}", //
3842        );
3843        while !new_text.is_empty() {
3844            let max_len = cmp::min(new_text.len(), 10);
3845            let len = rng.gen_range(1..=max_len);
3846            let (chunk, suffix) = new_text.split_at(len);
3847            chunks_tx.unbounded_send(chunk.to_string()).unwrap();
3848            new_text = suffix;
3849            cx.background_executor.run_until_parked();
3850        }
3851        drop(chunks_tx);
3852        cx.background_executor.run_until_parked();
3853
3854        assert_eq!(
3855            buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()),
3856            indoc! {"
3857                fn main() {
3858                    let mut x = 0;
3859                    while x < 10 {
3860                        x += 1;
3861                    }
3862                }
3863            "}
3864        );
3865    }
3866
3867    #[gpui::test(iterations = 10)]
3868    async fn test_autoindent_when_generating_before_indentation(
3869        cx: &mut TestAppContext,
3870        mut rng: StdRng,
3871    ) {
3872        cx.update(LanguageModelRegistry::test);
3873        cx.set_global(cx.update(SettingsStore::test));
3874        cx.update(language_settings::init);
3875
3876        let text = concat!(
3877            "fn main() {\n",
3878            "  \n",
3879            "}\n" //
3880        );
3881        let buffer = cx.new(|cx| Buffer::local(text, cx).with_language(Arc::new(rust_lang()), cx));
3882        let buffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx));
3883        let range = buffer.read_with(cx, |buffer, cx| {
3884            let snapshot = buffer.snapshot(cx);
3885            snapshot.anchor_before(Point::new(1, 2))..snapshot.anchor_after(Point::new(1, 2))
3886        });
3887        let prompt_builder = Arc::new(PromptBuilder::new(None).unwrap());
3888        let codegen = cx.new(|cx| {
3889            CodegenAlternative::new(
3890                buffer.clone(),
3891                range.clone(),
3892                true,
3893                None,
3894                prompt_builder,
3895                cx,
3896            )
3897        });
3898
3899        let chunks_tx = simulate_response_stream(codegen.clone(), cx);
3900
3901        cx.background_executor.run_until_parked();
3902
3903        let mut new_text = concat!(
3904            "let mut x = 0;\n",
3905            "while x < 10 {\n",
3906            "    x += 1;\n",
3907            "}", //
3908        );
3909        while !new_text.is_empty() {
3910            let max_len = cmp::min(new_text.len(), 10);
3911            let len = rng.gen_range(1..=max_len);
3912            let (chunk, suffix) = new_text.split_at(len);
3913            chunks_tx.unbounded_send(chunk.to_string()).unwrap();
3914            new_text = suffix;
3915            cx.background_executor.run_until_parked();
3916        }
3917        drop(chunks_tx);
3918        cx.background_executor.run_until_parked();
3919
3920        assert_eq!(
3921            buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()),
3922            indoc! {"
3923                fn main() {
3924                    let mut x = 0;
3925                    while x < 10 {
3926                        x += 1;
3927                    }
3928                }
3929            "}
3930        );
3931    }
3932
3933    #[gpui::test(iterations = 10)]
3934    async fn test_autoindent_respects_tabs_in_selection(cx: &mut TestAppContext) {
3935        cx.update(LanguageModelRegistry::test);
3936        cx.set_global(cx.update(SettingsStore::test));
3937        cx.update(language_settings::init);
3938
3939        let text = indoc! {"
3940            func main() {
3941            \tx := 0
3942            \tfor i := 0; i < 10; i++ {
3943            \t\tx++
3944            \t}
3945            }
3946        "};
3947        let buffer = cx.new(|cx| Buffer::local(text, cx));
3948        let buffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx));
3949        let range = buffer.read_with(cx, |buffer, cx| {
3950            let snapshot = buffer.snapshot(cx);
3951            snapshot.anchor_before(Point::new(0, 0))..snapshot.anchor_after(Point::new(4, 2))
3952        });
3953        let prompt_builder = Arc::new(PromptBuilder::new(None).unwrap());
3954        let codegen = cx.new(|cx| {
3955            CodegenAlternative::new(
3956                buffer.clone(),
3957                range.clone(),
3958                true,
3959                None,
3960                prompt_builder,
3961                cx,
3962            )
3963        });
3964
3965        let chunks_tx = simulate_response_stream(codegen.clone(), cx);
3966        let new_text = concat!(
3967            "func main() {\n",
3968            "\tx := 0\n",
3969            "\tfor x < 10 {\n",
3970            "\t\tx++\n",
3971            "\t}", //
3972        );
3973        chunks_tx.unbounded_send(new_text.to_string()).unwrap();
3974        drop(chunks_tx);
3975        cx.background_executor.run_until_parked();
3976
3977        assert_eq!(
3978            buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()),
3979            indoc! {"
3980                func main() {
3981                \tx := 0
3982                \tfor x < 10 {
3983                \t\tx++
3984                \t}
3985                }
3986            "}
3987        );
3988    }
3989
3990    #[gpui::test]
3991    async fn test_inactive_codegen_alternative(cx: &mut TestAppContext) {
3992        cx.update(LanguageModelRegistry::test);
3993        cx.set_global(cx.update(SettingsStore::test));
3994        cx.update(language_settings::init);
3995
3996        let text = indoc! {"
3997            fn main() {
3998                let x = 0;
3999            }
4000        "};
4001        let buffer = cx.new(|cx| Buffer::local(text, cx).with_language(Arc::new(rust_lang()), cx));
4002        let buffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx));
4003        let range = buffer.read_with(cx, |buffer, cx| {
4004            let snapshot = buffer.snapshot(cx);
4005            snapshot.anchor_before(Point::new(1, 0))..snapshot.anchor_after(Point::new(1, 14))
4006        });
4007        let prompt_builder = Arc::new(PromptBuilder::new(None).unwrap());
4008        let codegen = cx.new(|cx| {
4009            CodegenAlternative::new(
4010                buffer.clone(),
4011                range.clone(),
4012                false,
4013                None,
4014                prompt_builder,
4015                cx,
4016            )
4017        });
4018
4019        let chunks_tx = simulate_response_stream(codegen.clone(), cx);
4020        chunks_tx
4021            .unbounded_send("let mut x = 0;\nx += 1;".to_string())
4022            .unwrap();
4023        drop(chunks_tx);
4024        cx.run_until_parked();
4025
4026        // The codegen is inactive, so the buffer doesn't get modified.
4027        assert_eq!(
4028            buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()),
4029            text
4030        );
4031
4032        // Activating the codegen applies the changes.
4033        codegen.update(cx, |codegen, cx| codegen.set_active(true, cx));
4034        assert_eq!(
4035            buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()),
4036            indoc! {"
4037                fn main() {
4038                    let mut x = 0;
4039                    x += 1;
4040                }
4041            "}
4042        );
4043
4044        // Deactivating the codegen undoes the changes.
4045        codegen.update(cx, |codegen, cx| codegen.set_active(false, cx));
4046        cx.run_until_parked();
4047        assert_eq!(
4048            buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()),
4049            text
4050        );
4051    }
4052
4053    #[gpui::test]
4054    async fn test_strip_invalid_spans_from_codeblock() {
4055        assert_chunks("Lorem ipsum dolor", "Lorem ipsum dolor").await;
4056        assert_chunks("```\nLorem ipsum dolor", "Lorem ipsum dolor").await;
4057        assert_chunks("```\nLorem ipsum dolor\n```", "Lorem ipsum dolor").await;
4058        assert_chunks(
4059            "```html\n```js\nLorem ipsum dolor\n```\n```",
4060            "```js\nLorem ipsum dolor\n```",
4061        )
4062        .await;
4063        assert_chunks("``\nLorem ipsum dolor\n```", "``\nLorem ipsum dolor\n```").await;
4064        assert_chunks("Lorem<|CURSOR|> ipsum", "Lorem ipsum").await;
4065        assert_chunks("Lorem ipsum", "Lorem ipsum").await;
4066        assert_chunks("```\n<|CURSOR|>Lorem ipsum\n```", "Lorem ipsum").await;
4067
4068        async fn assert_chunks(text: &str, expected_text: &str) {
4069            for chunk_size in 1..=text.len() {
4070                let actual_text = StripInvalidSpans::new(chunks(text, chunk_size))
4071                    .map(|chunk| chunk.unwrap())
4072                    .collect::<String>()
4073                    .await;
4074                assert_eq!(
4075                    actual_text, expected_text,
4076                    "failed to strip invalid spans, chunk size: {}",
4077                    chunk_size
4078                );
4079            }
4080        }
4081
4082        fn chunks(text: &str, size: usize) -> impl Stream<Item = Result<String>> {
4083            stream::iter(
4084                text.chars()
4085                    .collect::<Vec<_>>()
4086                    .chunks(size)
4087                    .map(|chunk| Ok(chunk.iter().collect::<String>()))
4088                    .collect::<Vec<_>>(),
4089            )
4090        }
4091    }
4092
4093    fn simulate_response_stream(
4094        codegen: Entity<CodegenAlternative>,
4095        cx: &mut TestAppContext,
4096    ) -> mpsc::UnboundedSender<String> {
4097        let (chunks_tx, chunks_rx) = mpsc::unbounded();
4098        codegen.update(cx, |codegen, cx| {
4099            codegen.handle_stream(
4100                String::new(),
4101                String::new(),
4102                None,
4103                future::ready(Ok(LanguageModelTextStream {
4104                    message_id: None,
4105                    stream: chunks_rx.map(Ok).boxed(),
4106                    last_token_usage: Arc::new(Mutex::new(TokenUsage::default())),
4107                })),
4108                cx,
4109            );
4110        });
4111        chunks_tx
4112    }
4113
4114    fn rust_lang() -> Language {
4115        Language::new(
4116            LanguageConfig {
4117                name: "Rust".into(),
4118                matcher: LanguageMatcher {
4119                    path_suffixes: vec!["rs".to_string()],
4120                    ..Default::default()
4121                },
4122                ..Default::default()
4123            },
4124            Some(tree_sitter_rust::LANGUAGE.into()),
4125        )
4126        .with_indents_query(
4127            r#"
4128            (call_expression) @indent
4129            (field_expression) @indent
4130            (_ "(" ")" @end) @indent
4131            (_ "{" "}" @end) @indent
4132            "#,
4133        )
4134        .unwrap()
4135    }
4136}