inline_assistant.rs

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