From 0344c543afc30134333b2c1b244c1af214c0f57b Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 31 Jan 2022 11:25:00 -0700 Subject: [PATCH] Return anchored completions from Buffer::completions Co-Authored-By: Antonio Scandurra --- crates/language/src/buffer.rs | 59 ++++++++++++----------------------- 1 file changed, 20 insertions(+), 39 deletions(-) diff --git a/crates/language/src/buffer.rs b/crates/language/src/buffer.rs index 257d066b08..9dc7c112be 100644 --- a/crates/language/src/buffer.rs +++ b/crates/language/src/buffer.rs @@ -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::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::(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::>(); - // 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()))