inline_assistant.rs

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