@@ -9,10 +9,12 @@ use crate::{
99use futures_util::future;
1010use std::{
1111 path::{Path, PathBuf},
12+ pin::Pin,
1213 sync::Arc,
13− time::Duration,
1414};
1515
16+use tokio::time::{sleep, Duration, Instant, Sleep};
17+
1618use slotmap::SlotMap;
1719
1820use anyhow::Error;
@@ -24,6 +26,14 @@ use helix_core::Position;
2426
2527use serde::Deserialize;
2628
29+fn deserialize_duration_millis<'de, D>(deserializer: D) -> Result<Duration, D::Error>
30+where
31+ D: serde::Deserializer<'de>,
32+{
33+ let millis = u64::deserialize(deserializer)?;
34+ Ok(Duration::from_millis(millis))
35+}
36+
2737#[derive(Debug, Clone, PartialEq, Deserialize)]
2838#[serde(rename_all = "kebab-case", default)]
2939pub struct Config {
@@ -43,6 +53,9 @@ pub struct Config {
4353 pub smart_case: bool,
4454 /// Automatic insertion of pairs to parentheses, brackets, etc. Defaults to true.
4555 pub auto_pairs: bool,
56+ /// Time in milliseconds since last keypress before idle timers trigger. Used for autocompletion, set to 0 for instant. Defaults to 400ms.
57+ #[serde(skip_serializing, deserialize_with = "deserialize_duration_millis")]
58+ pub idle_timeout: Duration,
4659}
4760
4861#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
@@ -70,6 +83,7 @@ impl Default for Config {
7083 middle_click_paste: true,
7184 smart_case: true,
7285 auto_pairs: true,
86+ idle_timeout: Duration::from_millis(400),
7387 }
7488 }
7589}
@@ -91,6 +105,8 @@ pub struct Editor {
91105 pub status_msg: Option<(String, Severity)>,
92106
93107 pub config: Config,
108+
109+ pub idle_timer: Pin<Box<Sleep>>,
94110}
95111
96112#[derive(Debug, Copy, Clone)]
@@ -125,10 +141,24 @@ impl Editor {
125141 registers: Registers::default(),
126142 clipboard_provider: get_clipboard_provider(),
127143 status_msg: None,
144+ idle_timer: Box::pin(sleep(Duration::from_millis(500))),
128145 config,
129146 }
130147 }
131148
149+ pub fn clear_idle_timer(&mut self) {
150+ // equivalent to internal Instant::far_future() (30 years)
151+ self.idle_timer
152+ .as_mut()
153+ .reset(Instant::now() + Duration::from_secs(86400 * 365 * 30));
154+ }
155+
156+ pub fn reset_idle_timer(&mut self) {
157+ self.idle_timer
158+ .as_mut()
159+ .reset(Instant::now() + Duration::from_millis(500));
160+ }
161+
132162 pub fn clear_status(&mut self) {
133163 self.status_msg = None;
134164 }