inline_assistant.rs

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