From 8cb7cdfd7a01b9cb50b9142e9a5d133bd1e23256 Mon Sep 17 00:00:00 2001 From: Pascal Kuthe Date: Thu, 2 Mar 2023 23:12:50 +0100 Subject: discard stale completion requests Completion requests are computed asynchronously to avoid common micro freezes while editing. This means that once a completion request completes, the state of the editor might have changed. Currently, there is a check to ensure we are still in insert mode. However, we also need to ensure that the view and document hasn't changed to avoid accidentally using a savepoint with the wrong view/document. Furthermore, the editor might request a new completion while the previous completion request hasn't complemented yet. This can lead to weird flickering or an outdated completion request replacing a newer completion that has already completed (the LSP server is not required to process completion requests in order). This change also needed to ensure determinism/linear ordering so that completion popup always correspond to the last completion request. --- helix-view/src/editor.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'helix-view/src/editor.rs') diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 5b819b33..c6541105 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -31,7 +31,7 @@ use std::{ use tokio::{ sync::{ mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender}, - Notify, RwLock, + oneshot, Notify, RwLock, }, time::{sleep, Duration, Instant, Sleep}, }; @@ -852,6 +852,14 @@ pub struct Editor { /// avoid calculating the cursor position multiple /// times during rendering and should not be set by other functions. pub cursor_cache: Cell>>, + /// When a new completion request is sent to the server old + /// unifinished request must be dropped. Each completion + /// request is associated with a channel that cancels + /// when the channel is dropped. That channel is stored + /// here. When a new completion request is sent this + /// field is set and any old requests are automatically + /// canceled as a result + pub completion_request_handle: Option>, } pub type RedrawHandle = (Arc, Arc>); @@ -950,6 +958,7 @@ impl Editor { redraw_handle: Default::default(), needs_redraw: false, cursor_cache: Cell::new(None), + completion_request_handle: None, } } -- cgit v1.2.3-70-g09d2