Consolidate logic for completion alongside code actions in Project
This way, completions are dealt with more consistently with code actions, and the logic is not spread across so many places. The `language::File` trait and the multibuffer no longer need to deal with completions. Completions are no longer generic over an anchor type.
This commit is contained in:
parent
722c84c976
commit
93bcde953c
12 changed files with 324 additions and 402 deletions
|
@ -861,41 +861,6 @@ impl MultiBuffer {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn completions<T>(
|
||||
&self,
|
||||
position: T,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) -> Task<Result<Vec<Completion<Anchor>>>>
|
||||
where
|
||||
T: ToOffset,
|
||||
{
|
||||
let anchor = self.read(cx).anchor_before(position);
|
||||
let buffer = self.buffers.borrow()[&anchor.buffer_id].buffer.clone();
|
||||
let completions =
|
||||
buffer.update(cx, |buffer, cx| buffer.completions(anchor.text_anchor, cx));
|
||||
cx.spawn(|this, cx| async move {
|
||||
completions.await.map(|completions| {
|
||||
let snapshot = this.read_with(&cx, |buffer, cx| buffer.snapshot(cx));
|
||||
completions
|
||||
.into_iter()
|
||||
.map(|completion| Completion {
|
||||
old_range: snapshot.anchor_in_excerpt(
|
||||
anchor.excerpt_id.clone(),
|
||||
completion.old_range.start,
|
||||
)
|
||||
..snapshot.anchor_in_excerpt(
|
||||
anchor.excerpt_id.clone(),
|
||||
completion.old_range.end,
|
||||
),
|
||||
new_text: completion.new_text,
|
||||
label: completion.label,
|
||||
lsp_completion: completion.lsp_completion,
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
pub fn is_completion_trigger<T>(&self, position: T, text: &str, cx: &AppContext) -> bool
|
||||
where
|
||||
T: ToOffset,
|
||||
|
@ -924,40 +889,6 @@ impl MultiBuffer {
|
|||
.any(|string| string == text)
|
||||
}
|
||||
|
||||
pub fn apply_additional_edits_for_completion(
|
||||
&self,
|
||||
completion: Completion<Anchor>,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) -> Task<Result<()>> {
|
||||
let buffer = if let Some(buffer_state) = self
|
||||
.buffers
|
||||
.borrow()
|
||||
.get(&completion.old_range.start.buffer_id)
|
||||
{
|
||||
buffer_state.buffer.clone()
|
||||
} else {
|
||||
return Task::ready(Ok(()));
|
||||
};
|
||||
|
||||
let apply_edits = buffer.update(cx, |buffer, cx| {
|
||||
buffer.apply_additional_edits_for_completion(
|
||||
Completion {
|
||||
old_range: completion.old_range.start.text_anchor
|
||||
..completion.old_range.end.text_anchor,
|
||||
new_text: completion.new_text,
|
||||
label: completion.label,
|
||||
lsp_completion: completion.lsp_completion,
|
||||
},
|
||||
true,
|
||||
cx,
|
||||
)
|
||||
});
|
||||
cx.foreground().spawn(async move {
|
||||
apply_edits.await?;
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
pub fn language<'a>(&self, cx: &'a AppContext) -> Option<&'a Arc<Language>> {
|
||||
self.buffers
|
||||
.borrow()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue