inline_assistant.rs

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