From 40abec80e1062503d70055ed9e968cd4b31411a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Thu, 26 Aug 2021 11:14:46 +0900 Subject: [PATCH] Experiment with autocompletion on idle --- helix-term/src/application.rs | 38 +++++++++++++++++++++++++++++++++-- helix-term/src/commands.rs | 2 +- helix-term/src/ui/editor.rs | 2 +- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index dbd8755d..ab268041 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -200,9 +200,9 @@ impl Application { self.render(); } _ = &mut self.editor.idle_timer => { + // idle timeout self.editor.clear_idle_timer(); - println!("idle!") - // idle timeout + self.handle_idle_timeout(); } } } @@ -233,6 +233,40 @@ impl Application { } } + pub fn handle_idle_timeout(&mut self) { + use helix_view::document::Mode; + use crate::commands::{Context, completion}; + + + if doc_mut!(self.editor).mode != Mode::Insert { + return; + } + let editor_view = self + .compositor + .find(std::any::type_name::()) + .expect("expected at least one EditorView"); + let editor_view = editor_view + .as_any_mut() + .downcast_mut::() + .unwrap(); + + if editor_view.completion.is_some() { + return; + } + + let mut cx = Context { + selected_register: helix_view::RegisterSelection::default(), + editor: &mut self.editor, + jobs: &mut self.jobs, + count: None, + callback: None, + on_next_key_callback: None, + }; + completion(&mut cx); + // TODO: scan backwards for trigger and filter the box + self.render(); + } + pub fn handle_terminal_events(&mut self, event: Option>) { let mut cx = crate::compositor::Context { editor: &mut self.editor, diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index ec76c81d..1dbb5616 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -4045,7 +4045,7 @@ fn remove_primary_selection(cx: &mut Context) { doc.set_selection(view.id, selection); } -fn completion(cx: &mut Context) { +pub fn completion(cx: &mut Context) { // trigger on trigger char, or if user calls it // (or on word char typing??) // after it's triggered, if response marked is_incomplete, update on every subsequent keypress diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index aa2d6636..ee3b3c65 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -33,7 +33,7 @@ pub struct EditorView { keymaps: Keymaps, on_next_key: Option>, last_insert: (commands::Command, Vec), - completion: Option, + pub(crate) completion: Option, spinners: ProgressSpinners, autoinfo: Option, }