aboutsummaryrefslogtreecommitdiff
path: root/helix-term/src/ui/completion.rs
diff options
context:
space:
mode:
authorBlaž Hrastnik2021-10-10 13:11:01 +0000
committerGitHub2021-10-10 13:11:01 +0000
commitf8f63c55081ee966c7cb2b84139c25c6301b5fff (patch)
treec4c74c5c013d8c26f6b9b124821fabd9200c3218 /helix-term/src/ui/completion.rs
parenta7f49fa56fecd7f44efca7e6074e5cd9e5d91c92 (diff)
parent76b1bbc23ad5fc47765472cd9e83727a43c97ff3 (diff)
Merge pull request #821 from helix-editor/idle-timer
Idle timer / Autocompletion
Diffstat (limited to 'helix-term/src/ui/completion.rs')
-rw-r--r--helix-term/src/ui/completion.rs26
1 files changed, 20 insertions, 6 deletions
diff --git a/helix-term/src/ui/completion.rs b/helix-term/src/ui/completion.rs
index 6c9e3a80..ba009c50 100644
--- a/helix-term/src/ui/completion.rs
+++ b/helix-term/src/ui/completion.rs
@@ -69,14 +69,18 @@ impl menu::Item for CompletionItem {
/// Wraps a Menu.
pub struct Completion {
popup: Popup<Menu<CompletionItem>>,
+ start_offset: usize,
+ #[allow(dead_code)]
trigger_offset: usize,
// TODO: maintain a completioncontext with trigger kind & trigger char
}
impl Completion {
pub fn new(
+ editor: &Editor,
items: Vec<CompletionItem>,
offset_encoding: helix_lsp::OffsetEncoding,
+ start_offset: usize,
trigger_offset: usize,
) -> Self {
// let items: Vec<CompletionItem> = Vec::new();
@@ -175,16 +179,22 @@ impl Completion {
};
});
let popup = Popup::new(menu);
- Self {
+ let mut completion = Self {
popup,
+ start_offset,
trigger_offset,
- }
+ };
+
+ // need to recompute immediately in case start_offset != trigger_offset
+ completion.recompute_filter(editor);
+
+ completion
}
- pub fn update(&mut self, cx: &mut commands::Context) {
+ pub fn recompute_filter(&mut self, editor: &Editor) {
// recompute menu based on matches
let menu = self.popup.contents_mut();
- let (view, doc) = current!(cx.editor);
+ let (view, doc) = current_ref!(editor);
// cx.hooks()
// cx.add_hook(enum type, ||)
@@ -200,14 +210,18 @@ impl Completion {
.selection(view.id)
.primary()
.cursor(doc.text().slice(..));
- if self.trigger_offset <= cursor {
- let fragment = doc.text().slice(self.trigger_offset..cursor);
+ if self.start_offset <= cursor {
+ let fragment = doc.text().slice(self.start_offset..cursor);
let text = Cow::from(fragment);
// TODO: logic is same as ui/picker
menu.score(&text);
}
}
+ pub fn update(&mut self, cx: &mut commands::Context) {
+ self.recompute_filter(cx.editor)
+ }
+
pub fn is_empty(&self) -> bool {
self.popup.contents().is_empty()
}