Accept partial copilot suggestions (#8682)

Fixes https://github.com/zed-industries/zed/issues/8020
 
This PR adds a new shortcut cmd-right, if a copilot suggestion exists.
The suggestions is accepted word by word.
It emulates the behaviour of VS Code's Github Copilot implementation.


Release Notes:

- Added ability to accept partial copilot suggestions ([8020](https://github.com/zed-industries/zed/issues/8020))
This commit is contained in:
Jonathan
2024-03-03 18:24:48 +02:00
committed by GitHub
parent 56f0418c93
commit 08f9c3f568
6 changed files with 187 additions and 4 deletions
+37
View File
@@ -1627,6 +1627,10 @@ impl Editor {
key_context.set("extension", extension.to_string());
}
if self.has_active_copilot_suggestion(cx) {
key_context.add("copilot_suggestion");
}
key_context
}
@@ -3965,6 +3969,39 @@ impl Editor {
}
}
fn accept_partial_copilot_suggestion(
&mut self,
_: &AcceptPartialCopilotSuggestion,
cx: &mut ViewContext<Self>,
) {
if self.selections.count() == 1 && self.has_active_copilot_suggestion(cx) {
if let Some(suggestion) = self.take_active_copilot_suggestion(cx) {
let mut partial_suggestion = suggestion
.text
.chars()
.by_ref()
.take_while(|c| c.is_alphabetic())
.collect::<String>();
if partial_suggestion.is_empty() {
partial_suggestion = suggestion
.text
.chars()
.by_ref()
.take_while(|c| c.is_whitespace() || !c.is_alphabetic())
.collect::<String>();
}
cx.emit(EditorEvent::InputHandled {
utf16_range_to_replace: None,
text: partial_suggestion.clone().into(),
});
self.insert_with_autoindent_mode(&partial_suggestion, None, cx);
self.refresh_copilot_suggestions(true, cx);
cx.notify();
}
}
}
fn discard_copilot_suggestion(&mut self, cx: &mut ViewContext<Self>) -> bool {
if let Some(suggestion) = self.take_active_copilot_suggestion(cx) {
if let Some(copilot) = Copilot::global(cx) {