inline_assistant.rs

   1use crate::{
   2    humanize_token_count, prompts::PromptBuilder, AssistantPanel, AssistantPanelEvent,
   3    CharOperation, LineDiff, LineOperation, ModelSelector, StreamingDiff,
   4};
   5use anyhow::{anyhow, Context as _, Result};
   6use client::{telemetry::Telemetry, ErrorExt};
   7use collections::{hash_map, HashMap, HashSet, VecDeque};
   8use editor::{
   9    actions::{MoveDown, MoveUp, SelectAll},
  10    display_map::{
  11        BlockContext, BlockDisposition, BlockProperties, BlockStyle, CustomBlockId, RenderBlock,
  12        ToDisplayPoint,
  13    },
  14    Anchor, AnchorRangeExt, Editor, EditorElement, EditorEvent, EditorMode, EditorStyle,
  15    ExcerptRange, GutterDimensions, MultiBuffer, MultiBufferSnapshot, ToOffset, ToPoint,
  16};
  17use feature_flags::{FeatureFlagAppExt as _, ZedPro};
  18use fs::Fs;
  19use futures::{
  20    channel::mpsc,
  21    future::{BoxFuture, LocalBoxFuture},
  22    stream::{self, BoxStream},
  23    SinkExt, Stream, StreamExt,
  24};
  25use gpui::{
  26    anchored, deferred, point, AppContext, ClickEvent, EventEmitter, FocusHandle, FocusableView,
  27    FontWeight, Global, HighlightStyle, Model, ModelContext, Subscription, Task, TextStyle,
  28    UpdateGlobal, View, ViewContext, WeakView, WindowContext,
  29};
  30use language::{Buffer, IndentKind, Point, TransactionId};
  31use language_model::{
  32    LanguageModelRegistry, LanguageModelRequest, LanguageModelRequestMessage, Role,
  33};
  34use multi_buffer::MultiBufferRow;
  35use parking_lot::Mutex;
  36use rope::Rope;
  37use settings::Settings;
  38use smol::future::FutureExt;
  39use std::{
  40    future::{self, Future},
  41    mem,
  42    ops::{Range, RangeInclusive},
  43    pin::Pin,
  44    sync::Arc,
  45    task::{self, Poll},
  46    time::{Duration, Instant},
  47};
  48use text::ToOffset as _;
  49use theme::ThemeSettings;
  50use ui::{prelude::*, CheckboxWithLabel, IconButtonShape, Popover, Tooltip};
  51use util::{RangeExt, ResultExt};
  52use workspace::{notifications::NotificationId, Toast, Workspace};
  53
  54pub fn init(
  55    fs: Arc<dyn Fs>,
  56    prompt_builder: Arc<PromptBuilder>,
  57    telemetry: Arc<Telemetry>,
  58    cx: &mut AppContext,
  59) {
  60    cx.set_global(InlineAssistant::new(fs, prompt_builder, telemetry));
  61    cx.observe_new_views(|_, cx| {
  62        let workspace = cx.view().clone();
  63        InlineAssistant::update_global(cx, |inline_assistant, cx| {
  64            inline_assistant.register_workspace(&workspace, cx)
  65        })
  66    })
  67    .detach();
  68}
  69
  70const PROMPT_HISTORY_MAX_LEN: usize = 20;
  71
  72pub struct InlineAssistant {
  73    next_assist_id: InlineAssistId,
  74    next_assist_group_id: InlineAssistGroupId,
  75    assists: HashMap<InlineAssistId, InlineAssist>,
  76    assists_by_editor: HashMap<WeakView<Editor>, EditorInlineAssists>,
  77    assist_groups: HashMap<InlineAssistGroupId, InlineAssistGroup>,
  78    assist_observations:
  79        HashMap<InlineAssistId, (async_watch::Sender<()>, async_watch::Receiver<()>)>,
  80    confirmed_assists: HashMap<InlineAssistId, Model<Codegen>>,
  81    prompt_history: VecDeque<String>,
  82    prompt_builder: Arc<PromptBuilder>,
  83    telemetry: Option<Arc<Telemetry>>,
  84    fs: Arc<dyn Fs>,
  85}
  86
  87impl Global for InlineAssistant {}
  88
  89impl InlineAssistant {
  90    pub fn new(
  91        fs: Arc<dyn Fs>,
  92        prompt_builder: Arc<PromptBuilder>,
  93        telemetry: Arc<Telemetry>,
  94    ) -> Self {
  95        Self {
  96            next_assist_id: InlineAssistId::default(),
  97            next_assist_group_id: InlineAssistGroupId::default(),
  98            assists: HashMap::default(),
  99            assists_by_editor: HashMap::default(),
 100            assist_groups: HashMap::default(),
 101            assist_observations: HashMap::default(),
 102            confirmed_assists: HashMap::default(),
 103            prompt_history: VecDeque::default(),
 104            prompt_builder,
 105            telemetry: Some(telemetry),
 106            fs,
 107        }
 108    }
 109
 110    pub fn register_workspace(&mut self, workspace: &View<Workspace>, cx: &mut WindowContext) {
 111        cx.subscribe(workspace, |_, event, cx| {
 112            Self::update_global(cx, |this, cx| this.handle_workspace_event(event, cx));
 113        })
 114        .detach();
 115    }
 116
 117    fn handle_workspace_event(&mut self, event: &workspace::Event, cx: &mut WindowContext) {
 118        // When the user manually saves an editor, automatically accepts all finished transformations.
 119        if let workspace::Event::UserSavedItem { item, .. } = event {
 120            if let Some(editor) = item.upgrade().and_then(|item| item.act_as::<Editor>(cx)) {
 121                if let Some(editor_assists) = self.assists_by_editor.get(&editor.downgrade()) {
 122                    for assist_id in editor_assists.assist_ids.clone() {
 123                        let assist = &self.assists[&assist_id];
 124                        if let CodegenStatus::Done = &assist.codegen.read(cx).status {
 125                            self.finish_assist(assist_id, false, cx)
 126                        }
 127                    }
 128                }
 129            }
 130        }
 131    }
 132
 133    pub fn assist(
 134        &mut self,
 135        editor: &View<Editor>,
 136        workspace: Option<WeakView<Workspace>>,
 137        assistant_panel: Option<&View<AssistantPanel>>,
 138        initial_prompt: Option<String>,
 139        cx: &mut WindowContext,
 140    ) {
 141        let snapshot = editor.read(cx).buffer().read(cx).snapshot(cx);
 142
 143        struct CodegenRange {
 144            transform_range: Range<Point>,
 145            selection_ranges: Vec<Range<Point>>,
 146            focus_assist: bool,
 147        }
 148
 149        let newest_selection = editor.read(cx).selections.newest::<Point>(cx);
 150        let mut codegen_ranges: Vec<CodegenRange> = Vec::new();
 151        for selection in editor.read(cx).selections.all::<Point>(cx) {
 152            let selection_is_newest = selection.id == newest_selection.id;
 153            let mut transform_range = selection.start..selection.end;
 154
 155            // Expand the transform range to start/end of lines.
 156            // If a non-empty selection ends at the start of the last line, clip at the end of the penultimate line.
 157            transform_range.start.column = 0;
 158            if transform_range.end.column == 0 && transform_range.end > transform_range.start {
 159                transform_range.end.row -= 1;
 160            }
 161            transform_range.end.column = snapshot.line_len(MultiBufferRow(transform_range.end.row));
 162            let selection_range = selection.start..selection.end.min(transform_range.end);
 163
 164            // If we intersect the previous transform range,
 165            if let Some(CodegenRange {
 166                transform_range: prev_transform_range,
 167                selection_ranges,
 168                focus_assist,
 169            }) = codegen_ranges.last_mut()
 170            {
 171                if transform_range.start <= prev_transform_range.end {
 172                    prev_transform_range.end = transform_range.end;
 173                    selection_ranges.push(selection_range);
 174                    *focus_assist |= selection_is_newest;
 175                    continue;
 176                }
 177            }
 178
 179            codegen_ranges.push(CodegenRange {
 180                transform_range,
 181                selection_ranges: vec![selection_range],
 182                focus_assist: selection_is_newest,
 183            })
 184        }
 185
 186        let assist_group_id = self.next_assist_group_id.post_inc();
 187        let prompt_buffer =
 188            cx.new_model(|cx| Buffer::local(initial_prompt.unwrap_or_default(), cx));
 189        let prompt_buffer = cx.new_model(|cx| MultiBuffer::singleton(prompt_buffer, cx));
 190        let mut assists = Vec::new();
 191        let mut assist_to_focus = None;
 192
 193        for CodegenRange {
 194            transform_range,
 195            selection_ranges,
 196            focus_assist,
 197        } in codegen_ranges
 198        {
 199            let transform_range = snapshot.anchor_before(transform_range.start)
 200                ..snapshot.anchor_after(transform_range.end);
 201            let selection_ranges = selection_ranges
 202                .iter()
 203                .map(|range| snapshot.anchor_before(range.start)..snapshot.anchor_after(range.end))
 204                .collect::<Vec<_>>();
 205
 206            let codegen = cx.new_model(|cx| {
 207                Codegen::new(
 208                    editor.read(cx).buffer().clone(),
 209                    transform_range.clone(),
 210                    selection_ranges,
 211                    None,
 212                    self.telemetry.clone(),
 213                    self.prompt_builder.clone(),
 214                    cx,
 215                )
 216            });
 217
 218            let assist_id = self.next_assist_id.post_inc();
 219            let gutter_dimensions = Arc::new(Mutex::new(GutterDimensions::default()));
 220            let prompt_editor = cx.new_view(|cx| {
 221                PromptEditor::new(
 222                    assist_id,
 223                    gutter_dimensions.clone(),
 224                    self.prompt_history.clone(),
 225                    prompt_buffer.clone(),
 226                    codegen.clone(),
 227                    editor,
 228                    assistant_panel,
 229                    workspace.clone(),
 230                    self.fs.clone(),
 231                    cx,
 232                )
 233            });
 234
 235            if focus_assist {
 236                assist_to_focus = Some(assist_id);
 237            }
 238
 239            let [prompt_block_id, end_block_id] =
 240                self.insert_assist_blocks(editor, &transform_range, &prompt_editor, cx);
 241
 242            assists.push((
 243                assist_id,
 244                transform_range,
 245                prompt_editor,
 246                prompt_block_id,
 247                end_block_id,
 248            ));
 249        }
 250
 251        let editor_assists = self
 252            .assists_by_editor
 253            .entry(editor.downgrade())
 254            .or_insert_with(|| EditorInlineAssists::new(&editor, cx));
 255        let mut assist_group = InlineAssistGroup::new();
 256        for (assist_id, range, prompt_editor, prompt_block_id, end_block_id) in assists {
 257            self.assists.insert(
 258                assist_id,
 259                InlineAssist::new(
 260                    assist_id,
 261                    assist_group_id,
 262                    assistant_panel.is_some(),
 263                    editor,
 264                    &prompt_editor,
 265                    prompt_block_id,
 266                    end_block_id,
 267                    range,
 268                    prompt_editor.read(cx).codegen.clone(),
 269                    workspace.clone(),
 270                    cx,
 271                ),
 272            );
 273            assist_group.assist_ids.push(assist_id);
 274            editor_assists.assist_ids.push(assist_id);
 275        }
 276        self.assist_groups.insert(assist_group_id, assist_group);
 277
 278        if let Some(assist_id) = assist_to_focus {
 279            self.focus_assist(assist_id, cx);
 280        }
 281    }
 282
 283    #[allow(clippy::too_many_arguments)]
 284    pub fn suggest_assist(
 285        &mut self,
 286        editor: &View<Editor>,
 287        mut range: Range<Anchor>,
 288        initial_prompt: String,
 289        initial_transaction_id: Option<TransactionId>,
 290        workspace: Option<WeakView<Workspace>>,
 291        assistant_panel: Option<&View<AssistantPanel>>,
 292        cx: &mut WindowContext,
 293    ) -> InlineAssistId {
 294        let assist_group_id = self.next_assist_group_id.post_inc();
 295        let prompt_buffer = cx.new_model(|cx| Buffer::local(&initial_prompt, cx));
 296        let prompt_buffer = cx.new_model(|cx| MultiBuffer::singleton(prompt_buffer, cx));
 297
 298        let assist_id = self.next_assist_id.post_inc();
 299
 300        let buffer = editor.read(cx).buffer().clone();
 301        {
 302            let snapshot = buffer.read(cx).read(cx);
 303            range.start = range.start.bias_left(&snapshot);
 304            range.end = range.end.bias_right(&snapshot);
 305        }
 306
 307        let codegen = cx.new_model(|cx| {
 308            Codegen::new(
 309                editor.read(cx).buffer().clone(),
 310                range.clone(),
 311                vec![range.clone()],
 312                initial_transaction_id,
 313                self.telemetry.clone(),
 314                self.prompt_builder.clone(),
 315                cx,
 316            )
 317        });
 318
 319        let gutter_dimensions = Arc::new(Mutex::new(GutterDimensions::default()));
 320        let prompt_editor = cx.new_view(|cx| {
 321            PromptEditor::new(
 322                assist_id,
 323                gutter_dimensions.clone(),
 324                self.prompt_history.clone(),
 325                prompt_buffer.clone(),
 326                codegen.clone(),
 327                editor,
 328                assistant_panel,
 329                workspace.clone(),
 330                self.fs.clone(),
 331                cx,
 332            )
 333        });
 334
 335        let [prompt_block_id, end_block_id] =
 336            self.insert_assist_blocks(editor, &range, &prompt_editor, cx);
 337
 338        let editor_assists = self
 339            .assists_by_editor
 340            .entry(editor.downgrade())
 341            .or_insert_with(|| EditorInlineAssists::new(&editor, cx));
 342
 343        let mut assist_group = InlineAssistGroup::new();
 344        self.assists.insert(
 345            assist_id,
 346            InlineAssist::new(
 347                assist_id,
 348                assist_group_id,
 349                assistant_panel.is_some(),
 350                editor,
 351                &prompt_editor,
 352                prompt_block_id,
 353                end_block_id,
 354                range,
 355                prompt_editor.read(cx).codegen.clone(),
 356                workspace.clone(),
 357                cx,
 358            ),
 359        );
 360        assist_group.assist_ids.push(assist_id);
 361        editor_assists.assist_ids.push(assist_id);
 362        self.assist_groups.insert(assist_group_id, assist_group);
 363        assist_id
 364    }
 365
 366    fn insert_assist_blocks(
 367        &self,
 368        editor: &View<Editor>,
 369        range: &Range<Anchor>,
 370        prompt_editor: &View<PromptEditor>,
 371        cx: &mut WindowContext,
 372    ) -> [CustomBlockId; 2] {
 373        let prompt_editor_height = prompt_editor.update(cx, |prompt_editor, cx| {
 374            prompt_editor
 375                .editor
 376                .update(cx, |editor, cx| editor.max_point(cx).row().0 + 1 + 2)
 377        });
 378        let assist_blocks = vec![
 379            BlockProperties {
 380                style: BlockStyle::Sticky,
 381                position: range.start,
 382                height: prompt_editor_height,
 383                render: build_assist_editor_renderer(prompt_editor),
 384                disposition: BlockDisposition::Above,
 385                priority: 0,
 386            },
 387            BlockProperties {
 388                style: BlockStyle::Sticky,
 389                position: range.end,
 390                height: 0,
 391                render: Box::new(|cx| {
 392                    v_flex()
 393                        .h_full()
 394                        .w_full()
 395                        .border_t_1()
 396                        .border_color(cx.theme().status().info_border)
 397                        .into_any_element()
 398                }),
 399                disposition: BlockDisposition::Below,
 400                priority: 0,
 401            },
 402        ];
 403
 404        editor.update(cx, |editor, cx| {
 405            let block_ids = editor.insert_blocks(assist_blocks, None, cx);
 406            [block_ids[0], block_ids[1]]
 407        })
 408    }
 409
 410    fn handle_prompt_editor_focus_in(&mut self, assist_id: InlineAssistId, cx: &mut WindowContext) {
 411        let assist = &self.assists[&assist_id];
 412        let Some(decorations) = assist.decorations.as_ref() else {
 413            return;
 414        };
 415        let assist_group = self.assist_groups.get_mut(&assist.group_id).unwrap();
 416        let editor_assists = self.assists_by_editor.get_mut(&assist.editor).unwrap();
 417
 418        assist_group.active_assist_id = Some(assist_id);
 419        if assist_group.linked {
 420            for assist_id in &assist_group.assist_ids {
 421                if let Some(decorations) = self.assists[assist_id].decorations.as_ref() {
 422                    decorations.prompt_editor.update(cx, |prompt_editor, cx| {
 423                        prompt_editor.set_show_cursor_when_unfocused(true, cx)
 424                    });
 425                }
 426            }
 427        }
 428
 429        assist
 430            .editor
 431            .update(cx, |editor, cx| {
 432                let scroll_top = editor.scroll_position(cx).y;
 433                let scroll_bottom = scroll_top + editor.visible_line_count().unwrap_or(0.);
 434                let prompt_row = editor
 435                    .row_for_block(decorations.prompt_block_id, cx)
 436                    .unwrap()
 437                    .0 as f32;
 438
 439                if (scroll_top..scroll_bottom).contains(&prompt_row) {
 440                    editor_assists.scroll_lock = Some(InlineAssistScrollLock {
 441                        assist_id,
 442                        distance_from_top: prompt_row - scroll_top,
 443                    });
 444                } else {
 445                    editor_assists.scroll_lock = None;
 446                }
 447            })
 448            .ok();
 449    }
 450
 451    fn handle_prompt_editor_focus_out(
 452        &mut self,
 453        assist_id: InlineAssistId,
 454        cx: &mut WindowContext,
 455    ) {
 456        let assist = &self.assists[&assist_id];
 457        let assist_group = self.assist_groups.get_mut(&assist.group_id).unwrap();
 458        if assist_group.active_assist_id == Some(assist_id) {
 459            assist_group.active_assist_id = None;
 460            if assist_group.linked {
 461                for assist_id in &assist_group.assist_ids {
 462                    if let Some(decorations) = self.assists[assist_id].decorations.as_ref() {
 463                        decorations.prompt_editor.update(cx, |prompt_editor, cx| {
 464                            prompt_editor.set_show_cursor_when_unfocused(false, cx)
 465                        });
 466                    }
 467                }
 468            }
 469        }
 470    }
 471
 472    fn handle_prompt_editor_event(
 473        &mut self,
 474        prompt_editor: View<PromptEditor>,
 475        event: &PromptEditorEvent,
 476        cx: &mut WindowContext,
 477    ) {
 478        let assist_id = prompt_editor.read(cx).id;
 479        match event {
 480            PromptEditorEvent::StartRequested => {
 481                self.start_assist(assist_id, cx);
 482            }
 483            PromptEditorEvent::StopRequested => {
 484                self.stop_assist(assist_id, cx);
 485            }
 486            PromptEditorEvent::ConfirmRequested => {
 487                self.finish_assist(assist_id, false, cx);
 488            }
 489            PromptEditorEvent::CancelRequested => {
 490                self.finish_assist(assist_id, true, cx);
 491            }
 492            PromptEditorEvent::DismissRequested => {
 493                self.dismiss_assist(assist_id, cx);
 494            }
 495        }
 496    }
 497
 498    fn handle_editor_newline(&mut self, editor: View<Editor>, cx: &mut WindowContext) {
 499        let Some(editor_assists) = self.assists_by_editor.get(&editor.downgrade()) else {
 500            return;
 501        };
 502
 503        let editor = editor.read(cx);
 504        if editor.selections.count() == 1 {
 505            let selection = editor.selections.newest::<usize>(cx);
 506            let buffer = editor.buffer().read(cx).snapshot(cx);
 507            for assist_id in &editor_assists.assist_ids {
 508                let assist = &self.assists[assist_id];
 509                let assist_range = assist.range.to_offset(&buffer);
 510                if assist_range.contains(&selection.start) && assist_range.contains(&selection.end)
 511                {
 512                    if matches!(assist.codegen.read(cx).status, CodegenStatus::Pending) {
 513                        self.dismiss_assist(*assist_id, cx);
 514                    } else {
 515                        self.finish_assist(*assist_id, false, cx);
 516                    }
 517
 518                    return;
 519                }
 520            }
 521        }
 522
 523        cx.propagate();
 524    }
 525
 526    fn handle_editor_cancel(&mut self, editor: View<Editor>, cx: &mut WindowContext) {
 527        let Some(editor_assists) = self.assists_by_editor.get(&editor.downgrade()) else {
 528            return;
 529        };
 530
 531        let editor = editor.read(cx);
 532        if editor.selections.count() == 1 {
 533            let selection = editor.selections.newest::<usize>(cx);
 534            let buffer = editor.buffer().read(cx).snapshot(cx);
 535            let mut closest_assist_fallback = None;
 536            for assist_id in &editor_assists.assist_ids {
 537                let assist = &self.assists[assist_id];
 538                let assist_range = assist.range.to_offset(&buffer);
 539                if assist.decorations.is_some() {
 540                    if assist_range.contains(&selection.start)
 541                        && assist_range.contains(&selection.end)
 542                    {
 543                        self.focus_assist(*assist_id, cx);
 544                        return;
 545                    } else {
 546                        let distance_from_selection = assist_range
 547                            .start
 548                            .abs_diff(selection.start)
 549                            .min(assist_range.start.abs_diff(selection.end))
 550                            + assist_range
 551                                .end
 552                                .abs_diff(selection.start)
 553                                .min(assist_range.end.abs_diff(selection.end));
 554                        match closest_assist_fallback {
 555                            Some((_, old_distance)) => {
 556                                if distance_from_selection < old_distance {
 557                                    closest_assist_fallback =
 558                                        Some((assist_id, distance_from_selection));
 559                                }
 560                            }
 561                            None => {
 562                                closest_assist_fallback = Some((assist_id, distance_from_selection))
 563                            }
 564                        }
 565                    }
 566                }
 567            }
 568
 569            if let Some((&assist_id, _)) = closest_assist_fallback {
 570                self.focus_assist(assist_id, cx);
 571            }
 572        }
 573
 574        cx.propagate();
 575    }
 576
 577    fn handle_editor_release(&mut self, editor: WeakView<Editor>, cx: &mut WindowContext) {
 578        if let Some(editor_assists) = self.assists_by_editor.get_mut(&editor) {
 579            for assist_id in editor_assists.assist_ids.clone() {
 580                self.finish_assist(assist_id, true, cx);
 581            }
 582        }
 583    }
 584
 585    fn handle_editor_change(&mut self, editor: View<Editor>, cx: &mut WindowContext) {
 586        let Some(editor_assists) = self.assists_by_editor.get(&editor.downgrade()) else {
 587            return;
 588        };
 589        let Some(scroll_lock) = editor_assists.scroll_lock.as_ref() else {
 590            return;
 591        };
 592        let assist = &self.assists[&scroll_lock.assist_id];
 593        let Some(decorations) = assist.decorations.as_ref() else {
 594            return;
 595        };
 596
 597        editor.update(cx, |editor, cx| {
 598            let scroll_position = editor.scroll_position(cx);
 599            let target_scroll_top = editor
 600                .row_for_block(decorations.prompt_block_id, cx)
 601                .unwrap()
 602                .0 as f32
 603                - scroll_lock.distance_from_top;
 604            if target_scroll_top != scroll_position.y {
 605                editor.set_scroll_position(point(scroll_position.x, target_scroll_top), cx);
 606            }
 607        });
 608    }
 609
 610    fn handle_editor_event(
 611        &mut self,
 612        editor: View<Editor>,
 613        event: &EditorEvent,
 614        cx: &mut WindowContext,
 615    ) {
 616        let Some(editor_assists) = self.assists_by_editor.get_mut(&editor.downgrade()) else {
 617            return;
 618        };
 619
 620        match event {
 621            EditorEvent::Edited { transaction_id } => {
 622                let buffer = editor.read(cx).buffer().read(cx);
 623                let edited_ranges =
 624                    buffer.edited_ranges_for_transaction::<usize>(*transaction_id, cx);
 625                let snapshot = buffer.snapshot(cx);
 626
 627                for assist_id in editor_assists.assist_ids.clone() {
 628                    let assist = &self.assists[&assist_id];
 629                    if matches!(
 630                        assist.codegen.read(cx).status,
 631                        CodegenStatus::Error(_) | CodegenStatus::Done
 632                    ) {
 633                        let assist_range = assist.range.to_offset(&snapshot);
 634                        if edited_ranges
 635                            .iter()
 636                            .any(|range| range.overlaps(&assist_range))
 637                        {
 638                            self.finish_assist(assist_id, false, cx);
 639                        }
 640                    }
 641                }
 642            }
 643            EditorEvent::ScrollPositionChanged { .. } => {
 644                if let Some(scroll_lock) = editor_assists.scroll_lock.as_ref() {
 645                    let assist = &self.assists[&scroll_lock.assist_id];
 646                    if let Some(decorations) = assist.decorations.as_ref() {
 647                        let distance_from_top = editor.update(cx, |editor, cx| {
 648                            let scroll_top = editor.scroll_position(cx).y;
 649                            let prompt_row = editor
 650                                .row_for_block(decorations.prompt_block_id, cx)
 651                                .unwrap()
 652                                .0 as f32;
 653                            prompt_row - scroll_top
 654                        });
 655
 656                        if distance_from_top != scroll_lock.distance_from_top {
 657                            editor_assists.scroll_lock = None;
 658                        }
 659                    }
 660                }
 661            }
 662            EditorEvent::SelectionsChanged { .. } => {
 663                for assist_id in editor_assists.assist_ids.clone() {
 664                    let assist = &self.assists[&assist_id];
 665                    if let Some(decorations) = assist.decorations.as_ref() {
 666                        if decorations.prompt_editor.focus_handle(cx).is_focused(cx) {
 667                            return;
 668                        }
 669                    }
 670                }
 671
 672                editor_assists.scroll_lock = None;
 673            }
 674            _ => {}
 675        }
 676    }
 677
 678    pub fn finish_assist(&mut self, assist_id: InlineAssistId, undo: bool, cx: &mut WindowContext) {
 679        if let Some(assist) = self.assists.get(&assist_id) {
 680            let assist_group_id = assist.group_id;
 681            if self.assist_groups[&assist_group_id].linked {
 682                for assist_id in self.unlink_assist_group(assist_group_id, cx) {
 683                    self.finish_assist(assist_id, undo, cx);
 684                }
 685                return;
 686            }
 687        }
 688
 689        self.dismiss_assist(assist_id, cx);
 690
 691        if let Some(assist) = self.assists.remove(&assist_id) {
 692            if let hash_map::Entry::Occupied(mut entry) = self.assist_groups.entry(assist.group_id)
 693            {
 694                entry.get_mut().assist_ids.retain(|id| *id != assist_id);
 695                if entry.get().assist_ids.is_empty() {
 696                    entry.remove();
 697                }
 698            }
 699
 700            if let hash_map::Entry::Occupied(mut entry) =
 701                self.assists_by_editor.entry(assist.editor.clone())
 702            {
 703                entry.get_mut().assist_ids.retain(|id| *id != assist_id);
 704                if entry.get().assist_ids.is_empty() {
 705                    entry.remove();
 706                    if let Some(editor) = assist.editor.upgrade() {
 707                        self.update_editor_highlights(&editor, cx);
 708                    }
 709                } else {
 710                    entry.get().highlight_updates.send(()).ok();
 711                }
 712            }
 713
 714            if undo {
 715                assist.codegen.update(cx, |codegen, cx| codegen.undo(cx));
 716            } else {
 717                self.confirmed_assists.insert(assist_id, assist.codegen);
 718            }
 719        }
 720
 721        // Remove the assist from the status updates map
 722        self.assist_observations.remove(&assist_id);
 723    }
 724
 725    pub fn undo_assist(&mut self, assist_id: InlineAssistId, cx: &mut WindowContext) -> bool {
 726        let Some(codegen) = self.confirmed_assists.remove(&assist_id) else {
 727            return false;
 728        };
 729        codegen.update(cx, |this, cx| this.undo(cx));
 730        true
 731    }
 732
 733    fn dismiss_assist(&mut self, assist_id: InlineAssistId, cx: &mut WindowContext) -> bool {
 734        let Some(assist) = self.assists.get_mut(&assist_id) else {
 735            return false;
 736        };
 737        let Some(editor) = assist.editor.upgrade() else {
 738            return false;
 739        };
 740        let Some(decorations) = assist.decorations.take() else {
 741            return false;
 742        };
 743
 744        editor.update(cx, |editor, cx| {
 745            let mut to_remove = decorations.removed_line_block_ids;
 746            to_remove.insert(decorations.prompt_block_id);
 747            to_remove.insert(decorations.end_block_id);
 748            editor.remove_blocks(to_remove, None, cx);
 749        });
 750
 751        if decorations
 752            .prompt_editor
 753            .focus_handle(cx)
 754            .contains_focused(cx)
 755        {
 756            self.focus_next_assist(assist_id, cx);
 757        }
 758
 759        if let Some(editor_assists) = self.assists_by_editor.get_mut(&editor.downgrade()) {
 760            if editor_assists
 761                .scroll_lock
 762                .as_ref()
 763                .map_or(false, |lock| lock.assist_id == assist_id)
 764            {
 765                editor_assists.scroll_lock = None;
 766            }
 767            editor_assists.highlight_updates.send(()).ok();
 768        }
 769
 770        true
 771    }
 772
 773    fn focus_next_assist(&mut self, assist_id: InlineAssistId, cx: &mut WindowContext) {
 774        let Some(assist) = self.assists.get(&assist_id) else {
 775            return;
 776        };
 777
 778        let assist_group = &self.assist_groups[&assist.group_id];
 779        let assist_ix = assist_group
 780            .assist_ids
 781            .iter()
 782            .position(|id| *id == assist_id)
 783            .unwrap();
 784        let assist_ids = assist_group
 785            .assist_ids
 786            .iter()
 787            .skip(assist_ix + 1)
 788            .chain(assist_group.assist_ids.iter().take(assist_ix));
 789
 790        for assist_id in assist_ids {
 791            let assist = &self.assists[assist_id];
 792            if assist.decorations.is_some() {
 793                self.focus_assist(*assist_id, cx);
 794                return;
 795            }
 796        }
 797
 798        assist.editor.update(cx, |editor, cx| editor.focus(cx)).ok();
 799    }
 800
 801    fn focus_assist(&mut self, assist_id: InlineAssistId, cx: &mut WindowContext) {
 802        let Some(assist) = self.assists.get(&assist_id) else {
 803            return;
 804        };
 805
 806        if let Some(decorations) = assist.decorations.as_ref() {
 807            decorations.prompt_editor.update(cx, |prompt_editor, cx| {
 808                prompt_editor.editor.update(cx, |editor, cx| {
 809                    editor.focus(cx);
 810                    editor.select_all(&SelectAll, cx);
 811                })
 812            });
 813        }
 814
 815        self.scroll_to_assist(assist_id, cx);
 816    }
 817
 818    pub fn scroll_to_assist(&mut self, assist_id: InlineAssistId, cx: &mut WindowContext) {
 819        let Some(assist) = self.assists.get(&assist_id) else {
 820            return;
 821        };
 822        let Some(editor) = assist.editor.upgrade() else {
 823            return;
 824        };
 825
 826        let position = assist.range.start;
 827        editor.update(cx, |editor, cx| {
 828            editor.change_selections(None, cx, |selections| {
 829                selections.select_anchor_ranges([position..position])
 830            });
 831
 832            let mut scroll_target_top;
 833            let mut scroll_target_bottom;
 834            if let Some(decorations) = assist.decorations.as_ref() {
 835                scroll_target_top = editor
 836                    .row_for_block(decorations.prompt_block_id, cx)
 837                    .unwrap()
 838                    .0 as f32;
 839                scroll_target_bottom = editor
 840                    .row_for_block(decorations.end_block_id, cx)
 841                    .unwrap()
 842                    .0 as f32;
 843            } else {
 844                let snapshot = editor.snapshot(cx);
 845                let start_row = assist
 846                    .range
 847                    .start
 848                    .to_display_point(&snapshot.display_snapshot)
 849                    .row();
 850                scroll_target_top = start_row.0 as f32;
 851                scroll_target_bottom = scroll_target_top + 1.;
 852            }
 853            scroll_target_top -= editor.vertical_scroll_margin() as f32;
 854            scroll_target_bottom += editor.vertical_scroll_margin() as f32;
 855
 856            let height_in_lines = editor.visible_line_count().unwrap_or(0.);
 857            let scroll_top = editor.scroll_position(cx).y;
 858            let scroll_bottom = scroll_top + height_in_lines;
 859
 860            if scroll_target_top < scroll_top {
 861                editor.set_scroll_position(point(0., scroll_target_top), cx);
 862            } else if scroll_target_bottom > scroll_bottom {
 863                if (scroll_target_bottom - scroll_target_top) <= height_in_lines {
 864                    editor
 865                        .set_scroll_position(point(0., scroll_target_bottom - height_in_lines), cx);
 866                } else {
 867                    editor.set_scroll_position(point(0., scroll_target_top), cx);
 868                }
 869            }
 870        });
 871    }
 872
 873    fn unlink_assist_group(
 874        &mut self,
 875        assist_group_id: InlineAssistGroupId,
 876        cx: &mut WindowContext,
 877    ) -> Vec<InlineAssistId> {
 878        let assist_group = self.assist_groups.get_mut(&assist_group_id).unwrap();
 879        assist_group.linked = false;
 880        for assist_id in &assist_group.assist_ids {
 881            let assist = self.assists.get_mut(assist_id).unwrap();
 882            if let Some(editor_decorations) = assist.decorations.as_ref() {
 883                editor_decorations
 884                    .prompt_editor
 885                    .update(cx, |prompt_editor, cx| prompt_editor.unlink(cx));
 886            }
 887        }
 888        assist_group.assist_ids.clone()
 889    }
 890
 891    pub fn start_assist(&mut self, assist_id: InlineAssistId, cx: &mut WindowContext) {
 892        let assist = if let Some(assist) = self.assists.get_mut(&assist_id) {
 893            assist
 894        } else {
 895            return;
 896        };
 897
 898        let assist_group_id = assist.group_id;
 899        if self.assist_groups[&assist_group_id].linked {
 900            for assist_id in self.unlink_assist_group(assist_group_id, cx) {
 901                self.start_assist(assist_id, cx);
 902            }
 903            return;
 904        }
 905
 906        let Some(user_prompt) = assist.user_prompt(cx) else {
 907            return;
 908        };
 909
 910        self.prompt_history.retain(|prompt| *prompt != user_prompt);
 911        self.prompt_history.push_back(user_prompt.clone());
 912        if self.prompt_history.len() > PROMPT_HISTORY_MAX_LEN {
 913            self.prompt_history.pop_front();
 914        }
 915
 916        let assistant_panel_context = assist.assistant_panel_context(cx);
 917
 918        assist
 919            .codegen
 920            .update(cx, |codegen, cx| {
 921                codegen.start(user_prompt, assistant_panel_context, cx)
 922            })
 923            .log_err();
 924
 925        if let Some((tx, _)) = self.assist_observations.get(&assist_id) {
 926            tx.send(()).ok();
 927        }
 928    }
 929
 930    pub fn stop_assist(&mut self, assist_id: InlineAssistId, cx: &mut WindowContext) {
 931        let assist = if let Some(assist) = self.assists.get_mut(&assist_id) {
 932            assist
 933        } else {
 934            return;
 935        };
 936
 937        assist.codegen.update(cx, |codegen, cx| codegen.stop(cx));
 938
 939        if let Some((tx, _)) = self.assist_observations.get(&assist_id) {
 940            tx.send(()).ok();
 941        }
 942    }
 943
 944    pub fn assist_status(&self, assist_id: InlineAssistId, cx: &AppContext) -> InlineAssistStatus {
 945        if let Some(assist) = self.assists.get(&assist_id) {
 946            match &assist.codegen.read(cx).status {
 947                CodegenStatus::Idle => InlineAssistStatus::Idle,
 948                CodegenStatus::Pending => InlineAssistStatus::Pending,
 949                CodegenStatus::Done => InlineAssistStatus::Done,
 950                CodegenStatus::Error(_) => InlineAssistStatus::Error,
 951            }
 952        } else if self.confirmed_assists.contains_key(&assist_id) {
 953            InlineAssistStatus::Confirmed
 954        } else {
 955            InlineAssistStatus::Canceled
 956        }
 957    }
 958
 959    fn update_editor_highlights(&self, editor: &View<Editor>, cx: &mut WindowContext) {
 960        let mut gutter_pending_ranges = Vec::new();
 961        let mut gutter_transformed_ranges = Vec::new();
 962        let mut foreground_ranges = Vec::new();
 963        let mut inserted_row_ranges = Vec::new();
 964        let empty_assist_ids = Vec::new();
 965        let assist_ids = self
 966            .assists_by_editor
 967            .get(&editor.downgrade())
 968            .map_or(&empty_assist_ids, |editor_assists| {
 969                &editor_assists.assist_ids
 970            });
 971
 972        for assist_id in assist_ids {
 973            if let Some(assist) = self.assists.get(assist_id) {
 974                let codegen = assist.codegen.read(cx);
 975                let buffer = codegen.buffer.read(cx).read(cx);
 976                foreground_ranges.extend(codegen.last_equal_ranges().iter().cloned());
 977
 978                let pending_range =
 979                    codegen.edit_position.unwrap_or(assist.range.start)..assist.range.end;
 980                if pending_range.end.to_offset(&buffer) > pending_range.start.to_offset(&buffer) {
 981                    gutter_pending_ranges.push(pending_range);
 982                }
 983
 984                if let Some(edit_position) = codegen.edit_position {
 985                    let edited_range = assist.range.start..edit_position;
 986                    if edited_range.end.to_offset(&buffer) > edited_range.start.to_offset(&buffer) {
 987                        gutter_transformed_ranges.push(edited_range);
 988                    }
 989                }
 990
 991                if assist.decorations.is_some() {
 992                    inserted_row_ranges.extend(codegen.diff.inserted_row_ranges.iter().cloned());
 993                }
 994            }
 995        }
 996
 997        let snapshot = editor.read(cx).buffer().read(cx).snapshot(cx);
 998        merge_ranges(&mut foreground_ranges, &snapshot);
 999        merge_ranges(&mut gutter_pending_ranges, &snapshot);
1000        merge_ranges(&mut gutter_transformed_ranges, &snapshot);
1001        editor.update(cx, |editor, cx| {
1002            enum GutterPendingRange {}
1003            if gutter_pending_ranges.is_empty() {
1004                editor.clear_gutter_highlights::<GutterPendingRange>(cx);
1005            } else {
1006                editor.highlight_gutter::<GutterPendingRange>(
1007                    &gutter_pending_ranges,
1008                    |cx| cx.theme().status().info_background,
1009                    cx,
1010                )
1011            }
1012
1013            enum GutterTransformedRange {}
1014            if gutter_transformed_ranges.is_empty() {
1015                editor.clear_gutter_highlights::<GutterTransformedRange>(cx);
1016            } else {
1017                editor.highlight_gutter::<GutterTransformedRange>(
1018                    &gutter_transformed_ranges,
1019                    |cx| cx.theme().status().info,
1020                    cx,
1021                )
1022            }
1023
1024            if foreground_ranges.is_empty() {
1025                editor.clear_highlights::<InlineAssist>(cx);
1026            } else {
1027                editor.highlight_text::<InlineAssist>(
1028                    foreground_ranges,
1029                    HighlightStyle {
1030                        fade_out: Some(0.6),
1031                        ..Default::default()
1032                    },
1033                    cx,
1034                );
1035            }
1036
1037            editor.clear_row_highlights::<InlineAssist>();
1038            for row_range in inserted_row_ranges {
1039                editor.highlight_rows::<InlineAssist>(
1040                    row_range,
1041                    Some(cx.theme().status().info_background),
1042                    false,
1043                    cx,
1044                );
1045            }
1046        });
1047    }
1048
1049    fn update_editor_blocks(
1050        &mut self,
1051        editor: &View<Editor>,
1052        assist_id: InlineAssistId,
1053        cx: &mut WindowContext,
1054    ) {
1055        let Some(assist) = self.assists.get_mut(&assist_id) else {
1056            return;
1057        };
1058        let Some(decorations) = assist.decorations.as_mut() else {
1059            return;
1060        };
1061
1062        let codegen = assist.codegen.read(cx);
1063        let old_snapshot = codegen.snapshot.clone();
1064        let old_buffer = codegen.old_buffer.clone();
1065        let deleted_row_ranges = codegen.diff.deleted_row_ranges.clone();
1066
1067        editor.update(cx, |editor, cx| {
1068            let old_blocks = mem::take(&mut decorations.removed_line_block_ids);
1069            editor.remove_blocks(old_blocks, None, cx);
1070
1071            let mut new_blocks = Vec::new();
1072            for (new_row, old_row_range) in deleted_row_ranges {
1073                let (_, buffer_start) = old_snapshot
1074                    .point_to_buffer_offset(Point::new(*old_row_range.start(), 0))
1075                    .unwrap();
1076                let (_, buffer_end) = old_snapshot
1077                    .point_to_buffer_offset(Point::new(
1078                        *old_row_range.end(),
1079                        old_snapshot.line_len(MultiBufferRow(*old_row_range.end())),
1080                    ))
1081                    .unwrap();
1082
1083                let deleted_lines_editor = cx.new_view(|cx| {
1084                    let multi_buffer = cx.new_model(|_| {
1085                        MultiBuffer::without_headers(0, language::Capability::ReadOnly)
1086                    });
1087                    multi_buffer.update(cx, |multi_buffer, cx| {
1088                        multi_buffer.push_excerpts(
1089                            old_buffer.clone(),
1090                            Some(ExcerptRange {
1091                                context: buffer_start..buffer_end,
1092                                primary: None,
1093                            }),
1094                            cx,
1095                        );
1096                    });
1097
1098                    enum DeletedLines {}
1099                    let mut editor = Editor::for_multibuffer(multi_buffer, None, true, cx);
1100                    editor.set_soft_wrap_mode(language::language_settings::SoftWrap::None, cx);
1101                    editor.set_show_wrap_guides(false, cx);
1102                    editor.set_show_gutter(false, cx);
1103                    editor.scroll_manager.set_forbid_vertical_scroll(true);
1104                    editor.set_read_only(true);
1105                    editor.highlight_rows::<DeletedLines>(
1106                        Anchor::min()..=Anchor::max(),
1107                        Some(cx.theme().status().deleted_background),
1108                        false,
1109                        cx,
1110                    );
1111                    editor
1112                });
1113
1114                let height =
1115                    deleted_lines_editor.update(cx, |editor, cx| editor.max_point(cx).row().0 + 1);
1116                new_blocks.push(BlockProperties {
1117                    position: new_row,
1118                    height,
1119                    style: BlockStyle::Flex,
1120                    render: Box::new(move |cx| {
1121                        div()
1122                            .bg(cx.theme().status().deleted_background)
1123                            .size_full()
1124                            .h(height as f32 * cx.line_height())
1125                            .pl(cx.gutter_dimensions.full_width())
1126                            .child(deleted_lines_editor.clone())
1127                            .into_any_element()
1128                    }),
1129                    disposition: BlockDisposition::Above,
1130                    priority: 0,
1131                });
1132            }
1133
1134            decorations.removed_line_block_ids = editor
1135                .insert_blocks(new_blocks, None, cx)
1136                .into_iter()
1137                .collect();
1138        })
1139    }
1140
1141    pub fn observe_assist(&mut self, assist_id: InlineAssistId) -> async_watch::Receiver<()> {
1142        if let Some((_, rx)) = self.assist_observations.get(&assist_id) {
1143            rx.clone()
1144        } else {
1145            let (tx, rx) = async_watch::channel(());
1146            self.assist_observations.insert(assist_id, (tx, rx.clone()));
1147            rx
1148        }
1149    }
1150}
1151
1152pub enum InlineAssistStatus {
1153    Idle,
1154    Pending,
1155    Done,
1156    Error,
1157    Confirmed,
1158    Canceled,
1159}
1160
1161impl InlineAssistStatus {
1162    pub(crate) fn is_pending(&self) -> bool {
1163        matches!(self, Self::Pending)
1164    }
1165    pub(crate) fn is_confirmed(&self) -> bool {
1166        matches!(self, Self::Confirmed)
1167    }
1168    pub(crate) fn is_done(&self) -> bool {
1169        matches!(self, Self::Done)
1170    }
1171}
1172
1173struct EditorInlineAssists {
1174    assist_ids: Vec<InlineAssistId>,
1175    scroll_lock: Option<InlineAssistScrollLock>,
1176    highlight_updates: async_watch::Sender<()>,
1177    _update_highlights: Task<Result<()>>,
1178    _subscriptions: Vec<gpui::Subscription>,
1179}
1180
1181struct InlineAssistScrollLock {
1182    assist_id: InlineAssistId,
1183    distance_from_top: f32,
1184}
1185
1186impl EditorInlineAssists {
1187    #[allow(clippy::too_many_arguments)]
1188    fn new(editor: &View<Editor>, cx: &mut WindowContext) -> Self {
1189        let (highlight_updates_tx, mut highlight_updates_rx) = async_watch::channel(());
1190        Self {
1191            assist_ids: Vec::new(),
1192            scroll_lock: None,
1193            highlight_updates: highlight_updates_tx,
1194            _update_highlights: cx.spawn(|mut cx| {
1195                let editor = editor.downgrade();
1196                async move {
1197                    while let Ok(()) = highlight_updates_rx.changed().await {
1198                        let editor = editor.upgrade().context("editor was dropped")?;
1199                        cx.update_global(|assistant: &mut InlineAssistant, cx| {
1200                            assistant.update_editor_highlights(&editor, cx);
1201                        })?;
1202                    }
1203                    Ok(())
1204                }
1205            }),
1206            _subscriptions: vec![
1207                cx.observe_release(editor, {
1208                    let editor = editor.downgrade();
1209                    |_, cx| {
1210                        InlineAssistant::update_global(cx, |this, cx| {
1211                            this.handle_editor_release(editor, cx);
1212                        })
1213                    }
1214                }),
1215                cx.observe(editor, move |editor, cx| {
1216                    InlineAssistant::update_global(cx, |this, cx| {
1217                        this.handle_editor_change(editor, cx)
1218                    })
1219                }),
1220                cx.subscribe(editor, move |editor, event, cx| {
1221                    InlineAssistant::update_global(cx, |this, cx| {
1222                        this.handle_editor_event(editor, event, cx)
1223                    })
1224                }),
1225                editor.update(cx, |editor, cx| {
1226                    let editor_handle = cx.view().downgrade();
1227                    editor.register_action(
1228                        move |_: &editor::actions::Newline, cx: &mut WindowContext| {
1229                            InlineAssistant::update_global(cx, |this, cx| {
1230                                if let Some(editor) = editor_handle.upgrade() {
1231                                    this.handle_editor_newline(editor, cx)
1232                                }
1233                            })
1234                        },
1235                    )
1236                }),
1237                editor.update(cx, |editor, cx| {
1238                    let editor_handle = cx.view().downgrade();
1239                    editor.register_action(
1240                        move |_: &editor::actions::Cancel, cx: &mut WindowContext| {
1241                            InlineAssistant::update_global(cx, |this, cx| {
1242                                if let Some(editor) = editor_handle.upgrade() {
1243                                    this.handle_editor_cancel(editor, cx)
1244                                }
1245                            })
1246                        },
1247                    )
1248                }),
1249            ],
1250        }
1251    }
1252}
1253
1254struct InlineAssistGroup {
1255    assist_ids: Vec<InlineAssistId>,
1256    linked: bool,
1257    active_assist_id: Option<InlineAssistId>,
1258}
1259
1260impl InlineAssistGroup {
1261    fn new() -> Self {
1262        Self {
1263            assist_ids: Vec::new(),
1264            linked: true,
1265            active_assist_id: None,
1266        }
1267    }
1268}
1269
1270fn build_assist_editor_renderer(editor: &View<PromptEditor>) -> RenderBlock {
1271    let editor = editor.clone();
1272    Box::new(move |cx: &mut BlockContext| {
1273        *editor.read(cx).gutter_dimensions.lock() = *cx.gutter_dimensions;
1274        editor.clone().into_any_element()
1275    })
1276}
1277
1278#[derive(Copy, Clone, Debug, Eq, PartialEq)]
1279pub enum InitialInsertion {
1280    NewlineBefore,
1281    NewlineAfter,
1282}
1283
1284#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, Hash)]
1285pub struct InlineAssistId(usize);
1286
1287impl InlineAssistId {
1288    fn post_inc(&mut self) -> InlineAssistId {
1289        let id = *self;
1290        self.0 += 1;
1291        id
1292    }
1293}
1294
1295#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, Hash)]
1296struct InlineAssistGroupId(usize);
1297
1298impl InlineAssistGroupId {
1299    fn post_inc(&mut self) -> InlineAssistGroupId {
1300        let id = *self;
1301        self.0 += 1;
1302        id
1303    }
1304}
1305
1306enum PromptEditorEvent {
1307    StartRequested,
1308    StopRequested,
1309    ConfirmRequested,
1310    CancelRequested,
1311    DismissRequested,
1312}
1313
1314struct PromptEditor {
1315    id: InlineAssistId,
1316    fs: Arc<dyn Fs>,
1317    editor: View<Editor>,
1318    edited_since_done: bool,
1319    gutter_dimensions: Arc<Mutex<GutterDimensions>>,
1320    prompt_history: VecDeque<String>,
1321    prompt_history_ix: Option<usize>,
1322    pending_prompt: String,
1323    codegen: Model<Codegen>,
1324    _codegen_subscription: Subscription,
1325    editor_subscriptions: Vec<Subscription>,
1326    pending_token_count: Task<Result<()>>,
1327    token_counts: Option<TokenCounts>,
1328    _token_count_subscriptions: Vec<Subscription>,
1329    workspace: Option<WeakView<Workspace>>,
1330    show_rate_limit_notice: bool,
1331}
1332
1333#[derive(Copy, Clone)]
1334pub struct TokenCounts {
1335    total: usize,
1336    assistant_panel: usize,
1337}
1338
1339impl EventEmitter<PromptEditorEvent> for PromptEditor {}
1340
1341impl Render for PromptEditor {
1342    fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
1343        let gutter_dimensions = *self.gutter_dimensions.lock();
1344        let status = &self.codegen.read(cx).status;
1345        let buttons = match status {
1346            CodegenStatus::Idle => {
1347                vec![
1348                    IconButton::new("cancel", IconName::Close)
1349                        .icon_color(Color::Muted)
1350                        .shape(IconButtonShape::Square)
1351                        .tooltip(|cx| Tooltip::for_action("Cancel Assist", &menu::Cancel, cx))
1352                        .on_click(
1353                            cx.listener(|_, _, cx| cx.emit(PromptEditorEvent::CancelRequested)),
1354                        ),
1355                    IconButton::new("start", IconName::SparkleAlt)
1356                        .icon_color(Color::Muted)
1357                        .shape(IconButtonShape::Square)
1358                        .tooltip(|cx| Tooltip::for_action("Transform", &menu::Confirm, cx))
1359                        .on_click(
1360                            cx.listener(|_, _, cx| cx.emit(PromptEditorEvent::StartRequested)),
1361                        ),
1362                ]
1363            }
1364            CodegenStatus::Pending => {
1365                vec![
1366                    IconButton::new("cancel", IconName::Close)
1367                        .icon_color(Color::Muted)
1368                        .shape(IconButtonShape::Square)
1369                        .tooltip(|cx| Tooltip::text("Cancel Assist", cx))
1370                        .on_click(
1371                            cx.listener(|_, _, cx| cx.emit(PromptEditorEvent::CancelRequested)),
1372                        ),
1373                    IconButton::new("stop", IconName::Stop)
1374                        .icon_color(Color::Error)
1375                        .shape(IconButtonShape::Square)
1376                        .tooltip(|cx| {
1377                            Tooltip::with_meta(
1378                                "Interrupt Transformation",
1379                                Some(&menu::Cancel),
1380                                "Changes won't be discarded",
1381                                cx,
1382                            )
1383                        })
1384                        .on_click(
1385                            cx.listener(|_, _, cx| cx.emit(PromptEditorEvent::StopRequested)),
1386                        ),
1387                ]
1388            }
1389            CodegenStatus::Error(_) | CodegenStatus::Done => {
1390                vec![
1391                    IconButton::new("cancel", IconName::Close)
1392                        .icon_color(Color::Muted)
1393                        .shape(IconButtonShape::Square)
1394                        .tooltip(|cx| Tooltip::for_action("Cancel Assist", &menu::Cancel, cx))
1395                        .on_click(
1396                            cx.listener(|_, _, cx| cx.emit(PromptEditorEvent::CancelRequested)),
1397                        ),
1398                    if self.edited_since_done || matches!(status, CodegenStatus::Error(_)) {
1399                        IconButton::new("restart", IconName::RotateCw)
1400                            .icon_color(Color::Info)
1401                            .shape(IconButtonShape::Square)
1402                            .tooltip(|cx| {
1403                                Tooltip::with_meta(
1404                                    "Restart Transformation",
1405                                    Some(&menu::Confirm),
1406                                    "Changes will be discarded",
1407                                    cx,
1408                                )
1409                            })
1410                            .on_click(cx.listener(|_, _, cx| {
1411                                cx.emit(PromptEditorEvent::StartRequested);
1412                            }))
1413                    } else {
1414                        IconButton::new("confirm", IconName::Check)
1415                            .icon_color(Color::Info)
1416                            .shape(IconButtonShape::Square)
1417                            .tooltip(|cx| Tooltip::for_action("Confirm Assist", &menu::Confirm, cx))
1418                            .on_click(cx.listener(|_, _, cx| {
1419                                cx.emit(PromptEditorEvent::ConfirmRequested);
1420                            }))
1421                    },
1422                ]
1423            }
1424        };
1425
1426        h_flex()
1427            .bg(cx.theme().colors().editor_background)
1428            .border_y_1()
1429            .border_color(cx.theme().status().info_border)
1430            .size_full()
1431            .py(cx.line_height() / 2.)
1432            .on_action(cx.listener(Self::confirm))
1433            .on_action(cx.listener(Self::cancel))
1434            .on_action(cx.listener(Self::move_up))
1435            .on_action(cx.listener(Self::move_down))
1436            .child(
1437                h_flex()
1438                    .w(gutter_dimensions.full_width() + (gutter_dimensions.margin / 2.0))
1439                    .justify_center()
1440                    .gap_2()
1441                    .child(
1442                        ModelSelector::new(
1443                            self.fs.clone(),
1444                            IconButton::new("context", IconName::SlidersAlt)
1445                                .shape(IconButtonShape::Square)
1446                                .icon_size(IconSize::Small)
1447                                .icon_color(Color::Muted)
1448                                .tooltip(move |cx| {
1449                                    Tooltip::with_meta(
1450                                        format!(
1451                                            "Using {}",
1452                                            LanguageModelRegistry::read_global(cx)
1453                                                .active_model()
1454                                                .map(|model| model.name().0)
1455                                                .unwrap_or_else(|| "No model selected".into()),
1456                                        ),
1457                                        None,
1458                                        "Change Model",
1459                                        cx,
1460                                    )
1461                                }),
1462                        )
1463                        .with_info_text(
1464                            "Inline edits use context\n\
1465                            from the currently selected\n\
1466                            assistant panel tab.",
1467                        ),
1468                    )
1469                    .map(|el| {
1470                        let CodegenStatus::Error(error) = &self.codegen.read(cx).status else {
1471                            return el;
1472                        };
1473
1474                        let error_message = SharedString::from(error.to_string());
1475                        if error.error_code() == proto::ErrorCode::RateLimitExceeded
1476                            && cx.has_flag::<ZedPro>()
1477                        {
1478                            el.child(
1479                                v_flex()
1480                                    .child(
1481                                        IconButton::new("rate-limit-error", IconName::XCircle)
1482                                            .selected(self.show_rate_limit_notice)
1483                                            .shape(IconButtonShape::Square)
1484                                            .icon_size(IconSize::Small)
1485                                            .on_click(cx.listener(Self::toggle_rate_limit_notice)),
1486                                    )
1487                                    .children(self.show_rate_limit_notice.then(|| {
1488                                        deferred(
1489                                            anchored()
1490                                                .position_mode(gpui::AnchoredPositionMode::Local)
1491                                                .position(point(px(0.), px(24.)))
1492                                                .anchor(gpui::AnchorCorner::TopLeft)
1493                                                .child(self.render_rate_limit_notice(cx)),
1494                                        )
1495                                    })),
1496                            )
1497                        } else {
1498                            el.child(
1499                                div()
1500                                    .id("error")
1501                                    .tooltip(move |cx| Tooltip::text(error_message.clone(), cx))
1502                                    .child(
1503                                        Icon::new(IconName::XCircle)
1504                                            .size(IconSize::Small)
1505                                            .color(Color::Error),
1506                                    ),
1507                            )
1508                        }
1509                    }),
1510            )
1511            .child(div().flex_1().child(self.render_prompt_editor(cx)))
1512            .child(
1513                h_flex()
1514                    .gap_2()
1515                    .pr_6()
1516                    .children(self.render_token_count(cx))
1517                    .children(buttons),
1518            )
1519    }
1520}
1521
1522impl FocusableView for PromptEditor {
1523    fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
1524        self.editor.focus_handle(cx)
1525    }
1526}
1527
1528impl PromptEditor {
1529    const MAX_LINES: u8 = 8;
1530
1531    #[allow(clippy::too_many_arguments)]
1532    fn new(
1533        id: InlineAssistId,
1534        gutter_dimensions: Arc<Mutex<GutterDimensions>>,
1535        prompt_history: VecDeque<String>,
1536        prompt_buffer: Model<MultiBuffer>,
1537        codegen: Model<Codegen>,
1538        parent_editor: &View<Editor>,
1539        assistant_panel: Option<&View<AssistantPanel>>,
1540        workspace: Option<WeakView<Workspace>>,
1541        fs: Arc<dyn Fs>,
1542        cx: &mut ViewContext<Self>,
1543    ) -> Self {
1544        let prompt_editor = cx.new_view(|cx| {
1545            let mut editor = Editor::new(
1546                EditorMode::AutoHeight {
1547                    max_lines: Self::MAX_LINES as usize,
1548                },
1549                prompt_buffer,
1550                None,
1551                false,
1552                cx,
1553            );
1554            editor.set_soft_wrap_mode(language::language_settings::SoftWrap::EditorWidth, cx);
1555            // Since the prompt editors for all inline assistants are linked,
1556            // always show the cursor (even when it isn't focused) because
1557            // typing in one will make what you typed appear in all of them.
1558            editor.set_show_cursor_when_unfocused(true, cx);
1559            editor.set_placeholder_text("Add a prompt…", cx);
1560            editor
1561        });
1562
1563        let mut token_count_subscriptions = Vec::new();
1564        token_count_subscriptions
1565            .push(cx.subscribe(parent_editor, Self::handle_parent_editor_event));
1566        if let Some(assistant_panel) = assistant_panel {
1567            token_count_subscriptions
1568                .push(cx.subscribe(assistant_panel, Self::handle_assistant_panel_event));
1569        }
1570
1571        let mut this = Self {
1572            id,
1573            editor: prompt_editor,
1574            edited_since_done: false,
1575            gutter_dimensions,
1576            prompt_history,
1577            prompt_history_ix: None,
1578            pending_prompt: String::new(),
1579            _codegen_subscription: cx.observe(&codegen, Self::handle_codegen_changed),
1580            editor_subscriptions: Vec::new(),
1581            codegen,
1582            fs,
1583            pending_token_count: Task::ready(Ok(())),
1584            token_counts: None,
1585            _token_count_subscriptions: token_count_subscriptions,
1586            workspace,
1587            show_rate_limit_notice: false,
1588        };
1589        this.count_tokens(cx);
1590        this.subscribe_to_editor(cx);
1591        this
1592    }
1593
1594    fn subscribe_to_editor(&mut self, cx: &mut ViewContext<Self>) {
1595        self.editor_subscriptions.clear();
1596        self.editor_subscriptions
1597            .push(cx.subscribe(&self.editor, Self::handle_prompt_editor_events));
1598    }
1599
1600    fn set_show_cursor_when_unfocused(
1601        &mut self,
1602        show_cursor_when_unfocused: bool,
1603        cx: &mut ViewContext<Self>,
1604    ) {
1605        self.editor.update(cx, |editor, cx| {
1606            editor.set_show_cursor_when_unfocused(show_cursor_when_unfocused, cx)
1607        });
1608    }
1609
1610    fn unlink(&mut self, cx: &mut ViewContext<Self>) {
1611        let prompt = self.prompt(cx);
1612        let focus = self.editor.focus_handle(cx).contains_focused(cx);
1613        self.editor = cx.new_view(|cx| {
1614            let mut editor = Editor::auto_height(Self::MAX_LINES as usize, cx);
1615            editor.set_soft_wrap_mode(language::language_settings::SoftWrap::EditorWidth, cx);
1616            editor.set_placeholder_text("Add a prompt…", cx);
1617            editor.set_text(prompt, cx);
1618            if focus {
1619                editor.focus(cx);
1620            }
1621            editor
1622        });
1623        self.subscribe_to_editor(cx);
1624    }
1625
1626    fn prompt(&self, cx: &AppContext) -> String {
1627        self.editor.read(cx).text(cx)
1628    }
1629
1630    fn toggle_rate_limit_notice(&mut self, _: &ClickEvent, cx: &mut ViewContext<Self>) {
1631        self.show_rate_limit_notice = !self.show_rate_limit_notice;
1632        if self.show_rate_limit_notice {
1633            cx.focus_view(&self.editor);
1634        }
1635        cx.notify();
1636    }
1637
1638    fn handle_parent_editor_event(
1639        &mut self,
1640        _: View<Editor>,
1641        event: &EditorEvent,
1642        cx: &mut ViewContext<Self>,
1643    ) {
1644        if let EditorEvent::BufferEdited { .. } = event {
1645            self.count_tokens(cx);
1646        }
1647    }
1648
1649    fn handle_assistant_panel_event(
1650        &mut self,
1651        _: View<AssistantPanel>,
1652        event: &AssistantPanelEvent,
1653        cx: &mut ViewContext<Self>,
1654    ) {
1655        let AssistantPanelEvent::ContextEdited { .. } = event;
1656        self.count_tokens(cx);
1657    }
1658
1659    fn count_tokens(&mut self, cx: &mut ViewContext<Self>) {
1660        let assist_id = self.id;
1661        self.pending_token_count = cx.spawn(|this, mut cx| async move {
1662            cx.background_executor().timer(Duration::from_secs(1)).await;
1663            let token_count = cx
1664                .update_global(|inline_assistant: &mut InlineAssistant, cx| {
1665                    let assist = inline_assistant
1666                        .assists
1667                        .get(&assist_id)
1668                        .context("assist not found")?;
1669                    anyhow::Ok(assist.count_tokens(cx))
1670                })??
1671                .await?;
1672
1673            this.update(&mut cx, |this, cx| {
1674                this.token_counts = Some(token_count);
1675                cx.notify();
1676            })
1677        })
1678    }
1679
1680    fn handle_prompt_editor_events(
1681        &mut self,
1682        _: View<Editor>,
1683        event: &EditorEvent,
1684        cx: &mut ViewContext<Self>,
1685    ) {
1686        match event {
1687            EditorEvent::Edited { .. } => {
1688                let prompt = self.editor.read(cx).text(cx);
1689                if self
1690                    .prompt_history_ix
1691                    .map_or(true, |ix| self.prompt_history[ix] != prompt)
1692                {
1693                    self.prompt_history_ix.take();
1694                    self.pending_prompt = prompt;
1695                }
1696
1697                self.edited_since_done = true;
1698                cx.notify();
1699            }
1700            EditorEvent::BufferEdited => {
1701                self.count_tokens(cx);
1702            }
1703            EditorEvent::Blurred => {
1704                if self.show_rate_limit_notice {
1705                    self.show_rate_limit_notice = false;
1706                    cx.notify();
1707                }
1708            }
1709            _ => {}
1710        }
1711    }
1712
1713    fn handle_codegen_changed(&mut self, _: Model<Codegen>, cx: &mut ViewContext<Self>) {
1714        match &self.codegen.read(cx).status {
1715            CodegenStatus::Idle => {
1716                self.editor
1717                    .update(cx, |editor, _| editor.set_read_only(false));
1718            }
1719            CodegenStatus::Pending => {
1720                self.editor
1721                    .update(cx, |editor, _| editor.set_read_only(true));
1722            }
1723            CodegenStatus::Done => {
1724                self.edited_since_done = false;
1725                self.editor
1726                    .update(cx, |editor, _| editor.set_read_only(false));
1727            }
1728            CodegenStatus::Error(error) => {
1729                if cx.has_flag::<ZedPro>()
1730                    && error.error_code() == proto::ErrorCode::RateLimitExceeded
1731                    && !dismissed_rate_limit_notice()
1732                {
1733                    self.show_rate_limit_notice = true;
1734                    cx.notify();
1735                }
1736
1737                self.edited_since_done = false;
1738                self.editor
1739                    .update(cx, |editor, _| editor.set_read_only(false));
1740            }
1741        }
1742    }
1743
1744    fn cancel(&mut self, _: &editor::actions::Cancel, cx: &mut ViewContext<Self>) {
1745        match &self.codegen.read(cx).status {
1746            CodegenStatus::Idle | CodegenStatus::Done | CodegenStatus::Error(_) => {
1747                cx.emit(PromptEditorEvent::CancelRequested);
1748            }
1749            CodegenStatus::Pending => {
1750                cx.emit(PromptEditorEvent::StopRequested);
1751            }
1752        }
1753    }
1754
1755    fn confirm(&mut self, _: &menu::Confirm, cx: &mut ViewContext<Self>) {
1756        match &self.codegen.read(cx).status {
1757            CodegenStatus::Idle => {
1758                cx.emit(PromptEditorEvent::StartRequested);
1759            }
1760            CodegenStatus::Pending => {
1761                cx.emit(PromptEditorEvent::DismissRequested);
1762            }
1763            CodegenStatus::Done | CodegenStatus::Error(_) => {
1764                if self.edited_since_done {
1765                    cx.emit(PromptEditorEvent::StartRequested);
1766                } else {
1767                    cx.emit(PromptEditorEvent::ConfirmRequested);
1768                }
1769            }
1770        }
1771    }
1772
1773    fn move_up(&mut self, _: &MoveUp, cx: &mut ViewContext<Self>) {
1774        if let Some(ix) = self.prompt_history_ix {
1775            if ix > 0 {
1776                self.prompt_history_ix = Some(ix - 1);
1777                let prompt = self.prompt_history[ix - 1].as_str();
1778                self.editor.update(cx, |editor, cx| {
1779                    editor.set_text(prompt, cx);
1780                    editor.move_to_beginning(&Default::default(), cx);
1781                });
1782            }
1783        } else if !self.prompt_history.is_empty() {
1784            self.prompt_history_ix = Some(self.prompt_history.len() - 1);
1785            let prompt = self.prompt_history[self.prompt_history.len() - 1].as_str();
1786            self.editor.update(cx, |editor, cx| {
1787                editor.set_text(prompt, cx);
1788                editor.move_to_beginning(&Default::default(), cx);
1789            });
1790        }
1791    }
1792
1793    fn move_down(&mut self, _: &MoveDown, cx: &mut ViewContext<Self>) {
1794        if let Some(ix) = self.prompt_history_ix {
1795            if ix < self.prompt_history.len() - 1 {
1796                self.prompt_history_ix = Some(ix + 1);
1797                let prompt = self.prompt_history[ix + 1].as_str();
1798                self.editor.update(cx, |editor, cx| {
1799                    editor.set_text(prompt, cx);
1800                    editor.move_to_end(&Default::default(), cx)
1801                });
1802            } else {
1803                self.prompt_history_ix = None;
1804                let prompt = self.pending_prompt.as_str();
1805                self.editor.update(cx, |editor, cx| {
1806                    editor.set_text(prompt, cx);
1807                    editor.move_to_end(&Default::default(), cx)
1808                });
1809            }
1810        }
1811    }
1812
1813    fn render_token_count(&self, cx: &mut ViewContext<Self>) -> Option<impl IntoElement> {
1814        let model = LanguageModelRegistry::read_global(cx).active_model()?;
1815        let token_counts = self.token_counts?;
1816        let max_token_count = model.max_token_count();
1817
1818        let remaining_tokens = max_token_count as isize - token_counts.total as isize;
1819        let token_count_color = if remaining_tokens <= 0 {
1820            Color::Error
1821        } else if token_counts.total as f32 / max_token_count as f32 >= 0.8 {
1822            Color::Warning
1823        } else {
1824            Color::Muted
1825        };
1826
1827        let mut token_count = h_flex()
1828            .id("token_count")
1829            .gap_0p5()
1830            .child(
1831                Label::new(humanize_token_count(token_counts.total))
1832                    .size(LabelSize::Small)
1833                    .color(token_count_color),
1834            )
1835            .child(Label::new("/").size(LabelSize::Small).color(Color::Muted))
1836            .child(
1837                Label::new(humanize_token_count(max_token_count))
1838                    .size(LabelSize::Small)
1839                    .color(Color::Muted),
1840            );
1841        if let Some(workspace) = self.workspace.clone() {
1842            token_count = token_count
1843                .tooltip(move |cx| {
1844                    Tooltip::with_meta(
1845                        format!(
1846                            "Tokens Used ({} from the Assistant Panel)",
1847                            humanize_token_count(token_counts.assistant_panel)
1848                        ),
1849                        None,
1850                        "Click to open the Assistant Panel",
1851                        cx,
1852                    )
1853                })
1854                .cursor_pointer()
1855                .on_mouse_down(gpui::MouseButton::Left, |_, cx| cx.stop_propagation())
1856                .on_click(move |_, cx| {
1857                    cx.stop_propagation();
1858                    workspace
1859                        .update(cx, |workspace, cx| {
1860                            workspace.focus_panel::<AssistantPanel>(cx)
1861                        })
1862                        .ok();
1863                });
1864        } else {
1865            token_count = token_count
1866                .cursor_default()
1867                .tooltip(|cx| Tooltip::text("Tokens used", cx));
1868        }
1869
1870        Some(token_count)
1871    }
1872
1873    fn render_prompt_editor(&self, cx: &mut ViewContext<Self>) -> impl IntoElement {
1874        let settings = ThemeSettings::get_global(cx);
1875        let text_style = TextStyle {
1876            color: if self.editor.read(cx).read_only(cx) {
1877                cx.theme().colors().text_disabled
1878            } else {
1879                cx.theme().colors().text
1880            },
1881            font_family: settings.ui_font.family.clone(),
1882            font_features: settings.ui_font.features.clone(),
1883            font_fallbacks: settings.ui_font.fallbacks.clone(),
1884            font_size: rems(0.875).into(),
1885            font_weight: settings.ui_font.weight,
1886            line_height: relative(1.3),
1887            ..Default::default()
1888        };
1889        EditorElement::new(
1890            &self.editor,
1891            EditorStyle {
1892                background: cx.theme().colors().editor_background,
1893                local_player: cx.theme().players().local(),
1894                text: text_style,
1895                ..Default::default()
1896            },
1897        )
1898    }
1899
1900    fn render_rate_limit_notice(&self, cx: &mut ViewContext<Self>) -> impl IntoElement {
1901        Popover::new().child(
1902            v_flex()
1903                .occlude()
1904                .p_2()
1905                .child(
1906                    Label::new("Out of Tokens")
1907                        .size(LabelSize::Small)
1908                        .weight(FontWeight::BOLD),
1909                )
1910                .child(Label::new(
1911                    "Try Zed Pro for higher limits, a wider range of models, and more.",
1912                ))
1913                .child(
1914                    h_flex()
1915                        .justify_between()
1916                        .child(CheckboxWithLabel::new(
1917                            "dont-show-again",
1918                            Label::new("Don't show again"),
1919                            if dismissed_rate_limit_notice() {
1920                                ui::Selection::Selected
1921                            } else {
1922                                ui::Selection::Unselected
1923                            },
1924                            |selection, cx| {
1925                                let is_dismissed = match selection {
1926                                    ui::Selection::Unselected => false,
1927                                    ui::Selection::Indeterminate => return,
1928                                    ui::Selection::Selected => true,
1929                                };
1930
1931                                set_rate_limit_notice_dismissed(is_dismissed, cx)
1932                            },
1933                        ))
1934                        .child(
1935                            h_flex()
1936                                .gap_2()
1937                                .child(
1938                                    Button::new("dismiss", "Dismiss")
1939                                        .style(ButtonStyle::Transparent)
1940                                        .on_click(cx.listener(Self::toggle_rate_limit_notice)),
1941                                )
1942                                .child(Button::new("more-info", "More Info").on_click(
1943                                    |_event, cx| {
1944                                        cx.dispatch_action(Box::new(
1945                                            zed_actions::OpenAccountSettings,
1946                                        ))
1947                                    },
1948                                )),
1949                        ),
1950                ),
1951        )
1952    }
1953}
1954
1955const DISMISSED_RATE_LIMIT_NOTICE_KEY: &str = "dismissed-rate-limit-notice";
1956
1957fn dismissed_rate_limit_notice() -> bool {
1958    db::kvp::KEY_VALUE_STORE
1959        .read_kvp(DISMISSED_RATE_LIMIT_NOTICE_KEY)
1960        .log_err()
1961        .map_or(false, |s| s.is_some())
1962}
1963
1964fn set_rate_limit_notice_dismissed(is_dismissed: bool, cx: &mut AppContext) {
1965    db::write_and_log(cx, move || async move {
1966        if is_dismissed {
1967            db::kvp::KEY_VALUE_STORE
1968                .write_kvp(DISMISSED_RATE_LIMIT_NOTICE_KEY.into(), "1".into())
1969                .await
1970        } else {
1971            db::kvp::KEY_VALUE_STORE
1972                .delete_kvp(DISMISSED_RATE_LIMIT_NOTICE_KEY.into())
1973                .await
1974        }
1975    })
1976}
1977
1978struct InlineAssist {
1979    group_id: InlineAssistGroupId,
1980    range: Range<Anchor>,
1981    editor: WeakView<Editor>,
1982    decorations: Option<InlineAssistDecorations>,
1983    codegen: Model<Codegen>,
1984    _subscriptions: Vec<Subscription>,
1985    workspace: Option<WeakView<Workspace>>,
1986    include_context: bool,
1987}
1988
1989impl InlineAssist {
1990    #[allow(clippy::too_many_arguments)]
1991    fn new(
1992        assist_id: InlineAssistId,
1993        group_id: InlineAssistGroupId,
1994        include_context: bool,
1995        editor: &View<Editor>,
1996        prompt_editor: &View<PromptEditor>,
1997        prompt_block_id: CustomBlockId,
1998        end_block_id: CustomBlockId,
1999        range: Range<Anchor>,
2000        codegen: Model<Codegen>,
2001        workspace: Option<WeakView<Workspace>>,
2002        cx: &mut WindowContext,
2003    ) -> Self {
2004        let prompt_editor_focus_handle = prompt_editor.focus_handle(cx);
2005        InlineAssist {
2006            group_id,
2007            include_context,
2008            editor: editor.downgrade(),
2009            decorations: Some(InlineAssistDecorations {
2010                prompt_block_id,
2011                prompt_editor: prompt_editor.clone(),
2012                removed_line_block_ids: HashSet::default(),
2013                end_block_id,
2014            }),
2015            range,
2016            codegen: codegen.clone(),
2017            workspace: workspace.clone(),
2018            _subscriptions: vec![
2019                cx.on_focus_in(&prompt_editor_focus_handle, move |cx| {
2020                    InlineAssistant::update_global(cx, |this, cx| {
2021                        this.handle_prompt_editor_focus_in(assist_id, cx)
2022                    })
2023                }),
2024                cx.on_focus_out(&prompt_editor_focus_handle, move |_, cx| {
2025                    InlineAssistant::update_global(cx, |this, cx| {
2026                        this.handle_prompt_editor_focus_out(assist_id, cx)
2027                    })
2028                }),
2029                cx.subscribe(prompt_editor, |prompt_editor, event, cx| {
2030                    InlineAssistant::update_global(cx, |this, cx| {
2031                        this.handle_prompt_editor_event(prompt_editor, event, cx)
2032                    })
2033                }),
2034                cx.observe(&codegen, {
2035                    let editor = editor.downgrade();
2036                    move |_, cx| {
2037                        if let Some(editor) = editor.upgrade() {
2038                            InlineAssistant::update_global(cx, |this, cx| {
2039                                if let Some(editor_assists) =
2040                                    this.assists_by_editor.get(&editor.downgrade())
2041                                {
2042                                    editor_assists.highlight_updates.send(()).ok();
2043                                }
2044
2045                                this.update_editor_blocks(&editor, assist_id, cx);
2046                            })
2047                        }
2048                    }
2049                }),
2050                cx.subscribe(&codegen, move |codegen, event, cx| {
2051                    InlineAssistant::update_global(cx, |this, cx| match event {
2052                        CodegenEvent::Undone => this.finish_assist(assist_id, false, cx),
2053                        CodegenEvent::Finished => {
2054                            let assist = if let Some(assist) = this.assists.get(&assist_id) {
2055                                assist
2056                            } else {
2057                                return;
2058                            };
2059
2060                            if let CodegenStatus::Error(error) = &codegen.read(cx).status {
2061                                if assist.decorations.is_none() {
2062                                    if let Some(workspace) = assist
2063                                        .workspace
2064                                        .as_ref()
2065                                        .and_then(|workspace| workspace.upgrade())
2066                                    {
2067                                        let error = format!("Inline assistant error: {}", error);
2068                                        workspace.update(cx, |workspace, cx| {
2069                                            struct InlineAssistantError;
2070
2071                                            let id =
2072                                                NotificationId::identified::<InlineAssistantError>(
2073                                                    assist_id.0,
2074                                                );
2075
2076                                            workspace.show_toast(Toast::new(id, error), cx);
2077                                        })
2078                                    }
2079                                }
2080                            }
2081
2082                            if assist.decorations.is_none() {
2083                                this.finish_assist(assist_id, false, cx);
2084                            } else if let Some(tx) = this.assist_observations.get(&assist_id) {
2085                                tx.0.send(()).ok();
2086                            }
2087                        }
2088                    })
2089                }),
2090            ],
2091        }
2092    }
2093
2094    fn user_prompt(&self, cx: &AppContext) -> Option<String> {
2095        let decorations = self.decorations.as_ref()?;
2096        Some(decorations.prompt_editor.read(cx).prompt(cx))
2097    }
2098
2099    fn assistant_panel_context(&self, cx: &WindowContext) -> Option<LanguageModelRequest> {
2100        if self.include_context {
2101            let workspace = self.workspace.as_ref()?;
2102            let workspace = workspace.upgrade()?.read(cx);
2103            let assistant_panel = workspace.panel::<AssistantPanel>(cx)?;
2104            Some(
2105                assistant_panel
2106                    .read(cx)
2107                    .active_context(cx)?
2108                    .read(cx)
2109                    .to_completion_request(cx),
2110            )
2111        } else {
2112            None
2113        }
2114    }
2115
2116    pub fn count_tokens(&self, cx: &WindowContext) -> BoxFuture<'static, Result<TokenCounts>> {
2117        let Some(user_prompt) = self.user_prompt(cx) else {
2118            return future::ready(Err(anyhow!("no user prompt"))).boxed();
2119        };
2120        let assistant_panel_context = self.assistant_panel_context(cx);
2121        self.codegen
2122            .read(cx)
2123            .count_tokens(user_prompt, assistant_panel_context, cx)
2124    }
2125}
2126
2127struct InlineAssistDecorations {
2128    prompt_block_id: CustomBlockId,
2129    prompt_editor: View<PromptEditor>,
2130    removed_line_block_ids: HashSet<CustomBlockId>,
2131    end_block_id: CustomBlockId,
2132}
2133
2134#[derive(Debug)]
2135pub enum CodegenEvent {
2136    Finished,
2137    Undone,
2138}
2139
2140pub struct Codegen {
2141    buffer: Model<MultiBuffer>,
2142    old_buffer: Model<Buffer>,
2143    snapshot: MultiBufferSnapshot,
2144    transform_range: Range<Anchor>,
2145    selected_ranges: Vec<Range<Anchor>>,
2146    edit_position: Option<Anchor>,
2147    last_equal_ranges: Vec<Range<Anchor>>,
2148    initial_transaction_id: Option<TransactionId>,
2149    transformation_transaction_id: Option<TransactionId>,
2150    status: CodegenStatus,
2151    generation: Task<()>,
2152    diff: Diff,
2153    telemetry: Option<Arc<Telemetry>>,
2154    _subscription: gpui::Subscription,
2155    prompt_builder: Arc<PromptBuilder>,
2156}
2157
2158enum CodegenStatus {
2159    Idle,
2160    Pending,
2161    Done,
2162    Error(anyhow::Error),
2163}
2164
2165#[derive(Default)]
2166struct Diff {
2167    deleted_row_ranges: Vec<(Anchor, RangeInclusive<u32>)>,
2168    inserted_row_ranges: Vec<RangeInclusive<Anchor>>,
2169}
2170
2171impl Diff {
2172    fn is_empty(&self) -> bool {
2173        self.deleted_row_ranges.is_empty() && self.inserted_row_ranges.is_empty()
2174    }
2175}
2176
2177impl EventEmitter<CodegenEvent> for Codegen {}
2178
2179impl Codegen {
2180    pub fn new(
2181        buffer: Model<MultiBuffer>,
2182        transform_range: Range<Anchor>,
2183        selected_ranges: Vec<Range<Anchor>>,
2184        initial_transaction_id: Option<TransactionId>,
2185        telemetry: Option<Arc<Telemetry>>,
2186        builder: Arc<PromptBuilder>,
2187        cx: &mut ModelContext<Self>,
2188    ) -> Self {
2189        let snapshot = buffer.read(cx).snapshot(cx);
2190
2191        let (old_buffer, _, _) = buffer
2192            .read(cx)
2193            .range_to_buffer_ranges(transform_range.clone(), cx)
2194            .pop()
2195            .unwrap();
2196        let old_buffer = cx.new_model(|cx| {
2197            let old_buffer = old_buffer.read(cx);
2198            let text = old_buffer.as_rope().clone();
2199            let line_ending = old_buffer.line_ending();
2200            let language = old_buffer.language().cloned();
2201            let language_registry = old_buffer.language_registry();
2202
2203            let mut buffer = Buffer::local_normalized(text, line_ending, cx);
2204            buffer.set_language(language, cx);
2205            if let Some(language_registry) = language_registry {
2206                buffer.set_language_registry(language_registry)
2207            }
2208            buffer
2209        });
2210
2211        Self {
2212            buffer: buffer.clone(),
2213            old_buffer,
2214            edit_position: None,
2215            snapshot,
2216            last_equal_ranges: Default::default(),
2217            transformation_transaction_id: None,
2218            status: CodegenStatus::Idle,
2219            generation: Task::ready(()),
2220            diff: Diff::default(),
2221            telemetry,
2222            _subscription: cx.subscribe(&buffer, Self::handle_buffer_event),
2223            initial_transaction_id,
2224            prompt_builder: builder,
2225            transform_range,
2226            selected_ranges,
2227        }
2228    }
2229
2230    fn handle_buffer_event(
2231        &mut self,
2232        _buffer: Model<MultiBuffer>,
2233        event: &multi_buffer::Event,
2234        cx: &mut ModelContext<Self>,
2235    ) {
2236        if let multi_buffer::Event::TransactionUndone { transaction_id } = event {
2237            if self.transformation_transaction_id == Some(*transaction_id) {
2238                self.transformation_transaction_id = None;
2239                self.generation = Task::ready(());
2240                cx.emit(CodegenEvent::Undone);
2241            }
2242        }
2243    }
2244
2245    pub fn last_equal_ranges(&self) -> &[Range<Anchor>] {
2246        &self.last_equal_ranges
2247    }
2248
2249    pub fn count_tokens(
2250        &self,
2251        user_prompt: String,
2252        assistant_panel_context: Option<LanguageModelRequest>,
2253        cx: &AppContext,
2254    ) -> BoxFuture<'static, Result<TokenCounts>> {
2255        if let Some(model) = LanguageModelRegistry::read_global(cx).active_model() {
2256            let request = self.build_request(user_prompt, assistant_panel_context.clone(), cx);
2257            match request {
2258                Ok(request) => {
2259                    let total_count = model.count_tokens(request.clone(), cx);
2260                    let assistant_panel_count = assistant_panel_context
2261                        .map(|context| model.count_tokens(context, cx))
2262                        .unwrap_or_else(|| future::ready(Ok(0)).boxed());
2263
2264                    async move {
2265                        Ok(TokenCounts {
2266                            total: total_count.await?,
2267                            assistant_panel: assistant_panel_count.await?,
2268                        })
2269                    }
2270                    .boxed()
2271                }
2272                Err(error) => futures::future::ready(Err(error)).boxed(),
2273            }
2274        } else {
2275            future::ready(Err(anyhow!("no active model"))).boxed()
2276        }
2277    }
2278
2279    pub fn start(
2280        &mut self,
2281        user_prompt: String,
2282        assistant_panel_context: Option<LanguageModelRequest>,
2283        cx: &mut ModelContext<Self>,
2284    ) -> Result<()> {
2285        let model = LanguageModelRegistry::read_global(cx)
2286            .active_model()
2287            .context("no active model")?;
2288
2289        if let Some(transformation_transaction_id) = self.transformation_transaction_id.take() {
2290            self.buffer.update(cx, |buffer, cx| {
2291                buffer.undo_transaction(transformation_transaction_id, cx);
2292            });
2293        }
2294
2295        self.edit_position = Some(self.transform_range.start.bias_right(&self.snapshot));
2296
2297        let telemetry_id = model.telemetry_id();
2298        let chunks: LocalBoxFuture<Result<BoxStream<Result<String>>>> =
2299            if user_prompt.trim().to_lowercase() == "delete" {
2300                async { Ok(stream::empty().boxed()) }.boxed_local()
2301            } else {
2302                let request = self.build_request(user_prompt, assistant_panel_context, cx)?;
2303
2304                let chunks =
2305                    cx.spawn(|_, cx| async move { model.stream_completion(request, &cx).await });
2306                async move { Ok(chunks.await?.boxed()) }.boxed_local()
2307            };
2308        self.handle_stream(telemetry_id, self.transform_range.clone(), chunks, cx);
2309        Ok(())
2310    }
2311
2312    fn build_request(
2313        &self,
2314        user_prompt: String,
2315        assistant_panel_context: Option<LanguageModelRequest>,
2316        cx: &AppContext,
2317    ) -> Result<LanguageModelRequest> {
2318        let buffer = self.buffer.read(cx).snapshot(cx);
2319        let language = buffer.language_at(self.transform_range.start);
2320        let language_name = if let Some(language) = language.as_ref() {
2321            if Arc::ptr_eq(language, &language::PLAIN_TEXT) {
2322                None
2323            } else {
2324                Some(language.name())
2325            }
2326        } else {
2327            None
2328        };
2329
2330        // Higher Temperature increases the randomness of model outputs.
2331        // If Markdown or No Language is Known, increase the randomness for more creative output
2332        // If Code, decrease temperature to get more deterministic outputs
2333        let temperature = if let Some(language) = language_name.clone() {
2334            if language.as_ref() == "Markdown" {
2335                1.0
2336            } else {
2337                0.5
2338            }
2339        } else {
2340            1.0
2341        };
2342
2343        let language_name = language_name.as_deref();
2344        let start = buffer.point_to_buffer_offset(self.transform_range.start);
2345        let end = buffer.point_to_buffer_offset(self.transform_range.end);
2346        let (buffer, range) = if let Some((start, end)) = start.zip(end) {
2347            let (start_buffer, start_buffer_offset) = start;
2348            let (end_buffer, end_buffer_offset) = end;
2349            if start_buffer.remote_id() == end_buffer.remote_id() {
2350                (start_buffer.clone(), start_buffer_offset..end_buffer_offset)
2351            } else {
2352                return Err(anyhow::anyhow!("invalid transformation range"));
2353            }
2354        } else {
2355            return Err(anyhow::anyhow!("invalid transformation range"));
2356        };
2357
2358        let selected_ranges = self
2359            .selected_ranges
2360            .iter()
2361            .map(|range| {
2362                let start = range.start.text_anchor.to_offset(&buffer);
2363                let end = range.end.text_anchor.to_offset(&buffer);
2364                start..end
2365            })
2366            .collect::<Vec<_>>();
2367
2368        let prompt = self
2369            .prompt_builder
2370            .generate_content_prompt(user_prompt, language_name, buffer, range, selected_ranges)
2371            .map_err(|e| anyhow::anyhow!("Failed to generate content prompt: {}", e))?;
2372
2373        let mut messages = Vec::new();
2374        if let Some(context_request) = assistant_panel_context {
2375            messages = context_request.messages;
2376        }
2377
2378        messages.push(LanguageModelRequestMessage {
2379            role: Role::User,
2380            content: prompt,
2381        });
2382
2383        Ok(LanguageModelRequest {
2384            messages,
2385            stop: vec!["|END|>".to_string()],
2386            temperature,
2387        })
2388    }
2389
2390    pub fn handle_stream(
2391        &mut self,
2392        model_telemetry_id: String,
2393        edit_range: Range<Anchor>,
2394        stream: impl 'static + Future<Output = Result<BoxStream<'static, Result<String>>>>,
2395        cx: &mut ModelContext<Self>,
2396    ) {
2397        let snapshot = self.snapshot.clone();
2398        let selected_text = snapshot
2399            .text_for_range(edit_range.start..edit_range.end)
2400            .collect::<Rope>();
2401
2402        let selection_start = edit_range.start.to_point(&snapshot);
2403
2404        // Start with the indentation of the first line in the selection
2405        let mut suggested_line_indent = snapshot
2406            .suggested_indents(selection_start.row..=selection_start.row, cx)
2407            .into_values()
2408            .next()
2409            .unwrap_or_else(|| snapshot.indent_size_for_line(MultiBufferRow(selection_start.row)));
2410
2411        // If the first line in the selection does not have indentation, check the following lines
2412        if suggested_line_indent.len == 0 && suggested_line_indent.kind == IndentKind::Space {
2413            for row in selection_start.row..=edit_range.end.to_point(&snapshot).row {
2414                let line_indent = snapshot.indent_size_for_line(MultiBufferRow(row));
2415                // Prefer tabs if a line in the selection uses tabs as indentation
2416                if line_indent.kind == IndentKind::Tab {
2417                    suggested_line_indent.kind = IndentKind::Tab;
2418                    break;
2419                }
2420            }
2421        }
2422
2423        let telemetry = self.telemetry.clone();
2424        self.diff = Diff::default();
2425        self.status = CodegenStatus::Pending;
2426        let mut edit_start = edit_range.start.to_offset(&snapshot);
2427        self.generation = cx.spawn(|this, mut cx| {
2428            async move {
2429                let chunks = stream.await;
2430                let generate = async {
2431                    let (mut diff_tx, mut diff_rx) = mpsc::channel(1);
2432                    let diff: Task<anyhow::Result<()>> =
2433                        cx.background_executor().spawn(async move {
2434                            let mut response_latency = None;
2435                            let request_start = Instant::now();
2436                            let diff = async {
2437                                let chunks = StripInvalidSpans::new(chunks?);
2438                                futures::pin_mut!(chunks);
2439                                let mut diff = StreamingDiff::new(selected_text.to_string());
2440                                let mut line_diff = LineDiff::default();
2441
2442                                while let Some(chunk) = chunks.next().await {
2443                                    if response_latency.is_none() {
2444                                        response_latency = Some(request_start.elapsed());
2445                                    }
2446                                    let chunk = chunk?;
2447                                    let char_ops = diff.push_new(&chunk);
2448                                    line_diff.push_char_operations(&char_ops, &selected_text);
2449                                    diff_tx
2450                                        .send((char_ops, line_diff.line_operations()))
2451                                        .await?;
2452                                }
2453
2454                                let char_ops = diff.finish();
2455                                line_diff.push_char_operations(&char_ops, &selected_text);
2456                                line_diff.finish(&selected_text);
2457                                diff_tx
2458                                    .send((char_ops, line_diff.line_operations()))
2459                                    .await?;
2460
2461                                anyhow::Ok(())
2462                            };
2463
2464                            let result = diff.await;
2465
2466                            let error_message =
2467                                result.as_ref().err().map(|error| error.to_string());
2468                            if let Some(telemetry) = telemetry {
2469                                telemetry.report_assistant_event(
2470                                    None,
2471                                    telemetry_events::AssistantKind::Inline,
2472                                    model_telemetry_id,
2473                                    response_latency,
2474                                    error_message,
2475                                );
2476                            }
2477
2478                            result?;
2479                            Ok(())
2480                        });
2481
2482                    while let Some((char_ops, line_diff)) = diff_rx.next().await {
2483                        this.update(&mut cx, |this, cx| {
2484                            this.last_equal_ranges.clear();
2485
2486                            let transaction = this.buffer.update(cx, |buffer, cx| {
2487                                // Avoid grouping assistant edits with user edits.
2488                                buffer.finalize_last_transaction(cx);
2489
2490                                buffer.start_transaction(cx);
2491                                buffer.edit(
2492                                    char_ops
2493                                        .into_iter()
2494                                        .filter_map(|operation| match operation {
2495                                            CharOperation::Insert { text } => {
2496                                                let edit_start = snapshot.anchor_after(edit_start);
2497                                                Some((edit_start..edit_start, text))
2498                                            }
2499                                            CharOperation::Delete { bytes } => {
2500                                                let edit_end = edit_start + bytes;
2501                                                let edit_range = snapshot.anchor_after(edit_start)
2502                                                    ..snapshot.anchor_before(edit_end);
2503                                                edit_start = edit_end;
2504                                                Some((edit_range, String::new()))
2505                                            }
2506                                            CharOperation::Keep { bytes } => {
2507                                                let edit_end = edit_start + bytes;
2508                                                let edit_range = snapshot.anchor_after(edit_start)
2509                                                    ..snapshot.anchor_before(edit_end);
2510                                                edit_start = edit_end;
2511                                                this.last_equal_ranges.push(edit_range);
2512                                                None
2513                                            }
2514                                        }),
2515                                    None,
2516                                    cx,
2517                                );
2518                                this.edit_position = Some(snapshot.anchor_after(edit_start));
2519
2520                                buffer.end_transaction(cx)
2521                            });
2522
2523                            if let Some(transaction) = transaction {
2524                                if let Some(first_transaction) = this.transformation_transaction_id
2525                                {
2526                                    // Group all assistant edits into the first transaction.
2527                                    this.buffer.update(cx, |buffer, cx| {
2528                                        buffer.merge_transactions(
2529                                            transaction,
2530                                            first_transaction,
2531                                            cx,
2532                                        )
2533                                    });
2534                                } else {
2535                                    this.transformation_transaction_id = Some(transaction);
2536                                    this.buffer.update(cx, |buffer, cx| {
2537                                        buffer.finalize_last_transaction(cx)
2538                                    });
2539                                }
2540                            }
2541
2542                            this.update_diff(edit_range.clone(), line_diff, cx);
2543
2544                            cx.notify();
2545                        })?;
2546                    }
2547
2548                    diff.await?;
2549
2550                    anyhow::Ok(())
2551                };
2552
2553                let result = generate.await;
2554                this.update(&mut cx, |this, cx| {
2555                    this.last_equal_ranges.clear();
2556                    if let Err(error) = result {
2557                        this.status = CodegenStatus::Error(error);
2558                    } else {
2559                        this.status = CodegenStatus::Done;
2560                    }
2561                    cx.emit(CodegenEvent::Finished);
2562                    cx.notify();
2563                })
2564                .ok();
2565            }
2566        });
2567        cx.notify();
2568    }
2569
2570    pub fn stop(&mut self, cx: &mut ModelContext<Self>) {
2571        self.last_equal_ranges.clear();
2572        if self.diff.is_empty() {
2573            self.status = CodegenStatus::Idle;
2574        } else {
2575            self.status = CodegenStatus::Done;
2576        }
2577        self.generation = Task::ready(());
2578        cx.emit(CodegenEvent::Finished);
2579        cx.notify();
2580    }
2581
2582    pub fn undo(&mut self, cx: &mut ModelContext<Self>) {
2583        self.buffer.update(cx, |buffer, cx| {
2584            if let Some(transaction_id) = self.transformation_transaction_id.take() {
2585                buffer.undo_transaction(transaction_id, cx);
2586                buffer.refresh_preview(cx);
2587            }
2588
2589            if let Some(transaction_id) = self.initial_transaction_id.take() {
2590                buffer.undo_transaction(transaction_id, cx);
2591                buffer.refresh_preview(cx);
2592            }
2593        });
2594    }
2595
2596    fn update_diff(
2597        &mut self,
2598        edit_range: Range<Anchor>,
2599        line_operations: Vec<LineOperation>,
2600        cx: &mut ModelContext<Self>,
2601    ) {
2602        let old_snapshot = self.snapshot.clone();
2603        let old_range = edit_range.to_point(&old_snapshot);
2604        let new_snapshot = self.buffer.read(cx).snapshot(cx);
2605        let new_range = edit_range.to_point(&new_snapshot);
2606
2607        let mut old_row = old_range.start.row;
2608        let mut new_row = new_range.start.row;
2609
2610        self.diff.deleted_row_ranges.clear();
2611        self.diff.inserted_row_ranges.clear();
2612        for operation in line_operations {
2613            match operation {
2614                LineOperation::Keep { lines } => {
2615                    old_row += lines;
2616                    new_row += lines;
2617                }
2618                LineOperation::Delete { lines } => {
2619                    let old_end_row = old_row + lines - 1;
2620                    let new_row = new_snapshot.anchor_before(Point::new(new_row, 0));
2621
2622                    if let Some((_, last_deleted_row_range)) =
2623                        self.diff.deleted_row_ranges.last_mut()
2624                    {
2625                        if *last_deleted_row_range.end() + 1 == old_row {
2626                            *last_deleted_row_range = *last_deleted_row_range.start()..=old_end_row;
2627                        } else {
2628                            self.diff
2629                                .deleted_row_ranges
2630                                .push((new_row, old_row..=old_end_row));
2631                        }
2632                    } else {
2633                        self.diff
2634                            .deleted_row_ranges
2635                            .push((new_row, old_row..=old_end_row));
2636                    }
2637
2638                    old_row += lines;
2639                }
2640                LineOperation::Insert { lines } => {
2641                    let new_end_row = new_row + lines - 1;
2642                    let start = new_snapshot.anchor_before(Point::new(new_row, 0));
2643                    let end = new_snapshot.anchor_before(Point::new(
2644                        new_end_row,
2645                        new_snapshot.line_len(MultiBufferRow(new_end_row)),
2646                    ));
2647                    self.diff.inserted_row_ranges.push(start..=end);
2648                    new_row += lines;
2649                }
2650            }
2651
2652            cx.notify();
2653        }
2654    }
2655}
2656
2657struct StripInvalidSpans<T> {
2658    stream: T,
2659    stream_done: bool,
2660    buffer: String,
2661    first_line: bool,
2662    line_end: bool,
2663    starts_with_code_block: bool,
2664}
2665
2666impl<T> StripInvalidSpans<T>
2667where
2668    T: Stream<Item = Result<String>>,
2669{
2670    fn new(stream: T) -> Self {
2671        Self {
2672            stream,
2673            stream_done: false,
2674            buffer: String::new(),
2675            first_line: true,
2676            line_end: false,
2677            starts_with_code_block: false,
2678        }
2679    }
2680}
2681
2682impl<T> Stream for StripInvalidSpans<T>
2683where
2684    T: Stream<Item = Result<String>>,
2685{
2686    type Item = Result<String>;
2687
2688    fn poll_next(self: Pin<&mut Self>, cx: &mut task::Context) -> Poll<Option<Self::Item>> {
2689        const CODE_BLOCK_DELIMITER: &str = "```";
2690        const CURSOR_SPAN: &str = "<|CURSOR|>";
2691
2692        let this = unsafe { self.get_unchecked_mut() };
2693        loop {
2694            if !this.stream_done {
2695                let mut stream = unsafe { Pin::new_unchecked(&mut this.stream) };
2696                match stream.as_mut().poll_next(cx) {
2697                    Poll::Ready(Some(Ok(chunk))) => {
2698                        this.buffer.push_str(&chunk);
2699                    }
2700                    Poll::Ready(Some(Err(error))) => return Poll::Ready(Some(Err(error))),
2701                    Poll::Ready(None) => {
2702                        this.stream_done = true;
2703                    }
2704                    Poll::Pending => return Poll::Pending,
2705                }
2706            }
2707
2708            let mut chunk = String::new();
2709            let mut consumed = 0;
2710            if !this.buffer.is_empty() {
2711                let mut lines = this.buffer.split('\n').enumerate().peekable();
2712                while let Some((line_ix, line)) = lines.next() {
2713                    if line_ix > 0 {
2714                        this.first_line = false;
2715                    }
2716
2717                    if this.first_line {
2718                        let trimmed_line = line.trim();
2719                        if lines.peek().is_some() {
2720                            if trimmed_line.starts_with(CODE_BLOCK_DELIMITER) {
2721                                consumed += line.len() + 1;
2722                                this.starts_with_code_block = true;
2723                                continue;
2724                            }
2725                        } else if trimmed_line.is_empty()
2726                            || prefixes(CODE_BLOCK_DELIMITER)
2727                                .any(|prefix| trimmed_line.starts_with(prefix))
2728                        {
2729                            break;
2730                        }
2731                    }
2732
2733                    let line_without_cursor = line.replace(CURSOR_SPAN, "");
2734                    if lines.peek().is_some() {
2735                        if this.line_end {
2736                            chunk.push('\n');
2737                        }
2738
2739                        chunk.push_str(&line_without_cursor);
2740                        this.line_end = true;
2741                        consumed += line.len() + 1;
2742                    } else if this.stream_done {
2743                        if !this.starts_with_code_block
2744                            || !line_without_cursor.trim().ends_with(CODE_BLOCK_DELIMITER)
2745                        {
2746                            if this.line_end {
2747                                chunk.push('\n');
2748                            }
2749
2750                            chunk.push_str(&line);
2751                        }
2752
2753                        consumed += line.len();
2754                    } else {
2755                        let trimmed_line = line.trim();
2756                        if trimmed_line.is_empty()
2757                            || prefixes(CURSOR_SPAN).any(|prefix| trimmed_line.ends_with(prefix))
2758                            || prefixes(CODE_BLOCK_DELIMITER)
2759                                .any(|prefix| trimmed_line.ends_with(prefix))
2760                        {
2761                            break;
2762                        } else {
2763                            if this.line_end {
2764                                chunk.push('\n');
2765                                this.line_end = false;
2766                            }
2767
2768                            chunk.push_str(&line_without_cursor);
2769                            consumed += line.len();
2770                        }
2771                    }
2772                }
2773            }
2774
2775            this.buffer = this.buffer.split_off(consumed);
2776            if !chunk.is_empty() {
2777                return Poll::Ready(Some(Ok(chunk)));
2778            } else if this.stream_done {
2779                return Poll::Ready(None);
2780            }
2781        }
2782    }
2783}
2784
2785fn prefixes(text: &str) -> impl Iterator<Item = &str> {
2786    (0..text.len() - 1).map(|ix| &text[..ix + 1])
2787}
2788
2789fn merge_ranges(ranges: &mut Vec<Range<Anchor>>, buffer: &MultiBufferSnapshot) {
2790    ranges.sort_unstable_by(|a, b| {
2791        a.start
2792            .cmp(&b.start, buffer)
2793            .then_with(|| b.end.cmp(&a.end, buffer))
2794    });
2795
2796    let mut ix = 0;
2797    while ix + 1 < ranges.len() {
2798        let b = ranges[ix + 1].clone();
2799        let a = &mut ranges[ix];
2800        if a.end.cmp(&b.start, buffer).is_gt() {
2801            if a.end.cmp(&b.end, buffer).is_lt() {
2802                a.end = b.end;
2803            }
2804            ranges.remove(ix + 1);
2805        } else {
2806            ix += 1;
2807        }
2808    }
2809}
2810
2811#[cfg(test)]
2812mod tests {
2813    use super::*;
2814    use futures::stream::{self};
2815    use serde::Serialize;
2816
2817    #[derive(Serialize)]
2818    pub struct DummyCompletionRequest {
2819        pub name: String,
2820    }
2821
2822    #[gpui::test]
2823    async fn test_strip_invalid_spans_from_codeblock() {
2824        assert_chunks("Lorem ipsum dolor", "Lorem ipsum dolor").await;
2825        assert_chunks("```\nLorem ipsum dolor", "Lorem ipsum dolor").await;
2826        assert_chunks("```\nLorem ipsum dolor\n```", "Lorem ipsum dolor").await;
2827        assert_chunks(
2828            "```html\n```js\nLorem ipsum dolor\n```\n```",
2829            "```js\nLorem ipsum dolor\n```",
2830        )
2831        .await;
2832        assert_chunks("``\nLorem ipsum dolor\n```", "``\nLorem ipsum dolor\n```").await;
2833        assert_chunks("Lorem<|CURSOR|> ipsum", "Lorem ipsum").await;
2834        assert_chunks("Lorem ipsum", "Lorem ipsum").await;
2835        assert_chunks("```\n<|CURSOR|>Lorem ipsum\n```", "Lorem ipsum").await;
2836
2837        async fn assert_chunks(text: &str, expected_text: &str) {
2838            for chunk_size in 1..=text.len() {
2839                let actual_text = StripInvalidSpans::new(chunks(text, chunk_size))
2840                    .map(|chunk| chunk.unwrap())
2841                    .collect::<String>()
2842                    .await;
2843                assert_eq!(
2844                    actual_text, expected_text,
2845                    "failed to strip invalid spans, chunk size: {}",
2846                    chunk_size
2847                );
2848            }
2849        }
2850
2851        fn chunks(text: &str, size: usize) -> impl Stream<Item = Result<String>> {
2852            stream::iter(
2853                text.chars()
2854                    .collect::<Vec<_>>()
2855                    .chunks(size)
2856                    .map(|chunk| Ok(chunk.iter().collect::<String>()))
2857                    .collect::<Vec<_>>(),
2858            )
2859        }
2860    }
2861}