inline_assistant.rs

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