🎨 Return an option task from confirm_completion

This commit is contained in:
Max Brunsfeld 2022-02-01 14:27:01 -08:00
parent 55cc7bb868
commit 1371a20e58
4 changed files with 36 additions and 54 deletions

View File

@ -297,7 +297,9 @@ pub fn init(cx: &mut MutableAppContext, path_openers: &mut Vec<Box<dyn PathOpene
cx.add_action(Editor::fold_selected_ranges); cx.add_action(Editor::fold_selected_ranges);
cx.add_action(Editor::show_completions); cx.add_action(Editor::show_completions);
cx.add_action(|editor: &mut Editor, _: &ConfirmCompletion, cx| { cx.add_action(|editor: &mut Editor, _: &ConfirmCompletion, cx| {
editor.confirm_completion(cx).detach_and_log_err(cx); if let Some(task) = editor.confirm_completion(cx) {
task.detach_and_log_err(cx);
}
}); });
} }
@ -1648,20 +1650,17 @@ impl Editor {
self.completion_state.take() self.completion_state.take()
} }
fn confirm_completion(&mut self, cx: &mut ViewContext<Self>) -> Task<Result<()>> { fn confirm_completion(&mut self, cx: &mut ViewContext<Self>) -> Option<Task<Result<()>>> {
if let Some(completion_state) = self.hide_completions(cx) { let completion_state = self.hide_completions(cx)?;
if let Some(completion) = completion_state let mat = completion_state
.matches .matches
.get(completion_state.selected_item) .get(completion_state.selected_item)?;
.and_then(|mat| completion_state.completions.get(mat.candidate_id)) let completion = completion_state.completions.get(mat.candidate_id)?;
{ self.buffer.update(cx, |buffer, cx| {
return self.buffer.update(cx, |buffer, cx| { let mut completion = completion.clone();
buffer.apply_completion(completion.clone(), cx) // completion.
}); buffer.apply_completion(completion, cx)
} })
}
Task::ready(Ok(()))
} }
pub fn has_completions(&self) -> bool { pub fn has_completions(&self) -> bool {
@ -6658,7 +6657,7 @@ mod tests {
let apply_additional_edits = editor.update(&mut cx, |editor, cx| { let apply_additional_edits = editor.update(&mut cx, |editor, cx| {
editor.move_down(&MoveDown, cx); editor.move_down(&MoveDown, cx);
let apply_additional_edits = editor.confirm_completion(cx); let apply_additional_edits = editor.confirm_completion(cx).unwrap();
assert_eq!( assert_eq!(
editor.text(cx), editor.text(cx),
" "

View File

@ -1,7 +1,7 @@
mod anchor; mod anchor;
pub use anchor::{Anchor, AnchorRangeExt}; pub use anchor::{Anchor, AnchorRangeExt};
use anyhow::{anyhow, Result}; use anyhow::Result;
use clock::ReplicaId; use clock::ReplicaId;
use collections::{HashMap, HashSet}; use collections::{HashMap, HashSet};
use gpui::{AppContext, Entity, ModelContext, ModelHandle, Task}; use gpui::{AppContext, Entity, ModelContext, ModelHandle, Task};
@ -933,17 +933,13 @@ impl MultiBuffer {
&self, &self,
completion: Completion<Anchor>, completion: Completion<Anchor>,
cx: &mut ModelContext<Self>, cx: &mut ModelContext<Self>,
) -> Task<Result<()>> { ) -> Option<Task<Result<()>>> {
let buffer = if let Some(buffer) = self let buffer = self
.buffers .buffers
.borrow() .borrow()
.get(&completion.old_range.start.buffer_id) .get(&completion.old_range.start.buffer_id)?
{ .buffer
buffer.buffer.clone() .clone();
} else {
return Task::ready(Err(anyhow!("completion cannot be applied to any buffer")));
};
buffer.update(cx, |buffer, cx| { buffer.update(cx, |buffer, cx| {
buffer.apply_completion( buffer.apply_completion(
Completion { Completion {

View File

@ -1781,35 +1781,22 @@ impl Buffer {
&mut self, &mut self,
completion: Completion<Anchor>, completion: Completion<Anchor>,
cx: &mut ModelContext<Self>, cx: &mut ModelContext<Self>,
) -> Task<Result<()>> { ) -> Option<Task<Result<()>>> {
self.edit_with_autoindent([completion.old_range], completion.new_text.clone(), cx); self.edit_with_autoindent([completion.old_range], completion.new_text.clone(), cx);
let file = if let Some(file) = self.file.as_ref() { self.file.as_ref()?.as_local()?;
file let server = self.language_server.as_ref()?.server.clone();
} else { Some(cx.spawn(|this, mut cx| async move {
return Task::ready(Ok(Default::default())); let resolved_completion = server
}; .request::<lsp::request::ResolveCompletionItem>(completion.lsp_completion)
if file.is_local() { .await?;
let server = if let Some(lang) = self.language_server.as_ref() { if let Some(additional_edits) = resolved_completion.additional_text_edits {
lang.server.clone() this.update(&mut cx, |this, cx| {
} else { this.apply_lsp_edits(additional_edits, cx)
return Task::ready(Ok(Default::default())); })?;
}; }
Ok::<_, anyhow::Error>(())
cx.spawn(|this, mut cx| async move { }))
let resolved_completion = server
.request::<lsp::request::ResolveCompletionItem>(completion.lsp_completion)
.await?;
if let Some(additional_edits) = resolved_completion.additional_text_edits {
this.update(&mut cx, |this, cx| {
this.apply_lsp_edits(additional_edits, cx)
})?;
}
Ok::<_, anyhow::Error>(())
})
} else {
return Task::ready(Ok(Default::default()));
}
} }
} }

View File

@ -12,7 +12,7 @@ type TabStop = SmallVec<[Range<usize>; 2]>;
impl Snippet { impl Snippet {
pub fn parse(source: &str) -> Result<Self> { pub fn parse(source: &str) -> Result<Self> {
let mut text = String::new(); let mut text = String::with_capacity(source.len());
let mut tabstops = BTreeMap::new(); let mut tabstops = BTreeMap::new();
parse_snippet(source, false, &mut text, &mut tabstops) parse_snippet(source, false, &mut text, &mut tabstops)
.context("failed to parse snippet")?; .context("failed to parse snippet")?;