inline_assistant.rs

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