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(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 = self.build_request(user_prompt, assistant_panel_context.clone(), cx);
2874            match request {
2875                Ok(request) => {
2876                    let total_count = model.count_tokens(request.clone(), cx);
2877                    let assistant_panel_count = assistant_panel_context
2878                        .map(|context| model.count_tokens(context, cx))
2879                        .unwrap_or_else(|| future::ready(Ok(0)).boxed());
2880
2881                    async move {
2882                        Ok(TokenCounts {
2883                            total: total_count.await?,
2884                            assistant_panel: assistant_panel_count.await?,
2885                        })
2886                    }
2887                    .boxed()
2888                }
2889                Err(error) => futures::future::ready(Err(error)).boxed(),
2890            }
2891        } else {
2892            future::ready(Err(anyhow!("no active model"))).boxed()
2893        }
2894    }
2895
2896    pub fn start(
2897        &mut self,
2898        user_prompt: String,
2899        assistant_panel_context: Option<LanguageModelRequest>,
2900        model: Arc<dyn LanguageModel>,
2901        cx: &mut Context<Self>,
2902    ) -> Result<()> {
2903        if let Some(transformation_transaction_id) = self.transformation_transaction_id.take() {
2904            self.buffer.update(cx, |buffer, cx| {
2905                buffer.undo_transaction(transformation_transaction_id, cx);
2906            });
2907        }
2908
2909        self.edit_position = Some(self.range.start.bias_right(&self.snapshot));
2910
2911        let api_key = model.api_key(cx);
2912        let telemetry_id = model.telemetry_id();
2913        let provider_id = model.provider_id();
2914        let stream: LocalBoxFuture<Result<LanguageModelTextStream>> =
2915            if user_prompt.trim().to_lowercase() == "delete" {
2916                async { Ok(LanguageModelTextStream::default()) }.boxed_local()
2917            } else {
2918                let request = self.build_request(user_prompt, assistant_panel_context, cx)?;
2919                self.request = Some(request.clone());
2920
2921                cx.spawn(async move |_, cx| model.stream_completion_text(request, &cx).await)
2922                    .boxed_local()
2923            };
2924        self.handle_stream(telemetry_id, provider_id.to_string(), api_key, stream, cx);
2925        Ok(())
2926    }
2927
2928    fn build_request(
2929        &self,
2930        user_prompt: String,
2931        assistant_panel_context: Option<LanguageModelRequest>,
2932        cx: &App,
2933    ) -> Result<LanguageModelRequest> {
2934        let buffer = self.buffer.read(cx).snapshot(cx);
2935        let language = buffer.language_at(self.range.start);
2936        let language_name = if let Some(language) = language.as_ref() {
2937            if Arc::ptr_eq(language, &language::PLAIN_TEXT) {
2938                None
2939            } else {
2940                Some(language.name())
2941            }
2942        } else {
2943            None
2944        };
2945
2946        let language_name = language_name.as_ref();
2947        let start = buffer.point_to_buffer_offset(self.range.start);
2948        let end = buffer.point_to_buffer_offset(self.range.end);
2949        let (buffer, range) = if let Some((start, end)) = start.zip(end) {
2950            let (start_buffer, start_buffer_offset) = start;
2951            let (end_buffer, end_buffer_offset) = end;
2952            if start_buffer.remote_id() == end_buffer.remote_id() {
2953                (start_buffer.clone(), start_buffer_offset..end_buffer_offset)
2954            } else {
2955                return Err(anyhow::anyhow!("invalid transformation range"));
2956            }
2957        } else {
2958            return Err(anyhow::anyhow!("invalid transformation range"));
2959        };
2960
2961        let prompt = self
2962            .builder
2963            .generate_inline_transformation_prompt(user_prompt, language_name, buffer, range)
2964            .map_err(|e| anyhow::anyhow!("Failed to generate content prompt: {}", e))?;
2965
2966        let mut messages = Vec::new();
2967        if let Some(context_request) = assistant_panel_context {
2968            messages = context_request.messages;
2969        }
2970
2971        messages.push(LanguageModelRequestMessage {
2972            role: Role::User,
2973            content: vec![prompt.into()],
2974            cache: false,
2975        });
2976
2977        Ok(LanguageModelRequest {
2978            thread_id: None,
2979            prompt_id: None,
2980            mode: None,
2981            messages,
2982            tools: Vec::new(),
2983            stop: Vec::new(),
2984            temperature: None,
2985        })
2986    }
2987
2988    pub fn handle_stream(
2989        &mut self,
2990        model_telemetry_id: String,
2991        model_provider_id: String,
2992        model_api_key: Option<String>,
2993        stream: impl 'static + Future<Output = Result<LanguageModelTextStream>>,
2994        cx: &mut Context<Self>,
2995    ) {
2996        let start_time = Instant::now();
2997        let snapshot = self.snapshot.clone();
2998        let selected_text = snapshot
2999            .text_for_range(self.range.start..self.range.end)
3000            .collect::<Rope>();
3001
3002        let selection_start = self.range.start.to_point(&snapshot);
3003
3004        // Start with the indentation of the first line in the selection
3005        let mut suggested_line_indent = snapshot
3006            .suggested_indents(selection_start.row..=selection_start.row, cx)
3007            .into_values()
3008            .next()
3009            .unwrap_or_else(|| snapshot.indent_size_for_line(MultiBufferRow(selection_start.row)));
3010
3011        // If the first line in the selection does not have indentation, check the following lines
3012        if suggested_line_indent.len == 0 && suggested_line_indent.kind == IndentKind::Space {
3013            for row in selection_start.row..=self.range.end.to_point(&snapshot).row {
3014                let line_indent = snapshot.indent_size_for_line(MultiBufferRow(row));
3015                // Prefer tabs if a line in the selection uses tabs as indentation
3016                if line_indent.kind == IndentKind::Tab {
3017                    suggested_line_indent.kind = IndentKind::Tab;
3018                    break;
3019                }
3020            }
3021        }
3022
3023        let http_client = cx.http_client();
3024        let telemetry = self.telemetry.clone();
3025        let language_name = {
3026            let multibuffer = self.buffer.read(cx);
3027            let snapshot = multibuffer.snapshot(cx);
3028            let ranges = snapshot.range_to_buffer_ranges(self.range.clone());
3029            ranges
3030                .first()
3031                .and_then(|(buffer, _, _)| buffer.language())
3032                .map(|language| language.name())
3033        };
3034
3035        self.diff = Diff::default();
3036        self.status = CodegenStatus::Pending;
3037        let mut edit_start = self.range.start.to_offset(&snapshot);
3038        let completion = Arc::new(Mutex::new(String::new()));
3039        let completion_clone = completion.clone();
3040
3041        self.generation = cx.spawn(async move |codegen, cx| {
3042            let stream = stream.await;
3043            let message_id = stream
3044                .as_ref()
3045                .ok()
3046                .and_then(|stream| stream.message_id.clone());
3047            let generate = async {
3048                let (mut diff_tx, mut diff_rx) = mpsc::channel(1);
3049                let executor = cx.background_executor().clone();
3050                let message_id = message_id.clone();
3051                let line_based_stream_diff: Task<anyhow::Result<()>> =
3052                    cx.background_spawn(async move {
3053                        let mut response_latency = None;
3054                        let request_start = Instant::now();
3055                        let diff = async {
3056                            let chunks =
3057                                StripInvalidSpans::new(stream?.stream.map_err(|e| e.into()));
3058                            futures::pin_mut!(chunks);
3059                            let mut diff = StreamingDiff::new(selected_text.to_string());
3060                            let mut line_diff = LineDiff::default();
3061
3062                            let mut new_text = String::new();
3063                            let mut base_indent = None;
3064                            let mut line_indent = None;
3065                            let mut first_line = true;
3066
3067                            while let Some(chunk) = chunks.next().await {
3068                                if response_latency.is_none() {
3069                                    response_latency = Some(request_start.elapsed());
3070                                }
3071                                let chunk = chunk?;
3072                                completion_clone.lock().push_str(&chunk);
3073
3074                                let mut lines = chunk.split('\n').peekable();
3075                                while let Some(line) = lines.next() {
3076                                    new_text.push_str(line);
3077                                    if line_indent.is_none() {
3078                                        if let Some(non_whitespace_ch_ix) =
3079                                            new_text.find(|ch: char| !ch.is_whitespace())
3080                                        {
3081                                            line_indent = Some(non_whitespace_ch_ix);
3082                                            base_indent = base_indent.or(line_indent);
3083
3084                                            let line_indent = line_indent.unwrap();
3085                                            let base_indent = base_indent.unwrap();
3086                                            let indent_delta =
3087                                                line_indent as i32 - base_indent as i32;
3088                                            let mut corrected_indent_len = cmp::max(
3089                                                0,
3090                                                suggested_line_indent.len as i32 + indent_delta,
3091                                            )
3092                                                as usize;
3093                                            if first_line {
3094                                                corrected_indent_len = corrected_indent_len
3095                                                    .saturating_sub(
3096                                                        selection_start.column as usize,
3097                                                    );
3098                                            }
3099
3100                                            let indent_char = suggested_line_indent.char();
3101                                            let mut indent_buffer = [0; 4];
3102                                            let indent_str =
3103                                                indent_char.encode_utf8(&mut indent_buffer);
3104                                            new_text.replace_range(
3105                                                ..line_indent,
3106                                                &indent_str.repeat(corrected_indent_len),
3107                                            );
3108                                        }
3109                                    }
3110
3111                                    if line_indent.is_some() {
3112                                        let char_ops = diff.push_new(&new_text);
3113                                        line_diff.push_char_operations(&char_ops, &selected_text);
3114                                        diff_tx
3115                                            .send((char_ops, line_diff.line_operations()))
3116                                            .await?;
3117                                        new_text.clear();
3118                                    }
3119
3120                                    if lines.peek().is_some() {
3121                                        let char_ops = diff.push_new("\n");
3122                                        line_diff.push_char_operations(&char_ops, &selected_text);
3123                                        diff_tx
3124                                            .send((char_ops, line_diff.line_operations()))
3125                                            .await?;
3126                                        if line_indent.is_none() {
3127                                            // Don't write out the leading indentation in empty lines on the next line
3128                                            // This is the case where the above if statement didn't clear the buffer
3129                                            new_text.clear();
3130                                        }
3131                                        line_indent = None;
3132                                        first_line = false;
3133                                    }
3134                                }
3135                            }
3136
3137                            let mut char_ops = diff.push_new(&new_text);
3138                            char_ops.extend(diff.finish());
3139                            line_diff.push_char_operations(&char_ops, &selected_text);
3140                            line_diff.finish(&selected_text);
3141                            diff_tx
3142                                .send((char_ops, line_diff.line_operations()))
3143                                .await?;
3144
3145                            anyhow::Ok(())
3146                        };
3147
3148                        let result = diff.await;
3149
3150                        let error_message = result.as_ref().err().map(|error| error.to_string());
3151                        report_assistant_event(
3152                            AssistantEventData {
3153                                conversation_id: None,
3154                                message_id,
3155                                kind: AssistantKind::Inline,
3156                                phase: AssistantPhase::Response,
3157                                model: model_telemetry_id,
3158                                model_provider: model_provider_id.to_string(),
3159                                response_latency,
3160                                error_message,
3161                                language_name: language_name.map(|name| name.to_proto()),
3162                            },
3163                            telemetry,
3164                            http_client,
3165                            model_api_key,
3166                            &executor,
3167                        );
3168
3169                        result?;
3170                        Ok(())
3171                    });
3172
3173                while let Some((char_ops, line_ops)) = diff_rx.next().await {
3174                    codegen.update(cx, |codegen, cx| {
3175                        codegen.last_equal_ranges.clear();
3176
3177                        let edits = char_ops
3178                            .into_iter()
3179                            .filter_map(|operation| match operation {
3180                                CharOperation::Insert { text } => {
3181                                    let edit_start = snapshot.anchor_after(edit_start);
3182                                    Some((edit_start..edit_start, text))
3183                                }
3184                                CharOperation::Delete { bytes } => {
3185                                    let edit_end = edit_start + bytes;
3186                                    let edit_range = snapshot.anchor_after(edit_start)
3187                                        ..snapshot.anchor_before(edit_end);
3188                                    edit_start = edit_end;
3189                                    Some((edit_range, String::new()))
3190                                }
3191                                CharOperation::Keep { bytes } => {
3192                                    let edit_end = edit_start + bytes;
3193                                    let edit_range = snapshot.anchor_after(edit_start)
3194                                        ..snapshot.anchor_before(edit_end);
3195                                    edit_start = edit_end;
3196                                    codegen.last_equal_ranges.push(edit_range);
3197                                    None
3198                                }
3199                            })
3200                            .collect::<Vec<_>>();
3201
3202                        if codegen.active {
3203                            codegen.apply_edits(edits.iter().cloned(), cx);
3204                            codegen.reapply_line_based_diff(line_ops.iter().cloned(), cx);
3205                        }
3206                        codegen.edits.extend(edits);
3207                        codegen.line_operations = line_ops;
3208                        codegen.edit_position = Some(snapshot.anchor_after(edit_start));
3209
3210                        cx.notify();
3211                    })?;
3212                }
3213
3214                // Streaming stopped and we have the new text in the buffer, and a line-based diff applied for the whole new buffer.
3215                // That diff is not what a regular diff is and might look unexpected, ergo apply a regular diff.
3216                // It's fine to apply even if the rest of the line diffing fails, as no more hunks are coming through `diff_rx`.
3217                let batch_diff_task =
3218                    codegen.update(cx, |codegen, cx| codegen.reapply_batch_diff(cx))?;
3219                let (line_based_stream_diff, ()) = join!(line_based_stream_diff, batch_diff_task);
3220                line_based_stream_diff?;
3221
3222                anyhow::Ok(())
3223            };
3224
3225            let result = generate.await;
3226            let elapsed_time = start_time.elapsed().as_secs_f64();
3227
3228            codegen
3229                .update(cx, |this, cx| {
3230                    this.message_id = message_id;
3231                    this.last_equal_ranges.clear();
3232                    if let Err(error) = result {
3233                        this.status = CodegenStatus::Error(error);
3234                    } else {
3235                        this.status = CodegenStatus::Done;
3236                    }
3237                    this.elapsed_time = Some(elapsed_time);
3238                    this.completion = Some(completion.lock().clone());
3239                    cx.emit(CodegenEvent::Finished);
3240                    cx.notify();
3241                })
3242                .ok();
3243        });
3244        cx.notify();
3245    }
3246
3247    pub fn stop(&mut self, cx: &mut Context<Self>) {
3248        self.last_equal_ranges.clear();
3249        if self.diff.is_empty() {
3250            self.status = CodegenStatus::Idle;
3251        } else {
3252            self.status = CodegenStatus::Done;
3253        }
3254        self.generation = Task::ready(());
3255        cx.emit(CodegenEvent::Finished);
3256        cx.notify();
3257    }
3258
3259    pub fn undo(&mut self, cx: &mut Context<Self>) {
3260        self.buffer.update(cx, |buffer, cx| {
3261            if let Some(transaction_id) = self.transformation_transaction_id.take() {
3262                buffer.undo_transaction(transaction_id, cx);
3263                buffer.refresh_preview(cx);
3264            }
3265        });
3266    }
3267
3268    fn apply_edits(
3269        &mut self,
3270        edits: impl IntoIterator<Item = (Range<Anchor>, String)>,
3271        cx: &mut Context<CodegenAlternative>,
3272    ) {
3273        let transaction = self.buffer.update(cx, |buffer, cx| {
3274            // Avoid grouping assistant edits with user edits.
3275            buffer.finalize_last_transaction(cx);
3276            buffer.start_transaction(cx);
3277            buffer.edit(edits, None, cx);
3278            buffer.end_transaction(cx)
3279        });
3280
3281        if let Some(transaction) = transaction {
3282            if let Some(first_transaction) = self.transformation_transaction_id {
3283                // Group all assistant edits into the first transaction.
3284                self.buffer.update(cx, |buffer, cx| {
3285                    buffer.merge_transactions(transaction, first_transaction, cx)
3286                });
3287            } else {
3288                self.transformation_transaction_id = Some(transaction);
3289                self.buffer
3290                    .update(cx, |buffer, cx| buffer.finalize_last_transaction(cx));
3291            }
3292        }
3293    }
3294
3295    fn reapply_line_based_diff(
3296        &mut self,
3297        line_operations: impl IntoIterator<Item = LineOperation>,
3298        cx: &mut Context<Self>,
3299    ) {
3300        let old_snapshot = self.snapshot.clone();
3301        let old_range = self.range.to_point(&old_snapshot);
3302        let new_snapshot = self.buffer.read(cx).snapshot(cx);
3303        let new_range = self.range.to_point(&new_snapshot);
3304
3305        let mut old_row = old_range.start.row;
3306        let mut new_row = new_range.start.row;
3307
3308        self.diff.deleted_row_ranges.clear();
3309        self.diff.inserted_row_ranges.clear();
3310        for operation in line_operations {
3311            match operation {
3312                LineOperation::Keep { lines } => {
3313                    old_row += lines;
3314                    new_row += lines;
3315                }
3316                LineOperation::Delete { lines } => {
3317                    let old_end_row = old_row + lines - 1;
3318                    let new_row = new_snapshot.anchor_before(Point::new(new_row, 0));
3319
3320                    if let Some((_, last_deleted_row_range)) =
3321                        self.diff.deleted_row_ranges.last_mut()
3322                    {
3323                        if *last_deleted_row_range.end() + 1 == old_row {
3324                            *last_deleted_row_range = *last_deleted_row_range.start()..=old_end_row;
3325                        } else {
3326                            self.diff
3327                                .deleted_row_ranges
3328                                .push((new_row, old_row..=old_end_row));
3329                        }
3330                    } else {
3331                        self.diff
3332                            .deleted_row_ranges
3333                            .push((new_row, old_row..=old_end_row));
3334                    }
3335
3336                    old_row += lines;
3337                }
3338                LineOperation::Insert { lines } => {
3339                    let new_end_row = new_row + lines - 1;
3340                    let start = new_snapshot.anchor_before(Point::new(new_row, 0));
3341                    let end = new_snapshot.anchor_before(Point::new(
3342                        new_end_row,
3343                        new_snapshot.line_len(MultiBufferRow(new_end_row)),
3344                    ));
3345                    self.diff.inserted_row_ranges.push(start..end);
3346                    new_row += lines;
3347                }
3348            }
3349
3350            cx.notify();
3351        }
3352    }
3353
3354    fn reapply_batch_diff(&mut self, cx: &mut Context<Self>) -> Task<()> {
3355        let old_snapshot = self.snapshot.clone();
3356        let old_range = self.range.to_point(&old_snapshot);
3357        let new_snapshot = self.buffer.read(cx).snapshot(cx);
3358        let new_range = self.range.to_point(&new_snapshot);
3359
3360        cx.spawn(async move |codegen, cx| {
3361            let (deleted_row_ranges, inserted_row_ranges) = cx
3362                .background_spawn(async move {
3363                    let old_text = old_snapshot
3364                        .text_for_range(
3365                            Point::new(old_range.start.row, 0)
3366                                ..Point::new(
3367                                    old_range.end.row,
3368                                    old_snapshot.line_len(MultiBufferRow(old_range.end.row)),
3369                                ),
3370                        )
3371                        .collect::<String>();
3372                    let new_text = new_snapshot
3373                        .text_for_range(
3374                            Point::new(new_range.start.row, 0)
3375                                ..Point::new(
3376                                    new_range.end.row,
3377                                    new_snapshot.line_len(MultiBufferRow(new_range.end.row)),
3378                                ),
3379                        )
3380                        .collect::<String>();
3381
3382                    let old_start_row = old_range.start.row;
3383                    let new_start_row = new_range.start.row;
3384                    let mut deleted_row_ranges: Vec<(Anchor, RangeInclusive<u32>)> = Vec::new();
3385                    let mut inserted_row_ranges = Vec::new();
3386                    for (old_rows, new_rows) in line_diff(&old_text, &new_text) {
3387                        let old_rows = old_start_row + old_rows.start..old_start_row + old_rows.end;
3388                        let new_rows = new_start_row + new_rows.start..new_start_row + new_rows.end;
3389                        if !old_rows.is_empty() {
3390                            deleted_row_ranges.push((
3391                                new_snapshot.anchor_before(Point::new(new_rows.start, 0)),
3392                                old_rows.start..=old_rows.end - 1,
3393                            ));
3394                        }
3395                        if !new_rows.is_empty() {
3396                            let start = new_snapshot.anchor_before(Point::new(new_rows.start, 0));
3397                            let new_end_row = new_rows.end - 1;
3398                            let end = new_snapshot.anchor_before(Point::new(
3399                                new_end_row,
3400                                new_snapshot.line_len(MultiBufferRow(new_end_row)),
3401                            ));
3402                            inserted_row_ranges.push(start..end);
3403                        }
3404                    }
3405                    (deleted_row_ranges, inserted_row_ranges)
3406                })
3407                .await;
3408
3409            codegen
3410                .update(cx, |codegen, cx| {
3411                    codegen.diff.deleted_row_ranges = deleted_row_ranges;
3412                    codegen.diff.inserted_row_ranges = inserted_row_ranges;
3413                    cx.notify();
3414                })
3415                .ok();
3416        })
3417    }
3418}
3419
3420struct StripInvalidSpans<T> {
3421    stream: T,
3422    stream_done: bool,
3423    buffer: String,
3424    first_line: bool,
3425    line_end: bool,
3426    starts_with_code_block: bool,
3427}
3428
3429impl<T> StripInvalidSpans<T>
3430where
3431    T: Stream<Item = Result<String>>,
3432{
3433    fn new(stream: T) -> Self {
3434        Self {
3435            stream,
3436            stream_done: false,
3437            buffer: String::new(),
3438            first_line: true,
3439            line_end: false,
3440            starts_with_code_block: false,
3441        }
3442    }
3443}
3444
3445impl<T> Stream for StripInvalidSpans<T>
3446where
3447    T: Stream<Item = Result<String>>,
3448{
3449    type Item = Result<String>;
3450
3451    fn poll_next(self: Pin<&mut Self>, cx: &mut task::Context) -> Poll<Option<Self::Item>> {
3452        const CODE_BLOCK_DELIMITER: &str = "```";
3453        const CURSOR_SPAN: &str = "<|CURSOR|>";
3454
3455        let this = unsafe { self.get_unchecked_mut() };
3456        loop {
3457            if !this.stream_done {
3458                let mut stream = unsafe { Pin::new_unchecked(&mut this.stream) };
3459                match stream.as_mut().poll_next(cx) {
3460                    Poll::Ready(Some(Ok(chunk))) => {
3461                        this.buffer.push_str(&chunk);
3462                    }
3463                    Poll::Ready(Some(Err(error))) => return Poll::Ready(Some(Err(error))),
3464                    Poll::Ready(None) => {
3465                        this.stream_done = true;
3466                    }
3467                    Poll::Pending => return Poll::Pending,
3468                }
3469            }
3470
3471            let mut chunk = String::new();
3472            let mut consumed = 0;
3473            if !this.buffer.is_empty() {
3474                let mut lines = this.buffer.split('\n').enumerate().peekable();
3475                while let Some((line_ix, line)) = lines.next() {
3476                    if line_ix > 0 {
3477                        this.first_line = false;
3478                    }
3479
3480                    if this.first_line {
3481                        let trimmed_line = line.trim();
3482                        if lines.peek().is_some() {
3483                            if trimmed_line.starts_with(CODE_BLOCK_DELIMITER) {
3484                                consumed += line.len() + 1;
3485                                this.starts_with_code_block = true;
3486                                continue;
3487                            }
3488                        } else if trimmed_line.is_empty()
3489                            || prefixes(CODE_BLOCK_DELIMITER)
3490                                .any(|prefix| trimmed_line.starts_with(prefix))
3491                        {
3492                            break;
3493                        }
3494                    }
3495
3496                    let line_without_cursor = line.replace(CURSOR_SPAN, "");
3497                    if lines.peek().is_some() {
3498                        if this.line_end {
3499                            chunk.push('\n');
3500                        }
3501
3502                        chunk.push_str(&line_without_cursor);
3503                        this.line_end = true;
3504                        consumed += line.len() + 1;
3505                    } else if this.stream_done {
3506                        if !this.starts_with_code_block
3507                            || !line_without_cursor.trim().ends_with(CODE_BLOCK_DELIMITER)
3508                        {
3509                            if this.line_end {
3510                                chunk.push('\n');
3511                            }
3512
3513                            chunk.push_str(&line);
3514                        }
3515
3516                        consumed += line.len();
3517                    } else {
3518                        let trimmed_line = line.trim();
3519                        if trimmed_line.is_empty()
3520                            || prefixes(CURSOR_SPAN).any(|prefix| trimmed_line.ends_with(prefix))
3521                            || prefixes(CODE_BLOCK_DELIMITER)
3522                                .any(|prefix| trimmed_line.ends_with(prefix))
3523                        {
3524                            break;
3525                        } else {
3526                            if this.line_end {
3527                                chunk.push('\n');
3528                                this.line_end = false;
3529                            }
3530
3531                            chunk.push_str(&line_without_cursor);
3532                            consumed += line.len();
3533                        }
3534                    }
3535                }
3536            }
3537
3538            this.buffer = this.buffer.split_off(consumed);
3539            if !chunk.is_empty() {
3540                return Poll::Ready(Some(Ok(chunk)));
3541            } else if this.stream_done {
3542                return Poll::Ready(None);
3543            }
3544        }
3545    }
3546}
3547
3548struct AssistantCodeActionProvider {
3549    editor: WeakEntity<Editor>,
3550    workspace: WeakEntity<Workspace>,
3551}
3552
3553const ASSISTANT_CODE_ACTION_PROVIDER_ID: &str = "assistant";
3554
3555impl CodeActionProvider for AssistantCodeActionProvider {
3556    fn id(&self) -> Arc<str> {
3557        ASSISTANT_CODE_ACTION_PROVIDER_ID.into()
3558    }
3559
3560    fn code_actions(
3561        &self,
3562        buffer: &Entity<Buffer>,
3563        range: Range<text::Anchor>,
3564        _: &mut Window,
3565        cx: &mut App,
3566    ) -> Task<Result<Vec<CodeAction>>> {
3567        if !Assistant::enabled(cx) {
3568            return Task::ready(Ok(Vec::new()));
3569        }
3570
3571        let snapshot = buffer.read(cx).snapshot();
3572        let mut range = range.to_point(&snapshot);
3573
3574        // Expand the range to line boundaries.
3575        range.start.column = 0;
3576        range.end.column = snapshot.line_len(range.end.row);
3577
3578        let mut has_diagnostics = false;
3579        for diagnostic in snapshot.diagnostics_in_range::<_, Point>(range.clone(), false) {
3580            range.start = cmp::min(range.start, diagnostic.range.start);
3581            range.end = cmp::max(range.end, diagnostic.range.end);
3582            has_diagnostics = true;
3583        }
3584        if has_diagnostics {
3585            if let Some(symbols_containing_start) = snapshot.symbols_containing(range.start, None) {
3586                if let Some(symbol) = symbols_containing_start.last() {
3587                    range.start = cmp::min(range.start, symbol.range.start.to_point(&snapshot));
3588                    range.end = cmp::max(range.end, symbol.range.end.to_point(&snapshot));
3589                }
3590            }
3591
3592            if let Some(symbols_containing_end) = snapshot.symbols_containing(range.end, None) {
3593                if let Some(symbol) = symbols_containing_end.last() {
3594                    range.start = cmp::min(range.start, symbol.range.start.to_point(&snapshot));
3595                    range.end = cmp::max(range.end, symbol.range.end.to_point(&snapshot));
3596                }
3597            }
3598
3599            Task::ready(Ok(vec![CodeAction {
3600                server_id: language::LanguageServerId(0),
3601                range: snapshot.anchor_before(range.start)..snapshot.anchor_after(range.end),
3602                lsp_action: LspAction::Action(Box::new(lsp::CodeAction {
3603                    title: "Fix with Assistant".into(),
3604                    ..Default::default()
3605                })),
3606                resolved: true,
3607            }]))
3608        } else {
3609            Task::ready(Ok(Vec::new()))
3610        }
3611    }
3612
3613    fn apply_code_action(
3614        &self,
3615        buffer: Entity<Buffer>,
3616        action: CodeAction,
3617        excerpt_id: ExcerptId,
3618        _push_to_history: bool,
3619        window: &mut Window,
3620        cx: &mut App,
3621    ) -> Task<Result<ProjectTransaction>> {
3622        let editor = self.editor.clone();
3623        let workspace = self.workspace.clone();
3624        window.spawn(cx, async move |cx| {
3625            let editor = editor.upgrade().context("editor was released")?;
3626            let range = editor
3627                .update(cx, |editor, cx| {
3628                    editor.buffer().update(cx, |multibuffer, cx| {
3629                        let buffer = buffer.read(cx);
3630                        let multibuffer_snapshot = multibuffer.read(cx);
3631
3632                        let old_context_range =
3633                            multibuffer_snapshot.context_range_for_excerpt(excerpt_id)?;
3634                        let mut new_context_range = old_context_range.clone();
3635                        if action
3636                            .range
3637                            .start
3638                            .cmp(&old_context_range.start, buffer)
3639                            .is_lt()
3640                        {
3641                            new_context_range.start = action.range.start;
3642                        }
3643                        if action.range.end.cmp(&old_context_range.end, buffer).is_gt() {
3644                            new_context_range.end = action.range.end;
3645                        }
3646                        drop(multibuffer_snapshot);
3647
3648                        if new_context_range != old_context_range {
3649                            multibuffer.resize_excerpt(excerpt_id, new_context_range, cx);
3650                        }
3651
3652                        let multibuffer_snapshot = multibuffer.read(cx);
3653                        Some(
3654                            multibuffer_snapshot
3655                                .anchor_in_excerpt(excerpt_id, action.range.start)?
3656                                ..multibuffer_snapshot
3657                                    .anchor_in_excerpt(excerpt_id, action.range.end)?,
3658                        )
3659                    })
3660                })?
3661                .context("invalid range")?;
3662            let assistant_panel = workspace.update(cx, |workspace, cx| {
3663                workspace
3664                    .panel::<AssistantPanel>(cx)
3665                    .context("assistant panel was released")
3666            })??;
3667
3668            cx.update_global(|assistant: &mut InlineAssistant, window, cx| {
3669                let assist_id = assistant.suggest_assist(
3670                    &editor,
3671                    range,
3672                    "Fix Diagnostics".into(),
3673                    None,
3674                    true,
3675                    Some(workspace),
3676                    Some(&assistant_panel),
3677                    window,
3678                    cx,
3679                );
3680                assistant.start_assist(assist_id, window, cx);
3681            })?;
3682
3683            Ok(ProjectTransaction::default())
3684        })
3685    }
3686}
3687
3688fn prefixes(text: &str) -> impl Iterator<Item = &str> {
3689    (0..text.len() - 1).map(|ix| &text[..ix + 1])
3690}
3691
3692fn merge_ranges(ranges: &mut Vec<Range<Anchor>>, buffer: &MultiBufferSnapshot) {
3693    ranges.sort_unstable_by(|a, b| {
3694        a.start
3695            .cmp(&b.start, buffer)
3696            .then_with(|| b.end.cmp(&a.end, buffer))
3697    });
3698
3699    let mut ix = 0;
3700    while ix + 1 < ranges.len() {
3701        let b = ranges[ix + 1].clone();
3702        let a = &mut ranges[ix];
3703        if a.end.cmp(&b.start, buffer).is_gt() {
3704            if a.end.cmp(&b.end, buffer).is_lt() {
3705                a.end = b.end;
3706            }
3707            ranges.remove(ix + 1);
3708        } else {
3709            ix += 1;
3710        }
3711    }
3712}
3713
3714#[cfg(test)]
3715mod tests {
3716    use super::*;
3717    use futures::stream::{self};
3718    use gpui::TestAppContext;
3719    use indoc::indoc;
3720    use language::{
3721        Buffer, Language, LanguageConfig, LanguageMatcher, Point, language_settings,
3722        tree_sitter_rust,
3723    };
3724    use language_model::{LanguageModelRegistry, TokenUsage};
3725    use rand::prelude::*;
3726    use serde::Serialize;
3727    use settings::SettingsStore;
3728    use std::{future, sync::Arc};
3729
3730    #[derive(Serialize)]
3731    pub struct DummyCompletionRequest {
3732        pub name: String,
3733    }
3734
3735    #[gpui::test(iterations = 10)]
3736    async fn test_transform_autoindent(cx: &mut TestAppContext, mut rng: StdRng) {
3737        cx.set_global(cx.update(SettingsStore::test));
3738        cx.update(language_model::LanguageModelRegistry::test);
3739        cx.update(language_settings::init);
3740
3741        let text = indoc! {"
3742            fn main() {
3743                let x = 0;
3744                for _ in 0..10 {
3745                    x += 1;
3746                }
3747            }
3748        "};
3749        let buffer = cx.new(|cx| Buffer::local(text, cx).with_language(Arc::new(rust_lang()), cx));
3750        let buffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx));
3751        let range = buffer.read_with(cx, |buffer, cx| {
3752            let snapshot = buffer.snapshot(cx);
3753            snapshot.anchor_before(Point::new(1, 0))..snapshot.anchor_after(Point::new(4, 5))
3754        });
3755        let prompt_builder = Arc::new(PromptBuilder::new(None).unwrap());
3756        let codegen = cx.new(|cx| {
3757            CodegenAlternative::new(
3758                buffer.clone(),
3759                range.clone(),
3760                true,
3761                None,
3762                prompt_builder,
3763                cx,
3764            )
3765        });
3766
3767        let chunks_tx = simulate_response_stream(codegen.clone(), cx);
3768
3769        let mut new_text = concat!(
3770            "       let mut x = 0;\n",
3771            "       while x < 10 {\n",
3772            "           x += 1;\n",
3773            "       }",
3774        );
3775        while !new_text.is_empty() {
3776            let max_len = cmp::min(new_text.len(), 10);
3777            let len = rng.gen_range(1..=max_len);
3778            let (chunk, suffix) = new_text.split_at(len);
3779            chunks_tx.unbounded_send(chunk.to_string()).unwrap();
3780            new_text = suffix;
3781            cx.background_executor.run_until_parked();
3782        }
3783        drop(chunks_tx);
3784        cx.background_executor.run_until_parked();
3785
3786        assert_eq!(
3787            buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()),
3788            indoc! {"
3789                fn main() {
3790                    let mut x = 0;
3791                    while x < 10 {
3792                        x += 1;
3793                    }
3794                }
3795            "}
3796        );
3797    }
3798
3799    #[gpui::test(iterations = 10)]
3800    async fn test_autoindent_when_generating_past_indentation(
3801        cx: &mut TestAppContext,
3802        mut rng: StdRng,
3803    ) {
3804        cx.set_global(cx.update(SettingsStore::test));
3805        cx.update(language_settings::init);
3806
3807        let text = indoc! {"
3808            fn main() {
3809                le
3810            }
3811        "};
3812        let buffer = cx.new(|cx| Buffer::local(text, cx).with_language(Arc::new(rust_lang()), cx));
3813        let buffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx));
3814        let range = buffer.read_with(cx, |buffer, cx| {
3815            let snapshot = buffer.snapshot(cx);
3816            snapshot.anchor_before(Point::new(1, 6))..snapshot.anchor_after(Point::new(1, 6))
3817        });
3818        let prompt_builder = Arc::new(PromptBuilder::new(None).unwrap());
3819        let codegen = cx.new(|cx| {
3820            CodegenAlternative::new(
3821                buffer.clone(),
3822                range.clone(),
3823                true,
3824                None,
3825                prompt_builder,
3826                cx,
3827            )
3828        });
3829
3830        let chunks_tx = simulate_response_stream(codegen.clone(), cx);
3831
3832        cx.background_executor.run_until_parked();
3833
3834        let mut new_text = concat!(
3835            "t mut x = 0;\n",
3836            "while x < 10 {\n",
3837            "    x += 1;\n",
3838            "}", //
3839        );
3840        while !new_text.is_empty() {
3841            let max_len = cmp::min(new_text.len(), 10);
3842            let len = rng.gen_range(1..=max_len);
3843            let (chunk, suffix) = new_text.split_at(len);
3844            chunks_tx.unbounded_send(chunk.to_string()).unwrap();
3845            new_text = suffix;
3846            cx.background_executor.run_until_parked();
3847        }
3848        drop(chunks_tx);
3849        cx.background_executor.run_until_parked();
3850
3851        assert_eq!(
3852            buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()),
3853            indoc! {"
3854                fn main() {
3855                    let mut x = 0;
3856                    while x < 10 {
3857                        x += 1;
3858                    }
3859                }
3860            "}
3861        );
3862    }
3863
3864    #[gpui::test(iterations = 10)]
3865    async fn test_autoindent_when_generating_before_indentation(
3866        cx: &mut TestAppContext,
3867        mut rng: StdRng,
3868    ) {
3869        cx.update(LanguageModelRegistry::test);
3870        cx.set_global(cx.update(SettingsStore::test));
3871        cx.update(language_settings::init);
3872
3873        let text = concat!(
3874            "fn main() {\n",
3875            "  \n",
3876            "}\n" //
3877        );
3878        let buffer = cx.new(|cx| Buffer::local(text, cx).with_language(Arc::new(rust_lang()), cx));
3879        let buffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx));
3880        let range = buffer.read_with(cx, |buffer, cx| {
3881            let snapshot = buffer.snapshot(cx);
3882            snapshot.anchor_before(Point::new(1, 2))..snapshot.anchor_after(Point::new(1, 2))
3883        });
3884        let prompt_builder = Arc::new(PromptBuilder::new(None).unwrap());
3885        let codegen = cx.new(|cx| {
3886            CodegenAlternative::new(
3887                buffer.clone(),
3888                range.clone(),
3889                true,
3890                None,
3891                prompt_builder,
3892                cx,
3893            )
3894        });
3895
3896        let chunks_tx = simulate_response_stream(codegen.clone(), cx);
3897
3898        cx.background_executor.run_until_parked();
3899
3900        let mut new_text = concat!(
3901            "let mut x = 0;\n",
3902            "while x < 10 {\n",
3903            "    x += 1;\n",
3904            "}", //
3905        );
3906        while !new_text.is_empty() {
3907            let max_len = cmp::min(new_text.len(), 10);
3908            let len = rng.gen_range(1..=max_len);
3909            let (chunk, suffix) = new_text.split_at(len);
3910            chunks_tx.unbounded_send(chunk.to_string()).unwrap();
3911            new_text = suffix;
3912            cx.background_executor.run_until_parked();
3913        }
3914        drop(chunks_tx);
3915        cx.background_executor.run_until_parked();
3916
3917        assert_eq!(
3918            buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()),
3919            indoc! {"
3920                fn main() {
3921                    let mut x = 0;
3922                    while x < 10 {
3923                        x += 1;
3924                    }
3925                }
3926            "}
3927        );
3928    }
3929
3930    #[gpui::test(iterations = 10)]
3931    async fn test_autoindent_respects_tabs_in_selection(cx: &mut TestAppContext) {
3932        cx.update(LanguageModelRegistry::test);
3933        cx.set_global(cx.update(SettingsStore::test));
3934        cx.update(language_settings::init);
3935
3936        let text = indoc! {"
3937            func main() {
3938            \tx := 0
3939            \tfor i := 0; i < 10; i++ {
3940            \t\tx++
3941            \t}
3942            }
3943        "};
3944        let buffer = cx.new(|cx| Buffer::local(text, cx));
3945        let buffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx));
3946        let range = buffer.read_with(cx, |buffer, cx| {
3947            let snapshot = buffer.snapshot(cx);
3948            snapshot.anchor_before(Point::new(0, 0))..snapshot.anchor_after(Point::new(4, 2))
3949        });
3950        let prompt_builder = Arc::new(PromptBuilder::new(None).unwrap());
3951        let codegen = cx.new(|cx| {
3952            CodegenAlternative::new(
3953                buffer.clone(),
3954                range.clone(),
3955                true,
3956                None,
3957                prompt_builder,
3958                cx,
3959            )
3960        });
3961
3962        let chunks_tx = simulate_response_stream(codegen.clone(), cx);
3963        let new_text = concat!(
3964            "func main() {\n",
3965            "\tx := 0\n",
3966            "\tfor x < 10 {\n",
3967            "\t\tx++\n",
3968            "\t}", //
3969        );
3970        chunks_tx.unbounded_send(new_text.to_string()).unwrap();
3971        drop(chunks_tx);
3972        cx.background_executor.run_until_parked();
3973
3974        assert_eq!(
3975            buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()),
3976            indoc! {"
3977                func main() {
3978                \tx := 0
3979                \tfor x < 10 {
3980                \t\tx++
3981                \t}
3982                }
3983            "}
3984        );
3985    }
3986
3987    #[gpui::test]
3988    async fn test_inactive_codegen_alternative(cx: &mut TestAppContext) {
3989        cx.update(LanguageModelRegistry::test);
3990        cx.set_global(cx.update(SettingsStore::test));
3991        cx.update(language_settings::init);
3992
3993        let text = indoc! {"
3994            fn main() {
3995                let x = 0;
3996            }
3997        "};
3998        let buffer = cx.new(|cx| Buffer::local(text, cx).with_language(Arc::new(rust_lang()), cx));
3999        let buffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx));
4000        let range = buffer.read_with(cx, |buffer, cx| {
4001            let snapshot = buffer.snapshot(cx);
4002            snapshot.anchor_before(Point::new(1, 0))..snapshot.anchor_after(Point::new(1, 14))
4003        });
4004        let prompt_builder = Arc::new(PromptBuilder::new(None).unwrap());
4005        let codegen = cx.new(|cx| {
4006            CodegenAlternative::new(
4007                buffer.clone(),
4008                range.clone(),
4009                false,
4010                None,
4011                prompt_builder,
4012                cx,
4013            )
4014        });
4015
4016        let chunks_tx = simulate_response_stream(codegen.clone(), cx);
4017        chunks_tx
4018            .unbounded_send("let mut x = 0;\nx += 1;".to_string())
4019            .unwrap();
4020        drop(chunks_tx);
4021        cx.run_until_parked();
4022
4023        // The codegen is inactive, so the buffer doesn't get modified.
4024        assert_eq!(
4025            buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()),
4026            text
4027        );
4028
4029        // Activating the codegen applies the changes.
4030        codegen.update(cx, |codegen, cx| codegen.set_active(true, cx));
4031        assert_eq!(
4032            buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()),
4033            indoc! {"
4034                fn main() {
4035                    let mut x = 0;
4036                    x += 1;
4037                }
4038            "}
4039        );
4040
4041        // Deactivating the codegen undoes the changes.
4042        codegen.update(cx, |codegen, cx| codegen.set_active(false, cx));
4043        cx.run_until_parked();
4044        assert_eq!(
4045            buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()),
4046            text
4047        );
4048    }
4049
4050    #[gpui::test]
4051    async fn test_strip_invalid_spans_from_codeblock() {
4052        assert_chunks("Lorem ipsum dolor", "Lorem ipsum dolor").await;
4053        assert_chunks("```\nLorem ipsum dolor", "Lorem ipsum dolor").await;
4054        assert_chunks("```\nLorem ipsum dolor\n```", "Lorem ipsum dolor").await;
4055        assert_chunks(
4056            "```html\n```js\nLorem ipsum dolor\n```\n```",
4057            "```js\nLorem ipsum dolor\n```",
4058        )
4059        .await;
4060        assert_chunks("``\nLorem ipsum dolor\n```", "``\nLorem ipsum dolor\n```").await;
4061        assert_chunks("Lorem<|CURSOR|> ipsum", "Lorem ipsum").await;
4062        assert_chunks("Lorem ipsum", "Lorem ipsum").await;
4063        assert_chunks("```\n<|CURSOR|>Lorem ipsum\n```", "Lorem ipsum").await;
4064
4065        async fn assert_chunks(text: &str, expected_text: &str) {
4066            for chunk_size in 1..=text.len() {
4067                let actual_text = StripInvalidSpans::new(chunks(text, chunk_size))
4068                    .map(|chunk| chunk.unwrap())
4069                    .collect::<String>()
4070                    .await;
4071                assert_eq!(
4072                    actual_text, expected_text,
4073                    "failed to strip invalid spans, chunk size: {}",
4074                    chunk_size
4075                );
4076            }
4077        }
4078
4079        fn chunks(text: &str, size: usize) -> impl Stream<Item = Result<String>> {
4080            stream::iter(
4081                text.chars()
4082                    .collect::<Vec<_>>()
4083                    .chunks(size)
4084                    .map(|chunk| Ok(chunk.iter().collect::<String>()))
4085                    .collect::<Vec<_>>(),
4086            )
4087        }
4088    }
4089
4090    fn simulate_response_stream(
4091        codegen: Entity<CodegenAlternative>,
4092        cx: &mut TestAppContext,
4093    ) -> mpsc::UnboundedSender<String> {
4094        let (chunks_tx, chunks_rx) = mpsc::unbounded();
4095        codegen.update(cx, |codegen, cx| {
4096            codegen.handle_stream(
4097                String::new(),
4098                String::new(),
4099                None,
4100                future::ready(Ok(LanguageModelTextStream {
4101                    message_id: None,
4102                    stream: chunks_rx.map(Ok).boxed(),
4103                    last_token_usage: Arc::new(Mutex::new(TokenUsage::default())),
4104                })),
4105                cx,
4106            );
4107        });
4108        chunks_tx
4109    }
4110
4111    fn rust_lang() -> Language {
4112        Language::new(
4113            LanguageConfig {
4114                name: "Rust".into(),
4115                matcher: LanguageMatcher {
4116                    path_suffixes: vec!["rs".to_string()],
4117                    ..Default::default()
4118                },
4119                ..Default::default()
4120            },
4121            Some(tree_sitter_rust::LANGUAGE.into()),
4122        )
4123        .with_indents_query(
4124            r#"
4125            (call_expression) @indent
4126            (field_expression) @indent
4127            (_ "(" ")" @end) @indent
4128            (_ "{" "}" @end) @indent
4129            "#,
4130        )
4131        .unwrap()
4132    }
4133}