Merge messages whose header has been invalidated

This commit is contained in:
Antonio Scandurra 2023-06-13 15:09:19 +02:00
parent 2ae8b558b9
commit 2842fc2b1d

View file

@ -784,14 +784,24 @@ impl Assistant {
let buffer = self.buffer.read(cx); let buffer = self.buffer.read(cx);
let mut messages = self.messages.iter().peekable(); let mut messages = self.messages.iter().peekable();
iter::from_fn(move || { iter::from_fn(move || {
let message = messages.next()?; while let Some(message) = messages.next() {
let metadata = self.messages_metadata.get(&message.id)?; let metadata = self.messages_metadata.get(&message.id)?;
let message_start = message.start.to_offset(buffer); let message_start = message.start.to_offset(buffer);
let message_end = messages let mut message_end = None;
.peek() while let Some(next_message) = messages.peek() {
.map_or(language::Anchor::MAX, |message| message.start) if next_message.start.is_valid(buffer) {
.to_offset(buffer); message_end = Some(next_message.start);
Some((message, metadata, message_start..message_end)) break;
} else {
messages.next();
}
}
let message_end = message_end
.unwrap_or(language::Anchor::MAX)
.to_offset(buffer);
return Some((message, metadata, message_start..message_end));
}
None
}) })
} }
} }
@ -1368,7 +1378,18 @@ mod tests {
assert_eq!( assert_eq!(
messages(&assistant, cx), messages(&assistant, cx),
vec![ vec![
(message_1.id, Role::User, 0..6), (message_1.id, Role::User, 0..3),
(message_3.id, Role::User, 3..4),
]
);
buffer.update(cx, |buffer, cx| buffer.undo(cx));
assert_eq!(
messages(&assistant, cx),
vec![
(message_1.id, Role::User, 0..2),
(message_2.id, Role::Assistant, 2..4),
(message_4.id, Role::User, 4..6),
(message_3.id, Role::User, 6..7), (message_3.id, Role::User, 6..7),
] ]
); );