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