inline_assistant.rs

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