More responsive key input

Use biased select!, don't eagerly process lsp message since we want to
prioritize user input rather than lsp messages, but still limit rendering
for lsp messages.
imgbot
Ivan Tham 3 years ago committed by Blaž Hrastnik
parent 47a6882738
commit a0a5bd555b

@ -9,6 +9,7 @@ use log::error;
use std::{ use std::{
io::{stdout, Write}, io::{stdout, Write},
sync::Arc, sync::Arc,
time::{Duration, Instant},
}; };
use anyhow::Error; use anyhow::Error;
@ -130,6 +131,8 @@ impl Application {
pub async fn event_loop(&mut self) { pub async fn event_loop(&mut self) {
let mut reader = EventStream::new(); let mut reader = EventStream::new();
let mut last_render = Instant::now();
let deadline = Duration::from_secs(1) / 60;
self.render(); self.render();
@ -139,26 +142,22 @@ impl Application {
break; break;
} }
use futures_util::{FutureExt, StreamExt}; use futures_util::StreamExt;
tokio::select! { tokio::select! {
biased;
event = reader.next() => { event = reader.next() => {
self.handle_terminal_events(event) self.handle_terminal_events(event)
} }
Some((id, call)) = self.editor.language_servers.incoming.next() => { Some((id, call)) = self.editor.language_servers.incoming.next() => {
self.handle_language_server_message(call, id).await; self.handle_language_server_message(call, id).await;
// limit render calls for fast language server messages
// eagerly process any other available notifications/calls let last = self.editor.language_servers.incoming.is_empty();
let now = std::time::Instant::now(); if last || last_render.elapsed() > deadline {
let deadline = std::time::Duration::from_millis(10); self.render();
while let Some(Some((id, call))) = self.editor.language_servers.incoming.next().now_or_never() { last_render = Instant::now();
self.handle_language_server_message(call, id).await;
if now.elapsed() > deadline { // use a deadline so we don't block too long
break;
}
} }
self.render();
} }
Some(callback) = self.jobs.futures.next() => { Some(callback) = self.jobs.futures.next() => {
self.jobs.handle_callback(&mut self.editor, &mut self.compositor, callback); self.jobs.handle_callback(&mut self.editor, &mut self.compositor, callback);

Loading…
Cancel
Save