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, LanguageModelSelectorPopoverMenu};
  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    editor: View<Editor>,
1362    language_model_selector: View<LanguageModelSelector>,
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(LanguageModelSelectorPopoverMenu::new(
1504                        self.language_model_selector.clone(),
1505                        IconButton::new("context", IconName::SettingsAlt)
1506                            .shape(IconButtonShape::Square)
1507                            .icon_size(IconSize::Small)
1508                            .icon_color(Color::Muted)
1509                            .tooltip(move |cx| {
1510                                Tooltip::with_meta(
1511                                    format!(
1512                                        "Using {}",
1513                                        LanguageModelRegistry::read_global(cx)
1514                                            .active_model()
1515                                            .map(|model| model.name().0)
1516                                            .unwrap_or_else(|| "No model selected".into()),
1517                                    ),
1518                                    None,
1519                                    "Change Model",
1520                                    cx,
1521                                )
1522                            }),
1523                    ))
1524                    .map(|el| {
1525                        let CodegenStatus::Error(error) = self.codegen.read(cx).status(cx) else {
1526                            return el;
1527                        };
1528
1529                        let error_message = SharedString::from(error.to_string());
1530                        if error.error_code() == proto::ErrorCode::RateLimitExceeded
1531                            && cx.has_flag::<ZedPro>()
1532                        {
1533                            el.child(
1534                                v_flex()
1535                                    .child(
1536                                        IconButton::new("rate-limit-error", IconName::XCircle)
1537                                            .toggle_state(self.show_rate_limit_notice)
1538                                            .shape(IconButtonShape::Square)
1539                                            .icon_size(IconSize::Small)
1540                                            .on_click(cx.listener(Self::toggle_rate_limit_notice)),
1541                                    )
1542                                    .children(self.show_rate_limit_notice.then(|| {
1543                                        deferred(
1544                                            anchored()
1545                                                .position_mode(gpui::AnchoredPositionMode::Local)
1546                                                .position(point(px(0.), px(24.)))
1547                                                .anchor(gpui::AnchorCorner::TopLeft)
1548                                                .child(self.render_rate_limit_notice(cx)),
1549                                        )
1550                                    })),
1551                            )
1552                        } else {
1553                            el.child(
1554                                div()
1555                                    .id("error")
1556                                    .tooltip(move |cx| Tooltip::text(error_message.clone(), cx))
1557                                    .child(
1558                                        Icon::new(IconName::XCircle)
1559                                            .size(IconSize::Small)
1560                                            .color(Color::Error),
1561                                    ),
1562                            )
1563                        }
1564                    }),
1565            )
1566            .child(div().flex_1().child(self.render_prompt_editor(cx)))
1567            .child(
1568                h_flex()
1569                    .gap_2()
1570                    .pr_6()
1571                    .children(self.render_token_count(cx))
1572                    .children(buttons),
1573            )
1574    }
1575}
1576
1577impl FocusableView for PromptEditor {
1578    fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
1579        self.editor.focus_handle(cx)
1580    }
1581}
1582
1583impl PromptEditor {
1584    const MAX_LINES: u8 = 8;
1585
1586    #[allow(clippy::too_many_arguments)]
1587    fn new(
1588        id: InlineAssistId,
1589        gutter_dimensions: Arc<Mutex<GutterDimensions>>,
1590        prompt_history: VecDeque<String>,
1591        prompt_buffer: Model<MultiBuffer>,
1592        codegen: Model<Codegen>,
1593        parent_editor: &View<Editor>,
1594        assistant_panel: Option<&View<AssistantPanel>>,
1595        workspace: Option<WeakView<Workspace>>,
1596        fs: Arc<dyn Fs>,
1597        cx: &mut ViewContext<Self>,
1598    ) -> Self {
1599        let prompt_editor = cx.new_view(|cx| {
1600            let mut editor = Editor::new(
1601                EditorMode::AutoHeight {
1602                    max_lines: Self::MAX_LINES as usize,
1603                },
1604                prompt_buffer,
1605                None,
1606                false,
1607                cx,
1608            );
1609            editor.set_soft_wrap_mode(language::language_settings::SoftWrap::EditorWidth, cx);
1610            // Since the prompt editors for all inline assistants are linked,
1611            // always show the cursor (even when it isn't focused) because
1612            // typing in one will make what you typed appear in all of them.
1613            editor.set_show_cursor_when_unfocused(true, cx);
1614            editor.set_placeholder_text(Self::placeholder_text(codegen.read(cx), cx), cx);
1615            editor
1616        });
1617
1618        let mut token_count_subscriptions = Vec::new();
1619        token_count_subscriptions
1620            .push(cx.subscribe(parent_editor, Self::handle_parent_editor_event));
1621        if let Some(assistant_panel) = assistant_panel {
1622            token_count_subscriptions
1623                .push(cx.subscribe(assistant_panel, Self::handle_assistant_panel_event));
1624        }
1625
1626        let mut this = Self {
1627            id,
1628            editor: prompt_editor,
1629            language_model_selector: cx.new_view(|cx| {
1630                let fs = fs.clone();
1631                LanguageModelSelector::new(
1632                    move |model, cx| {
1633                        update_settings_file::<AssistantSettings>(
1634                            fs.clone(),
1635                            cx,
1636                            move |settings, _| settings.set_model(model.clone()),
1637                        );
1638                    },
1639                    cx,
1640                )
1641            }),
1642            edited_since_done: false,
1643            gutter_dimensions,
1644            prompt_history,
1645            prompt_history_ix: None,
1646            pending_prompt: String::new(),
1647            _codegen_subscription: cx.observe(&codegen, Self::handle_codegen_changed),
1648            editor_subscriptions: Vec::new(),
1649            codegen,
1650            pending_token_count: Task::ready(Ok(())),
1651            token_counts: None,
1652            _token_count_subscriptions: token_count_subscriptions,
1653            workspace,
1654            show_rate_limit_notice: false,
1655        };
1656        this.count_tokens(cx);
1657        this.subscribe_to_editor(cx);
1658        this
1659    }
1660
1661    fn subscribe_to_editor(&mut self, cx: &mut ViewContext<Self>) {
1662        self.editor_subscriptions.clear();
1663        self.editor_subscriptions
1664            .push(cx.subscribe(&self.editor, Self::handle_prompt_editor_events));
1665    }
1666
1667    fn set_show_cursor_when_unfocused(
1668        &mut self,
1669        show_cursor_when_unfocused: bool,
1670        cx: &mut ViewContext<Self>,
1671    ) {
1672        self.editor.update(cx, |editor, cx| {
1673            editor.set_show_cursor_when_unfocused(show_cursor_when_unfocused, cx)
1674        });
1675    }
1676
1677    fn unlink(&mut self, cx: &mut ViewContext<Self>) {
1678        let prompt = self.prompt(cx);
1679        let focus = self.editor.focus_handle(cx).contains_focused(cx);
1680        self.editor = cx.new_view(|cx| {
1681            let mut editor = Editor::auto_height(Self::MAX_LINES as usize, cx);
1682            editor.set_soft_wrap_mode(language::language_settings::SoftWrap::EditorWidth, cx);
1683            editor.set_placeholder_text(Self::placeholder_text(self.codegen.read(cx), cx), cx);
1684            editor.set_placeholder_text("Add a prompt…", cx);
1685            editor.set_text(prompt, cx);
1686            if focus {
1687                editor.focus(cx);
1688            }
1689            editor
1690        });
1691        self.subscribe_to_editor(cx);
1692    }
1693
1694    fn placeholder_text(codegen: &Codegen, cx: &WindowContext) -> String {
1695        let context_keybinding = text_for_action(&crate::ToggleFocus, cx)
1696            .map(|keybinding| format!("{keybinding} for context"))
1697            .unwrap_or_default();
1698
1699        let action = if codegen.is_insertion {
1700            "Generate"
1701        } else {
1702            "Transform"
1703        };
1704
1705        format!("{action}{context_keybinding} • ↓↑ for history")
1706    }
1707
1708    fn prompt(&self, cx: &AppContext) -> String {
1709        self.editor.read(cx).text(cx)
1710    }
1711
1712    fn toggle_rate_limit_notice(&mut self, _: &ClickEvent, cx: &mut ViewContext<Self>) {
1713        self.show_rate_limit_notice = !self.show_rate_limit_notice;
1714        if self.show_rate_limit_notice {
1715            cx.focus_view(&self.editor);
1716        }
1717        cx.notify();
1718    }
1719
1720    fn handle_parent_editor_event(
1721        &mut self,
1722        _: View<Editor>,
1723        event: &EditorEvent,
1724        cx: &mut ViewContext<Self>,
1725    ) {
1726        if let EditorEvent::BufferEdited { .. } = event {
1727            self.count_tokens(cx);
1728        }
1729    }
1730
1731    fn handle_assistant_panel_event(
1732        &mut self,
1733        _: View<AssistantPanel>,
1734        event: &AssistantPanelEvent,
1735        cx: &mut ViewContext<Self>,
1736    ) {
1737        let AssistantPanelEvent::ContextEdited { .. } = event;
1738        self.count_tokens(cx);
1739    }
1740
1741    fn count_tokens(&mut self, cx: &mut ViewContext<Self>) {
1742        let assist_id = self.id;
1743        self.pending_token_count = cx.spawn(|this, mut cx| async move {
1744            cx.background_executor().timer(Duration::from_secs(1)).await;
1745            let token_count = cx
1746                .update_global(|inline_assistant: &mut InlineAssistant, cx| {
1747                    let assist = inline_assistant
1748                        .assists
1749                        .get(&assist_id)
1750                        .context("assist not found")?;
1751                    anyhow::Ok(assist.count_tokens(cx))
1752                })??
1753                .await?;
1754
1755            this.update(&mut cx, |this, cx| {
1756                this.token_counts = Some(token_count);
1757                cx.notify();
1758            })
1759        })
1760    }
1761
1762    fn handle_prompt_editor_events(
1763        &mut self,
1764        _: View<Editor>,
1765        event: &EditorEvent,
1766        cx: &mut ViewContext<Self>,
1767    ) {
1768        match event {
1769            EditorEvent::Edited { .. } => {
1770                if let Some(workspace) = cx.window_handle().downcast::<Workspace>() {
1771                    workspace
1772                        .update(cx, |workspace, cx| {
1773                            let is_via_ssh = workspace
1774                                .project()
1775                                .update(cx, |project, _| project.is_via_ssh());
1776
1777                            workspace
1778                                .client()
1779                                .telemetry()
1780                                .log_edit_event("inline assist", is_via_ssh);
1781                        })
1782                        .log_err();
1783                }
1784                let prompt = self.editor.read(cx).text(cx);
1785                if self
1786                    .prompt_history_ix
1787                    .map_or(true, |ix| self.prompt_history[ix] != prompt)
1788                {
1789                    self.prompt_history_ix.take();
1790                    self.pending_prompt = prompt;
1791                }
1792
1793                self.edited_since_done = true;
1794                cx.notify();
1795            }
1796            EditorEvent::BufferEdited => {
1797                self.count_tokens(cx);
1798            }
1799            EditorEvent::Blurred => {
1800                if self.show_rate_limit_notice {
1801                    self.show_rate_limit_notice = false;
1802                    cx.notify();
1803                }
1804            }
1805            _ => {}
1806        }
1807    }
1808
1809    fn handle_codegen_changed(&mut self, _: Model<Codegen>, cx: &mut ViewContext<Self>) {
1810        match self.codegen.read(cx).status(cx) {
1811            CodegenStatus::Idle => {
1812                self.editor
1813                    .update(cx, |editor, _| editor.set_read_only(false));
1814            }
1815            CodegenStatus::Pending => {
1816                self.editor
1817                    .update(cx, |editor, _| editor.set_read_only(true));
1818            }
1819            CodegenStatus::Done => {
1820                self.edited_since_done = false;
1821                self.editor
1822                    .update(cx, |editor, _| editor.set_read_only(false));
1823            }
1824            CodegenStatus::Error(error) => {
1825                if cx.has_flag::<ZedPro>()
1826                    && error.error_code() == proto::ErrorCode::RateLimitExceeded
1827                    && !dismissed_rate_limit_notice()
1828                {
1829                    self.show_rate_limit_notice = true;
1830                    cx.notify();
1831                }
1832
1833                self.edited_since_done = false;
1834                self.editor
1835                    .update(cx, |editor, _| editor.set_read_only(false));
1836            }
1837        }
1838    }
1839
1840    fn cancel(&mut self, _: &editor::actions::Cancel, cx: &mut ViewContext<Self>) {
1841        match self.codegen.read(cx).status(cx) {
1842            CodegenStatus::Idle | CodegenStatus::Done | CodegenStatus::Error(_) => {
1843                cx.emit(PromptEditorEvent::CancelRequested);
1844            }
1845            CodegenStatus::Pending => {
1846                cx.emit(PromptEditorEvent::StopRequested);
1847            }
1848        }
1849    }
1850
1851    fn confirm(&mut self, _: &menu::Confirm, cx: &mut ViewContext<Self>) {
1852        match self.codegen.read(cx).status(cx) {
1853            CodegenStatus::Idle => {
1854                cx.emit(PromptEditorEvent::StartRequested);
1855            }
1856            CodegenStatus::Pending => {
1857                cx.emit(PromptEditorEvent::DismissRequested);
1858            }
1859            CodegenStatus::Done => {
1860                if self.edited_since_done {
1861                    cx.emit(PromptEditorEvent::StartRequested);
1862                } else {
1863                    cx.emit(PromptEditorEvent::ConfirmRequested);
1864                }
1865            }
1866            CodegenStatus::Error(_) => {
1867                cx.emit(PromptEditorEvent::StartRequested);
1868            }
1869        }
1870    }
1871
1872    fn move_up(&mut self, _: &MoveUp, cx: &mut ViewContext<Self>) {
1873        if let Some(ix) = self.prompt_history_ix {
1874            if ix > 0 {
1875                self.prompt_history_ix = Some(ix - 1);
1876                let prompt = self.prompt_history[ix - 1].as_str();
1877                self.editor.update(cx, |editor, cx| {
1878                    editor.set_text(prompt, cx);
1879                    editor.move_to_beginning(&Default::default(), cx);
1880                });
1881            }
1882        } else if !self.prompt_history.is_empty() {
1883            self.prompt_history_ix = Some(self.prompt_history.len() - 1);
1884            let prompt = self.prompt_history[self.prompt_history.len() - 1].as_str();
1885            self.editor.update(cx, |editor, cx| {
1886                editor.set_text(prompt, cx);
1887                editor.move_to_beginning(&Default::default(), cx);
1888            });
1889        }
1890    }
1891
1892    fn move_down(&mut self, _: &MoveDown, cx: &mut ViewContext<Self>) {
1893        if let Some(ix) = self.prompt_history_ix {
1894            if ix < self.prompt_history.len() - 1 {
1895                self.prompt_history_ix = Some(ix + 1);
1896                let prompt = self.prompt_history[ix + 1].as_str();
1897                self.editor.update(cx, |editor, cx| {
1898                    editor.set_text(prompt, cx);
1899                    editor.move_to_end(&Default::default(), cx)
1900                });
1901            } else {
1902                self.prompt_history_ix = None;
1903                let prompt = self.pending_prompt.as_str();
1904                self.editor.update(cx, |editor, cx| {
1905                    editor.set_text(prompt, cx);
1906                    editor.move_to_end(&Default::default(), cx)
1907                });
1908            }
1909        }
1910    }
1911
1912    fn cycle_prev(&mut self, _: &CyclePreviousInlineAssist, cx: &mut ViewContext<Self>) {
1913        self.codegen
1914            .update(cx, |codegen, cx| codegen.cycle_prev(cx));
1915    }
1916
1917    fn cycle_next(&mut self, _: &CycleNextInlineAssist, cx: &mut ViewContext<Self>) {
1918        self.codegen
1919            .update(cx, |codegen, cx| codegen.cycle_next(cx));
1920    }
1921
1922    fn render_cycle_controls(&self, cx: &ViewContext<Self>) -> AnyElement {
1923        let codegen = self.codegen.read(cx);
1924        let disabled = matches!(codegen.status(cx), CodegenStatus::Idle);
1925
1926        let model_registry = LanguageModelRegistry::read_global(cx);
1927        let default_model = model_registry.active_model();
1928        let alternative_models = model_registry.inline_alternative_models();
1929
1930        let get_model_name = |index: usize| -> String {
1931            let name = |model: &Arc<dyn LanguageModel>| model.name().0.to_string();
1932
1933            match index {
1934                0 => default_model.as_ref().map_or_else(String::new, name),
1935                index if index <= alternative_models.len() => alternative_models
1936                    .get(index - 1)
1937                    .map_or_else(String::new, name),
1938                _ => String::new(),
1939            }
1940        };
1941
1942        let total_models = alternative_models.len() + 1;
1943
1944        if total_models <= 1 {
1945            return div().into_any_element();
1946        }
1947
1948        let current_index = codegen.active_alternative;
1949        let prev_index = (current_index + total_models - 1) % total_models;
1950        let next_index = (current_index + 1) % total_models;
1951
1952        let prev_model_name = get_model_name(prev_index);
1953        let next_model_name = get_model_name(next_index);
1954
1955        h_flex()
1956            .child(
1957                IconButton::new("previous", IconName::ChevronLeft)
1958                    .icon_color(Color::Muted)
1959                    .disabled(disabled || current_index == 0)
1960                    .shape(IconButtonShape::Square)
1961                    .tooltip({
1962                        let focus_handle = self.editor.focus_handle(cx);
1963                        move |cx| {
1964                            cx.new_view(|cx| {
1965                                let mut tooltip = Tooltip::new("Previous Alternative").key_binding(
1966                                    KeyBinding::for_action_in(
1967                                        &CyclePreviousInlineAssist,
1968                                        &focus_handle,
1969                                        cx,
1970                                    ),
1971                                );
1972                                if !disabled && current_index != 0 {
1973                                    tooltip = tooltip.meta(prev_model_name.clone());
1974                                }
1975                                tooltip
1976                            })
1977                            .into()
1978                        }
1979                    })
1980                    .on_click(cx.listener(|this, _, cx| {
1981                        this.codegen
1982                            .update(cx, |codegen, cx| codegen.cycle_prev(cx))
1983                    })),
1984            )
1985            .child(
1986                Label::new(format!(
1987                    "{}/{}",
1988                    codegen.active_alternative + 1,
1989                    codegen.alternative_count(cx)
1990                ))
1991                .size(LabelSize::Small)
1992                .color(if disabled {
1993                    Color::Disabled
1994                } else {
1995                    Color::Muted
1996                }),
1997            )
1998            .child(
1999                IconButton::new("next", IconName::ChevronRight)
2000                    .icon_color(Color::Muted)
2001                    .disabled(disabled || current_index == total_models - 1)
2002                    .shape(IconButtonShape::Square)
2003                    .tooltip({
2004                        let focus_handle = self.editor.focus_handle(cx);
2005                        move |cx| {
2006                            cx.new_view(|cx| {
2007                                let mut tooltip = Tooltip::new("Next Alternative").key_binding(
2008                                    KeyBinding::for_action_in(
2009                                        &CycleNextInlineAssist,
2010                                        &focus_handle,
2011                                        cx,
2012                                    ),
2013                                );
2014                                if !disabled && current_index != total_models - 1 {
2015                                    tooltip = tooltip.meta(next_model_name.clone());
2016                                }
2017                                tooltip
2018                            })
2019                            .into()
2020                        }
2021                    })
2022                    .on_click(cx.listener(|this, _, cx| {
2023                        this.codegen
2024                            .update(cx, |codegen, cx| codegen.cycle_next(cx))
2025                    })),
2026            )
2027            .into_any_element()
2028    }
2029
2030    fn render_token_count(&self, cx: &mut ViewContext<Self>) -> Option<impl IntoElement> {
2031        let model = LanguageModelRegistry::read_global(cx).active_model()?;
2032        let token_counts = self.token_counts?;
2033        let max_token_count = model.max_token_count();
2034
2035        let remaining_tokens = max_token_count as isize - token_counts.total as isize;
2036        let token_count_color = if remaining_tokens <= 0 {
2037            Color::Error
2038        } else if token_counts.total as f32 / max_token_count as f32 >= 0.8 {
2039            Color::Warning
2040        } else {
2041            Color::Muted
2042        };
2043
2044        let mut token_count = h_flex()
2045            .id("token_count")
2046            .gap_0p5()
2047            .child(
2048                Label::new(humanize_token_count(token_counts.total))
2049                    .size(LabelSize::Small)
2050                    .color(token_count_color),
2051            )
2052            .child(Label::new("/").size(LabelSize::Small).color(Color::Muted))
2053            .child(
2054                Label::new(humanize_token_count(max_token_count))
2055                    .size(LabelSize::Small)
2056                    .color(Color::Muted),
2057            );
2058        if let Some(workspace) = self.workspace.clone() {
2059            token_count = token_count
2060                .tooltip(move |cx| {
2061                    Tooltip::with_meta(
2062                        format!(
2063                            "Tokens Used ({} from the Assistant Panel)",
2064                            humanize_token_count(token_counts.assistant_panel)
2065                        ),
2066                        None,
2067                        "Click to open the Assistant Panel",
2068                        cx,
2069                    )
2070                })
2071                .cursor_pointer()
2072                .on_mouse_down(gpui::MouseButton::Left, |_, cx| cx.stop_propagation())
2073                .on_click(move |_, cx| {
2074                    cx.stop_propagation();
2075                    workspace
2076                        .update(cx, |workspace, cx| {
2077                            workspace.focus_panel::<AssistantPanel>(cx)
2078                        })
2079                        .ok();
2080                });
2081        } else {
2082            token_count = token_count
2083                .cursor_default()
2084                .tooltip(|cx| Tooltip::text("Tokens used", cx));
2085        }
2086
2087        Some(token_count)
2088    }
2089
2090    fn render_prompt_editor(&self, cx: &mut ViewContext<Self>) -> impl IntoElement {
2091        let settings = ThemeSettings::get_global(cx);
2092        let text_style = TextStyle {
2093            color: if self.editor.read(cx).read_only(cx) {
2094                cx.theme().colors().text_disabled
2095            } else {
2096                cx.theme().colors().text
2097            },
2098            font_family: settings.buffer_font.family.clone(),
2099            font_fallbacks: settings.buffer_font.fallbacks.clone(),
2100            font_size: settings.buffer_font_size.into(),
2101            font_weight: settings.buffer_font.weight,
2102            line_height: relative(settings.buffer_line_height.value()),
2103            ..Default::default()
2104        };
2105        EditorElement::new(
2106            &self.editor,
2107            EditorStyle {
2108                background: cx.theme().colors().editor_background,
2109                local_player: cx.theme().players().local(),
2110                text: text_style,
2111                ..Default::default()
2112            },
2113        )
2114    }
2115
2116    fn render_rate_limit_notice(&self, cx: &mut ViewContext<Self>) -> impl IntoElement {
2117        Popover::new().child(
2118            v_flex()
2119                .occlude()
2120                .p_2()
2121                .child(
2122                    Label::new("Out of Tokens")
2123                        .size(LabelSize::Small)
2124                        .weight(FontWeight::BOLD),
2125                )
2126                .child(Label::new(
2127                    "Try Zed Pro for higher limits, a wider range of models, and more.",
2128                ))
2129                .child(
2130                    h_flex()
2131                        .justify_between()
2132                        .child(CheckboxWithLabel::new(
2133                            "dont-show-again",
2134                            Label::new("Don't show again"),
2135                            if dismissed_rate_limit_notice() {
2136                                ui::ToggleState::Selected
2137                            } else {
2138                                ui::ToggleState::Unselected
2139                            },
2140                            |selection, cx| {
2141                                let is_dismissed = match selection {
2142                                    ui::ToggleState::Unselected => false,
2143                                    ui::ToggleState::Indeterminate => return,
2144                                    ui::ToggleState::Selected => true,
2145                                };
2146
2147                                set_rate_limit_notice_dismissed(is_dismissed, cx)
2148                            },
2149                        ))
2150                        .child(
2151                            h_flex()
2152                                .gap_2()
2153                                .child(
2154                                    Button::new("dismiss", "Dismiss")
2155                                        .style(ButtonStyle::Transparent)
2156                                        .on_click(cx.listener(Self::toggle_rate_limit_notice)),
2157                                )
2158                                .child(Button::new("more-info", "More Info").on_click(
2159                                    |_event, cx| {
2160                                        cx.dispatch_action(Box::new(
2161                                            zed_actions::OpenAccountSettings,
2162                                        ))
2163                                    },
2164                                )),
2165                        ),
2166                ),
2167        )
2168    }
2169}
2170
2171const DISMISSED_RATE_LIMIT_NOTICE_KEY: &str = "dismissed-rate-limit-notice";
2172
2173fn dismissed_rate_limit_notice() -> bool {
2174    db::kvp::KEY_VALUE_STORE
2175        .read_kvp(DISMISSED_RATE_LIMIT_NOTICE_KEY)
2176        .log_err()
2177        .map_or(false, |s| s.is_some())
2178}
2179
2180fn set_rate_limit_notice_dismissed(is_dismissed: bool, cx: &mut AppContext) {
2181    db::write_and_log(cx, move || async move {
2182        if is_dismissed {
2183            db::kvp::KEY_VALUE_STORE
2184                .write_kvp(DISMISSED_RATE_LIMIT_NOTICE_KEY.into(), "1".into())
2185                .await
2186        } else {
2187            db::kvp::KEY_VALUE_STORE
2188                .delete_kvp(DISMISSED_RATE_LIMIT_NOTICE_KEY.into())
2189                .await
2190        }
2191    })
2192}
2193
2194struct InlineAssist {
2195    group_id: InlineAssistGroupId,
2196    range: Range<Anchor>,
2197    editor: WeakView<Editor>,
2198    decorations: Option<InlineAssistDecorations>,
2199    codegen: Model<Codegen>,
2200    _subscriptions: Vec<Subscription>,
2201    workspace: Option<WeakView<Workspace>>,
2202    include_context: bool,
2203}
2204
2205impl InlineAssist {
2206    #[allow(clippy::too_many_arguments)]
2207    fn new(
2208        assist_id: InlineAssistId,
2209        group_id: InlineAssistGroupId,
2210        include_context: bool,
2211        editor: &View<Editor>,
2212        prompt_editor: &View<PromptEditor>,
2213        prompt_block_id: CustomBlockId,
2214        end_block_id: CustomBlockId,
2215        range: Range<Anchor>,
2216        codegen: Model<Codegen>,
2217        workspace: Option<WeakView<Workspace>>,
2218        cx: &mut WindowContext,
2219    ) -> Self {
2220        let prompt_editor_focus_handle = prompt_editor.focus_handle(cx);
2221        InlineAssist {
2222            group_id,
2223            include_context,
2224            editor: editor.downgrade(),
2225            decorations: Some(InlineAssistDecorations {
2226                prompt_block_id,
2227                prompt_editor: prompt_editor.clone(),
2228                removed_line_block_ids: HashSet::default(),
2229                end_block_id,
2230            }),
2231            range,
2232            codegen: codegen.clone(),
2233            workspace: workspace.clone(),
2234            _subscriptions: vec![
2235                cx.on_focus_in(&prompt_editor_focus_handle, move |cx| {
2236                    InlineAssistant::update_global(cx, |this, cx| {
2237                        this.handle_prompt_editor_focus_in(assist_id, cx)
2238                    })
2239                }),
2240                cx.on_focus_out(&prompt_editor_focus_handle, move |_, cx| {
2241                    InlineAssistant::update_global(cx, |this, cx| {
2242                        this.handle_prompt_editor_focus_out(assist_id, cx)
2243                    })
2244                }),
2245                cx.subscribe(prompt_editor, |prompt_editor, event, cx| {
2246                    InlineAssistant::update_global(cx, |this, cx| {
2247                        this.handle_prompt_editor_event(prompt_editor, event, cx)
2248                    })
2249                }),
2250                cx.observe(&codegen, {
2251                    let editor = editor.downgrade();
2252                    move |_, cx| {
2253                        if let Some(editor) = editor.upgrade() {
2254                            InlineAssistant::update_global(cx, |this, cx| {
2255                                if let Some(editor_assists) =
2256                                    this.assists_by_editor.get(&editor.downgrade())
2257                                {
2258                                    editor_assists.highlight_updates.send(()).ok();
2259                                }
2260
2261                                this.update_editor_blocks(&editor, assist_id, cx);
2262                            })
2263                        }
2264                    }
2265                }),
2266                cx.subscribe(&codegen, move |codegen, event, cx| {
2267                    InlineAssistant::update_global(cx, |this, cx| match event {
2268                        CodegenEvent::Undone => this.finish_assist(assist_id, false, cx),
2269                        CodegenEvent::Finished => {
2270                            let assist = if let Some(assist) = this.assists.get(&assist_id) {
2271                                assist
2272                            } else {
2273                                return;
2274                            };
2275
2276                            if let CodegenStatus::Error(error) = codegen.read(cx).status(cx) {
2277                                if assist.decorations.is_none() {
2278                                    if let Some(workspace) = assist
2279                                        .workspace
2280                                        .as_ref()
2281                                        .and_then(|workspace| workspace.upgrade())
2282                                    {
2283                                        let error = format!("Inline assistant error: {}", error);
2284                                        workspace.update(cx, |workspace, cx| {
2285                                            struct InlineAssistantError;
2286
2287                                            let id =
2288                                                NotificationId::composite::<InlineAssistantError>(
2289                                                    assist_id.0,
2290                                                );
2291
2292                                            workspace.show_toast(Toast::new(id, error), cx);
2293                                        })
2294                                    }
2295                                }
2296                            }
2297
2298                            if assist.decorations.is_none() {
2299                                this.finish_assist(assist_id, false, cx);
2300                            }
2301                        }
2302                    })
2303                }),
2304            ],
2305        }
2306    }
2307
2308    fn user_prompt(&self, cx: &AppContext) -> Option<String> {
2309        let decorations = self.decorations.as_ref()?;
2310        Some(decorations.prompt_editor.read(cx).prompt(cx))
2311    }
2312
2313    fn assistant_panel_context(&self, cx: &WindowContext) -> Option<LanguageModelRequest> {
2314        if self.include_context {
2315            let workspace = self.workspace.as_ref()?;
2316            let workspace = workspace.upgrade()?.read(cx);
2317            let assistant_panel = workspace.panel::<AssistantPanel>(cx)?;
2318            Some(
2319                assistant_panel
2320                    .read(cx)
2321                    .active_context(cx)?
2322                    .read(cx)
2323                    .to_completion_request(RequestType::Chat, cx),
2324            )
2325        } else {
2326            None
2327        }
2328    }
2329
2330    pub fn count_tokens(&self, cx: &WindowContext) -> BoxFuture<'static, Result<TokenCounts>> {
2331        let Some(user_prompt) = self.user_prompt(cx) else {
2332            return future::ready(Err(anyhow!("no user prompt"))).boxed();
2333        };
2334        let assistant_panel_context = self.assistant_panel_context(cx);
2335        self.codegen
2336            .read(cx)
2337            .count_tokens(user_prompt, assistant_panel_context, cx)
2338    }
2339}
2340
2341struct InlineAssistDecorations {
2342    prompt_block_id: CustomBlockId,
2343    prompt_editor: View<PromptEditor>,
2344    removed_line_block_ids: HashSet<CustomBlockId>,
2345    end_block_id: CustomBlockId,
2346}
2347
2348#[derive(Copy, Clone, Debug)]
2349pub enum CodegenEvent {
2350    Finished,
2351    Undone,
2352}
2353
2354pub struct Codegen {
2355    alternatives: Vec<Model<CodegenAlternative>>,
2356    active_alternative: usize,
2357    seen_alternatives: HashSet<usize>,
2358    subscriptions: Vec<Subscription>,
2359    buffer: Model<MultiBuffer>,
2360    range: Range<Anchor>,
2361    initial_transaction_id: Option<TransactionId>,
2362    telemetry: Arc<Telemetry>,
2363    builder: Arc<PromptBuilder>,
2364    is_insertion: bool,
2365}
2366
2367impl Codegen {
2368    pub fn new(
2369        buffer: Model<MultiBuffer>,
2370        range: Range<Anchor>,
2371        initial_transaction_id: Option<TransactionId>,
2372        telemetry: Arc<Telemetry>,
2373        builder: Arc<PromptBuilder>,
2374        cx: &mut ModelContext<Self>,
2375    ) -> Self {
2376        let codegen = cx.new_model(|cx| {
2377            CodegenAlternative::new(
2378                buffer.clone(),
2379                range.clone(),
2380                false,
2381                Some(telemetry.clone()),
2382                builder.clone(),
2383                cx,
2384            )
2385        });
2386        let mut this = Self {
2387            is_insertion: range.to_offset(&buffer.read(cx).snapshot(cx)).is_empty(),
2388            alternatives: vec![codegen],
2389            active_alternative: 0,
2390            seen_alternatives: HashSet::default(),
2391            subscriptions: Vec::new(),
2392            buffer,
2393            range,
2394            initial_transaction_id,
2395            telemetry,
2396            builder,
2397        };
2398        this.activate(0, cx);
2399        this
2400    }
2401
2402    fn subscribe_to_alternative(&mut self, cx: &mut ModelContext<Self>) {
2403        let codegen = self.active_alternative().clone();
2404        self.subscriptions.clear();
2405        self.subscriptions
2406            .push(cx.observe(&codegen, |_, _, cx| cx.notify()));
2407        self.subscriptions
2408            .push(cx.subscribe(&codegen, |_, _, event, cx| cx.emit(*event)));
2409    }
2410
2411    fn active_alternative(&self) -> &Model<CodegenAlternative> {
2412        &self.alternatives[self.active_alternative]
2413    }
2414
2415    fn status<'a>(&self, cx: &'a AppContext) -> &'a CodegenStatus {
2416        &self.active_alternative().read(cx).status
2417    }
2418
2419    fn alternative_count(&self, cx: &AppContext) -> usize {
2420        LanguageModelRegistry::read_global(cx)
2421            .inline_alternative_models()
2422            .len()
2423            + 1
2424    }
2425
2426    pub fn cycle_prev(&mut self, cx: &mut ModelContext<Self>) {
2427        let next_active_ix = if self.active_alternative == 0 {
2428            self.alternatives.len() - 1
2429        } else {
2430            self.active_alternative - 1
2431        };
2432        self.activate(next_active_ix, cx);
2433    }
2434
2435    pub fn cycle_next(&mut self, cx: &mut ModelContext<Self>) {
2436        let next_active_ix = (self.active_alternative + 1) % self.alternatives.len();
2437        self.activate(next_active_ix, cx);
2438    }
2439
2440    fn activate(&mut self, index: usize, cx: &mut ModelContext<Self>) {
2441        self.active_alternative()
2442            .update(cx, |codegen, cx| codegen.set_active(false, cx));
2443        self.seen_alternatives.insert(index);
2444        self.active_alternative = index;
2445        self.active_alternative()
2446            .update(cx, |codegen, cx| codegen.set_active(true, cx));
2447        self.subscribe_to_alternative(cx);
2448        cx.notify();
2449    }
2450
2451    pub fn start(
2452        &mut self,
2453        user_prompt: String,
2454        assistant_panel_context: Option<LanguageModelRequest>,
2455        cx: &mut ModelContext<Self>,
2456    ) -> Result<()> {
2457        let alternative_models = LanguageModelRegistry::read_global(cx)
2458            .inline_alternative_models()
2459            .to_vec();
2460
2461        self.active_alternative()
2462            .update(cx, |alternative, cx| alternative.undo(cx));
2463        self.activate(0, cx);
2464        self.alternatives.truncate(1);
2465
2466        for _ in 0..alternative_models.len() {
2467            self.alternatives.push(cx.new_model(|cx| {
2468                CodegenAlternative::new(
2469                    self.buffer.clone(),
2470                    self.range.clone(),
2471                    false,
2472                    Some(self.telemetry.clone()),
2473                    self.builder.clone(),
2474                    cx,
2475                )
2476            }));
2477        }
2478
2479        let primary_model = LanguageModelRegistry::read_global(cx)
2480            .active_model()
2481            .context("no active model")?;
2482
2483        for (model, alternative) in iter::once(primary_model)
2484            .chain(alternative_models)
2485            .zip(&self.alternatives)
2486        {
2487            alternative.update(cx, |alternative, cx| {
2488                alternative.start(
2489                    user_prompt.clone(),
2490                    assistant_panel_context.clone(),
2491                    model.clone(),
2492                    cx,
2493                )
2494            })?;
2495        }
2496
2497        Ok(())
2498    }
2499
2500    pub fn stop(&mut self, cx: &mut ModelContext<Self>) {
2501        for codegen in &self.alternatives {
2502            codegen.update(cx, |codegen, cx| codegen.stop(cx));
2503        }
2504    }
2505
2506    pub fn undo(&mut self, cx: &mut ModelContext<Self>) {
2507        self.active_alternative()
2508            .update(cx, |codegen, cx| codegen.undo(cx));
2509
2510        self.buffer.update(cx, |buffer, cx| {
2511            if let Some(transaction_id) = self.initial_transaction_id.take() {
2512                buffer.undo_transaction(transaction_id, cx);
2513                buffer.refresh_preview(cx);
2514            }
2515        });
2516    }
2517
2518    pub fn count_tokens(
2519        &self,
2520        user_prompt: String,
2521        assistant_panel_context: Option<LanguageModelRequest>,
2522        cx: &AppContext,
2523    ) -> BoxFuture<'static, Result<TokenCounts>> {
2524        self.active_alternative()
2525            .read(cx)
2526            .count_tokens(user_prompt, assistant_panel_context, cx)
2527    }
2528
2529    pub fn buffer(&self, cx: &AppContext) -> Model<MultiBuffer> {
2530        self.active_alternative().read(cx).buffer.clone()
2531    }
2532
2533    pub fn old_buffer(&self, cx: &AppContext) -> Model<Buffer> {
2534        self.active_alternative().read(cx).old_buffer.clone()
2535    }
2536
2537    pub fn snapshot(&self, cx: &AppContext) -> MultiBufferSnapshot {
2538        self.active_alternative().read(cx).snapshot.clone()
2539    }
2540
2541    pub fn edit_position(&self, cx: &AppContext) -> Option<Anchor> {
2542        self.active_alternative().read(cx).edit_position
2543    }
2544
2545    fn diff<'a>(&self, cx: &'a AppContext) -> &'a Diff {
2546        &self.active_alternative().read(cx).diff
2547    }
2548
2549    pub fn last_equal_ranges<'a>(&self, cx: &'a AppContext) -> &'a [Range<Anchor>] {
2550        self.active_alternative().read(cx).last_equal_ranges()
2551    }
2552}
2553
2554impl EventEmitter<CodegenEvent> for Codegen {}
2555
2556pub struct CodegenAlternative {
2557    buffer: Model<MultiBuffer>,
2558    old_buffer: Model<Buffer>,
2559    snapshot: MultiBufferSnapshot,
2560    edit_position: Option<Anchor>,
2561    range: Range<Anchor>,
2562    last_equal_ranges: Vec<Range<Anchor>>,
2563    transformation_transaction_id: Option<TransactionId>,
2564    status: CodegenStatus,
2565    generation: Task<()>,
2566    diff: Diff,
2567    telemetry: Option<Arc<Telemetry>>,
2568    _subscription: gpui::Subscription,
2569    builder: Arc<PromptBuilder>,
2570    active: bool,
2571    edits: Vec<(Range<Anchor>, String)>,
2572    line_operations: Vec<LineOperation>,
2573    request: Option<LanguageModelRequest>,
2574    elapsed_time: Option<f64>,
2575    completion: Option<String>,
2576    message_id: Option<String>,
2577}
2578
2579enum CodegenStatus {
2580    Idle,
2581    Pending,
2582    Done,
2583    Error(anyhow::Error),
2584}
2585
2586#[derive(Default)]
2587struct Diff {
2588    deleted_row_ranges: Vec<(Anchor, RangeInclusive<u32>)>,
2589    inserted_row_ranges: Vec<Range<Anchor>>,
2590}
2591
2592impl Diff {
2593    fn is_empty(&self) -> bool {
2594        self.deleted_row_ranges.is_empty() && self.inserted_row_ranges.is_empty()
2595    }
2596}
2597
2598impl EventEmitter<CodegenEvent> for CodegenAlternative {}
2599
2600impl CodegenAlternative {
2601    pub fn new(
2602        buffer: Model<MultiBuffer>,
2603        range: Range<Anchor>,
2604        active: bool,
2605        telemetry: Option<Arc<Telemetry>>,
2606        builder: Arc<PromptBuilder>,
2607        cx: &mut ModelContext<Self>,
2608    ) -> Self {
2609        let snapshot = buffer.read(cx).snapshot(cx);
2610
2611        let (old_buffer, _, _) = buffer
2612            .read(cx)
2613            .range_to_buffer_ranges(range.clone(), cx)
2614            .pop()
2615            .unwrap();
2616        let old_buffer = cx.new_model(|cx| {
2617            let old_buffer = old_buffer.read(cx);
2618            let text = old_buffer.as_rope().clone();
2619            let line_ending = old_buffer.line_ending();
2620            let language = old_buffer.language().cloned();
2621            let language_registry = old_buffer.language_registry();
2622
2623            let mut buffer = Buffer::local_normalized(text, line_ending, cx);
2624            buffer.set_language(language, cx);
2625            if let Some(language_registry) = language_registry {
2626                buffer.set_language_registry(language_registry)
2627            }
2628            buffer
2629        });
2630
2631        Self {
2632            buffer: buffer.clone(),
2633            old_buffer,
2634            edit_position: None,
2635            message_id: None,
2636            snapshot,
2637            last_equal_ranges: Default::default(),
2638            transformation_transaction_id: None,
2639            status: CodegenStatus::Idle,
2640            generation: Task::ready(()),
2641            diff: Diff::default(),
2642            telemetry,
2643            _subscription: cx.subscribe(&buffer, Self::handle_buffer_event),
2644            builder,
2645            active,
2646            edits: Vec::new(),
2647            line_operations: Vec::new(),
2648            range,
2649            request: None,
2650            elapsed_time: None,
2651            completion: None,
2652        }
2653    }
2654
2655    fn set_active(&mut self, active: bool, cx: &mut ModelContext<Self>) {
2656        if active != self.active {
2657            self.active = active;
2658
2659            if self.active {
2660                let edits = self.edits.clone();
2661                self.apply_edits(edits, cx);
2662                if matches!(self.status, CodegenStatus::Pending) {
2663                    let line_operations = self.line_operations.clone();
2664                    self.reapply_line_based_diff(line_operations, cx);
2665                } else {
2666                    self.reapply_batch_diff(cx).detach();
2667                }
2668            } else if let Some(transaction_id) = self.transformation_transaction_id.take() {
2669                self.buffer.update(cx, |buffer, cx| {
2670                    buffer.undo_transaction(transaction_id, cx);
2671                    buffer.forget_transaction(transaction_id, cx);
2672                });
2673            }
2674        }
2675    }
2676
2677    fn handle_buffer_event(
2678        &mut self,
2679        _buffer: Model<MultiBuffer>,
2680        event: &multi_buffer::Event,
2681        cx: &mut ModelContext<Self>,
2682    ) {
2683        if let multi_buffer::Event::TransactionUndone { transaction_id } = event {
2684            if self.transformation_transaction_id == Some(*transaction_id) {
2685                self.transformation_transaction_id = None;
2686                self.generation = Task::ready(());
2687                cx.emit(CodegenEvent::Undone);
2688            }
2689        }
2690    }
2691
2692    pub fn last_equal_ranges(&self) -> &[Range<Anchor>] {
2693        &self.last_equal_ranges
2694    }
2695
2696    pub fn count_tokens(
2697        &self,
2698        user_prompt: String,
2699        assistant_panel_context: Option<LanguageModelRequest>,
2700        cx: &AppContext,
2701    ) -> BoxFuture<'static, Result<TokenCounts>> {
2702        if let Some(model) = LanguageModelRegistry::read_global(cx).active_model() {
2703            let request = self.build_request(user_prompt, assistant_panel_context.clone(), cx);
2704            match request {
2705                Ok(request) => {
2706                    let total_count = model.count_tokens(request.clone(), cx);
2707                    let assistant_panel_count = assistant_panel_context
2708                        .map(|context| model.count_tokens(context, cx))
2709                        .unwrap_or_else(|| future::ready(Ok(0)).boxed());
2710
2711                    async move {
2712                        Ok(TokenCounts {
2713                            total: total_count.await?,
2714                            assistant_panel: assistant_panel_count.await?,
2715                        })
2716                    }
2717                    .boxed()
2718                }
2719                Err(error) => futures::future::ready(Err(error)).boxed(),
2720            }
2721        } else {
2722            future::ready(Err(anyhow!("no active model"))).boxed()
2723        }
2724    }
2725
2726    pub fn start(
2727        &mut self,
2728        user_prompt: String,
2729        assistant_panel_context: Option<LanguageModelRequest>,
2730        model: Arc<dyn LanguageModel>,
2731        cx: &mut ModelContext<Self>,
2732    ) -> Result<()> {
2733        if let Some(transformation_transaction_id) = self.transformation_transaction_id.take() {
2734            self.buffer.update(cx, |buffer, cx| {
2735                buffer.undo_transaction(transformation_transaction_id, cx);
2736            });
2737        }
2738
2739        self.edit_position = Some(self.range.start.bias_right(&self.snapshot));
2740
2741        let api_key = model.api_key(cx);
2742        let telemetry_id = model.telemetry_id();
2743        let provider_id = model.provider_id();
2744        let stream: LocalBoxFuture<Result<LanguageModelTextStream>> =
2745            if user_prompt.trim().to_lowercase() == "delete" {
2746                async { Ok(LanguageModelTextStream::default()) }.boxed_local()
2747            } else {
2748                let request = self.build_request(user_prompt, assistant_panel_context, cx)?;
2749                self.request = Some(request.clone());
2750
2751                cx.spawn(|_, cx| async move { model.stream_completion_text(request, &cx).await })
2752                    .boxed_local()
2753            };
2754        self.handle_stream(telemetry_id, provider_id.to_string(), api_key, stream, cx);
2755        Ok(())
2756    }
2757
2758    fn build_request(
2759        &self,
2760        user_prompt: String,
2761        assistant_panel_context: Option<LanguageModelRequest>,
2762        cx: &AppContext,
2763    ) -> Result<LanguageModelRequest> {
2764        let buffer = self.buffer.read(cx).snapshot(cx);
2765        let language = buffer.language_at(self.range.start);
2766        let language_name = if let Some(language) = language.as_ref() {
2767            if Arc::ptr_eq(language, &language::PLAIN_TEXT) {
2768                None
2769            } else {
2770                Some(language.name())
2771            }
2772        } else {
2773            None
2774        };
2775
2776        let language_name = language_name.as_ref();
2777        let start = buffer.point_to_buffer_offset(self.range.start);
2778        let end = buffer.point_to_buffer_offset(self.range.end);
2779        let (buffer, range) = if let Some((start, end)) = start.zip(end) {
2780            let (start_buffer, start_buffer_offset) = start;
2781            let (end_buffer, end_buffer_offset) = end;
2782            if start_buffer.remote_id() == end_buffer.remote_id() {
2783                (start_buffer.clone(), start_buffer_offset..end_buffer_offset)
2784            } else {
2785                return Err(anyhow::anyhow!("invalid transformation range"));
2786            }
2787        } else {
2788            return Err(anyhow::anyhow!("invalid transformation range"));
2789        };
2790
2791        let prompt = self
2792            .builder
2793            .generate_inline_transformation_prompt(user_prompt, language_name, buffer, range)
2794            .map_err(|e| anyhow::anyhow!("Failed to generate content prompt: {}", e))?;
2795
2796        let mut messages = Vec::new();
2797        if let Some(context_request) = assistant_panel_context {
2798            messages = context_request.messages;
2799        }
2800
2801        messages.push(LanguageModelRequestMessage {
2802            role: Role::User,
2803            content: vec![prompt.into()],
2804            cache: false,
2805        });
2806
2807        Ok(LanguageModelRequest {
2808            messages,
2809            tools: Vec::new(),
2810            stop: Vec::new(),
2811            temperature: None,
2812        })
2813    }
2814
2815    pub fn handle_stream(
2816        &mut self,
2817        model_telemetry_id: String,
2818        model_provider_id: String,
2819        model_api_key: Option<String>,
2820        stream: impl 'static + Future<Output = Result<LanguageModelTextStream>>,
2821        cx: &mut ModelContext<Self>,
2822    ) {
2823        let start_time = Instant::now();
2824        let snapshot = self.snapshot.clone();
2825        let selected_text = snapshot
2826            .text_for_range(self.range.start..self.range.end)
2827            .collect::<Rope>();
2828
2829        let selection_start = self.range.start.to_point(&snapshot);
2830
2831        // Start with the indentation of the first line in the selection
2832        let mut suggested_line_indent = snapshot
2833            .suggested_indents(selection_start.row..=selection_start.row, cx)
2834            .into_values()
2835            .next()
2836            .unwrap_or_else(|| snapshot.indent_size_for_line(MultiBufferRow(selection_start.row)));
2837
2838        // If the first line in the selection does not have indentation, check the following lines
2839        if suggested_line_indent.len == 0 && suggested_line_indent.kind == IndentKind::Space {
2840            for row in selection_start.row..=self.range.end.to_point(&snapshot).row {
2841                let line_indent = snapshot.indent_size_for_line(MultiBufferRow(row));
2842                // Prefer tabs if a line in the selection uses tabs as indentation
2843                if line_indent.kind == IndentKind::Tab {
2844                    suggested_line_indent.kind = IndentKind::Tab;
2845                    break;
2846                }
2847            }
2848        }
2849
2850        let http_client = cx.http_client().clone();
2851        let telemetry = self.telemetry.clone();
2852        let language_name = {
2853            let multibuffer = self.buffer.read(cx);
2854            let ranges = multibuffer.range_to_buffer_ranges(self.range.clone(), cx);
2855            ranges
2856                .first()
2857                .and_then(|(buffer, _, _)| buffer.read(cx).language())
2858                .map(|language| language.name())
2859        };
2860
2861        self.diff = Diff::default();
2862        self.status = CodegenStatus::Pending;
2863        let mut edit_start = self.range.start.to_offset(&snapshot);
2864        let completion = Arc::new(Mutex::new(String::new()));
2865        let completion_clone = completion.clone();
2866
2867        self.generation = cx.spawn(|codegen, mut cx| {
2868            async move {
2869                let stream = stream.await;
2870                let message_id = stream
2871                    .as_ref()
2872                    .ok()
2873                    .and_then(|stream| stream.message_id.clone());
2874                let generate = async {
2875                    let (mut diff_tx, mut diff_rx) = mpsc::channel(1);
2876                    let executor = cx.background_executor().clone();
2877                    let message_id = message_id.clone();
2878                    let line_based_stream_diff: Task<anyhow::Result<()>> =
2879                        cx.background_executor().spawn(async move {
2880                            let mut response_latency = None;
2881                            let request_start = Instant::now();
2882                            let diff = async {
2883                                let chunks = StripInvalidSpans::new(stream?.stream);
2884                                futures::pin_mut!(chunks);
2885                                let mut diff = StreamingDiff::new(selected_text.to_string());
2886                                let mut line_diff = LineDiff::default();
2887
2888                                let mut new_text = String::new();
2889                                let mut base_indent = None;
2890                                let mut line_indent = None;
2891                                let mut first_line = true;
2892
2893                                while let Some(chunk) = chunks.next().await {
2894                                    if response_latency.is_none() {
2895                                        response_latency = Some(request_start.elapsed());
2896                                    }
2897                                    let chunk = chunk?;
2898                                    completion_clone.lock().push_str(&chunk);
2899
2900                                    let mut lines = chunk.split('\n').peekable();
2901                                    while let Some(line) = lines.next() {
2902                                        new_text.push_str(line);
2903                                        if line_indent.is_none() {
2904                                            if let Some(non_whitespace_ch_ix) =
2905                                                new_text.find(|ch: char| !ch.is_whitespace())
2906                                            {
2907                                                line_indent = Some(non_whitespace_ch_ix);
2908                                                base_indent = base_indent.or(line_indent);
2909
2910                                                let line_indent = line_indent.unwrap();
2911                                                let base_indent = base_indent.unwrap();
2912                                                let indent_delta =
2913                                                    line_indent as i32 - base_indent as i32;
2914                                                let mut corrected_indent_len = cmp::max(
2915                                                    0,
2916                                                    suggested_line_indent.len as i32 + indent_delta,
2917                                                )
2918                                                    as usize;
2919                                                if first_line {
2920                                                    corrected_indent_len = corrected_indent_len
2921                                                        .saturating_sub(
2922                                                            selection_start.column as usize,
2923                                                        );
2924                                                }
2925
2926                                                let indent_char = suggested_line_indent.char();
2927                                                let mut indent_buffer = [0; 4];
2928                                                let indent_str =
2929                                                    indent_char.encode_utf8(&mut indent_buffer);
2930                                                new_text.replace_range(
2931                                                    ..line_indent,
2932                                                    &indent_str.repeat(corrected_indent_len),
2933                                                );
2934                                            }
2935                                        }
2936
2937                                        if line_indent.is_some() {
2938                                            let char_ops = diff.push_new(&new_text);
2939                                            line_diff
2940                                                .push_char_operations(&char_ops, &selected_text);
2941                                            diff_tx
2942                                                .send((char_ops, line_diff.line_operations()))
2943                                                .await?;
2944                                            new_text.clear();
2945                                        }
2946
2947                                        if lines.peek().is_some() {
2948                                            let char_ops = diff.push_new("\n");
2949                                            line_diff
2950                                                .push_char_operations(&char_ops, &selected_text);
2951                                            diff_tx
2952                                                .send((char_ops, line_diff.line_operations()))
2953                                                .await?;
2954                                            if line_indent.is_none() {
2955                                                // Don't write out the leading indentation in empty lines on the next line
2956                                                // This is the case where the above if statement didn't clear the buffer
2957                                                new_text.clear();
2958                                            }
2959                                            line_indent = None;
2960                                            first_line = false;
2961                                        }
2962                                    }
2963                                }
2964
2965                                let mut char_ops = diff.push_new(&new_text);
2966                                char_ops.extend(diff.finish());
2967                                line_diff.push_char_operations(&char_ops, &selected_text);
2968                                line_diff.finish(&selected_text);
2969                                diff_tx
2970                                    .send((char_ops, line_diff.line_operations()))
2971                                    .await?;
2972
2973                                anyhow::Ok(())
2974                            };
2975
2976                            let result = diff.await;
2977
2978                            let error_message =
2979                                result.as_ref().err().map(|error| error.to_string());
2980                            report_assistant_event(
2981                                AssistantEvent {
2982                                    conversation_id: None,
2983                                    message_id,
2984                                    kind: AssistantKind::Inline,
2985                                    phase: AssistantPhase::Response,
2986                                    model: model_telemetry_id,
2987                                    model_provider: model_provider_id.to_string(),
2988                                    response_latency,
2989                                    error_message,
2990                                    language_name: language_name.map(|name| name.to_proto()),
2991                                },
2992                                telemetry,
2993                                http_client,
2994                                model_api_key,
2995                                &executor,
2996                            );
2997
2998                            result?;
2999                            Ok(())
3000                        });
3001
3002                    while let Some((char_ops, line_ops)) = diff_rx.next().await {
3003                        codegen.update(&mut cx, |codegen, cx| {
3004                            codegen.last_equal_ranges.clear();
3005
3006                            let edits = char_ops
3007                                .into_iter()
3008                                .filter_map(|operation| match operation {
3009                                    CharOperation::Insert { text } => {
3010                                        let edit_start = snapshot.anchor_after(edit_start);
3011                                        Some((edit_start..edit_start, text))
3012                                    }
3013                                    CharOperation::Delete { bytes } => {
3014                                        let edit_end = edit_start + bytes;
3015                                        let edit_range = snapshot.anchor_after(edit_start)
3016                                            ..snapshot.anchor_before(edit_end);
3017                                        edit_start = edit_end;
3018                                        Some((edit_range, String::new()))
3019                                    }
3020                                    CharOperation::Keep { bytes } => {
3021                                        let edit_end = edit_start + bytes;
3022                                        let edit_range = snapshot.anchor_after(edit_start)
3023                                            ..snapshot.anchor_before(edit_end);
3024                                        edit_start = edit_end;
3025                                        codegen.last_equal_ranges.push(edit_range);
3026                                        None
3027                                    }
3028                                })
3029                                .collect::<Vec<_>>();
3030
3031                            if codegen.active {
3032                                codegen.apply_edits(edits.iter().cloned(), cx);
3033                                codegen.reapply_line_based_diff(line_ops.iter().cloned(), cx);
3034                            }
3035                            codegen.edits.extend(edits);
3036                            codegen.line_operations = line_ops;
3037                            codegen.edit_position = Some(snapshot.anchor_after(edit_start));
3038
3039                            cx.notify();
3040                        })?;
3041                    }
3042
3043                    // Streaming stopped and we have the new text in the buffer, and a line-based diff applied for the whole new buffer.
3044                    // That diff is not what a regular diff is and might look unexpected, ergo apply a regular diff.
3045                    // It's fine to apply even if the rest of the line diffing fails, as no more hunks are coming through `diff_rx`.
3046                    let batch_diff_task =
3047                        codegen.update(&mut cx, |codegen, cx| codegen.reapply_batch_diff(cx))?;
3048                    let (line_based_stream_diff, ()) =
3049                        join!(line_based_stream_diff, batch_diff_task);
3050                    line_based_stream_diff?;
3051
3052                    anyhow::Ok(())
3053                };
3054
3055                let result = generate.await;
3056                let elapsed_time = start_time.elapsed().as_secs_f64();
3057
3058                codegen
3059                    .update(&mut cx, |this, cx| {
3060                        this.message_id = message_id;
3061                        this.last_equal_ranges.clear();
3062                        if let Err(error) = result {
3063                            this.status = CodegenStatus::Error(error);
3064                        } else {
3065                            this.status = CodegenStatus::Done;
3066                        }
3067                        this.elapsed_time = Some(elapsed_time);
3068                        this.completion = Some(completion.lock().clone());
3069                        cx.emit(CodegenEvent::Finished);
3070                        cx.notify();
3071                    })
3072                    .ok();
3073            }
3074        });
3075        cx.notify();
3076    }
3077
3078    pub fn stop(&mut self, cx: &mut ModelContext<Self>) {
3079        self.last_equal_ranges.clear();
3080        if self.diff.is_empty() {
3081            self.status = CodegenStatus::Idle;
3082        } else {
3083            self.status = CodegenStatus::Done;
3084        }
3085        self.generation = Task::ready(());
3086        cx.emit(CodegenEvent::Finished);
3087        cx.notify();
3088    }
3089
3090    pub fn undo(&mut self, cx: &mut ModelContext<Self>) {
3091        self.buffer.update(cx, |buffer, cx| {
3092            if let Some(transaction_id) = self.transformation_transaction_id.take() {
3093                buffer.undo_transaction(transaction_id, cx);
3094                buffer.refresh_preview(cx);
3095            }
3096        });
3097    }
3098
3099    fn apply_edits(
3100        &mut self,
3101        edits: impl IntoIterator<Item = (Range<Anchor>, String)>,
3102        cx: &mut ModelContext<CodegenAlternative>,
3103    ) {
3104        let transaction = self.buffer.update(cx, |buffer, cx| {
3105            // Avoid grouping assistant edits with user edits.
3106            buffer.finalize_last_transaction(cx);
3107            buffer.start_transaction(cx);
3108            buffer.edit(edits, None, cx);
3109            buffer.end_transaction(cx)
3110        });
3111
3112        if let Some(transaction) = transaction {
3113            if let Some(first_transaction) = self.transformation_transaction_id {
3114                // Group all assistant edits into the first transaction.
3115                self.buffer.update(cx, |buffer, cx| {
3116                    buffer.merge_transactions(transaction, first_transaction, cx)
3117                });
3118            } else {
3119                self.transformation_transaction_id = Some(transaction);
3120                self.buffer
3121                    .update(cx, |buffer, cx| buffer.finalize_last_transaction(cx));
3122            }
3123        }
3124    }
3125
3126    fn reapply_line_based_diff(
3127        &mut self,
3128        line_operations: impl IntoIterator<Item = LineOperation>,
3129        cx: &mut ModelContext<Self>,
3130    ) {
3131        let old_snapshot = self.snapshot.clone();
3132        let old_range = self.range.to_point(&old_snapshot);
3133        let new_snapshot = self.buffer.read(cx).snapshot(cx);
3134        let new_range = self.range.to_point(&new_snapshot);
3135
3136        let mut old_row = old_range.start.row;
3137        let mut new_row = new_range.start.row;
3138
3139        self.diff.deleted_row_ranges.clear();
3140        self.diff.inserted_row_ranges.clear();
3141        for operation in line_operations {
3142            match operation {
3143                LineOperation::Keep { lines } => {
3144                    old_row += lines;
3145                    new_row += lines;
3146                }
3147                LineOperation::Delete { lines } => {
3148                    let old_end_row = old_row + lines - 1;
3149                    let new_row = new_snapshot.anchor_before(Point::new(new_row, 0));
3150
3151                    if let Some((_, last_deleted_row_range)) =
3152                        self.diff.deleted_row_ranges.last_mut()
3153                    {
3154                        if *last_deleted_row_range.end() + 1 == old_row {
3155                            *last_deleted_row_range = *last_deleted_row_range.start()..=old_end_row;
3156                        } else {
3157                            self.diff
3158                                .deleted_row_ranges
3159                                .push((new_row, old_row..=old_end_row));
3160                        }
3161                    } else {
3162                        self.diff
3163                            .deleted_row_ranges
3164                            .push((new_row, old_row..=old_end_row));
3165                    }
3166
3167                    old_row += lines;
3168                }
3169                LineOperation::Insert { lines } => {
3170                    let new_end_row = new_row + lines - 1;
3171                    let start = new_snapshot.anchor_before(Point::new(new_row, 0));
3172                    let end = new_snapshot.anchor_before(Point::new(
3173                        new_end_row,
3174                        new_snapshot.line_len(MultiBufferRow(new_end_row)),
3175                    ));
3176                    self.diff.inserted_row_ranges.push(start..end);
3177                    new_row += lines;
3178                }
3179            }
3180
3181            cx.notify();
3182        }
3183    }
3184
3185    fn reapply_batch_diff(&mut self, cx: &mut ModelContext<Self>) -> Task<()> {
3186        let old_snapshot = self.snapshot.clone();
3187        let old_range = self.range.to_point(&old_snapshot);
3188        let new_snapshot = self.buffer.read(cx).snapshot(cx);
3189        let new_range = self.range.to_point(&new_snapshot);
3190
3191        cx.spawn(|codegen, mut cx| async move {
3192            let (deleted_row_ranges, inserted_row_ranges) = cx
3193                .background_executor()
3194                .spawn(async move {
3195                    let old_text = old_snapshot
3196                        .text_for_range(
3197                            Point::new(old_range.start.row, 0)
3198                                ..Point::new(
3199                                    old_range.end.row,
3200                                    old_snapshot.line_len(MultiBufferRow(old_range.end.row)),
3201                                ),
3202                        )
3203                        .collect::<String>();
3204                    let new_text = new_snapshot
3205                        .text_for_range(
3206                            Point::new(new_range.start.row, 0)
3207                                ..Point::new(
3208                                    new_range.end.row,
3209                                    new_snapshot.line_len(MultiBufferRow(new_range.end.row)),
3210                                ),
3211                        )
3212                        .collect::<String>();
3213
3214                    let mut old_row = old_range.start.row;
3215                    let mut new_row = new_range.start.row;
3216                    let batch_diff =
3217                        similar::TextDiff::from_lines(old_text.as_str(), new_text.as_str());
3218
3219                    let mut deleted_row_ranges: Vec<(Anchor, RangeInclusive<u32>)> = Vec::new();
3220                    let mut inserted_row_ranges = Vec::new();
3221                    for change in batch_diff.iter_all_changes() {
3222                        let line_count = change.value().lines().count() as u32;
3223                        match change.tag() {
3224                            similar::ChangeTag::Equal => {
3225                                old_row += line_count;
3226                                new_row += line_count;
3227                            }
3228                            similar::ChangeTag::Delete => {
3229                                let old_end_row = old_row + line_count - 1;
3230                                let new_row = new_snapshot.anchor_before(Point::new(new_row, 0));
3231
3232                                if let Some((_, last_deleted_row_range)) =
3233                                    deleted_row_ranges.last_mut()
3234                                {
3235                                    if *last_deleted_row_range.end() + 1 == old_row {
3236                                        *last_deleted_row_range =
3237                                            *last_deleted_row_range.start()..=old_end_row;
3238                                    } else {
3239                                        deleted_row_ranges.push((new_row, old_row..=old_end_row));
3240                                    }
3241                                } else {
3242                                    deleted_row_ranges.push((new_row, old_row..=old_end_row));
3243                                }
3244
3245                                old_row += line_count;
3246                            }
3247                            similar::ChangeTag::Insert => {
3248                                let new_end_row = new_row + line_count - 1;
3249                                let start = new_snapshot.anchor_before(Point::new(new_row, 0));
3250                                let end = new_snapshot.anchor_before(Point::new(
3251                                    new_end_row,
3252                                    new_snapshot.line_len(MultiBufferRow(new_end_row)),
3253                                ));
3254                                inserted_row_ranges.push(start..end);
3255                                new_row += line_count;
3256                            }
3257                        }
3258                    }
3259
3260                    (deleted_row_ranges, inserted_row_ranges)
3261                })
3262                .await;
3263
3264            codegen
3265                .update(&mut cx, |codegen, cx| {
3266                    codegen.diff.deleted_row_ranges = deleted_row_ranges;
3267                    codegen.diff.inserted_row_ranges = inserted_row_ranges;
3268                    cx.notify();
3269                })
3270                .ok();
3271        })
3272    }
3273}
3274
3275struct StripInvalidSpans<T> {
3276    stream: T,
3277    stream_done: bool,
3278    buffer: String,
3279    first_line: bool,
3280    line_end: bool,
3281    starts_with_code_block: bool,
3282}
3283
3284impl<T> StripInvalidSpans<T>
3285where
3286    T: Stream<Item = Result<String>>,
3287{
3288    fn new(stream: T) -> Self {
3289        Self {
3290            stream,
3291            stream_done: false,
3292            buffer: String::new(),
3293            first_line: true,
3294            line_end: false,
3295            starts_with_code_block: false,
3296        }
3297    }
3298}
3299
3300impl<T> Stream for StripInvalidSpans<T>
3301where
3302    T: Stream<Item = Result<String>>,
3303{
3304    type Item = Result<String>;
3305
3306    fn poll_next(self: Pin<&mut Self>, cx: &mut task::Context) -> Poll<Option<Self::Item>> {
3307        const CODE_BLOCK_DELIMITER: &str = "```";
3308        const CURSOR_SPAN: &str = "<|CURSOR|>";
3309
3310        let this = unsafe { self.get_unchecked_mut() };
3311        loop {
3312            if !this.stream_done {
3313                let mut stream = unsafe { Pin::new_unchecked(&mut this.stream) };
3314                match stream.as_mut().poll_next(cx) {
3315                    Poll::Ready(Some(Ok(chunk))) => {
3316                        this.buffer.push_str(&chunk);
3317                    }
3318                    Poll::Ready(Some(Err(error))) => return Poll::Ready(Some(Err(error))),
3319                    Poll::Ready(None) => {
3320                        this.stream_done = true;
3321                    }
3322                    Poll::Pending => return Poll::Pending,
3323                }
3324            }
3325
3326            let mut chunk = String::new();
3327            let mut consumed = 0;
3328            if !this.buffer.is_empty() {
3329                let mut lines = this.buffer.split('\n').enumerate().peekable();
3330                while let Some((line_ix, line)) = lines.next() {
3331                    if line_ix > 0 {
3332                        this.first_line = false;
3333                    }
3334
3335                    if this.first_line {
3336                        let trimmed_line = line.trim();
3337                        if lines.peek().is_some() {
3338                            if trimmed_line.starts_with(CODE_BLOCK_DELIMITER) {
3339                                consumed += line.len() + 1;
3340                                this.starts_with_code_block = true;
3341                                continue;
3342                            }
3343                        } else if trimmed_line.is_empty()
3344                            || prefixes(CODE_BLOCK_DELIMITER)
3345                                .any(|prefix| trimmed_line.starts_with(prefix))
3346                        {
3347                            break;
3348                        }
3349                    }
3350
3351                    let line_without_cursor = line.replace(CURSOR_SPAN, "");
3352                    if lines.peek().is_some() {
3353                        if this.line_end {
3354                            chunk.push('\n');
3355                        }
3356
3357                        chunk.push_str(&line_without_cursor);
3358                        this.line_end = true;
3359                        consumed += line.len() + 1;
3360                    } else if this.stream_done {
3361                        if !this.starts_with_code_block
3362                            || !line_without_cursor.trim().ends_with(CODE_BLOCK_DELIMITER)
3363                        {
3364                            if this.line_end {
3365                                chunk.push('\n');
3366                            }
3367
3368                            chunk.push_str(&line);
3369                        }
3370
3371                        consumed += line.len();
3372                    } else {
3373                        let trimmed_line = line.trim();
3374                        if trimmed_line.is_empty()
3375                            || prefixes(CURSOR_SPAN).any(|prefix| trimmed_line.ends_with(prefix))
3376                            || prefixes(CODE_BLOCK_DELIMITER)
3377                                .any(|prefix| trimmed_line.ends_with(prefix))
3378                        {
3379                            break;
3380                        } else {
3381                            if this.line_end {
3382                                chunk.push('\n');
3383                                this.line_end = false;
3384                            }
3385
3386                            chunk.push_str(&line_without_cursor);
3387                            consumed += line.len();
3388                        }
3389                    }
3390                }
3391            }
3392
3393            this.buffer = this.buffer.split_off(consumed);
3394            if !chunk.is_empty() {
3395                return Poll::Ready(Some(Ok(chunk)));
3396            } else if this.stream_done {
3397                return Poll::Ready(None);
3398            }
3399        }
3400    }
3401}
3402
3403struct AssistantCodeActionProvider {
3404    editor: WeakView<Editor>,
3405    workspace: WeakView<Workspace>,
3406}
3407
3408impl CodeActionProvider for AssistantCodeActionProvider {
3409    fn code_actions(
3410        &self,
3411        buffer: &Model<Buffer>,
3412        range: Range<text::Anchor>,
3413        cx: &mut WindowContext,
3414    ) -> Task<Result<Vec<CodeAction>>> {
3415        if !AssistantSettings::get_global(cx).enabled {
3416            return Task::ready(Ok(Vec::new()));
3417        }
3418
3419        let snapshot = buffer.read(cx).snapshot();
3420        let mut range = range.to_point(&snapshot);
3421
3422        // Expand the range to line boundaries.
3423        range.start.column = 0;
3424        range.end.column = snapshot.line_len(range.end.row);
3425
3426        let mut has_diagnostics = false;
3427        for diagnostic in snapshot.diagnostics_in_range::<_, Point>(range.clone(), false) {
3428            range.start = cmp::min(range.start, diagnostic.range.start);
3429            range.end = cmp::max(range.end, diagnostic.range.end);
3430            has_diagnostics = true;
3431        }
3432        if has_diagnostics {
3433            if let Some(symbols_containing_start) = snapshot.symbols_containing(range.start, None) {
3434                if let Some(symbol) = symbols_containing_start.last() {
3435                    range.start = cmp::min(range.start, symbol.range.start.to_point(&snapshot));
3436                    range.end = cmp::max(range.end, symbol.range.end.to_point(&snapshot));
3437                }
3438            }
3439
3440            if let Some(symbols_containing_end) = snapshot.symbols_containing(range.end, None) {
3441                if let Some(symbol) = symbols_containing_end.last() {
3442                    range.start = cmp::min(range.start, symbol.range.start.to_point(&snapshot));
3443                    range.end = cmp::max(range.end, symbol.range.end.to_point(&snapshot));
3444                }
3445            }
3446
3447            Task::ready(Ok(vec![CodeAction {
3448                server_id: language::LanguageServerId(0),
3449                range: snapshot.anchor_before(range.start)..snapshot.anchor_after(range.end),
3450                lsp_action: lsp::CodeAction {
3451                    title: "Fix with Assistant".into(),
3452                    ..Default::default()
3453                },
3454            }]))
3455        } else {
3456            Task::ready(Ok(Vec::new()))
3457        }
3458    }
3459
3460    fn apply_code_action(
3461        &self,
3462        buffer: Model<Buffer>,
3463        action: CodeAction,
3464        excerpt_id: ExcerptId,
3465        _push_to_history: bool,
3466        cx: &mut WindowContext,
3467    ) -> Task<Result<ProjectTransaction>> {
3468        let editor = self.editor.clone();
3469        let workspace = self.workspace.clone();
3470        cx.spawn(|mut cx| async move {
3471            let editor = editor.upgrade().context("editor was released")?;
3472            let range = editor
3473                .update(&mut cx, |editor, cx| {
3474                    editor.buffer().update(cx, |multibuffer, cx| {
3475                        let buffer = buffer.read(cx);
3476                        let multibuffer_snapshot = multibuffer.read(cx);
3477
3478                        let old_context_range =
3479                            multibuffer_snapshot.context_range_for_excerpt(excerpt_id)?;
3480                        let mut new_context_range = old_context_range.clone();
3481                        if action
3482                            .range
3483                            .start
3484                            .cmp(&old_context_range.start, buffer)
3485                            .is_lt()
3486                        {
3487                            new_context_range.start = action.range.start;
3488                        }
3489                        if action.range.end.cmp(&old_context_range.end, buffer).is_gt() {
3490                            new_context_range.end = action.range.end;
3491                        }
3492                        drop(multibuffer_snapshot);
3493
3494                        if new_context_range != old_context_range {
3495                            multibuffer.resize_excerpt(excerpt_id, new_context_range, cx);
3496                        }
3497
3498                        let multibuffer_snapshot = multibuffer.read(cx);
3499                        Some(
3500                            multibuffer_snapshot
3501                                .anchor_in_excerpt(excerpt_id, action.range.start)?
3502                                ..multibuffer_snapshot
3503                                    .anchor_in_excerpt(excerpt_id, action.range.end)?,
3504                        )
3505                    })
3506                })?
3507                .context("invalid range")?;
3508            let assistant_panel = workspace.update(&mut cx, |workspace, cx| {
3509                workspace
3510                    .panel::<AssistantPanel>(cx)
3511                    .context("assistant panel was released")
3512            })??;
3513
3514            cx.update_global(|assistant: &mut InlineAssistant, cx| {
3515                let assist_id = assistant.suggest_assist(
3516                    &editor,
3517                    range,
3518                    "Fix Diagnostics".into(),
3519                    None,
3520                    true,
3521                    Some(workspace),
3522                    Some(&assistant_panel),
3523                    cx,
3524                );
3525                assistant.start_assist(assist_id, cx);
3526            })?;
3527
3528            Ok(ProjectTransaction::default())
3529        })
3530    }
3531}
3532
3533fn prefixes(text: &str) -> impl Iterator<Item = &str> {
3534    (0..text.len() - 1).map(|ix| &text[..ix + 1])
3535}
3536
3537fn merge_ranges(ranges: &mut Vec<Range<Anchor>>, buffer: &MultiBufferSnapshot) {
3538    ranges.sort_unstable_by(|a, b| {
3539        a.start
3540            .cmp(&b.start, buffer)
3541            .then_with(|| b.end.cmp(&a.end, buffer))
3542    });
3543
3544    let mut ix = 0;
3545    while ix + 1 < ranges.len() {
3546        let b = ranges[ix + 1].clone();
3547        let a = &mut ranges[ix];
3548        if a.end.cmp(&b.start, buffer).is_gt() {
3549            if a.end.cmp(&b.end, buffer).is_lt() {
3550                a.end = b.end;
3551            }
3552            ranges.remove(ix + 1);
3553        } else {
3554            ix += 1;
3555        }
3556    }
3557}
3558
3559#[cfg(test)]
3560mod tests {
3561    use super::*;
3562    use futures::stream::{self};
3563    use gpui::{Context, TestAppContext};
3564    use indoc::indoc;
3565    use language::{
3566        language_settings, tree_sitter_rust, Buffer, Language, LanguageConfig, LanguageMatcher,
3567        Point,
3568    };
3569    use language_model::LanguageModelRegistry;
3570    use rand::prelude::*;
3571    use serde::Serialize;
3572    use settings::SettingsStore;
3573    use std::{future, sync::Arc};
3574
3575    #[derive(Serialize)]
3576    pub struct DummyCompletionRequest {
3577        pub name: String,
3578    }
3579
3580    #[gpui::test(iterations = 10)]
3581    async fn test_transform_autoindent(cx: &mut TestAppContext, mut rng: StdRng) {
3582        cx.set_global(cx.update(SettingsStore::test));
3583        cx.update(language_model::LanguageModelRegistry::test);
3584        cx.update(language_settings::init);
3585
3586        let text = indoc! {"
3587            fn main() {
3588                let x = 0;
3589                for _ in 0..10 {
3590                    x += 1;
3591                }
3592            }
3593        "};
3594        let buffer =
3595            cx.new_model(|cx| Buffer::local(text, cx).with_language(Arc::new(rust_lang()), cx));
3596        let buffer = cx.new_model(|cx| MultiBuffer::singleton(buffer, cx));
3597        let range = buffer.read_with(cx, |buffer, cx| {
3598            let snapshot = buffer.snapshot(cx);
3599            snapshot.anchor_before(Point::new(1, 0))..snapshot.anchor_after(Point::new(4, 5))
3600        });
3601        let prompt_builder = Arc::new(PromptBuilder::new(None).unwrap());
3602        let codegen = cx.new_model(|cx| {
3603            CodegenAlternative::new(
3604                buffer.clone(),
3605                range.clone(),
3606                true,
3607                None,
3608                prompt_builder,
3609                cx,
3610            )
3611        });
3612
3613        let chunks_tx = simulate_response_stream(codegen.clone(), cx);
3614
3615        let mut new_text = concat!(
3616            "       let mut x = 0;\n",
3617            "       while x < 10 {\n",
3618            "           x += 1;\n",
3619            "       }",
3620        );
3621        while !new_text.is_empty() {
3622            let max_len = cmp::min(new_text.len(), 10);
3623            let len = rng.gen_range(1..=max_len);
3624            let (chunk, suffix) = new_text.split_at(len);
3625            chunks_tx.unbounded_send(chunk.to_string()).unwrap();
3626            new_text = suffix;
3627            cx.background_executor.run_until_parked();
3628        }
3629        drop(chunks_tx);
3630        cx.background_executor.run_until_parked();
3631
3632        assert_eq!(
3633            buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()),
3634            indoc! {"
3635                fn main() {
3636                    let mut x = 0;
3637                    while x < 10 {
3638                        x += 1;
3639                    }
3640                }
3641            "}
3642        );
3643    }
3644
3645    #[gpui::test(iterations = 10)]
3646    async fn test_autoindent_when_generating_past_indentation(
3647        cx: &mut TestAppContext,
3648        mut rng: StdRng,
3649    ) {
3650        cx.set_global(cx.update(SettingsStore::test));
3651        cx.update(language_settings::init);
3652
3653        let text = indoc! {"
3654            fn main() {
3655                le
3656            }
3657        "};
3658        let buffer =
3659            cx.new_model(|cx| Buffer::local(text, cx).with_language(Arc::new(rust_lang()), cx));
3660        let buffer = cx.new_model(|cx| MultiBuffer::singleton(buffer, cx));
3661        let range = buffer.read_with(cx, |buffer, cx| {
3662            let snapshot = buffer.snapshot(cx);
3663            snapshot.anchor_before(Point::new(1, 6))..snapshot.anchor_after(Point::new(1, 6))
3664        });
3665        let prompt_builder = Arc::new(PromptBuilder::new(None).unwrap());
3666        let codegen = cx.new_model(|cx| {
3667            CodegenAlternative::new(
3668                buffer.clone(),
3669                range.clone(),
3670                true,
3671                None,
3672                prompt_builder,
3673                cx,
3674            )
3675        });
3676
3677        let chunks_tx = simulate_response_stream(codegen.clone(), cx);
3678
3679        cx.background_executor.run_until_parked();
3680
3681        let mut new_text = concat!(
3682            "t mut x = 0;\n",
3683            "while x < 10 {\n",
3684            "    x += 1;\n",
3685            "}", //
3686        );
3687        while !new_text.is_empty() {
3688            let max_len = cmp::min(new_text.len(), 10);
3689            let len = rng.gen_range(1..=max_len);
3690            let (chunk, suffix) = new_text.split_at(len);
3691            chunks_tx.unbounded_send(chunk.to_string()).unwrap();
3692            new_text = suffix;
3693            cx.background_executor.run_until_parked();
3694        }
3695        drop(chunks_tx);
3696        cx.background_executor.run_until_parked();
3697
3698        assert_eq!(
3699            buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()),
3700            indoc! {"
3701                fn main() {
3702                    let mut x = 0;
3703                    while x < 10 {
3704                        x += 1;
3705                    }
3706                }
3707            "}
3708        );
3709    }
3710
3711    #[gpui::test(iterations = 10)]
3712    async fn test_autoindent_when_generating_before_indentation(
3713        cx: &mut TestAppContext,
3714        mut rng: StdRng,
3715    ) {
3716        cx.update(LanguageModelRegistry::test);
3717        cx.set_global(cx.update(SettingsStore::test));
3718        cx.update(language_settings::init);
3719
3720        let text = concat!(
3721            "fn main() {\n",
3722            "  \n",
3723            "}\n" //
3724        );
3725        let buffer =
3726            cx.new_model(|cx| Buffer::local(text, cx).with_language(Arc::new(rust_lang()), cx));
3727        let buffer = cx.new_model(|cx| MultiBuffer::singleton(buffer, cx));
3728        let range = buffer.read_with(cx, |buffer, cx| {
3729            let snapshot = buffer.snapshot(cx);
3730            snapshot.anchor_before(Point::new(1, 2))..snapshot.anchor_after(Point::new(1, 2))
3731        });
3732        let prompt_builder = Arc::new(PromptBuilder::new(None).unwrap());
3733        let codegen = cx.new_model(|cx| {
3734            CodegenAlternative::new(
3735                buffer.clone(),
3736                range.clone(),
3737                true,
3738                None,
3739                prompt_builder,
3740                cx,
3741            )
3742        });
3743
3744        let chunks_tx = simulate_response_stream(codegen.clone(), cx);
3745
3746        cx.background_executor.run_until_parked();
3747
3748        let mut new_text = concat!(
3749            "let mut x = 0;\n",
3750            "while x < 10 {\n",
3751            "    x += 1;\n",
3752            "}", //
3753        );
3754        while !new_text.is_empty() {
3755            let max_len = cmp::min(new_text.len(), 10);
3756            let len = rng.gen_range(1..=max_len);
3757            let (chunk, suffix) = new_text.split_at(len);
3758            chunks_tx.unbounded_send(chunk.to_string()).unwrap();
3759            new_text = suffix;
3760            cx.background_executor.run_until_parked();
3761        }
3762        drop(chunks_tx);
3763        cx.background_executor.run_until_parked();
3764
3765        assert_eq!(
3766            buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()),
3767            indoc! {"
3768                fn main() {
3769                    let mut x = 0;
3770                    while x < 10 {
3771                        x += 1;
3772                    }
3773                }
3774            "}
3775        );
3776    }
3777
3778    #[gpui::test(iterations = 10)]
3779    async fn test_autoindent_respects_tabs_in_selection(cx: &mut TestAppContext) {
3780        cx.update(LanguageModelRegistry::test);
3781        cx.set_global(cx.update(SettingsStore::test));
3782        cx.update(language_settings::init);
3783
3784        let text = indoc! {"
3785            func main() {
3786            \tx := 0
3787            \tfor i := 0; i < 10; i++ {
3788            \t\tx++
3789            \t}
3790            }
3791        "};
3792        let buffer = cx.new_model(|cx| Buffer::local(text, cx));
3793        let buffer = cx.new_model(|cx| MultiBuffer::singleton(buffer, cx));
3794        let range = buffer.read_with(cx, |buffer, cx| {
3795            let snapshot = buffer.snapshot(cx);
3796            snapshot.anchor_before(Point::new(0, 0))..snapshot.anchor_after(Point::new(4, 2))
3797        });
3798        let prompt_builder = Arc::new(PromptBuilder::new(None).unwrap());
3799        let codegen = cx.new_model(|cx| {
3800            CodegenAlternative::new(
3801                buffer.clone(),
3802                range.clone(),
3803                true,
3804                None,
3805                prompt_builder,
3806                cx,
3807            )
3808        });
3809
3810        let chunks_tx = simulate_response_stream(codegen.clone(), cx);
3811        let new_text = concat!(
3812            "func main() {\n",
3813            "\tx := 0\n",
3814            "\tfor x < 10 {\n",
3815            "\t\tx++\n",
3816            "\t}", //
3817        );
3818        chunks_tx.unbounded_send(new_text.to_string()).unwrap();
3819        drop(chunks_tx);
3820        cx.background_executor.run_until_parked();
3821
3822        assert_eq!(
3823            buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()),
3824            indoc! {"
3825                func main() {
3826                \tx := 0
3827                \tfor x < 10 {
3828                \t\tx++
3829                \t}
3830                }
3831            "}
3832        );
3833    }
3834
3835    #[gpui::test]
3836    async fn test_inactive_codegen_alternative(cx: &mut TestAppContext) {
3837        cx.update(LanguageModelRegistry::test);
3838        cx.set_global(cx.update(SettingsStore::test));
3839        cx.update(language_settings::init);
3840
3841        let text = indoc! {"
3842            fn main() {
3843                let x = 0;
3844            }
3845        "};
3846        let buffer =
3847            cx.new_model(|cx| Buffer::local(text, cx).with_language(Arc::new(rust_lang()), cx));
3848        let buffer = cx.new_model(|cx| MultiBuffer::singleton(buffer, cx));
3849        let range = buffer.read_with(cx, |buffer, cx| {
3850            let snapshot = buffer.snapshot(cx);
3851            snapshot.anchor_before(Point::new(1, 0))..snapshot.anchor_after(Point::new(1, 14))
3852        });
3853        let prompt_builder = Arc::new(PromptBuilder::new(None).unwrap());
3854        let codegen = cx.new_model(|cx| {
3855            CodegenAlternative::new(
3856                buffer.clone(),
3857                range.clone(),
3858                false,
3859                None,
3860                prompt_builder,
3861                cx,
3862            )
3863        });
3864
3865        let chunks_tx = simulate_response_stream(codegen.clone(), cx);
3866        chunks_tx
3867            .unbounded_send("let mut x = 0;\nx += 1;".to_string())
3868            .unwrap();
3869        drop(chunks_tx);
3870        cx.run_until_parked();
3871
3872        // The codegen is inactive, so the buffer doesn't get modified.
3873        assert_eq!(
3874            buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()),
3875            text
3876        );
3877
3878        // Activating the codegen applies the changes.
3879        codegen.update(cx, |codegen, cx| codegen.set_active(true, cx));
3880        assert_eq!(
3881            buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()),
3882            indoc! {"
3883                fn main() {
3884                    let mut x = 0;
3885                    x += 1;
3886                }
3887            "}
3888        );
3889
3890        // Deactivating the codegen undoes the changes.
3891        codegen.update(cx, |codegen, cx| codegen.set_active(false, cx));
3892        cx.run_until_parked();
3893        assert_eq!(
3894            buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()),
3895            text
3896        );
3897    }
3898
3899    #[gpui::test]
3900    async fn test_strip_invalid_spans_from_codeblock() {
3901        assert_chunks("Lorem ipsum dolor", "Lorem ipsum dolor").await;
3902        assert_chunks("```\nLorem ipsum dolor", "Lorem ipsum dolor").await;
3903        assert_chunks("```\nLorem ipsum dolor\n```", "Lorem ipsum dolor").await;
3904        assert_chunks(
3905            "```html\n```js\nLorem ipsum dolor\n```\n```",
3906            "```js\nLorem ipsum dolor\n```",
3907        )
3908        .await;
3909        assert_chunks("``\nLorem ipsum dolor\n```", "``\nLorem ipsum dolor\n```").await;
3910        assert_chunks("Lorem<|CURSOR|> ipsum", "Lorem ipsum").await;
3911        assert_chunks("Lorem ipsum", "Lorem ipsum").await;
3912        assert_chunks("```\n<|CURSOR|>Lorem ipsum\n```", "Lorem ipsum").await;
3913
3914        async fn assert_chunks(text: &str, expected_text: &str) {
3915            for chunk_size in 1..=text.len() {
3916                let actual_text = StripInvalidSpans::new(chunks(text, chunk_size))
3917                    .map(|chunk| chunk.unwrap())
3918                    .collect::<String>()
3919                    .await;
3920                assert_eq!(
3921                    actual_text, expected_text,
3922                    "failed to strip invalid spans, chunk size: {}",
3923                    chunk_size
3924                );
3925            }
3926        }
3927
3928        fn chunks(text: &str, size: usize) -> impl Stream<Item = Result<String>> {
3929            stream::iter(
3930                text.chars()
3931                    .collect::<Vec<_>>()
3932                    .chunks(size)
3933                    .map(|chunk| Ok(chunk.iter().collect::<String>()))
3934                    .collect::<Vec<_>>(),
3935            )
3936        }
3937    }
3938
3939    fn simulate_response_stream(
3940        codegen: Model<CodegenAlternative>,
3941        cx: &mut TestAppContext,
3942    ) -> mpsc::UnboundedSender<String> {
3943        let (chunks_tx, chunks_rx) = mpsc::unbounded();
3944        codegen.update(cx, |codegen, cx| {
3945            codegen.handle_stream(
3946                String::new(),
3947                String::new(),
3948                None,
3949                future::ready(Ok(LanguageModelTextStream {
3950                    message_id: None,
3951                    stream: chunks_rx.map(Ok).boxed(),
3952                })),
3953                cx,
3954            );
3955        });
3956        chunks_tx
3957    }
3958
3959    fn rust_lang() -> Language {
3960        Language::new(
3961            LanguageConfig {
3962                name: "Rust".into(),
3963                matcher: LanguageMatcher {
3964                    path_suffixes: vec!["rs".to_string()],
3965                    ..Default::default()
3966                },
3967                ..Default::default()
3968            },
3969            Some(tree_sitter_rust::LANGUAGE.into()),
3970        )
3971        .with_indents_query(
3972            r#"
3973            (call_expression) @indent
3974            (field_expression) @indent
3975            (_ "(" ")" @end) @indent
3976            (_ "{" "}" @end) @indent
3977            "#,
3978        )
3979        .unwrap()
3980    }
3981}