Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

assistant: Normalize line endings for prompts loaded from templates (#16808)

张小白5ee4c036f95f
diff --git a/crates/assistant/src/prompts.rs b/crates/assistant/src/prompts.rs
index 462f74f5d3e..ef49c5ced6a 100644
--- a/crates/assistant/src/prompts.rs
+++ b/crates/assistant/src/prompts.rs
@@ -8,6 +8,7 @@ use language::BufferSnapshot;
use parking_lot::Mutex;
use serde::Serialize;
use std::{ops::Range, path::PathBuf, sync::Arc, time::Duration};
+use text::LineEnding;
use util::ResultExt;
#[derive(Serialize)]
@@ -191,8 +192,8 @@ impl PromptBuilder {
if let Some(id) = path.split('/').last().and_then(|s| s.strip_suffix(".hbs")) {
if let Some(prompt) = Assets.load(path.as_ref()).log_err().flatten() {
log::info!("Registering built-in prompt template: {}", id);
- handlebars
- .register_template_string(id, String::from_utf8_lossy(prompt.as_ref()))?
+ let prompt = String::from_utf8_lossy(prompt.as_ref());
+ handlebars.register_template_string(id, LineEnding::normalize_cow(prompt))?
}
}
}
diff --git a/crates/text/src/text.rs b/crates/text/src/text.rs
index 6e10068d47b..b17748c6d06 100644
--- a/crates/text/src/text.rs
+++ b/crates/text/src/text.rs
@@ -3041,4 +3041,12 @@ impl LineEnding {
text
}
}
+
+ pub fn normalize_cow(text: Cow<str>) -> Cow<str> {
+ if let Cow::Owned(replaced) = LINE_SEPARATORS_REGEX.replace_all(&text, "\n") {
+ replaced.into()
+ } else {
+ text
+ }
+ }
}