Route completion requests through remote protocol, if needed

This commit is contained in:
Kirill Bulatov 2023-08-31 15:22:13 +03:00
parent 5bc5831032
commit e682db7101

View File

@ -4496,10 +4496,10 @@ impl Project {
position: T,
cx: &mut ModelContext<Self>,
) -> Task<Result<Vec<Completion>>> {
let position = position.to_point_utf16(buffer.read(cx));
if self.is_local() {
let snapshot = buffer.read(cx).snapshot();
let offset = position.to_offset(&snapshot);
let position = position.to_point_utf16(buffer.read(cx));
let scope = snapshot.language_scope_at(offset);
let server_ids: Vec<_> = self
@ -4537,6 +4537,11 @@ impl Project {
Ok(completions)
})
} else if let Some(project_id) = self.remote_id() {
self.send_lsp_proto_request(buffer.clone(), project_id, GetCompletions { position }, cx)
} else {
Task::ready(Ok(Default::default()))
}
}
pub fn apply_additional_edits_for_completion(
@ -5587,16 +5592,27 @@ impl Project {
});
}
} else if let Some(project_id) = self.remote_id() {
return self.send_lsp_proto_request(buffer_handle, project_id, request, cx);
}
Task::ready(Ok(Default::default()))
}
fn send_lsp_proto_request<R: LspCommand>(
&self,
buffer: ModelHandle<Buffer>,
project_id: u64,
request: R,
cx: &mut ModelContext<'_, Project>,
) -> Task<anyhow::Result<<R as LspCommand>::Response>> {
let rpc = self.client.clone();
let message = request.to_proto(project_id, buffer);
return cx.spawn_weak(|this, cx| async move {
let message = request.to_proto(project_id, buffer.read(cx));
cx.spawn_weak(|this, cx| async move {
// Ensure the project is still alive by the time the task
// is scheduled.
this.upgrade(&cx)
.ok_or_else(|| anyhow!("project dropped"))?;
let response = rpc.request(message).await?;
let this = this
.upgrade(&cx)
.ok_or_else(|| anyhow!("project dropped"))?;
@ -5604,13 +5620,10 @@ impl Project {
Err(anyhow!("disconnected before completing request"))
} else {
request
.response_from_proto(response, this, buffer_handle, cx)
.response_from_proto(response, this, buffer, cx)
.await
}
});
}
Task::ready(Ok(Default::default()))
})
}
fn sort_candidates_and_open_buffers(