use idle timer instead of fixed timeout

pull/2359/head
Skyler Hawthorne 3 years ago
parent 40120967e9
commit 2386c81ebc

@ -40,6 +40,8 @@ use {
#[cfg(windows)] #[cfg(windows)]
type Signals = futures_util::stream::Empty<()>; type Signals = futures_util::stream::Empty<()>;
const LSP_DEADLINE: Duration = Duration::from_millis(16);
pub struct Application { pub struct Application {
compositor: Compositor, compositor: Compositor,
pub editor: Editor, pub editor: Editor,
@ -54,6 +56,7 @@ pub struct Application {
signals: Signals, signals: Signals,
jobs: Jobs, jobs: Jobs,
lsp_progress: LspProgressMap, lsp_progress: LspProgressMap,
last_render: Instant,
} }
#[cfg(feature = "integration")] #[cfg(feature = "integration")]
@ -203,6 +206,7 @@ impl Application {
signals, signals,
jobs: Jobs::new(), jobs: Jobs::new(),
lsp_progress: LspProgressMap::new(), lsp_progress: LspProgressMap::new(),
last_render: Instant::now(),
}; };
Ok(app) Ok(app)
@ -230,58 +234,79 @@ impl Application {
where where
S: Stream<Item = crossterm::Result<crossterm::event::Event>> + Unpin, S: Stream<Item = crossterm::Result<crossterm::event::Event>> + Unpin,
{ {
let mut last_render = Instant::now();
let deadline = Duration::from_secs(1) / 60;
self.render(); self.render();
self.last_render = Instant::now();
loop { loop {
if self.editor.should_close() { if self.editor.should_close() {
break; break;
} }
self.event_loop_until_idle(input_stream).await;
}
}
pub async fn event_loop_until_idle<S>(&mut self, input_stream: &mut S) -> bool
where
S: Stream<Item = crossterm::Result<crossterm::event::Event>> + Unpin,
{
loop {
if self.editor.should_close() {
return false;
}
use futures_util::StreamExt; use futures_util::StreamExt;
tokio::select! { tokio::select! {
biased; biased;
Some(event) = input_stream.next() => { Some(event) = input_stream.next() => {
self.handle_terminal_events(event) self.handle_terminal_events(event);
self.editor.reset_idle_timer();
} }
Some(signal) = self.signals.next() => { Some(signal) = self.signals.next() => {
self.handle_signals(signal).await; self.handle_signals(signal).await;
self.editor.reset_idle_timer();
} }
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 // limit render calls for fast language server messages
let last = self.editor.language_servers.incoming.is_empty(); let last = self.editor.language_servers.incoming.is_empty();
if last || last_render.elapsed() > deadline {
if last || self.last_render.elapsed() > LSP_DEADLINE {
self.render(); self.render();
last_render = Instant::now(); self.last_render = Instant::now();
} }
self.editor.reset_idle_timer();
} }
Some(payload) = self.editor.debugger_events.next() => { Some(payload) = self.editor.debugger_events.next() => {
let needs_render = self.editor.handle_debugger_message(payload).await; let needs_render = self.editor.handle_debugger_message(payload).await;
if needs_render { if needs_render {
self.render(); self.render();
} }
self.editor.reset_idle_timer();
} }
Some(config_event) = self.editor.config_events.1.recv() => { Some(config_event) = self.editor.config_events.1.recv() => {
self.handle_config_events(config_event); self.handle_config_events(config_event);
self.render(); self.render();
self.editor.reset_idle_timer();
} }
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);
self.render(); self.render();
self.editor.reset_idle_timer();
} }
Some(callback) = self.jobs.wait_futures.next() => { Some(callback) = self.jobs.wait_futures.next() => {
self.jobs.handle_callback(&mut self.editor, &mut self.compositor, callback); self.jobs.handle_callback(&mut self.editor, &mut self.compositor, callback);
self.render(); self.render();
self.editor.reset_idle_timer();
} }
_ = &mut self.editor.idle_timer => { _ = &mut self.editor.idle_timer => {
// idle timeout // idle timeout
self.editor.clear_idle_timer(); self.editor.clear_idle_timer();
self.handle_idle_timeout(); self.handle_idle_timeout();
return true;
} }
} }
} }

@ -17,7 +17,6 @@ mod integration {
Args::default(), Args::default(),
Config::default(), Config::default(),
("#[\n|]#", "ihello world<esc>", "hello world#[|\n]#"), ("#[\n|]#", "ihello world<esc>", "hello world#[|\n]#"),
None,
) )
.await?; .await?;

@ -18,7 +18,6 @@ async fn auto_indent_c() -> anyhow::Result<()> {
} }
"}, "},
), ),
None,
) )
.await?; .await?;

