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