on master help sign in
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

commit9ac0c951615d624041836cdd7afd869391c72fff authorCossonLeo <20379044+cossonleo@users.noreply.github.com> dateMon 18 Oct 2021, 14:14 (5 years ago) parenta03b125
4 files changed+32−14
book/src/configuration.md+1 helix-term/src/application.rs+2−2 helix-term/src/commands.rs+27−12 helix-view/src/editor.rs+2
book/src/configuration.md+1
@@ -21,6 +21,7 @@ To override global configuration parameters, create a `config.toml` file located
2121| `auto-pairs` | Enable automatic insertion of pairs to parenthese, brackets, etc. | `true` |
2222| `auto-completion` | Enable automatic pop up of auto-completion. | `true` |
2323| `idle-timeout` | Time in milliseconds since last keypress before idle timers trigger. Used for autocompletion, set to 0 for instant. | `400` |
24+| `completion-trigger-len` | The min-length of word under cursor to trigger autocompletion | `2` |
2425
2526## LSP
2627
helix-term/src/application.rs+2−2
@@ -235,7 +235,7 @@ impl Application {
235235 }
236236
237237 pub fn handle_idle_timeout(&mut self) {
238− use crate::commands::{completion, Context};
238+ use crate::commands::{insert::idle_completion, Context};
239239 use helix_view::document::Mode;
240240
241241 if doc_mut!(self.editor).mode != Mode::Insert || !self.config.editor.auto_completion {
@@ -262,7 +262,7 @@ impl Application {
262262 callback: None,
263263 on_next_key_callback: None,
264264 };
265− completion(&mut cx);
265+ idle_completion(&mut cx);
266266 self.render();
267267 }
268268
helix-term/src/commands.rs+27−12
@@ -3308,7 +3308,26 @@ pub mod insert {
33083308 pub type Hook = fn(&Rope, &Selection, char) -> Option<Transaction>;
33093309 pub type PostHook = fn(&mut Context, char);
33103310
3311− fn completion(cx: &mut Context, ch: char) {
3311+ // It trigger completion when idle timer reaches deadline
3312+ // Only trigger completion if the word under cursor is longer than n characters
3313+ pub fn idle_completion(cx: &mut Context) {
3314+ let (view, doc) = current!(cx.editor);
3315+ let text = doc.text().slice(..);
3316+ let cursor = doc.selection(view.id).primary().cursor(text);
3317+
3318+ use helix_core::chars::char_is_word;
3319+ let mut iter = text.chars_at(cursor);
3320+ iter.reverse();
3321+ for _ in 0..cx.editor.config.completion_trigger_len {
3322+ match iter.next() {
3323+ Some(c) if char_is_word(c) => {}
3324+ _ => return,
3325+ }
3326+ }
3327+ super::completion(cx);
3328+ }
3329+
3330+ fn language_server_completion(cx: &mut Context, ch: char) {
33123331 // if ch matches completion char, trigger completion
33133332 let doc = doc_mut!(cx.editor);
33143333 let language_server = match doc.language_server() {
@@ -3318,19 +3337,14 @@ pub mod insert {
33183337
33193338 let capabilities = language_server.capabilities();
33203339
3321− if let lsp::ServerCapabilities {
3322− completion_provider:
3323− Some(lsp::CompletionOptions {
3324− trigger_characters: Some(triggers),
3325− ..
3326− }),
3340+ if let Some(lsp::CompletionOptions {
3341+ trigger_characters: Some(triggers),
33273342 ..
3328− } = capabilities
3343+ }) = &capabilities.completion_provider
33293344 {
33303345 // TODO: what if trigger is multiple chars long
3331− let is_trigger = triggers.iter().any(|trigger| trigger.contains(ch));
3332−
3333− if is_trigger {
3346+ if triggers.iter().any(|trigger| trigger.contains(ch)) {
3347+ cx.editor.clear_idle_timer();
33343348 super::completion(cx);
33353349 }
33363350 }
@@ -3412,7 +3426,8 @@ pub mod insert {
34123426 // TODO: need a post insert hook too for certain triggers (autocomplete, signature help, etc)
34133427 // this could also generically look at Transaction, but it's a bit annoying to look at
34143428 // Operation instead of Change.
3415− for hook in &[completion, signature_help] {
3429+ for hook in &[language_server_completion, signature_help] {
3430+ // for hook in &[signature_help] {
34163431 hook(cx, c);
34173432 }
34183433 }
helix-view/src/editor.rs+2
@@ -58,6 +58,7 @@ pub struct Config {
5858 /// Time in milliseconds since last keypress before idle timers trigger. Used for autocompletion, set to 0 for instant. Defaults to 400ms.
5959 #[serde(skip_serializing, deserialize_with = "deserialize_duration_millis")]
6060 pub idle_timeout: Duration,
61+ pub completion_trigger_len: u8,
6162}
6263
6364#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
@@ -87,6 +88,7 @@ impl Default for Config {
8788 auto_pairs: true,
8889 auto_completion: true,
8990 idle_timeout: Duration::from_millis(400),
91+ completion_trigger_len: 2,
9092 }
9193 }
9294}