aboutsummaryrefslogtreecommitdiff
path: root/helix-term
diff options
context:
space:
mode:
Diffstat (limited to 'helix-term')
-rw-r--r--helix-term/src/commands.rs31
-rw-r--r--helix-term/src/ui/editor.rs1
2 files changed, 30 insertions, 2 deletions
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs
index 01673c89..574e1edf 100644
--- a/helix-term/src/commands.rs
+++ b/helix-term/src/commands.rs
@@ -5,6 +5,7 @@ pub(crate) mod typed;
pub use dap::*;
use helix_vcs::Hunk;
pub use lsp::*;
+use tokio::sync::oneshot;
use tui::widgets::Row;
pub use typed::*;
@@ -4171,6 +4172,24 @@ pub fn completion(cx: &mut Context) {
None => return,
};
+ // setup a chanel that allows the request to be canceled
+ let (tx, rx) = oneshot::channel();
+ // set completion_request so that this request can be canceled
+ // by setting completion_request, the old channel stored there is dropped
+ // and the associated request is automatically dropped
+ cx.editor.completion_request_handle = Some(tx);
+ let future = async move {
+ tokio::select! {
+ biased;
+ _ = rx => {
+ Ok(serde_json::Value::Null)
+ }
+ res = future => {
+ res
+ }
+ }
+ };
+
let trigger_offset = cursor;
// TODO: trigger_offset should be the cursor offset but we also need a starting offset from where we want to apply
@@ -4183,11 +4202,19 @@ pub fn completion(cx: &mut Context) {
let start_offset = cursor.saturating_sub(offset);
let savepoint = doc.savepoint(view);
+ let trigger_doc = doc.id();
+ let trigger_view = view.id;
+
cx.callback(
future,
move |editor, compositor, response: Option<lsp::CompletionResponse>| {
- if editor.mode != Mode::Insert {
- // we're not in insert mode anymore
+ let (view, doc) = current_ref!(editor);
+ // check if the completion request is stale.
+ //
+ // Completions are completed asynchrounsly and therefore the user could
+ //switch document/view or leave insert mode. In all of thoise cases the
+ // completion should be discarded
+ if editor.mode != Mode::Insert || view.id != trigger_view || doc.id() != trigger_doc {
return;
}
diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs
index c81ae635..859176fb 100644
--- a/helix-term/src/ui/editor.rs
+++ b/helix-term/src/ui/editor.rs
@@ -820,6 +820,7 @@ impl EditorView {
(Mode::Insert, Mode::Normal) => {
// if exiting insert mode, remove completion
self.completion = None;
+ cxt.editor.completion_request_handle = None;
// TODO: Use an on_mode_change hook to remove signature help
cxt.jobs.callback(async {