Skip to content
This repository was archived by the owner on Jan 2, 2025. It is now read-only.

Commit db22a44

Browse files
committed
Change server to render multiline hunks correctly
1 parent bfaeb4a commit db22a44

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

server/bleep/src/webserver/answer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,7 @@ impl Conversation {
921921
}
922922
item
923923
})
924+
.flat_map(|v| v.expand())
924925
.map(|s| s.substitute_path_alias(&self.paths))
925926
.collect::<Vec<_>>();
926927

server/bleep/src/webserver/answer/response.rs

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@ impl SearchResult {
139139
s => s,
140140
}
141141
}
142+
143+
pub(crate) fn expand(self) -> Vec<Self> {
144+
match self {
145+
SearchResult::Modify(modify) => modify.expand(),
146+
_ => vec![self],
147+
}
148+
}
142149
}
143150

144151
#[derive(serde::Serialize, serde::Deserialize, Default, Debug, Clone)]
@@ -268,18 +275,11 @@ impl ModifyResult {
268275
.get(1)
269276
.and_then(serde_json::Value::as_str)
270277
.map(ToOwned::to_owned);
278+
271279
let diff = v
272280
.get(3)
273281
.and_then(serde_json::Value::as_str)
274-
.map(|raw_hunk| {
275-
let header = raw_hunk.lines().next().and_then(|s| s.parse().ok());
276-
let lines = raw_hunk
277-
.lines()
278-
.skip(1)
279-
.map(ToOwned::to_owned)
280-
.collect::<Vec<_>>();
281-
ModifyResultHunk { header, lines }
282-
});
282+
.and_then(Self::parse_hunk);
283283

284284
let raw = v
285285
.get(3)
@@ -320,6 +320,39 @@ impl ModifyResult {
320320
});
321321
self
322322
}
323+
324+
fn parse_hunk(raw_hunk: &str) -> Option<ModifyResultHunk> {
325+
let mut raw = raw_hunk.lines();
326+
let header = raw.next().and_then(|s| s.parse().ok());
327+
let lines = raw.map(ToOwned::to_owned).collect::<Vec<_>>();
328+
329+
Some(ModifyResultHunk { header, lines })
330+
}
331+
332+
fn expand(self) -> Vec<SearchResult> {
333+
self.raw
334+
.iter()
335+
.flat_map(|raw_hunk| raw_hunk.split("\n@@"))
336+
.map(|hunk| {
337+
if hunk.starts_with("@@") {
338+
hunk.to_owned()
339+
} else {
340+
format!("@@{hunk}")
341+
}
342+
})
343+
.map(|raw| {
344+
SearchResult::Modify(ModifyResult {
345+
diff: Self::parse_hunk(&raw),
346+
path_alias: self.path_alias,
347+
path: self.path.clone(),
348+
language: self.language.clone(),
349+
description: self.description.clone(),
350+
raw: Some(raw),
351+
new_file: self.new_file.clone(),
352+
})
353+
})
354+
.collect::<Vec<_>>()
355+
}
323356
}
324357

325358
impl std::str::FromStr for ModifyResultHunkHeader {

0 commit comments

Comments
 (0)