| 1 | use std::{borrow::Cow, sync::Arc}; |
| 2 | |
| 3 | use helix_core::{ |
| 4 | self as core, chars::char_is_word, completion::CompletionProvider, movement, Transaction, |
| 5 | }; |
| 6 | use helix_event::TaskHandle; |
| 7 | use helix_stdx::rope::RopeSliceExt as _; |
| 8 | use helix_view::{ |
| 9 | document::SavePoint, handlers::completion::ResponseContext, Document, Editor, ViewId, |
| 10 | }; |
| 11 | |
| 12 | use super::{request::TriggerKind, CompletionItem, CompletionItems, CompletionResponse, Trigger}; |
| 13 | |
| 14 | const COMPLETION_KIND: &str = "word"; |
| 15 | |
| 16 | pub(super) fn completion( |
| 17 | editor: &Editor, |
| 18 | trigger: Trigger, |
| 19 | handle: TaskHandle, |
| 20 | savepoint: Arc<SavePoint>, |
| 21 | ) -> Option<impl FnOnce() -> CompletionResponse> { |
| 22 | if !doc!(editor).word_completion_enabled() { |
| 23 | return None; |
| 24 | } |
| 25 | let config = editor.config().word_completion; |
| 26 | let doc_config = doc!(editor) |
| 27 | .language_config() |
| 28 | .and_then(|config| config.word_completion); |
| 29 | let trigger_length = doc_config |
| 30 | .and_then(|c| c.trigger_length) |
| 31 | .unwrap_or(config.trigger_length) |
| 32 | .get() as usize; |
| 33 | |
| 34 | let (view, doc) = current_ref!(editor); |
| 35 | let rope = doc.text().clone(); |
| 36 | let word_index = editor.handlers.word_index().clone(); |
| 37 | let text = doc.text().slice(..); |
| 38 | let selection = doc.selection(view.id).clone(); |
| 39 | let pos = selection.primary().cursor(text); |
| 40 | |
| 41 | let cursor = movement::move_prev_word_start(text, core::Range::point(pos), 1); |
| 42 | if cursor.head == pos { |
| 43 | return None; |
| 44 | } |
| 45 | if trigger.kind != TriggerKind::Manual |
| 46 | && text |
| 47 | .slice(cursor.head..) |
| 48 | .graphemes() |
| 49 | .take(trigger_length) |
| 50 | .take_while(|g| g.chars().all(char_is_word)) |
| 51 | .count() |
| 52 | != trigger_length |
| 53 | { |
| 54 | return None; |
| 55 | } |
| 56 | |
| 57 | let typed_word_range = cursor.head..pos; |
| 58 | let typed_word = text.slice(typed_word_range.clone()); |
| 59 | let edit_diff = if typed_word |
| 60 | .char(typed_word.len_chars().saturating_sub(1)) |
| 61 | .is_whitespace() |
| 62 | { |
| 63 | 0 |
| 64 | } else { |
| 65 | typed_word.len_chars() |
| 66 | }; |
| 67 | |
| 68 | if handle.is_canceled() { |
| 69 | return None; |
| 70 | } |
| 71 | |
| 72 | let future = move || { |
| 73 | let text = rope.slice(..); |
| 74 | let typed_word: Cow<_> = text.slice(typed_word_range).into(); |
| 75 | let items = word_index |
| 76 | .matches(&typed_word) |
| 77 | .into_iter() |
| 78 | .filter(|word| word.as_str() != typed_word.as_ref()) |
| 79 | .map(|word| { |
| 80 | let transaction = Transaction::change_by_selection(&rope, &selection, |range| { |
| 81 | let cursor = range.cursor(text); |
| 82 | (cursor - edit_diff, cursor, Some((&word).into())) |
| 83 | }); |
| 84 | CompletionItem::Other(core::CompletionItem { |
| 85 | transaction, |
| 86 | label: word.into(), |
| 87 | kind: Cow::Borrowed(COMPLETION_KIND), |
| 88 | documentation: None, |
| 89 | provider: CompletionProvider::Word, |
| 90 | }) |
| 91 | }) |
| 92 | .collect(); |
| 93 | |
| 94 | CompletionResponse { |
| 95 | items: CompletionItems::Other(items), |
| 96 | provider: CompletionProvider::Word, |
| 97 | context: ResponseContext { |
| 98 | is_incomplete: false, |
| 99 | priority: 0, |
| 100 | savepoint, |
| 101 | }, |
| 102 | } |
| 103 | }; |
| 104 | |
| 105 | Some(future) |
| 106 | } |
| 107 | |
| 108 | pub(super) fn retain_valid_completions( |
| 109 | trigger: Trigger, |
| 110 | doc: &Document, |
| 111 | view_id: ViewId, |
| 112 | items: &mut Vec<CompletionItem>, |
| 113 | ) { |
| 114 | if trigger.kind == TriggerKind::Manual { |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | let text = doc.text().slice(..); |
| 119 | let cursor = doc.selection(view_id).primary().cursor(text); |
| 120 | if text |
| 121 | .get_char(cursor.saturating_sub(1)) |
| 122 | .is_some_and(|ch| ch.is_whitespace()) |
| 123 | { |
| 124 | items.retain(|item| { |
| 125 | !matches!( |
| 126 | item, |
| 127 | CompletionItem::Other(core::CompletionItem { |
| 128 | provider: CompletionProvider::Word, |
| 129 | .. |
| 130 | }) |
| 131 | ) |
| 132 | }); |
| 133 | } |
| 134 | } |