buffer_codegen.rs

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