Skip to content

Commit 39f7ba3

Browse files
ignore Enter keypress when menu has no selection (#1704)
* ignore Enter keypress when menu has no selection supersedes #1622 Builds on the work in #1285. I want to allow Enter to create a newline when there is no selection in the autocomplete menu. This occurs somewhat often when using LSP autocomplete in Elixir which uses `do/end` blocks (and I set the autocomplete menu delay to 0 which exacerbates the problem): ```elixir defmodule MyModule do def do_foo(x) do x end def other_function(y) do| end ``` Here the cursor is `|` in insert mode. The LSP suggests `do_foo` but I want to create a newline. Hitting Enter currently closes the menu, so I end up having to hit Enter twice when the module contains any local with a `do` prefix, which can be inconsistent. With this change, we ignore the Enter keypress to end up creating the newline in this case. * pop compositor layer when ignoring Enter keypress * move closing function out of consumed event result closure * explicitly label close_fn as an 'Option<Callback>'
1 parent c1251ae commit 39f7ba3

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

helix-term/src/ui/menu.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
compositor::{Component, Compositor, Context, EventResult},
2+
compositor::{Callback, Component, Compositor, Context, EventResult},
33
ctrl, key, shift,
44
};
55
use crossterm::event::Event;
@@ -205,16 +205,16 @@ impl<T: Item + 'static> Component for Menu<T> {
205205
_ => return EventResult::Ignored(None),
206206
};
207207

208-
let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor, _| {
208+
let close_fn: Option<Callback> = Some(Box::new(|compositor: &mut Compositor, _| {
209209
// remove the layer
210210
compositor.pop();
211-
})));
211+
}));
212212

213213
match event.into() {
214214
// esc or ctrl-c aborts the completion and closes the menu
215215
key!(Esc) | ctrl!('c') => {
216216
(self.callback_fn)(cx.editor, self.selection(), MenuEvent::Abort);
217-
return close_fn;
217+
return EventResult::Consumed(close_fn);
218218
}
219219
// arrow up/ctrl-p/shift-tab prev completion choice (including updating the doc)
220220
shift!(Tab) | key!(Up) | ctrl!('p') | ctrl!('k') => {
@@ -231,8 +231,10 @@ impl<T: Item + 'static> Component for Menu<T> {
231231
key!(Enter) => {
232232
if let Some(selection) = self.selection() {
233233
(self.callback_fn)(cx.editor, Some(selection), MenuEvent::Validate);
234+
return EventResult::Consumed(close_fn);
235+
} else {
236+
return EventResult::Ignored(close_fn);
234237
}
235-
return close_fn;
236238
}
237239
// KeyEvent {
238240
// code: KeyCode::Char(c),

0 commit comments

Comments
 (0)