@ -6,7 +6,6 @@ async fn auto_pairs_basic() -> anyhow::Result<()> {
Args::default(), Args::default(),
Config::default(), Config::default(),
("#[\n|]#", "i(<esc>", "(#[|)]#\n"), ("#[\n|]#", "i(<esc>", "(#[|)]#\n"),
None,
) )
.await?; .await?;
@ -20,7 +19,6 @@ async fn auto_pairs_basic() -> anyhow::Result<()> {
..Default::default() ..Default::default()
}, },
("#[\n|]#", "i(<esc>", "(#[|\n]#"), ("#[\n|]#", "i(<esc>", "(#[|\n]#"),
None,
) )
.await?; .await?;

@ -1,7 +1,6 @@
use std::{ use std::{
io::{Read, Write}, io::{Read, Write},
ops::RangeInclusive, ops::RangeInclusive,
time::Duration,
}; };
use helix_core::diagnostic::Severity; use helix_core::diagnostic::Severity;
@ -23,7 +22,6 @@ async fn test_write_quit_fail() -> anyhow::Result<()> {
Some(&|app| { Some(&|app| {
assert_eq!(&Severity::Error, app.editor.get_status().unwrap().1); assert_eq!(&Severity::Error, app.editor.get_status().unwrap().1);
}), }),
None,
) )
.await?; .await?;
@ -57,7 +55,6 @@ async fn test_buffer_close() -> anyhow::Result<()> {
}), }),
), ),
], ],
None,
) )
.await?; .await?;
@ -88,7 +85,6 @@ async fn test_buffer_close() -> anyhow::Result<()> {
let doc = app.editor.document_by_path(file.path()); let doc = app.editor.document_by_path(file.path());
assert!(doc.is_none(), "found doc: {:?}", doc); assert!(doc.is_none(), "found doc: {:?}", doc);
}), }),
Some(Duration::from_millis(5000)),
) )
.await?; .await?;

@ -36,17 +36,15 @@ pub async fn test_key_sequence(
app: &mut Application, app: &mut Application,
in_keys: Option<&str>, in_keys: Option<&str>,
test_fn: Option<&dyn Fn(&Application)>, test_fn: Option<&dyn Fn(&Application)>,
timeout: Option<Duration>,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
test_key_sequences(app, vec![(in_keys, test_fn)], timeout).await test_key_sequences(app, vec![(in_keys, test_fn)]).await
} }
pub async fn test_key_sequences( pub async fn test_key_sequences(
app: &mut Application, app: &mut Application,
inputs: Vec<(Option<&str>, Option<&dyn Fn(&Application)>)>, inputs: Vec<(Option<&str>, Option<&dyn Fn(&Application)>)>,
timeout: Option<Duration>,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let timeout = timeout.unwrap_or(Duration::from_millis(500)); const TIMEOUT: Duration = Duration::from_millis(500);
let (tx, rx) = tokio::sync::mpsc::unbounded_channel(); let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
let mut rx_stream = UnboundedReceiverStream::new(rx); let mut rx_stream = UnboundedReceiverStream::new(rx);
@ -57,10 +55,7 @@ pub async fn test_key_sequences(
} }
} }
let event_loop = app.event_loop(&mut rx_stream); if !app.event_loop_until_idle(&mut rx_stream).await {
let result = tokio::time::timeout(timeout, event_loop).await;
if result.is_ok() {
bail!("application exited before test function could run"); bail!("application exited before test function could run");
} }
@ -74,7 +69,7 @@ pub async fn test_key_sequences(
} }
let event_loop = app.event_loop(&mut rx_stream); let event_loop = app.event_loop(&mut rx_stream);
tokio::time::timeout(timeout, event_loop).await?; tokio::time::timeout(TIMEOUT, event_loop).await?;
app.close().await?; app.close().await?;
Ok(()) Ok(())
@ -84,7 +79,6 @@ pub async fn test_key_sequence_with_input_text<T: Into<TestCase>>(
app: Option<Application>, app: Option<Application>,
test_case: T, test_case: T,
test_fn: &dyn Fn(&Application), test_fn: &dyn Fn(&Application),
timeout: Option<Duration>,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let test_case = test_case.into(); let test_case = test_case.into();
let mut app = let mut app =
@ -102,7 +96,7 @@ pub async fn test_key_sequence_with_input_text<T: Into<TestCase>>(
view.id, view.id,
); );
test_key_sequence(&mut app, Some(&test_case.in_keys), Some(test_fn), timeout).await test_key_sequence(&mut app, Some(&test_case.in_keys), Some(test_fn)).await
} }
/// Use this for very simple test cases where there is one input /// Use this for very simple test cases where there is one input
@ -112,26 +106,20 @@ pub async fn test_key_sequence_text_result<T: Into<TestCase>>(
args: Args, args: Args,
config: Config, config: Config,
test_case: T, test_case: T,
timeout: Option<Duration>,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let test_case = test_case.into(); let test_case = test_case.into();
let app = Application::new(args, config).unwrap(); let app = Application::new(args, config).unwrap();
test_key_sequence_with_input_text( test_key_sequence_with_input_text(Some(app), test_case.clone(), &|app| {
Some(app), let doc = doc!(app.editor);
test_case.clone(), assert_eq!(&test_case.out_text, doc.text());
&|app| {
let doc = doc!(app.editor); let mut selections: Vec<_> = doc.selections().values().cloned().collect();
assert_eq!(&test_case.out_text, doc.text()); assert_eq!(1, selections.len());
let mut selections: Vec<_> = doc.selections().values().cloned().collect(); let sel = selections.pop().unwrap();
assert_eq!(1, selections.len()); assert_eq!(test_case.out_selection, sel);
})
let sel = selections.pop().unwrap();
assert_eq!(test_case.out_selection, sel);
},
timeout,
)
.await .await
} }

