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