Make autocompletion and copilot mutually exclusive

This commit is contained in:
Antonio Scandurra 2023-04-03 13:56:11 +02:00
parent b6a9d90609
commit ec5309b543
3 changed files with 36 additions and 53 deletions

View File

@ -230,6 +230,10 @@ impl DisplayMap {
self.text_highlights.remove(&Some(type_id)) self.text_highlights.remove(&Some(type_id))
} }
pub fn has_suggestion(&self) -> bool {
self.suggestion_map.has_suggestion()
}
pub fn replace_suggestion<T>( pub fn replace_suggestion<T>(
&self, &self,
new_suggestion: Option<Suggestion<T>>, new_suggestion: Option<Suggestion<T>>,

View File

@ -171,6 +171,11 @@ impl SuggestionMap {
(snapshot.clone(), suggestion_edits) (snapshot.clone(), suggestion_edits)
} }
pub fn has_suggestion(&self) -> bool {
let snapshot = self.0.lock();
snapshot.suggestion.is_some()
}
} }
#[derive(Clone)] #[derive(Clone)]

View File

@ -2188,7 +2188,9 @@ impl Editor {
} }
fn trigger_completion_on_input(&mut self, text: &str, cx: &mut ViewContext<Self>) { fn trigger_completion_on_input(&mut self, text: &str, cx: &mut ViewContext<Self>) {
if !cx.global::<Settings>().show_completions_on_input { if !cx.global::<Settings>().show_completions_on_input
|| self.has_active_copilot_suggestion(cx)
{
return; return;
} }
@ -2373,11 +2375,11 @@ impl Editor {
} }
this.completion_tasks.retain(|(id, _)| *id > menu.id); this.completion_tasks.retain(|(id, _)| *id > menu.id);
if this.focused { if this.focused && !menu.matches.is_empty() {
this.show_context_menu(ContextMenu::Completions(menu), cx); this.show_context_menu(ContextMenu::Completions(menu), cx);
} else {
this.hide_context_menu(cx);
} }
cx.notify();
}); });
} }
Ok::<_, anyhow::Error>(()) Ok::<_, anyhow::Error>(())
@ -2806,33 +2808,9 @@ impl Editor {
let cursor = if selection.start == selection.end { let cursor = if selection.start == selection.end {
selection.start.bias_left(&snapshot) selection.start.bias_left(&snapshot)
} else { } else {
self.clear_copilot_suggestions(cx);
return None; return None;
}; };
self.refresh_active_copilot_suggestion(cx);
if let Some(new_text) = self
.copilot_state
.text_for_active_completion(cursor, &snapshot)
{
self.display_map.update(cx, |map, cx| {
map.replace_suggestion(
Some(Suggestion {
position: cursor,
text: new_text.into(),
}),
cx,
)
});
self.copilot_state
.completions
.swap(0, self.copilot_state.active_completion_index);
self.copilot_state.completions.truncate(1);
self.copilot_state.active_completion_index = 0;
cx.notify();
} else {
self.display_map
.update(cx, |map, cx| map.replace_suggestion::<usize>(None, cx));
}
if !copilot.read(cx).status().is_authorized() { if !copilot.read(cx).status().is_authorized() {
return None; return None;
@ -2860,23 +2838,7 @@ impl Editor {
for completion in completions { for completion in completions {
this.copilot_state.push_completion(completion); this.copilot_state.push_completion(completion);
} }
this.refresh_active_copilot_suggestion(cx);
let buffer = this.buffer.read(cx).snapshot(cx);
if let Some(text) = this
.copilot_state
.text_for_active_completion(cursor, &buffer)
{
this.display_map.update(cx, |map, cx| {
map.replace_suggestion(
Some(Suggestion {
position: cursor,
text: text.into(),
}),
cx,
)
});
}
cx.notify();
} }
}); });
@ -2901,7 +2863,7 @@ impl Editor {
self.copilot_state.active_completion_index = self.copilot_state.active_completion_index =
(self.copilot_state.active_completion_index + 1) % self.copilot_state.completions.len(); (self.copilot_state.active_completion_index + 1) % self.copilot_state.completions.len();
self.sync_suggestion(cx); self.refresh_active_copilot_suggestion(cx);
} }
fn previous_copilot_suggestion( fn previous_copilot_suggestion(
@ -2927,7 +2889,7 @@ impl Editor {
self.copilot_state.active_completion_index - 1 self.copilot_state.active_completion_index - 1
}; };
self.sync_suggestion(cx); self.refresh_active_copilot_suggestion(cx);
} }
fn toggle_copilot_suggestions(&mut self, _: &copilot::Toggle, cx: &mut ViewContext<Self>) { fn toggle_copilot_suggestions(&mut self, _: &copilot::Toggle, cx: &mut ViewContext<Self>) {
@ -2957,11 +2919,13 @@ impl Editor {
cx.notify(); cx.notify();
} }
fn sync_suggestion(&mut self, cx: &mut ViewContext<Self>) { fn refresh_active_copilot_suggestion(&mut self, cx: &mut ViewContext<Self>) {
let snapshot = self.buffer.read(cx).snapshot(cx); let snapshot = self.buffer.read(cx).snapshot(cx);
let cursor = self.selections.newest_anchor().head(); let cursor = self.selections.newest_anchor().head();
if self.context_menu.is_some() {
if let Some(text) = self self.display_map
.update(cx, |map, cx| map.replace_suggestion::<usize>(None, cx));
} else if let Some(text) = self
.copilot_state .copilot_state
.text_for_active_completion(cursor, &snapshot) .text_for_active_completion(cursor, &snapshot)
{ {
@ -2974,8 +2938,11 @@ impl Editor {
cx, cx,
) )
}); });
cx.notify(); } else {
self.display_map
.update(cx, |map, cx| map.replace_suggestion::<usize>(None, cx));
} }
cx.notify();
} }
fn accept_copilot_suggestion(&mut self, cx: &mut ViewContext<Self>) -> bool { fn accept_copilot_suggestion(&mut self, cx: &mut ViewContext<Self>) -> bool {
@ -3005,6 +2972,10 @@ impl Editor {
!was_empty !was_empty
} }
fn has_active_copilot_suggestion(&self, cx: &AppContext) -> bool {
self.display_map.read(cx).has_suggestion()
}
pub fn render_code_actions_indicator( pub fn render_code_actions_indicator(
&self, &self,
style: &EditorStyle, style: &EditorStyle,
@ -3120,13 +3091,16 @@ impl Editor {
self.completion_tasks.clear(); self.completion_tasks.clear();
} }
self.context_menu = Some(menu); self.context_menu = Some(menu);
self.refresh_active_copilot_suggestion(cx);
cx.notify(); cx.notify();
} }
fn hide_context_menu(&mut self, cx: &mut ViewContext<Self>) -> Option<ContextMenu> { fn hide_context_menu(&mut self, cx: &mut ViewContext<Self>) -> Option<ContextMenu> {
cx.notify(); cx.notify();
self.completion_tasks.clear(); self.completion_tasks.clear();
self.context_menu.take() let context_menu = self.context_menu.take();
self.refresh_active_copilot_suggestion(cx);
context_menu
} }
pub fn insert_snippet( pub fn insert_snippet(