@ -14,7 +14,6 @@ async fn insert_mode_cursor_position() -> anyhow::Result<()> {
out_text: String::new(), out_text: String::new(),
out_selection: Selection::single(0, 0), out_selection: Selection::single(0, 0),
}, },
None,
) )
.await?; .await?;
@ -22,7 +21,6 @@ async fn insert_mode_cursor_position() -> anyhow::Result<()> {
Args::default(), Args::default(),
Config::default(), Config::default(),
("#[\n|]#", "i", "#[|\n]#"), ("#[\n|]#", "i", "#[|\n]#"),
None,
) )
.await?; .await?;
@ -30,7 +28,6 @@ async fn insert_mode_cursor_position() -> anyhow::Result<()> {
Args::default(), Args::default(),
Config::default(), Config::default(),
("#[\n|]#", "i<esc>", "#[|\n]#"), ("#[\n|]#", "i<esc>", "#[|\n]#"),
None,
) )
.await?; .await?;
@ -38,7 +35,6 @@ async fn insert_mode_cursor_position() -> anyhow::Result<()> {
Args::default(), Args::default(),
Config::default(), Config::default(),
("#[\n|]#", "i<esc>i", "#[|\n]#"), ("#[\n|]#", "i<esc>i", "#[|\n]#"),
None,
) )
.await?; .await?;
@ -52,7 +48,6 @@ async fn insert_to_normal_mode_cursor_position() -> anyhow::Result<()> {
Args::default(), Args::default(),
Config::default(), Config::default(),
("#[f|]#oo\n", "vll<A-;><esc>", "#[|foo]#\n"), ("#[f|]#oo\n", "vll<A-;><esc>", "#[|foo]#\n"),
None,
) )
.await?; .await?;
@ -70,7 +65,6 @@ async fn insert_to_normal_mode_cursor_position() -> anyhow::Result<()> {
#(|bar)#" #(|bar)#"
}, },
), ),
None,
) )
.await?; .await?;
@ -88,7 +82,6 @@ async fn insert_to_normal_mode_cursor_position() -> anyhow::Result<()> {
#(ba|)#r" #(ba|)#r"
}, },
), ),
None,
) )
.await?; .await?;
@ -106,7 +99,6 @@ async fn insert_to_normal_mode_cursor_position() -> anyhow::Result<()> {
#(b|)#ar" #(b|)#ar"
}, },
), ),
None,
) )
.await?; .await?;

@ -1,7 +1,6 @@
use std::{ use std::{
io::{Read, Write}, io::{Read, Write},
ops::RangeInclusive, ops::RangeInclusive,
time::Duration,
}; };
use helix_core::diagnostic::Severity; use helix_core::diagnostic::Severity;
@ -24,7 +23,6 @@ async fn test_write() -> anyhow::Result<()> {
)?, )?,
Some("ii can eat glass, it will not hurt me<ret><esc>:w<ret>"), Some("ii can eat glass, it will not hurt me<ret><esc>:w<ret>"),
None, None,
Some(Duration::from_millis(1000)),
) )
.await?; .await?;
@ -59,7 +57,6 @@ async fn test_write_concurrent() -> anyhow::Result<()> {
)?, )?,
Some(&command), Some(&command),
None, None,
Some(Duration::from_millis(10000)),
) )
.await?; .await?;
@ -108,7 +105,6 @@ async fn test_write_fail_mod_flag() -> anyhow::Result<()> {
}), }),
), ),
], ],
None,
) )
.await?; .await?;
@ -138,7 +134,6 @@ async fn test_write_fail_new_path() -> anyhow::Result<()> {
}), }),
), ),
], ],
Some(Duration::from_millis(1000)),
) )
.await?; .await?;

Loading…
Cancel
Save