inline_assistant.rs

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