diff --git a/crates/assistant/src/assistant_panel.rs b/crates/assistant/src/assistant_panel.rs index ac7f31f396434289d74f97fa1d7559de261184e6..895a2a7d4bd9ee8cea0c0d0cd77adbc22df305af 100644 --- a/crates/assistant/src/assistant_panel.rs +++ b/crates/assistant/src/assistant_panel.rs @@ -2088,14 +2088,14 @@ impl ContextEditor { command_range: Range, name: &str, arguments: &[String], - insert_trailing_newline: bool, + ensure_trailing_newline: bool, workspace: WeakView, cx: &mut ViewContext, ) { if let Some(command) = SlashCommandRegistry::global(cx).command(name) { let output = command.run(arguments, workspace, self.lsp_adapter_delegate.clone(), cx); self.context.update(cx, |context, cx| { - context.insert_command_output(command_range, output, insert_trailing_newline, cx) + context.insert_command_output(command_range, output, ensure_trailing_newline, cx) }); } } diff --git a/crates/assistant/src/context.rs b/crates/assistant/src/context.rs index 5b0209b433671e590cc9d2a9c02d8bf01c414207..4608c5df4b25ff7bc51de7eea9bd12b11e4829d1 100644 --- a/crates/assistant/src/context.rs +++ b/crates/assistant/src/context.rs @@ -1395,7 +1395,7 @@ impl Context { &mut self, command_range: Range, output: Task>, - insert_trailing_newline: bool, + ensure_trailing_newline: bool, cx: &mut ModelContext, ) { self.reparse_slash_commands(cx); @@ -1406,8 +1406,27 @@ impl Context { let output = output.await; this.update(&mut cx, |this, cx| match output { Ok(mut output) => { - if insert_trailing_newline { - output.text.push('\n'); + // Ensure section ranges are valid. + for section in &mut output.sections { + section.range.start = section.range.start.min(output.text.len()); + section.range.end = section.range.end.min(output.text.len()); + while !output.text.is_char_boundary(section.range.start) { + section.range.start -= 1; + } + while !output.text.is_char_boundary(section.range.end) { + section.range.end += 1; + } + } + + // Ensure there is a newline after the last section. + if ensure_trailing_newline { + let has_newline_after_last_section = + output.sections.last().map_or(false, |last_section| { + output.text[last_section.range.end..].ends_with('\n') + }); + if !has_newline_after_last_section { + output.text.push('\n'); + } } let version = this.version.clone();