Compare commits

...

2 Commits

Author SHA1 Message Date
Nathan Sobo
c7e68a043e Update test assertions to pass with new cursor bias
This is a behavior change, but I honestly think it's an improvement.
2024-05-22 20:46:37 -06:00
Nathan Sobo
79ee03f1a1 Preserve a trailing user message in the context editor
To make this work, I had to switch the bias of selections to always
be left on the start and the end. Worried about what this could break,
but we've wanted to make this change for a long time anyway.
2024-05-22 20:30:03 -06:00
3 changed files with 32 additions and 16 deletions

View File

@@ -1625,6 +1625,7 @@ impl Conversation {
slash_command_registry,
};
this.set_language(cx);
this.ensure_trailing_user_message(cx);
this.reparse_edit_suggestions(cx);
this.count_remaining_tokens(cx);
this
@@ -1704,13 +1705,33 @@ impl Conversation {
cx: &mut ModelContext<Self>,
) {
if *event == language::Event::Edited {
self.count_remaining_tokens(cx);
self.ensure_trailing_user_message(cx);
self.reparse_edit_suggestions(cx);
self.reparse_slash_command_calls(cx);
self.count_remaining_tokens(cx);
cx.emit(ConversationEvent::MessagesEdited);
}
}
/// If we find a valid trailing message that isn't a user message, then add one
fn ensure_trailing_user_message(&mut self, cx: &mut ModelContext<Self>) {
let mut trailing_id = None;
for MessageAnchor { id, start } in self.message_anchors.iter().rev() {
if start.is_valid(self.buffer.read(cx)) {
if self.messages_metadata[id].role == Role::User {
return;
} else {
trailing_id = Some(*id);
break;
}
}
}
if let Some(id) = trailing_id {
self.insert_message_after(id, Role::User, MessageStatus::Done, cx);
}
}
pub(crate) fn count_remaining_tokens(&mut self, cx: &mut ModelContext<Self>) {
let request = self.to_completion_request(cx);
self.pending_token_count = cx.spawn(|this, mut cx| {
@@ -2207,6 +2228,7 @@ impl Conversation {
for id in ids {
if let Some(metadata) = self.messages_metadata.get_mut(&id) {
metadata.role.cycle();
self.ensure_trailing_user_message(cx);
cx.emit(ConversationEvent::MessagesEdited);
cx.notify();
}

View File

@@ -6958,7 +6958,7 @@ async fn test_advance_downward_on_toggle_comment(cx: &mut gpui::TestAppContext)
cx.assert_editor_state(indoc!(
"fn a() {
// dog();
catˇ();
ˇcat();
}"
));
@@ -6974,7 +6974,7 @@ async fn test_advance_downward_on_toggle_comment(cx: &mut gpui::TestAppContext)
});
cx.assert_editor_state(indoc!(
"fn a() {
// «dog()ˇ»;
«// dog()ˇ»;
cat();
}"
));
@@ -6992,7 +6992,7 @@ async fn test_advance_downward_on_toggle_comment(cx: &mut gpui::TestAppContext)
cx.assert_editor_state(indoc!(
"fn a() {
// dog();
catˇ(ˇ);
ˇcat(ˇ);
}"
));
@@ -7008,7 +7008,7 @@ async fn test_advance_downward_on_toggle_comment(cx: &mut gpui::TestAppContext)
});
cx.assert_editor_state(indoc!(
"fn a() {
// ˇdˇog«()ˇ»;
ˇ// dˇog«()ˇ»;
cat();
}"
));

View File

@@ -534,20 +534,14 @@ impl<'a> MutableSelectionsCollection<'a> {
}
}
self.collection.disjoint = Arc::from_iter(selections.into_iter().map(|selection| {
let end_bias = if selection.end > selection.start {
Bias::Left
} else {
Bias::Right
};
Selection {
self.collection.disjoint =
Arc::from_iter(selections.into_iter().map(|selection| Selection {
id: selection.id,
start: buffer.anchor_after(selection.start),
end: buffer.anchor_at(selection.end, end_bias),
start: buffer.anchor_before(selection.start),
end: buffer.anchor_before(selection.end),
reversed: selection.reversed,
goal: selection.goal,
}
}));
}));
self.collection.pending = None;
self.selections_changed = true;