inline_assistant.rs

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