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