Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faster completion support for remotes #17993

Closed
wants to merge 4 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 62 additions & 12 deletions crates/editor/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,10 +546,12 @@ pub struct Editor {
nav_history: Option<ItemNavHistory>,
context_menu: RwLock<Option<ContextMenu>>,
mouse_context_menu: Option<MouseContextMenu>,
completion_tasks: Vec<(CompletionId, Task<Option<()>>)>,
signature_help_state: SignatureHelpState,
auto_signature_help: Option<bool>,
find_all_references_task_sources: Vec<Anchor>,
// invariant: backup_completion_task.is_some() -> latest_completion_task.is_some()
backup_completion_task: Option<(CompletionId, Task<Option<()>>)>,
latest_completion_task: Option<(CompletionId, Task<Option<()>>)>,
next_completion_id: CompletionId,
completion_documentation_pre_resolve_debounce: DebouncedDelay,
available_code_actions: Option<(Location, Arc<[CodeAction]>)>,
Expand Down Expand Up @@ -1891,7 +1893,8 @@ impl Editor {
nav_history: None,
context_menu: RwLock::new(None),
mouse_context_menu: None,
completion_tasks: Default::default(),
latest_completion_task: None,
backup_completion_task: None,
signature_help_state: SignatureHelpState::default(),
auto_signature_help: None,
find_all_references_task_sources: Vec::new(),
Expand Down Expand Up @@ -4218,16 +4221,35 @@ impl Editor {
}),
trigger_kind,
};
let snapshot = buffer.read(cx).snapshot();
let classifier = snapshot.char_classifier_at(&buffer_position);
let first_char = options
.trigger
.as_ref()
.and_then(|trigger| trigger.chars().next());
let should_cancel_backup = first_char.is_some_and(|char| !classifier.is_word(char));
let completions = provider.completions(&buffer, buffer_position, completion_context, cx);
let sort_completions = provider.sort_completions();

let id = post_inc(&mut self.next_completion_id);
dbg!(
id,
&options.trigger,
&should_cancel_backup,
&self.backup_completion_task,
&self.latest_completion_task,
);
if should_cancel_backup {
self.backup_completion_task = None;
}
let task = cx.spawn(|this, mut cx| {
async move {
this.update(&mut cx, |this, _| {
this.completion_tasks.retain(|(task_id, _)| *task_id >= id);
})?;
// TODO: if a menu exists already, then filter it before wait for completions.
// looks like this is something else filtering the completion menu.
let completions = completions.await.log_err();
cx.background_executor()
.timer(Duration::from_millis(1000))
.await;
let menu = if let Some(completions) = completions {
let mut menu = CompletionsMenu {
id,
Expand All @@ -4253,7 +4275,11 @@ impl Editor {
DebouncedDelay::new(),
)),
};
menu.filter(query.as_deref(), cx.background_executor().clone())
let completion_query = this.update(&mut cx, |this, cx| {
Self::completion_query(&this.buffer.read(cx).read(cx), position)
})?;
let query = completion_query.or(query);
menu.filter(dbg!(query.as_deref()), cx.background_executor().clone())
.await;

if menu.matches.is_empty() {
Expand Down Expand Up @@ -4299,13 +4325,33 @@ impl Editor {
_ => return,
}

let is_latest = if this
.latest_completion_task
.as_ref()
.map_or(false, |(task_id, _)| *task_id == id)
{
// we drop the both tasks
this.backup_completion_task = None;
this.latest_completion_task = None;
true
} else if this
.backup_completion_task
.as_ref()
.map_or(false, |(task_id, _)| *task_id == id)
{
this.backup_completion_task = None;
false
} else {
// the task didn't get cancelled, so we manually return
return;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

otherwise completions from this show sometimes after this.

};
if this.focus_handle.is_focused(cx) && menu.is_some() {
let menu = menu.unwrap();
*context_menu = Some(ContextMenu::Completions(menu));
drop(context_menu);
this.discard_inline_completion(false, cx);
cx.notify();
} else if this.completion_tasks.len() <= 1 {
} else if is_latest {
// If there are no more completion tasks and the last menu was
// empty, we should hide it. If it was already hidden, we should
// also show the copilot completion when available.
Expand All @@ -4320,8 +4366,10 @@ impl Editor {
}
.log_err()
});

self.completion_tasks.push((id, task));
let prev_task = std::mem::replace(&mut self.latest_completion_task, Some((id, task)));
if self.backup_completion_task.is_none() {
self.backup_completion_task = prev_task;
}
}

pub fn confirm_completion(
Expand Down Expand Up @@ -4583,7 +4631,8 @@ impl Editor {
return None;
}

editor.completion_tasks.clear();
editor.latest_completion_task = None;
editor.backup_completion_task = None;
editor.discard_inline_completion(false, cx);
let task_context =
tasks
Expand Down Expand Up @@ -5191,7 +5240,7 @@ impl Editor {
let excerpt_id = cursor.excerpt_id;

if self.context_menu.read().is_none()
&& self.completion_tasks.is_empty()
&& self.latest_completion_task.is_none()
&& selection.start == selection.end
{
if let Some(provider) = self.inline_completion_provider() {
Expand Down Expand Up @@ -5363,7 +5412,8 @@ impl Editor {

fn hide_context_menu(&mut self, cx: &mut ViewContext<Self>) -> Option<ContextMenu> {
cx.notify();
self.completion_tasks.clear();
self.latest_completion_task = None;
self.backup_completion_task = None;
let context_menu = self.context_menu.write().take();
if context_menu.is_some() {
self.update_visible_inline_completion(cx);
Expand Down
Loading