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