Skip to content

Commit 9faaa33

Browse files
author
Rose Hogenson
committed
Handle prompt completions with AsyncHook.
This change allows prompt completions to be calculated in the background, to avoid blocking the UI on slow file IO such as over a networked FS.
1 parent 30aa375 commit 9faaa33

File tree

4 files changed

+219
-120
lines changed

4 files changed

+219
-120
lines changed

helix-term/src/commands.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ use crate::{
5959
compositor::{self, Component, Compositor},
6060
filter_picker_entry,
6161
job::Callback,
62-
ui::{self, overlay::overlaid, Picker, PickerColumn, Popup, Prompt, PromptEvent},
62+
ui::{
63+
self, completers::CompletionResult, overlay::overlaid, Picker, PickerColumn, Popup, Prompt,
64+
PromptEvent,
65+
},
6366
};
6467

6568
use crate::job::{self, Jobs};
@@ -2183,11 +2186,13 @@ fn searcher(cx: &mut Context, direction: Direction) {
21832186
"search:".into(),
21842187
Some(reg),
21852188
move |_editor: &Editor, input: &str| {
2186-
completions
2187-
.iter()
2188-
.filter(|comp| comp.starts_with(input))
2189-
.map(|comp| (0.., std::borrow::Cow::Owned(comp.clone())))
2190-
.collect()
2189+
CompletionResult::Immediate(
2190+
completions
2191+
.iter()
2192+
.filter(|comp| comp.starts_with(input))
2193+
.map(|comp| (0.., std::borrow::Cow::Owned(comp.clone())))
2194+
.collect(),
2195+
)
21912196
},
21922197
move |cx, regex, event| {
21932198
if event == PromptEvent::Validate {

helix-term/src/commands/typed.rs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use helix_core::{line_ending, shellwords::Shellwords};
1212
use helix_view::document::{read_to_string, DEFAULT_LANGUAGE_NAME};
1313
use helix_view::editor::{CloseError, ConfigEvent};
1414
use serde_json::Value;
15-
use ui::completers::{self, Completer};
15+
use ui::completers::{self, Completer, CompletionResult};
1616

1717
#[derive(Clone)]
1818
pub struct TypableCommand {
@@ -3164,14 +3164,16 @@ pub(super) fn command_mode(cx: &mut Context) {
31643164
let words = shellwords.words();
31653165

31663166
if words.is_empty() || (words.len() == 1 && !shellwords.ends_with_whitespace()) {
3167-
fuzzy_match(
3168-
input,
3169-
TYPABLE_COMMAND_LIST.iter().map(|command| command.name),
3170-
false,
3167+
CompletionResult::Immediate(
3168+
fuzzy_match(
3169+
input,
3170+
TYPABLE_COMMAND_LIST.iter().map(|command| command.name),
3171+
false,
3172+
)
3173+
.into_iter()
3174+
.map(|(name, _)| (0.., name.into()))
3175+
.collect(),
31713176
)
3172-
.into_iter()
3173-
.map(|(name, _)| (0.., name.into()))
3174-
.collect()
31753177
} else {
31763178
// Otherwise, use the command's completer and the last shellword
31773179
// as completion input.
@@ -3187,19 +3189,22 @@ pub(super) fn command_mode(cx: &mut Context) {
31873189
.get(&words[0] as &str)
31883190
.map(|tc| tc.completer_for_argument_number(argument_number))
31893191
{
3190-
completer(editor, word)
3191-
.into_iter()
3192-
.map(|(range, file)| {
3193-
let file = shellwords::escape(file);
3194-
3195-
// offset ranges to input
3196-
let offset = input.len() - word_len;
3197-
let range = (range.start + offset)..;
3198-
(range, file)
3199-
})
3200-
.collect()
3192+
let input = String::from(input);
3193+
completer(editor, word).map(move |completion| {
3194+
completion
3195+
.into_iter()
3196+
.map(|(range, file)| {
3197+
let file = shellwords::escape(file);
3198+
3199+
// offset ranges to input
3200+
let offset = input.len() - word_len;
3201+
let range = (range.start + offset)..;
3202+
(range, file)
3203+
})
3204+
.collect()
3205+
})
32013206
} else {
3202-
Vec::new()
3207+
CompletionResult::Immediate(Vec::new())
32033208
}
32043209
}
32053210
}, // completion

0 commit comments

Comments
 (0)