Return anchored completions from Buffer::completions

Co-Authored-By: Antonio Scandurra <me@as-cii.com>
This commit is contained in:
Nathan Sobo 2022-01-31 11:25:00 -07:00
parent 03bcbdc33d
commit 0344c543af

View file

@ -12,7 +12,6 @@ use crate::{
use anyhow::{anyhow, Result};
use clock::ReplicaId;
use futures::FutureExt as _;
use fuzzy::StringMatchCandidate;
use gpui::{fonts::HighlightStyle, AppContext, Entity, ModelContext, MutableAppContext, Task};
use lazy_static::lazy_static;
use lsp::LanguageServer;
@ -1642,8 +1641,7 @@ impl Buffer {
let abs_path = file.abs_path(cx);
let position = self.offset_to_point_utf16(position.to_offset(self));
cx.spawn(|this, mut cx| async move {
let t0 = Instant::now();
cx.spawn(|this, cx| async move {
let completions = server
.request::<lsp::request::Completion>(lsp::CompletionParams {
text_document_position: lsp::TextDocumentPositionParams::new(
@ -1657,10 +1655,8 @@ impl Buffer {
partial_result_params: Default::default(),
})
.await?;
dbg!("completions", t0.elapsed());
// fuzzy::match_strings(candidates, query, smart_case, max_results, cancel_flag, background)
let mut completions = if let Some(completions) = completions {
let completions = if let Some(completions) = completions {
match completions {
lsp::CompletionResponse::Array(completions) => completions,
lsp::CompletionResponse::List(list) => list.items,
@ -1669,40 +1665,25 @@ impl Buffer {
Default::default()
};
this.update(&mut cx, |this, cx| {
this.edit([0..0], "use std::sync::Arc;\n", cx)
});
this.read_with(&cx, |this, _| {
Ok(completions.into_iter().filter_map(|lsp_completion| {
let (old_range, new_text) = match lsp_completion.text_edit.as_ref()? {
lsp::CompletionTextEdit::Edit(edit) => (range_from_lsp(edit.range), edit.new_text.clone()),
lsp::CompletionTextEdit::InsertAndReplace(_) => {
log::info!("received an insert and replace completion but we don't yet support that");
return None
},
};
let mut futures = Vec::new();
for completion in completions {
futures.push(server.request::<lsp::request::ResolveCompletionItem>(completion));
}
let completions = futures::future::try_join_all(futures).await?;
dbg!("resolution", t0.elapsed(), completions);
// let candidates = completions
// .iter()
// .enumerate()
// .map(|(id, completion)| {
// let text = completion
// .filter_text
// .clone()
// .unwrap_or_else(|| completion.label.clone());
// StringMatchCandidate::new(id, text)
// })
// .collect::<Vec<_>>();
// let matches = fuzzy::match_strings(
// &candidates,
// "Arc",
// false,
// 100,
// &Default::default(),
// cx.background(),
// )
// .await;
// dbg!(matches);
Ok(Default::default())
let old_range = this.anchor_after(old_range.start)..this.anchor_before(old_range.end);
Some(Completion {
old_range,
new_text,
lsp_completion,
})
}).collect())
})
})
} else {
Task::ready(Ok(Default::default()))