buffer_codegen.rs

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