diff options
author | CossonLeo | 2021-10-18 06:14:50 +0000 |
---|---|---|
committer | GitHub | 2021-10-18 06:14:50 +0000 |
commit | 9ac0c951615d624041836cdd7afd869391c72fff (patch) | |
tree | ace936ce161eaa56bbf59590a2c0270d03ca5cd8 /helix-term | |
parent | a03b12530c8231111184dbb35b57bfb1727f4d60 (diff) |
Improve completion trigger (#838)
* improve idle completion trigger
* add completion-trigger-len to book
* rename semantics_completion to language_server_completion and optimize idle completion trigger
Diffstat (limited to 'helix-term')
-rw-r--r-- | helix-term/src/application.rs | 4 | ||||
-rw-r--r-- | helix-term/src/commands.rs | 39 |
2 files changed, 29 insertions, 14 deletions
diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 0e7d0e55..a7281ecf 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -235,7 +235,7 @@ impl Application { } pub fn handle_idle_timeout(&mut self) { - use crate::commands::{completion, Context}; + use crate::commands::{insert::idle_completion, Context}; use helix_view::document::Mode; if doc_mut!(self.editor).mode != Mode::Insert || !self.config.editor.auto_completion { @@ -262,7 +262,7 @@ impl Application { callback: None, on_next_key_callback: None, }; - completion(&mut cx); + idle_completion(&mut cx); self.render(); } diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 95c46a4e..2811f293 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -3308,7 +3308,26 @@ pub mod insert { pub type Hook = fn(&Rope, &Selection, char) -> Option<Transaction>; pub type PostHook = fn(&mut Context, char); - fn completion(cx: &mut Context, ch: char) { + // It trigger completion when idle timer reaches deadline + // Only trigger completion if the word under cursor is longer than n characters + pub fn idle_completion(cx: &mut Context) { + let (view, doc) = current!(cx.editor); + let text = doc.text().slice(..); + let cursor = doc.selection(view.id).primary().cursor(text); + + use helix_core::chars::char_is_word; + let mut iter = text.chars_at(cursor); + iter.reverse(); + for _ in 0..cx.editor.config.completion_trigger_len { + match iter.next() { + Some(c) if char_is_word(c) => {} + _ => return, + } + } + super::completion(cx); + } + + fn language_server_completion(cx: &mut Context, ch: char) { // if ch matches completion char, trigger completion let doc = doc_mut!(cx.editor); let language_server = match doc.language_server() { @@ -3318,19 +3337,14 @@ pub mod insert { let capabilities = language_server.capabilities(); - if let lsp::ServerCapabilities { - completion_provider: - Some(lsp::CompletionOptions { - trigger_characters: Some(triggers), - .. - }), + if let Some(lsp::CompletionOptions { + trigger_characters: Some(triggers), .. - } = capabilities + }) = &capabilities.completion_provider { // TODO: what if trigger is multiple chars long - let is_trigger = triggers.iter().any(|trigger| trigger.contains(ch)); - - if is_trigger { + if triggers.iter().any(|trigger| trigger.contains(ch)) { + cx.editor.clear_idle_timer(); super::completion(cx); } } @@ -3412,7 +3426,8 @@ pub mod insert { // TODO: need a post insert hook too for certain triggers (autocomplete, signature help, etc) // this could also generically look at Transaction, but it's a bit annoying to look at // Operation instead of Change. - for hook in &[completion, signature_help] { + for hook in &[language_server_completion, signature_help] { + // for hook in &[signature_help] { hook(cx, c); } } |