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