1use crate::{
   2    context::load_context, context_store::ContextStore, inline_prompt_editor::CodegenStatus,
   3};
   4use agent_settings::AgentSettings;
   5use anyhow::{Context as _, Result};
   6use client::telemetry::Telemetry;
   7use cloud_llm_client::CompletionIntent;
   8use collections::HashSet;
   9use editor::{Anchor, AnchorRangeExt, MultiBuffer, MultiBufferSnapshot, ToOffset as _, ToPoint};
  10use futures::{
  11    SinkExt, Stream, StreamExt, TryStreamExt as _, channel::mpsc, future::LocalBoxFuture, join,
  12};
  13use gpui::{App, AppContext as _, Context, Entity, EventEmitter, Subscription, Task, WeakEntity};
  14use language::{Buffer, IndentKind, Point, TransactionId, line_diff};
  15use language_model::{
  16    LanguageModel, LanguageModelRegistry, LanguageModelRequest, LanguageModelRequestMessage,
  17    LanguageModelTextStream, Role, report_assistant_event,
  18};
  19use multi_buffer::MultiBufferRow;
  20use parking_lot::Mutex;
  21use project::Project;
  22use prompt_store::{PromptBuilder, PromptStore};
  23use rope::Rope;
  24use smol::future::FutureExt;
  25use std::{
  26    cmp,
  27    future::Future,
  28    iter,
  29    ops::{Range, RangeInclusive},
  30    pin::Pin,
  31    sync::Arc,
  32    task::{self, Poll},
  33    time::Instant,
  34};
  35use streaming_diff::{CharOperation, LineDiff, LineOperation, StreamingDiff};
  36use telemetry_events::{AssistantEventData, AssistantKind, AssistantPhase};
  37
  38pub struct BufferCodegen {
  39    alternatives: Vec<Entity<CodegenAlternative>>,
  40    pub active_alternative: usize,
  41    seen_alternatives: HashSet<usize>,
  42    subscriptions: Vec<Subscription>,
  43    buffer: Entity<MultiBuffer>,
  44    range: Range<Anchor>,
  45    initial_transaction_id: Option<TransactionId>,
  46    context_store: Entity<ContextStore>,
  47    project: WeakEntity<Project>,
  48    prompt_store: Option<Entity<PromptStore>>,
  49    telemetry: Arc<Telemetry>,
  50    builder: Arc<PromptBuilder>,
  51    pub is_insertion: bool,
  52}
  53
  54impl BufferCodegen {
  55    pub fn new(
  56        buffer: Entity<MultiBuffer>,
  57        range: Range<Anchor>,
  58        initial_transaction_id: Option<TransactionId>,
  59        context_store: Entity<ContextStore>,
  60        project: WeakEntity<Project>,
  61        prompt_store: Option<Entity<PromptStore>>,
  62        telemetry: Arc<Telemetry>,
  63        builder: Arc<PromptBuilder>,
  64        cx: &mut Context<Self>,
  65    ) -> Self {
  66        let codegen = cx.new(|cx| {
  67            CodegenAlternative::new(
  68                buffer.clone(),
  69                range.clone(),
  70                false,
  71                Some(context_store.clone()),
  72                project.clone(),
  73                prompt_store.clone(),
  74                Some(telemetry.clone()),
  75                builder.clone(),
  76                cx,
  77            )
  78        });
  79        let mut this = Self {
  80            is_insertion: range.to_offset(&buffer.read(cx).snapshot(cx)).is_empty(),
  81            alternatives: vec![codegen],
  82            active_alternative: 0,
  83            seen_alternatives: HashSet::default(),
  84            subscriptions: Vec::new(),
  85            buffer,
  86            range,
  87            initial_transaction_id,
  88            context_store,
  89            project,
  90            prompt_store,
  91            telemetry,
  92            builder,
  93        };
  94        this.activate(0, cx);
  95        this
  96    }
  97
  98    fn subscribe_to_alternative(&mut self, cx: &mut Context<Self>) {
  99        let codegen = self.active_alternative().clone();
 100        self.subscriptions.clear();
 101        self.subscriptions
 102            .push(cx.observe(&codegen, |_, _, cx| cx.notify()));
 103        self.subscriptions
 104            .push(cx.subscribe(&codegen, |_, _, event, cx| cx.emit(*event)));
 105    }
 106
 107    pub fn active_alternative(&self) -> &Entity<CodegenAlternative> {
 108        &self.alternatives[self.active_alternative]
 109    }
 110
 111    pub fn status<'a>(&self, cx: &'a App) -> &'a CodegenStatus {
 112        &self.active_alternative().read(cx).status
 113    }
 114
 115    pub fn alternative_count(&self, cx: &App) -> usize {
 116        LanguageModelRegistry::read_global(cx)
 117            .inline_alternative_models()
 118            .len()
 119            + 1
 120    }
 121
 122    pub fn cycle_prev(&mut self, cx: &mut Context<Self>) {
 123        let next_active_ix = if self.active_alternative == 0 {
 124            self.alternatives.len() - 1
 125        } else {
 126            self.active_alternative - 1
 127        };
 128        self.activate(next_active_ix, cx);
 129    }
 130
 131    pub fn cycle_next(&mut self, cx: &mut Context<Self>) {
 132        let next_active_ix = (self.active_alternative + 1) % self.alternatives.len();
 133        self.activate(next_active_ix, cx);
 134    }
 135
 136    fn activate(&mut self, index: usize, cx: &mut Context<Self>) {
 137        self.active_alternative()
 138            .update(cx, |codegen, cx| codegen.set_active(false, cx));
 139        self.seen_alternatives.insert(index);
 140        self.active_alternative = index;
 141        self.active_alternative()
 142            .update(cx, |codegen, cx| codegen.set_active(true, cx));
 143        self.subscribe_to_alternative(cx);
 144        cx.notify();
 145    }
 146
 147    pub fn start(
 148        &mut self,
 149        primary_model: Arc<dyn LanguageModel>,
 150        user_prompt: String,
 151        cx: &mut Context<Self>,
 152    ) -> Result<()> {
 153        let alternative_models = LanguageModelRegistry::read_global(cx)
 154            .inline_alternative_models()
 155            .to_vec();
 156
 157        self.active_alternative()
 158            .update(cx, |alternative, cx| alternative.undo(cx));
 159        self.activate(0, cx);
 160        self.alternatives.truncate(1);
 161
 162        for _ in 0..alternative_models.len() {
 163            self.alternatives.push(cx.new(|cx| {
 164                CodegenAlternative::new(
 165                    self.buffer.clone(),
 166                    self.range.clone(),
 167                    false,
 168                    Some(self.context_store.clone()),
 169                    self.project.clone(),
 170                    self.prompt_store.clone(),
 171                    Some(self.telemetry.clone()),
 172                    self.builder.clone(),
 173                    cx,
 174                )
 175            }));
 176        }
 177
 178        for (model, alternative) in iter::once(primary_model)
 179            .chain(alternative_models)
 180            .zip(&self.alternatives)
 181        {
 182            alternative.update(cx, |alternative, cx| {
 183                alternative.start(user_prompt.clone(), model.clone(), cx)
 184            })?;
 185        }
 186
 187        Ok(())
 188    }
 189
 190    pub fn stop(&mut self, cx: &mut Context<Self>) {
 191        for codegen in &self.alternatives {
 192            codegen.update(cx, |codegen, cx| codegen.stop(cx));
 193        }
 194    }
 195
 196    pub fn undo(&mut self, cx: &mut Context<Self>) {
 197        self.active_alternative()
 198            .update(cx, |codegen, cx| codegen.undo(cx));
 199
 200        self.buffer.update(cx, |buffer, cx| {
 201            if let Some(transaction_id) = self.initial_transaction_id.take() {
 202                buffer.undo_transaction(transaction_id, cx);
 203                buffer.refresh_preview(cx);
 204            }
 205        });
 206    }
 207
 208    pub fn buffer(&self, cx: &App) -> Entity<MultiBuffer> {
 209        self.active_alternative().read(cx).buffer.clone()
 210    }
 211
 212    pub fn old_buffer(&self, cx: &App) -> Entity<Buffer> {
 213        self.active_alternative().read(cx).old_buffer.clone()
 214    }
 215
 216    pub fn snapshot(&self, cx: &App) -> MultiBufferSnapshot {
 217        self.active_alternative().read(cx).snapshot.clone()
 218    }
 219
 220    pub fn edit_position(&self, cx: &App) -> Option<Anchor> {
 221        self.active_alternative().read(cx).edit_position
 222    }
 223
 224    pub fn diff<'a>(&self, cx: &'a App) -> &'a Diff {
 225        &self.active_alternative().read(cx).diff
 226    }
 227
 228    pub fn last_equal_ranges<'a>(&self, cx: &'a App) -> &'a [Range<Anchor>] {
 229        self.active_alternative().read(cx).last_equal_ranges()
 230    }
 231}
 232
 233impl EventEmitter<CodegenEvent> for BufferCodegen {}
 234
 235pub struct CodegenAlternative {
 236    buffer: Entity<MultiBuffer>,
 237    old_buffer: Entity<Buffer>,
 238    snapshot: MultiBufferSnapshot,
 239    edit_position: Option<Anchor>,
 240    range: Range<Anchor>,
 241    last_equal_ranges: Vec<Range<Anchor>>,
 242    transformation_transaction_id: Option<TransactionId>,
 243    status: CodegenStatus,
 244    generation: Task<()>,
 245    diff: Diff,
 246    context_store: Option<Entity<ContextStore>>,
 247    project: WeakEntity<Project>,
 248    prompt_store: Option<Entity<PromptStore>>,
 249    telemetry: Option<Arc<Telemetry>>,
 250    _subscription: gpui::Subscription,
 251    builder: Arc<PromptBuilder>,
 252    active: bool,
 253    edits: Vec<(Range<Anchor>, String)>,
 254    line_operations: Vec<LineOperation>,
 255    elapsed_time: Option<f64>,
 256    completion: Option<String>,
 257    pub message_id: Option<String>,
 258}
 259
 260impl EventEmitter<CodegenEvent> for CodegenAlternative {}
 261
 262impl CodegenAlternative {
 263    pub fn new(
 264        buffer: Entity<MultiBuffer>,
 265        range: Range<Anchor>,
 266        active: bool,
 267        context_store: Option<Entity<ContextStore>>,
 268        project: WeakEntity<Project>,
 269        prompt_store: Option<Entity<PromptStore>>,
 270        telemetry: Option<Arc<Telemetry>>,
 271        builder: Arc<PromptBuilder>,
 272        cx: &mut Context<Self>,
 273    ) -> Self {
 274        let snapshot = buffer.read(cx).snapshot(cx);
 275
 276        let (old_buffer, _, _) = snapshot
 277            .range_to_buffer_ranges(range.clone())
 278            .pop()
 279            .unwrap();
 280        let old_buffer = cx.new(|cx| {
 281            let text = old_buffer.as_rope().clone();
 282            let line_ending = old_buffer.line_ending();
 283            let language = old_buffer.language().cloned();
 284            let language_registry = buffer
 285                .read(cx)
 286                .buffer(old_buffer.remote_id())
 287                .unwrap()
 288                .read(cx)
 289                .language_registry();
 290
 291            let mut buffer = Buffer::local_normalized(text, line_ending, cx);
 292            buffer.set_language(language, cx);
 293            if let Some(language_registry) = language_registry {
 294                buffer.set_language_registry(language_registry)
 295            }
 296            buffer
 297        });
 298
 299        Self {
 300            buffer: buffer.clone(),
 301            old_buffer,
 302            edit_position: None,
 303            message_id: None,
 304            snapshot,
 305            last_equal_ranges: Default::default(),
 306            transformation_transaction_id: None,
 307            status: CodegenStatus::Idle,
 308            generation: Task::ready(()),
 309            diff: Diff::default(),
 310            context_store,
 311            project,
 312            prompt_store,
 313            telemetry,
 314            _subscription: cx.subscribe(&buffer, Self::handle_buffer_event),
 315            builder,
 316            active,
 317            edits: Vec::new(),
 318            line_operations: Vec::new(),
 319            range,
 320            elapsed_time: None,
 321            completion: None,
 322        }
 323    }
 324
 325    pub fn set_active(&mut self, active: bool, cx: &mut Context<Self>) {
 326        if active != self.active {
 327            self.active = active;
 328
 329            if self.active {
 330                let edits = self.edits.clone();
 331                self.apply_edits(edits, cx);
 332                if matches!(self.status, CodegenStatus::Pending) {
 333                    let line_operations = self.line_operations.clone();
 334                    self.reapply_line_based_diff(line_operations, cx);
 335                } else {
 336                    self.reapply_batch_diff(cx).detach();
 337                }
 338            } else if let Some(transaction_id) = self.transformation_transaction_id.take() {
 339                self.buffer.update(cx, |buffer, cx| {
 340                    buffer.undo_transaction(transaction_id, cx);
 341                    buffer.forget_transaction(transaction_id, cx);
 342                });
 343            }
 344        }
 345    }
 346
 347    fn handle_buffer_event(
 348        &mut self,
 349        _buffer: Entity<MultiBuffer>,
 350        event: &multi_buffer::Event,
 351        cx: &mut Context<Self>,
 352    ) {
 353        if let multi_buffer::Event::TransactionUndone { transaction_id } = event
 354            && self.transformation_transaction_id == Some(*transaction_id)
 355        {
 356            self.transformation_transaction_id = None;
 357            self.generation = Task::ready(());
 358            cx.emit(CodegenEvent::Undone);
 359        }
 360    }
 361
 362    pub fn last_equal_ranges(&self) -> &[Range<Anchor>] {
 363        &self.last_equal_ranges
 364    }
 365
 366    pub fn start(
 367        &mut self,
 368        user_prompt: String,
 369        model: Arc<dyn LanguageModel>,
 370        cx: &mut Context<Self>,
 371    ) -> Result<()> {
 372        if let Some(transformation_transaction_id) = self.transformation_transaction_id.take() {
 373            self.buffer.update(cx, |buffer, cx| {
 374                buffer.undo_transaction(transformation_transaction_id, cx);
 375            });
 376        }
 377
 378        self.edit_position = Some(self.range.start.bias_right(&self.snapshot));
 379
 380        let api_key = model.api_key(cx);
 381        let telemetry_id = model.telemetry_id();
 382        let provider_id = model.provider_id();
 383        let stream: LocalBoxFuture<Result<LanguageModelTextStream>> =
 384            if user_prompt.trim().to_lowercase() == "delete" {
 385                async { Ok(LanguageModelTextStream::default()) }.boxed_local()
 386            } else {
 387                let request = self.build_request(&model, user_prompt, cx)?;
 388                cx.spawn(async move |_, cx| {
 389                    Ok(model.stream_completion_text(request.await, cx).await?)
 390                })
 391                .boxed_local()
 392            };
 393        self.handle_stream(telemetry_id, provider_id.to_string(), api_key, stream, cx);
 394        Ok(())
 395    }
 396
 397    fn build_request(
 398        &self,
 399        model: &Arc<dyn LanguageModel>,
 400        user_prompt: String,
 401        cx: &mut App,
 402    ) -> Result<Task<LanguageModelRequest>> {
 403        let buffer = self.buffer.read(cx).snapshot(cx);
 404        let language = buffer.language_at(self.range.start);
 405        let language_name = if let Some(language) = language.as_ref() {
 406            if Arc::ptr_eq(language, &language::PLAIN_TEXT) {
 407                None
 408            } else {
 409                Some(language.name())
 410            }
 411        } else {
 412            None
 413        };
 414
 415        let language_name = language_name.as_ref();
 416        let start = buffer.point_to_buffer_offset(self.range.start);
 417        let end = buffer.point_to_buffer_offset(self.range.end);
 418        let (buffer, range) = if let Some((start, end)) = start.zip(end) {
 419            let (start_buffer, start_buffer_offset) = start;
 420            let (end_buffer, end_buffer_offset) = end;
 421            if start_buffer.remote_id() == end_buffer.remote_id() {
 422                (start_buffer.clone(), start_buffer_offset..end_buffer_offset)
 423            } else {
 424                anyhow::bail!("invalid transformation range");
 425            }
 426        } else {
 427            anyhow::bail!("invalid transformation range");
 428        };
 429
 430        let prompt = self
 431            .builder
 432            .generate_inline_transformation_prompt(user_prompt, language_name, buffer, range)
 433            .context("generating content prompt")?;
 434
 435        let context_task = self.context_store.as_ref().and_then(|context_store| {
 436            if let Some(project) = self.project.upgrade() {
 437                let context = context_store
 438                    .read(cx)
 439                    .context()
 440                    .cloned()
 441                    .collect::<Vec<_>>();
 442                Some(load_context(context, &project, &self.prompt_store, cx))
 443            } else {
 444                None
 445            }
 446        });
 447
 448        let temperature = AgentSettings::temperature_for_model(model, cx);
 449
 450        Ok(cx.spawn(async move |_cx| {
 451            let mut request_message = LanguageModelRequestMessage {
 452                role: Role::User,
 453                content: Vec::new(),
 454                cache: false,
 455            };
 456
 457            if let Some(context_task) = context_task {
 458                context_task
 459                    .await
 460                    .add_to_request_message(&mut request_message);
 461            }
 462
 463            request_message.content.push(prompt.into());
 464
 465            LanguageModelRequest {
 466                thread_id: None,
 467                prompt_id: None,
 468                intent: Some(CompletionIntent::InlineAssist),
 469                mode: None,
 470                tools: Vec::new(),
 471                tool_choice: None,
 472                stop: Vec::new(),
 473                temperature,
 474                messages: vec![request_message],
 475                thinking_allowed: false,
 476            }
 477        }))
 478    }
 479
 480    pub fn handle_stream(
 481        &mut self,
 482        model_telemetry_id: String,
 483        model_provider_id: String,
 484        model_api_key: Option<String>,
 485        stream: impl 'static + Future<Output = Result<LanguageModelTextStream>>,
 486        cx: &mut Context<Self>,
 487    ) {
 488        let start_time = Instant::now();
 489        let snapshot = self.snapshot.clone();
 490        let selected_text = snapshot
 491            .text_for_range(self.range.start..self.range.end)
 492            .collect::<Rope>();
 493
 494        let selection_start = self.range.start.to_point(&snapshot);
 495
 496        // Start with the indentation of the first line in the selection
 497        let mut suggested_line_indent = snapshot
 498            .suggested_indents(selection_start.row..=selection_start.row, cx)
 499            .into_values()
 500            .next()
 501            .unwrap_or_else(|| snapshot.indent_size_for_line(MultiBufferRow(selection_start.row)));
 502
 503        // If the first line in the selection does not have indentation, check the following lines
 504        if suggested_line_indent.len == 0 && suggested_line_indent.kind == IndentKind::Space {
 505            for row in selection_start.row..=self.range.end.to_point(&snapshot).row {
 506                let line_indent = snapshot.indent_size_for_line(MultiBufferRow(row));
 507                // Prefer tabs if a line in the selection uses tabs as indentation
 508                if line_indent.kind == IndentKind::Tab {
 509                    suggested_line_indent.kind = IndentKind::Tab;
 510                    break;
 511                }
 512            }
 513        }
 514
 515        let http_client = cx.http_client();
 516        let telemetry = self.telemetry.clone();
 517        let language_name = {
 518            let multibuffer = self.buffer.read(cx);
 519            let snapshot = multibuffer.snapshot(cx);
 520            let ranges = snapshot.range_to_buffer_ranges(self.range.clone());
 521            ranges
 522                .first()
 523                .and_then(|(buffer, _, _)| buffer.language())
 524                .map(|language| language.name())
 525        };
 526
 527        self.diff = Diff::default();
 528        self.status = CodegenStatus::Pending;
 529        let mut edit_start = self.range.start.to_offset(&snapshot);
 530        let completion = Arc::new(Mutex::new(String::new()));
 531        let completion_clone = completion.clone();
 532
 533        self.generation = cx.spawn(async move |codegen, cx| {
 534            let stream = stream.await;
 535            let token_usage = stream
 536                .as_ref()
 537                .ok()
 538                .map(|stream| stream.last_token_usage.clone());
 539            let message_id = stream
 540                .as_ref()
 541                .ok()
 542                .and_then(|stream| stream.message_id.clone());
 543            let generate = async {
 544                let model_telemetry_id = model_telemetry_id.clone();
 545                let model_provider_id = model_provider_id.clone();
 546                let (mut diff_tx, mut diff_rx) = mpsc::channel(1);
 547                let executor = cx.background_executor().clone();
 548                let message_id = message_id.clone();
 549                let line_based_stream_diff: Task<anyhow::Result<()>> =
 550                    cx.background_spawn(async move {
 551                        let mut response_latency = None;
 552                        let request_start = Instant::now();
 553                        let diff = async {
 554                            let chunks = StripInvalidSpans::new(
 555                                stream?.stream.map_err(|error| error.into()),
 556                            );
 557                            futures::pin_mut!(chunks);
 558                            let mut diff = StreamingDiff::new(selected_text.to_string());
 559                            let mut line_diff = LineDiff::default();
 560
 561                            let mut new_text = String::new();
 562                            let mut base_indent = None;
 563                            let mut line_indent = None;
 564                            let mut first_line = true;
 565
 566                            while let Some(chunk) = chunks.next().await {
 567                                if response_latency.is_none() {
 568                                    response_latency = Some(request_start.elapsed());
 569                                }
 570                                let chunk = chunk?;
 571                                completion_clone.lock().push_str(&chunk);
 572
 573                                let mut lines = chunk.split('\n').peekable();
 574                                while let Some(line) = lines.next() {
 575                                    new_text.push_str(line);
 576                                    if line_indent.is_none()
 577                                        && let Some(non_whitespace_ch_ix) =
 578                                            new_text.find(|ch: char| !ch.is_whitespace())
 579                                    {
 580                                        line_indent = Some(non_whitespace_ch_ix);
 581                                        base_indent = base_indent.or(line_indent);
 582
 583                                        let line_indent = line_indent.unwrap();
 584                                        let base_indent = base_indent.unwrap();
 585                                        let indent_delta = line_indent as i32 - base_indent as i32;
 586                                        let mut corrected_indent_len = cmp::max(
 587                                            0,
 588                                            suggested_line_indent.len as i32 + indent_delta,
 589                                        )
 590                                            as usize;
 591                                        if first_line {
 592                                            corrected_indent_len = corrected_indent_len
 593                                                .saturating_sub(selection_start.column as usize);
 594                                        }
 595
 596                                        let indent_char = suggested_line_indent.char();
 597                                        let mut indent_buffer = [0; 4];
 598                                        let indent_str =
 599                                            indent_char.encode_utf8(&mut indent_buffer);
 600                                        new_text.replace_range(
 601                                            ..line_indent,
 602                                            &indent_str.repeat(corrected_indent_len),
 603                                        );
 604                                    }
 605
 606                                    if line_indent.is_some() {
 607                                        let char_ops = diff.push_new(&new_text);
 608                                        line_diff.push_char_operations(&char_ops, &selected_text);
 609                                        diff_tx
 610                                            .send((char_ops, line_diff.line_operations()))
 611                                            .await?;
 612                                        new_text.clear();
 613                                    }
 614
 615                                    if lines.peek().is_some() {
 616                                        let char_ops = diff.push_new("\n");
 617                                        line_diff.push_char_operations(&char_ops, &selected_text);
 618                                        diff_tx
 619                                            .send((char_ops, line_diff.line_operations()))
 620                                            .await?;
 621                                        if line_indent.is_none() {
 622                                            // Don't write out the leading indentation in empty lines on the next line
 623                                            // This is the case where the above if statement didn't clear the buffer
 624                                            new_text.clear();
 625                                        }
 626                                        line_indent = None;
 627                                        first_line = false;
 628                                    }
 629                                }
 630                            }
 631
 632                            let mut char_ops = diff.push_new(&new_text);
 633                            char_ops.extend(diff.finish());
 634                            line_diff.push_char_operations(&char_ops, &selected_text);
 635                            line_diff.finish(&selected_text);
 636                            diff_tx
 637                                .send((char_ops, line_diff.line_operations()))
 638                                .await?;
 639
 640                            anyhow::Ok(())
 641                        };
 642
 643                        let result = diff.await;
 644
 645                        let error_message = result.as_ref().err().map(|error| error.to_string());
 646                        report_assistant_event(
 647                            AssistantEventData {
 648                                conversation_id: None,
 649                                message_id,
 650                                kind: AssistantKind::Inline,
 651                                phase: AssistantPhase::Response,
 652                                model: model_telemetry_id,
 653                                model_provider: model_provider_id,
 654                                response_latency,
 655                                error_message,
 656                                language_name: language_name.map(|name| name.to_proto()),
 657                            },
 658                            telemetry,
 659                            http_client,
 660                            model_api_key,
 661                            &executor,
 662                        );
 663
 664                        result?;
 665                        Ok(())
 666                    });
 667
 668                while let Some((char_ops, line_ops)) = diff_rx.next().await {
 669                    codegen.update(cx, |codegen, cx| {
 670                        codegen.last_equal_ranges.clear();
 671
 672                        let edits = char_ops
 673                            .into_iter()
 674                            .filter_map(|operation| match operation {
 675                                CharOperation::Insert { text } => {
 676                                    let edit_start = snapshot.anchor_after(edit_start);
 677                                    Some((edit_start..edit_start, text))
 678                                }
 679                                CharOperation::Delete { bytes } => {
 680                                    let edit_end = edit_start + bytes;
 681                                    let edit_range = snapshot.anchor_after(edit_start)
 682                                        ..snapshot.anchor_before(edit_end);
 683                                    edit_start = edit_end;
 684                                    Some((edit_range, String::new()))
 685                                }
 686                                CharOperation::Keep { bytes } => {
 687                                    let edit_end = edit_start + bytes;
 688                                    let edit_range = snapshot.anchor_after(edit_start)
 689                                        ..snapshot.anchor_before(edit_end);
 690                                    edit_start = edit_end;
 691                                    codegen.last_equal_ranges.push(edit_range);
 692                                    None
 693                                }
 694                            })
 695                            .collect::<Vec<_>>();
 696
 697                        if codegen.active {
 698                            codegen.apply_edits(edits.iter().cloned(), cx);
 699                            codegen.reapply_line_based_diff(line_ops.iter().cloned(), cx);
 700                        }
 701                        codegen.edits.extend(edits);
 702                        codegen.line_operations = line_ops;
 703                        codegen.edit_position = Some(snapshot.anchor_after(edit_start));
 704
 705                        cx.notify();
 706                    })?;
 707                }
 708
 709                // Streaming stopped and we have the new text in the buffer, and a line-based diff applied for the whole new buffer.
 710                // That diff is not what a regular diff is and might look unexpected, ergo apply a regular diff.
 711                // It's fine to apply even if the rest of the line diffing fails, as no more hunks are coming through `diff_rx`.
 712                let batch_diff_task =
 713                    codegen.update(cx, |codegen, cx| codegen.reapply_batch_diff(cx))?;
 714                let (line_based_stream_diff, ()) = join!(line_based_stream_diff, batch_diff_task);
 715                line_based_stream_diff?;
 716
 717                anyhow::Ok(())
 718            };
 719
 720            let result = generate.await;
 721            let elapsed_time = start_time.elapsed().as_secs_f64();
 722
 723            codegen
 724                .update(cx, |this, cx| {
 725                    this.message_id = message_id;
 726                    this.last_equal_ranges.clear();
 727                    if let Err(error) = result {
 728                        this.status = CodegenStatus::Error(error);
 729                    } else {
 730                        this.status = CodegenStatus::Done;
 731                    }
 732                    this.elapsed_time = Some(elapsed_time);
 733                    this.completion = Some(completion.lock().clone());
 734                    if let Some(usage) = token_usage {
 735                        let usage = usage.lock();
 736                        telemetry::event!(
 737                            "Inline Assistant Completion",
 738                            model = model_telemetry_id,
 739                            model_provider = model_provider_id,
 740                            input_tokens = usage.input_tokens,
 741                            output_tokens = usage.output_tokens,
 742                        )
 743                    }
 744                    cx.emit(CodegenEvent::Finished);
 745                    cx.notify();
 746                })
 747                .ok();
 748        });
 749        cx.notify();
 750    }
 751
 752    pub fn stop(&mut self, cx: &mut Context<Self>) {
 753        self.last_equal_ranges.clear();
 754        if self.diff.is_empty() {
 755            self.status = CodegenStatus::Idle;
 756        } else {
 757            self.status = CodegenStatus::Done;
 758        }
 759        self.generation = Task::ready(());
 760        cx.emit(CodegenEvent::Finished);
 761        cx.notify();
 762    }
 763
 764    pub fn undo(&mut self, cx: &mut Context<Self>) {
 765        self.buffer.update(cx, |buffer, cx| {
 766            if let Some(transaction_id) = self.transformation_transaction_id.take() {
 767                buffer.undo_transaction(transaction_id, cx);
 768                buffer.refresh_preview(cx);
 769            }
 770        });
 771    }
 772
 773    fn apply_edits(
 774        &mut self,
 775        edits: impl IntoIterator<Item = (Range<Anchor>, String)>,
 776        cx: &mut Context<CodegenAlternative>,
 777    ) {
 778        let transaction = self.buffer.update(cx, |buffer, cx| {
 779            // Avoid grouping agent edits with user edits.
 780            buffer.finalize_last_transaction(cx);
 781            buffer.start_transaction(cx);
 782            buffer.edit(edits, None, cx);
 783            buffer.end_transaction(cx)
 784        });
 785
 786        if let Some(transaction) = transaction {
 787            if let Some(first_transaction) = self.transformation_transaction_id {
 788                // Group all agent edits into the first transaction.
 789                self.buffer.update(cx, |buffer, cx| {
 790                    buffer.merge_transactions(transaction, first_transaction, cx)
 791                });
 792            } else {
 793                self.transformation_transaction_id = Some(transaction);
 794                self.buffer
 795                    .update(cx, |buffer, cx| buffer.finalize_last_transaction(cx));
 796            }
 797        }
 798    }
 799
 800    fn reapply_line_based_diff(
 801        &mut self,
 802        line_operations: impl IntoIterator<Item = LineOperation>,
 803        cx: &mut Context<Self>,
 804    ) {
 805        let old_snapshot = self.snapshot.clone();
 806        let old_range = self.range.to_point(&old_snapshot);
 807        let new_snapshot = self.buffer.read(cx).snapshot(cx);
 808        let new_range = self.range.to_point(&new_snapshot);
 809
 810        let mut old_row = old_range.start.row;
 811        let mut new_row = new_range.start.row;
 812
 813        self.diff.deleted_row_ranges.clear();
 814        self.diff.inserted_row_ranges.clear();
 815        for operation in line_operations {
 816            match operation {
 817                LineOperation::Keep { lines } => {
 818                    old_row += lines;
 819                    new_row += lines;
 820                }
 821                LineOperation::Delete { lines } => {
 822                    let old_end_row = old_row + lines - 1;
 823                    let new_row = new_snapshot.anchor_before(Point::new(new_row, 0));
 824
 825                    if let Some((_, last_deleted_row_range)) =
 826                        self.diff.deleted_row_ranges.last_mut()
 827                    {
 828                        if *last_deleted_row_range.end() + 1 == old_row {
 829                            *last_deleted_row_range = *last_deleted_row_range.start()..=old_end_row;
 830                        } else {
 831                            self.diff
 832                                .deleted_row_ranges
 833                                .push((new_row, old_row..=old_end_row));
 834                        }
 835                    } else {
 836                        self.diff
 837                            .deleted_row_ranges
 838                            .push((new_row, old_row..=old_end_row));
 839                    }
 840
 841                    old_row += lines;
 842                }
 843                LineOperation::Insert { lines } => {
 844                    let new_end_row = new_row + lines - 1;
 845                    let start = new_snapshot.anchor_before(Point::new(new_row, 0));
 846                    let end = new_snapshot.anchor_before(Point::new(
 847                        new_end_row,
 848                        new_snapshot.line_len(MultiBufferRow(new_end_row)),
 849                    ));
 850                    self.diff.inserted_row_ranges.push(start..end);
 851                    new_row += lines;
 852                }
 853            }
 854
 855            cx.notify();
 856        }
 857    }
 858
 859    fn reapply_batch_diff(&mut self, cx: &mut Context<Self>) -> Task<()> {
 860        let old_snapshot = self.snapshot.clone();
 861        let old_range = self.range.to_point(&old_snapshot);
 862        let new_snapshot = self.buffer.read(cx).snapshot(cx);
 863        let new_range = self.range.to_point(&new_snapshot);
 864
 865        cx.spawn(async move |codegen, cx| {
 866            let (deleted_row_ranges, inserted_row_ranges) = cx
 867                .background_spawn(async move {
 868                    let old_text = old_snapshot
 869                        .text_for_range(
 870                            Point::new(old_range.start.row, 0)
 871                                ..Point::new(
 872                                    old_range.end.row,
 873                                    old_snapshot.line_len(MultiBufferRow(old_range.end.row)),
 874                                ),
 875                        )
 876                        .collect::<String>();
 877                    let new_text = new_snapshot
 878                        .text_for_range(
 879                            Point::new(new_range.start.row, 0)
 880                                ..Point::new(
 881                                    new_range.end.row,
 882                                    new_snapshot.line_len(MultiBufferRow(new_range.end.row)),
 883                                ),
 884                        )
 885                        .collect::<String>();
 886
 887                    let old_start_row = old_range.start.row;
 888                    let new_start_row = new_range.start.row;
 889                    let mut deleted_row_ranges: Vec<(Anchor, RangeInclusive<u32>)> = Vec::new();
 890                    let mut inserted_row_ranges = Vec::new();
 891                    for (old_rows, new_rows) in line_diff(&old_text, &new_text) {
 892                        let old_rows = old_start_row + old_rows.start..old_start_row + old_rows.end;
 893                        let new_rows = new_start_row + new_rows.start..new_start_row + new_rows.end;
 894                        if !old_rows.is_empty() {
 895                            deleted_row_ranges.push((
 896                                new_snapshot.anchor_before(Point::new(new_rows.start, 0)),
 897                                old_rows.start..=old_rows.end - 1,
 898                            ));
 899                        }
 900                        if !new_rows.is_empty() {
 901                            let start = new_snapshot.anchor_before(Point::new(new_rows.start, 0));
 902                            let new_end_row = new_rows.end - 1;
 903                            let end = new_snapshot.anchor_before(Point::new(
 904                                new_end_row,
 905                                new_snapshot.line_len(MultiBufferRow(new_end_row)),
 906                            ));
 907                            inserted_row_ranges.push(start..end);
 908                        }
 909                    }
 910                    (deleted_row_ranges, inserted_row_ranges)
 911                })
 912                .await;
 913
 914            codegen
 915                .update(cx, |codegen, cx| {
 916                    codegen.diff.deleted_row_ranges = deleted_row_ranges;
 917                    codegen.diff.inserted_row_ranges = inserted_row_ranges;
 918                    cx.notify();
 919                })
 920                .ok();
 921        })
 922    }
 923}
 924
 925#[derive(Copy, Clone, Debug)]
 926pub enum CodegenEvent {
 927    Finished,
 928    Undone,
 929}
 930
 931struct StripInvalidSpans<T> {
 932    stream: T,
 933    stream_done: bool,
 934    buffer: String,
 935    first_line: bool,
 936    line_end: bool,
 937    starts_with_code_block: bool,
 938}
 939
 940impl<T> StripInvalidSpans<T>
 941where
 942    T: Stream<Item = Result<String>>,
 943{
 944    fn new(stream: T) -> Self {
 945        Self {
 946            stream,
 947            stream_done: false,
 948            buffer: String::new(),
 949            first_line: true,
 950            line_end: false,
 951            starts_with_code_block: false,
 952        }
 953    }
 954}
 955
 956impl<T> Stream for StripInvalidSpans<T>
 957where
 958    T: Stream<Item = Result<String>>,
 959{
 960    type Item = Result<String>;
 961
 962    fn poll_next(self: Pin<&mut Self>, cx: &mut task::Context) -> Poll<Option<Self::Item>> {
 963        const CODE_BLOCK_DELIMITER: &str = "```";
 964        const CURSOR_SPAN: &str = "<|CURSOR|>";
 965
 966        let this = unsafe { self.get_unchecked_mut() };
 967        loop {
 968            if !this.stream_done {
 969                let mut stream = unsafe { Pin::new_unchecked(&mut this.stream) };
 970                match stream.as_mut().poll_next(cx) {
 971                    Poll::Ready(Some(Ok(chunk))) => {
 972                        this.buffer.push_str(&chunk);
 973                    }
 974                    Poll::Ready(Some(Err(error))) => return Poll::Ready(Some(Err(error))),
 975                    Poll::Ready(None) => {
 976                        this.stream_done = true;
 977                    }
 978                    Poll::Pending => return Poll::Pending,
 979                }
 980            }
 981
 982            let mut chunk = String::new();
 983            let mut consumed = 0;
 984            if !this.buffer.is_empty() {
 985                let mut lines = this.buffer.split('\n').enumerate().peekable();
 986                while let Some((line_ix, line)) = lines.next() {
 987                    if line_ix > 0 {
 988                        this.first_line = false;
 989                    }
 990
 991                    if this.first_line {
 992                        let trimmed_line = line.trim();
 993                        if lines.peek().is_some() {
 994                            if trimmed_line.starts_with(CODE_BLOCK_DELIMITER) {
 995                                consumed += line.len() + 1;
 996                                this.starts_with_code_block = true;
 997                                continue;
 998                            }
 999                        } else if trimmed_line.is_empty()
1000                            || prefixes(CODE_BLOCK_DELIMITER)
1001                                .any(|prefix| trimmed_line.starts_with(prefix))
1002                        {
1003                            break;
1004                        }
1005                    }
1006
1007                    let line_without_cursor = line.replace(CURSOR_SPAN, "");
1008                    if lines.peek().is_some() {
1009                        if this.line_end {
1010                            chunk.push('\n');
1011                        }
1012
1013                        chunk.push_str(&line_without_cursor);
1014                        this.line_end = true;
1015                        consumed += line.len() + 1;
1016                    } else if this.stream_done {
1017                        if !this.starts_with_code_block
1018                            || !line_without_cursor.trim().ends_with(CODE_BLOCK_DELIMITER)
1019                        {
1020                            if this.line_end {
1021                                chunk.push('\n');
1022                            }
1023
1024                            chunk.push_str(line);
1025                        }
1026
1027                        consumed += line.len();
1028                    } else {
1029                        let trimmed_line = line.trim();
1030                        if trimmed_line.is_empty()
1031                            || prefixes(CURSOR_SPAN).any(|prefix| trimmed_line.ends_with(prefix))
1032                            || prefixes(CODE_BLOCK_DELIMITER)
1033                                .any(|prefix| trimmed_line.ends_with(prefix))
1034                        {
1035                            break;
1036                        } else {
1037                            if this.line_end {
1038                                chunk.push('\n');
1039                                this.line_end = false;
1040                            }
1041
1042                            chunk.push_str(&line_without_cursor);
1043                            consumed += line.len();
1044                        }
1045                    }
1046                }
1047            }
1048
1049            this.buffer = this.buffer.split_off(consumed);
1050            if !chunk.is_empty() {
1051                return Poll::Ready(Some(Ok(chunk)));
1052            } else if this.stream_done {
1053                return Poll::Ready(None);
1054            }
1055        }
1056    }
1057}
1058
1059fn prefixes(text: &str) -> impl Iterator<Item = &str> {
1060    (0..text.len() - 1).map(|ix| &text[..ix + 1])
1061}
1062
1063#[derive(Default)]
1064pub struct Diff {
1065    pub deleted_row_ranges: Vec<(Anchor, RangeInclusive<u32>)>,
1066    pub inserted_row_ranges: Vec<Range<Anchor>>,
1067}
1068
1069impl Diff {
1070    fn is_empty(&self) -> bool {
1071        self.deleted_row_ranges.is_empty() && self.inserted_row_ranges.is_empty()
1072    }
1073}
1074
1075#[cfg(test)]
1076mod tests {
1077    use super::*;
1078    use fs::FakeFs;
1079    use futures::{
1080        Stream,
1081        stream::{self},
1082    };
1083    use gpui::TestAppContext;
1084    use indoc::indoc;
1085    use language::{
1086        Buffer, Language, LanguageConfig, LanguageMatcher, Point, language_settings,
1087        tree_sitter_rust,
1088    };
1089    use language_model::{LanguageModelRegistry, TokenUsage};
1090    use rand::prelude::*;
1091    use settings::SettingsStore;
1092    use std::{future, sync::Arc};
1093
1094    #[gpui::test(iterations = 10)]
1095    async fn test_transform_autoindent(cx: &mut TestAppContext, mut rng: StdRng) {
1096        init_test(cx);
1097
1098        let text = indoc! {"
1099            fn main() {
1100                let x = 0;
1101                for _ in 0..10 {
1102                    x += 1;
1103                }
1104            }
1105        "};
1106        let buffer = cx.new(|cx| Buffer::local(text, cx).with_language(Arc::new(rust_lang()), cx));
1107        let buffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx));
1108        let range = buffer.read_with(cx, |buffer, cx| {
1109            let snapshot = buffer.snapshot(cx);
1110            snapshot.anchor_before(Point::new(1, 0))..snapshot.anchor_after(Point::new(4, 5))
1111        });
1112        let prompt_builder = Arc::new(PromptBuilder::new(None).unwrap());
1113        let fs = FakeFs::new(cx.executor());
1114        let project = Project::test(fs, vec![], cx).await;
1115        let codegen = cx.new(|cx| {
1116            CodegenAlternative::new(
1117                buffer.clone(),
1118                range.clone(),
1119                true,
1120                None,
1121                project.downgrade(),
1122                None,
1123                None,
1124                prompt_builder,
1125                cx,
1126            )
1127        });
1128
1129        let chunks_tx = simulate_response_stream(&codegen, cx);
1130
1131        let mut new_text = concat!(
1132            "       let mut x = 0;\n",
1133            "       while x < 10 {\n",
1134            "           x += 1;\n",
1135            "       }",
1136        );
1137        while !new_text.is_empty() {
1138            let max_len = cmp::min(new_text.len(), 10);
1139            let len = rng.random_range(1..=max_len);
1140            let (chunk, suffix) = new_text.split_at(len);
1141            chunks_tx.unbounded_send(chunk.to_string()).unwrap();
1142            new_text = suffix;
1143            cx.background_executor.run_until_parked();
1144        }
1145        drop(chunks_tx);
1146        cx.background_executor.run_until_parked();
1147
1148        assert_eq!(
1149            buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()),
1150            indoc! {"
1151                fn main() {
1152                    let mut x = 0;
1153                    while x < 10 {
1154                        x += 1;
1155                    }
1156                }
1157            "}
1158        );
1159    }
1160
1161    #[gpui::test(iterations = 10)]
1162    async fn test_autoindent_when_generating_past_indentation(
1163        cx: &mut TestAppContext,
1164        mut rng: StdRng,
1165    ) {
1166        init_test(cx);
1167
1168        let text = indoc! {"
1169            fn main() {
1170                le
1171            }
1172        "};
1173        let buffer = cx.new(|cx| Buffer::local(text, cx).with_language(Arc::new(rust_lang()), cx));
1174        let buffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx));
1175        let range = buffer.read_with(cx, |buffer, cx| {
1176            let snapshot = buffer.snapshot(cx);
1177            snapshot.anchor_before(Point::new(1, 6))..snapshot.anchor_after(Point::new(1, 6))
1178        });
1179        let prompt_builder = Arc::new(PromptBuilder::new(None).unwrap());
1180        let fs = FakeFs::new(cx.executor());
1181        let project = Project::test(fs, vec![], cx).await;
1182        let codegen = cx.new(|cx| {
1183            CodegenAlternative::new(
1184                buffer.clone(),
1185                range.clone(),
1186                true,
1187                None,
1188                project.downgrade(),
1189                None,
1190                None,
1191                prompt_builder,
1192                cx,
1193            )
1194        });
1195
1196        let chunks_tx = simulate_response_stream(&codegen, cx);
1197
1198        cx.background_executor.run_until_parked();
1199
1200        let mut new_text = concat!(
1201            "t mut x = 0;\n",
1202            "while x < 10 {\n",
1203            "    x += 1;\n",
1204            "}", //
1205        );
1206        while !new_text.is_empty() {
1207            let max_len = cmp::min(new_text.len(), 10);
1208            let len = rng.random_range(1..=max_len);
1209            let (chunk, suffix) = new_text.split_at(len);
1210            chunks_tx.unbounded_send(chunk.to_string()).unwrap();
1211            new_text = suffix;
1212            cx.background_executor.run_until_parked();
1213        }
1214        drop(chunks_tx);
1215        cx.background_executor.run_until_parked();
1216
1217        assert_eq!(
1218            buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()),
1219            indoc! {"
1220                fn main() {
1221                    let mut x = 0;
1222                    while x < 10 {
1223                        x += 1;
1224                    }
1225                }
1226            "}
1227        );
1228    }
1229
1230    #[gpui::test(iterations = 10)]
1231    async fn test_autoindent_when_generating_before_indentation(
1232        cx: &mut TestAppContext,
1233        mut rng: StdRng,
1234    ) {
1235        init_test(cx);
1236
1237        let text = concat!(
1238            "fn main() {\n",
1239            "  \n",
1240            "}\n" //
1241        );
1242        let buffer = cx.new(|cx| Buffer::local(text, cx).with_language(Arc::new(rust_lang()), cx));
1243        let buffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx));
1244        let range = buffer.read_with(cx, |buffer, cx| {
1245            let snapshot = buffer.snapshot(cx);
1246            snapshot.anchor_before(Point::new(1, 2))..snapshot.anchor_after(Point::new(1, 2))
1247        });
1248        let prompt_builder = Arc::new(PromptBuilder::new(None).unwrap());
1249        let fs = FakeFs::new(cx.executor());
1250        let project = Project::test(fs, vec![], cx).await;
1251        let codegen = cx.new(|cx| {
1252            CodegenAlternative::new(
1253                buffer.clone(),
1254                range.clone(),
1255                true,
1256                None,
1257                project.downgrade(),
1258                None,
1259                None,
1260                prompt_builder,
1261                cx,
1262            )
1263        });
1264
1265        let chunks_tx = simulate_response_stream(&codegen, cx);
1266
1267        cx.background_executor.run_until_parked();
1268
1269        let mut new_text = concat!(
1270            "let mut x = 0;\n",
1271            "while x < 10 {\n",
1272            "    x += 1;\n",
1273            "}", //
1274        );
1275        while !new_text.is_empty() {
1276            let max_len = cmp::min(new_text.len(), 10);
1277            let len = rng.random_range(1..=max_len);
1278            let (chunk, suffix) = new_text.split_at(len);
1279            chunks_tx.unbounded_send(chunk.to_string()).unwrap();
1280            new_text = suffix;
1281            cx.background_executor.run_until_parked();
1282        }
1283        drop(chunks_tx);
1284        cx.background_executor.run_until_parked();
1285
1286        assert_eq!(
1287            buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()),
1288            indoc! {"
1289                fn main() {
1290                    let mut x = 0;
1291                    while x < 10 {
1292                        x += 1;
1293                    }
1294                }
1295            "}
1296        );
1297    }
1298
1299    #[gpui::test(iterations = 10)]
1300    async fn test_autoindent_respects_tabs_in_selection(cx: &mut TestAppContext) {
1301        init_test(cx);
1302
1303        let text = indoc! {"
1304            func main() {
1305            \tx := 0
1306            \tfor i := 0; i < 10; i++ {
1307            \t\tx++
1308            \t}
1309            }
1310        "};
1311        let buffer = cx.new(|cx| Buffer::local(text, cx));
1312        let buffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx));
1313        let range = buffer.read_with(cx, |buffer, cx| {
1314            let snapshot = buffer.snapshot(cx);
1315            snapshot.anchor_before(Point::new(0, 0))..snapshot.anchor_after(Point::new(4, 2))
1316        });
1317        let prompt_builder = Arc::new(PromptBuilder::new(None).unwrap());
1318        let fs = FakeFs::new(cx.executor());
1319        let project = Project::test(fs, vec![], cx).await;
1320        let codegen = cx.new(|cx| {
1321            CodegenAlternative::new(
1322                buffer.clone(),
1323                range.clone(),
1324                true,
1325                None,
1326                project.downgrade(),
1327                None,
1328                None,
1329                prompt_builder,
1330                cx,
1331            )
1332        });
1333
1334        let chunks_tx = simulate_response_stream(&codegen, cx);
1335        let new_text = concat!(
1336            "func main() {\n",
1337            "\tx := 0\n",
1338            "\tfor x < 10 {\n",
1339            "\t\tx++\n",
1340            "\t}", //
1341        );
1342        chunks_tx.unbounded_send(new_text.to_string()).unwrap();
1343        drop(chunks_tx);
1344        cx.background_executor.run_until_parked();
1345
1346        assert_eq!(
1347            buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()),
1348            indoc! {"
1349                func main() {
1350                \tx := 0
1351                \tfor x < 10 {
1352                \t\tx++
1353                \t}
1354                }
1355            "}
1356        );
1357    }
1358
1359    #[gpui::test]
1360    async fn test_inactive_codegen_alternative(cx: &mut TestAppContext) {
1361        init_test(cx);
1362
1363        let text = indoc! {"
1364            fn main() {
1365                let x = 0;
1366            }
1367        "};
1368        let buffer = cx.new(|cx| Buffer::local(text, cx).with_language(Arc::new(rust_lang()), cx));
1369        let buffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx));
1370        let range = buffer.read_with(cx, |buffer, cx| {
1371            let snapshot = buffer.snapshot(cx);
1372            snapshot.anchor_before(Point::new(1, 0))..snapshot.anchor_after(Point::new(1, 14))
1373        });
1374        let prompt_builder = Arc::new(PromptBuilder::new(None).unwrap());
1375        let fs = FakeFs::new(cx.executor());
1376        let project = Project::test(fs, vec![], cx).await;
1377        let codegen = cx.new(|cx| {
1378            CodegenAlternative::new(
1379                buffer.clone(),
1380                range.clone(),
1381                false,
1382                None,
1383                project.downgrade(),
1384                None,
1385                None,
1386                prompt_builder,
1387                cx,
1388            )
1389        });
1390
1391        let chunks_tx = simulate_response_stream(&codegen, cx);
1392        chunks_tx
1393            .unbounded_send("let mut x = 0;\nx += 1;".to_string())
1394            .unwrap();
1395        drop(chunks_tx);
1396        cx.run_until_parked();
1397
1398        // The codegen is inactive, so the buffer doesn't get modified.
1399        assert_eq!(
1400            buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()),
1401            text
1402        );
1403
1404        // Activating the codegen applies the changes.
1405        codegen.update(cx, |codegen, cx| codegen.set_active(true, cx));
1406        assert_eq!(
1407            buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()),
1408            indoc! {"
1409                fn main() {
1410                    let mut x = 0;
1411                    x += 1;
1412                }
1413            "}
1414        );
1415
1416        // Deactivating the codegen undoes the changes.
1417        codegen.update(cx, |codegen, cx| codegen.set_active(false, cx));
1418        cx.run_until_parked();
1419        assert_eq!(
1420            buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()),
1421            text
1422        );
1423    }
1424
1425    #[gpui::test]
1426    async fn test_strip_invalid_spans_from_codeblock() {
1427        assert_chunks("Lorem ipsum dolor", "Lorem ipsum dolor").await;
1428        assert_chunks("```\nLorem ipsum dolor", "Lorem ipsum dolor").await;
1429        assert_chunks("```\nLorem ipsum dolor\n```", "Lorem ipsum dolor").await;
1430        assert_chunks(
1431            "```html\n```js\nLorem ipsum dolor\n```\n```",
1432            "```js\nLorem ipsum dolor\n```",
1433        )
1434        .await;
1435        assert_chunks("``\nLorem ipsum dolor\n```", "``\nLorem ipsum dolor\n```").await;
1436        assert_chunks("Lorem<|CURSOR|> ipsum", "Lorem ipsum").await;
1437        assert_chunks("Lorem ipsum", "Lorem ipsum").await;
1438        assert_chunks("```\n<|CURSOR|>Lorem ipsum\n```", "Lorem ipsum").await;
1439
1440        async fn assert_chunks(text: &str, expected_text: &str) {
1441            for chunk_size in 1..=text.len() {
1442                let actual_text = StripInvalidSpans::new(chunks(text, chunk_size))
1443                    .map(|chunk| chunk.unwrap())
1444                    .collect::<String>()
1445                    .await;
1446                assert_eq!(
1447                    actual_text, expected_text,
1448                    "failed to strip invalid spans, chunk size: {}",
1449                    chunk_size
1450                );
1451            }
1452        }
1453
1454        fn chunks(text: &str, size: usize) -> impl Stream<Item = Result<String>> {
1455            stream::iter(
1456                text.chars()
1457                    .collect::<Vec<_>>()
1458                    .chunks(size)
1459                    .map(|chunk| Ok(chunk.iter().collect::<String>()))
1460                    .collect::<Vec<_>>(),
1461            )
1462        }
1463    }
1464
1465    fn init_test(cx: &mut TestAppContext) {
1466        cx.update(LanguageModelRegistry::test);
1467        cx.set_global(cx.update(SettingsStore::test));
1468        cx.update(Project::init_settings);
1469        cx.update(language_settings::init);
1470    }
1471
1472    fn simulate_response_stream(
1473        codegen: &Entity<CodegenAlternative>,
1474        cx: &mut TestAppContext,
1475    ) -> mpsc::UnboundedSender<String> {
1476        let (chunks_tx, chunks_rx) = mpsc::unbounded();
1477        codegen.update(cx, |codegen, cx| {
1478            codegen.handle_stream(
1479                String::new(),
1480                String::new(),
1481                None,
1482                future::ready(Ok(LanguageModelTextStream {
1483                    message_id: None,
1484                    stream: chunks_rx.map(Ok).boxed(),
1485                    last_token_usage: Arc::new(Mutex::new(TokenUsage::default())),
1486                })),
1487                cx,
1488            );
1489        });
1490        chunks_tx
1491    }
1492
1493    fn rust_lang() -> Language {
1494        Language::new(
1495            LanguageConfig {
1496                name: "Rust".into(),
1497                matcher: LanguageMatcher {
1498                    path_suffixes: vec!["rs".to_string()],
1499                    ..Default::default()
1500                },
1501                ..Default::default()
1502            },
1503            Some(tree_sitter_rust::LANGUAGE.into()),
1504        )
1505        .with_indents_query(
1506            r#"
1507            (call_expression) @indent
1508            (field_expression) @indent
1509            (_ "(" ")" @end) @indent
1510            (_ "{" "}" @end) @indent
1511            "#,
1512        )
1513        .unwrap()
1514    }
1515}