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