Apply additional edits when confirming a completion

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2022-02-01 17:38:11 +01:00
parent bcc57036a5
commit 6c7d2cf6b5
3 changed files with 145 additions and 43 deletions

View file

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