Skip to content

Commit 336e41e

Browse files
author
Rose Hogenson
committed
Handle prompt completions with AsyncHook.
This change updates prompt completions in the background, to avoid blocking the UI on slow file IO such as over a networked FS. I followed the example of how LSP completions are handled asynchronously.
1 parent 30aa375 commit 336e41e

File tree

7 files changed

+305
-221
lines changed

7 files changed

+305
-221
lines changed

helix-term/src/commands.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2182,7 +2182,7 @@ fn searcher(cx: &mut Context, direction: Direction) {
21822182
cx,
21832183
"search:".into(),
21842184
Some(reg),
2185-
move |_editor: &Editor, input: &str| {
2185+
move |input: &str| {
21862186
completions
21872187
.iter()
21882188
.filter(|comp| comp.starts_with(input))

helix-term/src/commands/dap.rs

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -334,12 +334,8 @@ fn debug_parameter_prompt(
334334
.to_owned();
335335

336336
let completer = match field_type {
337-
"filename" => |editor: &Editor, input: &str| {
338-
ui::completers::filename_with_git_ignore(editor, input, false)
339-
},
340-
"directory" => |editor: &Editor, input: &str| {
341-
ui::completers::directory_with_git_ignore(editor, input, false)
342-
},
337+
"filename" => |input: &str| ui::completers::filename_with_git_ignore(input, false),
338+
"directory" => |input: &str| ui::completers::directory_with_git_ignore(input, false),
343339
_ => ui::completers::none,
344340
};
345341

@@ -631,35 +627,37 @@ pub fn dap_edit_condition(cx: &mut Context) {
631627
None => return,
632628
};
633629
let callback = Box::pin(async move {
634-
let call: Callback = Callback::EditorCompositor(Box::new(move |editor, compositor| {
635-
let mut prompt = Prompt::new(
636-
"condition:".into(),
637-
None,
638-
ui::completers::none,
639-
move |cx, input: &str, event: PromptEvent| {
640-
if event != PromptEvent::Validate {
641-
return;
642-
}
643-
644-
let breakpoints = &mut cx.editor.breakpoints.get_mut(&path).unwrap();
645-
breakpoints[pos].condition = match input {
646-
"" => None,
647-
input => Some(input.to_owned()),
648-
};
649-
650-
let debugger = debugger!(cx.editor);
651-
652-
if let Err(e) = breakpoints_changed(debugger, path.clone(), breakpoints) {
653-
cx.editor
654-
.set_error(format!("Failed to set breakpoints: {}", e));
655-
}
656-
},
657-
);
658-
if let Some(condition) = breakpoint.condition {
659-
prompt.insert_str(&condition, editor)
660-
}
661-
compositor.push(Box::new(prompt));
662-
}));
630+
let call: Callback =
631+
Callback::EditorCompositor(Box::new(move |_editor, compositor| {
632+
let mut prompt = Prompt::new(
633+
"condition:".into(),
634+
None,
635+
ui::completers::none,
636+
move |cx, input: &str, event: PromptEvent| {
637+
if event != PromptEvent::Validate {
638+
return;
639+
}
640+
641+
let breakpoints = &mut cx.editor.breakpoints.get_mut(&path).unwrap();
642+
breakpoints[pos].condition = match input {
643+
"" => None,
644+
input => Some(input.to_owned()),
645+
};
646+
647+
let debugger = debugger!(cx.editor);
648+
649+
if let Err(e) = breakpoints_changed(debugger, path.clone(), breakpoints)
650+
{
651+
cx.editor
652+
.set_error(format!("Failed to set breakpoints: {}", e));
653+
}
654+
},
655+
);
656+
if let Some(condition) = breakpoint.condition {
657+
prompt.insert_str(&condition)
658+
}
659+
compositor.push(Box::new(prompt));
660+
}));
663661
Ok(call)
664662
});
665663
cx.jobs.callback(callback);
@@ -673,34 +671,36 @@ pub fn dap_edit_log(cx: &mut Context) {
673671
None => return,
674672
};
675673
let callback = Box::pin(async move {
676-
let call: Callback = Callback::EditorCompositor(Box::new(move |editor, compositor| {
677-
let mut prompt = Prompt::new(
678-
"log-message:".into(),
679-
None,
680-
ui::completers::none,
681-
move |cx, input: &str, event: PromptEvent| {
682-
if event != PromptEvent::Validate {
683-
return;
684-
}
685-
686-
let breakpoints = &mut cx.editor.breakpoints.get_mut(&path).unwrap();
687-
breakpoints[pos].log_message = match input {
688-
"" => None,
689-
input => Some(input.to_owned()),
690-
};
691-
692-
let debugger = debugger!(cx.editor);
693-
if let Err(e) = breakpoints_changed(debugger, path.clone(), breakpoints) {
694-
cx.editor
695-
.set_error(format!("Failed to set breakpoints: {}", e));
696-
}
697-
},
698-
);
699-
if let Some(log_message) = breakpoint.log_message {
700-
prompt.insert_str(&log_message, editor);
701-
}
702-
compositor.push(Box::new(prompt));
703-
}));
674+
let call: Callback =
675+
Callback::EditorCompositor(Box::new(move |_editor, compositor| {
676+
let mut prompt = Prompt::new(
677+
"log-message:".into(),
678+
None,
679+
ui::completers::none,
680+
move |cx, input: &str, event: PromptEvent| {
681+
if event != PromptEvent::Validate {
682+
return;
683+
}
684+
685+
let breakpoints = &mut cx.editor.breakpoints.get_mut(&path).unwrap();
686+
breakpoints[pos].log_message = match input {
687+
"" => None,
688+
input => Some(input.to_owned()),
689+
};
690+
691+
let debugger = debugger!(cx.editor);
692+
if let Err(e) = breakpoints_changed(debugger, path.clone(), breakpoints)
693+
{
694+
cx.editor
695+
.set_error(format!("Failed to set breakpoints: {}", e));
696+
}
697+
},
698+
);
699+
if let Some(log_message) = breakpoint.log_message {
700+
prompt.insert_str(&log_message);
701+
}
702+
compositor.push(Box::new(prompt));
703+
}));
704704
Ok(call)
705705
});
706706
cx.jobs.callback(callback);

helix-term/src/commands/lsp.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,6 @@ pub fn rename_symbol(cx: &mut Context) {
11251125
}
11261126

11271127
fn create_rename_prompt(
1128-
editor: &Editor,
11291128
prefill: String,
11301129
history_register: Option<char>,
11311130
language_server_id: Option<LanguageServerId>,
@@ -1163,7 +1162,7 @@ pub fn rename_symbol(cx: &mut Context) {
11631162
}
11641163
},
11651164
)
1166-
.with_line(prefill, editor);
1165+
.with_line(prefill);
11671166

11681167
Box::new(prompt)
11691168
}
@@ -1212,14 +1211,14 @@ pub fn rename_symbol(cx: &mut Context) {
12121211
}
12131212
};
12141213

1215-
let prompt = create_rename_prompt(editor, prefill, history_register, Some(ls_id));
1214+
let prompt = create_rename_prompt(prefill, history_register, Some(ls_id));
12161215

12171216
compositor.push(prompt);
12181217
},
12191218
);
12201219
} else {
12211220
let prefill = get_prefill_from_word_boundary(cx.editor);
1222-
let prompt = create_rename_prompt(cx.editor, prefill, history_register, None);
1221+
let prompt = create_rename_prompt(prefill, history_register, None);
12231222
cx.push_layer(prompt);
12241223
}
12251224
}

helix-term/src/commands/typed.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3159,7 +3159,7 @@ pub(super) fn command_mode(cx: &mut Context) {
31593159
let mut prompt = Prompt::new(
31603160
":".into(),
31613161
Some(':'),
3162-
|editor: &Editor, input: &str| {
3162+
|input: &str| {
31633163
let shellwords = Shellwords::from(input);
31643164
let words = shellwords.words();
31653165

@@ -3187,7 +3187,7 @@ pub(super) fn command_mode(cx: &mut Context) {
31873187
.get(&words[0] as &str)
31883188
.map(|tc| tc.completer_for_argument_number(argument_number))
31893189
{
3190-
completer(editor, word)
3190+
completer(word)
31913191
.into_iter()
31923192
.map(|(range, file)| {
31933193
let file = shellwords::escape(file);
@@ -3247,7 +3247,7 @@ pub(super) fn command_mode(cx: &mut Context) {
32473247
});
32483248

32493249
// Calculate initial completion
3250-
prompt.recalculate_completion(cx.editor);
3250+
prompt.recalculate_completion();
32513251
cx.push_layer(Box::new(prompt));
32523252
}
32533253

0 commit comments

Comments
 (0)