Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move mode transition logic to handle_keymap_event() #2634

Merged
merged 1 commit into from
Aug 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 42 additions & 36 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -771,15 +771,55 @@ impl EditorView {
cxt: &mut commands::Context,
event: KeyEvent,
) -> Option<KeymapResult> {
let mut last_mode = mode;
let key_result = self.keymaps.get(mode, event);
cxt.editor.autoinfo = self.keymaps.sticky().map(|node| node.infobox());

// Track the currently open doc
let view = view!(cxt.editor);
let doc_id = view.doc;

let mut execute_command = |command: &commands::MappableCommand| {
command.execute(cxt);
let doc = cxt.editor.documents.get_mut(&doc_id).unwrap();
Comment on lines +783 to +784
Copy link
Contributor

@erasin erasin Aug 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I set remaping for close buffer, It back error.

[keys.normal.space]
x = [":buffer-close"]

thread 'main' panicked at 'called Option::unwrap() on a None value', helix-term/src/ui/editor.rs:784:61

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a PR to fix this case: #3613

Copy link
Contributor

@erasin erasin Sep 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when close the last buffer , It will take error:

thread 'main' panicked at 'invalid HopSlotMap key used', helix-view/src/tree.rs:288:20
stack backtrace:
   0: std::panicking::begin_panic
             at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:616:12
   1: <slotmap::hop::HopSlotMap<K,V> as core::ops::index::IndexMut<K>>::index_mut
             at /Users/erasin/.cargo/registry/src/github.aaakk.us.kg-1ecc6299db9ec823/slotmap-1.0.6/src/hop.rs:948:21
   2: helix_view::tree::Tree::get_mut
-             at ./helix-view/src/tree.rs:288:20
   3: <helix_term::ui::editor::EditorView as helix_term::compositor::Component>::handle_event
-             at ./helix-term/src/ui/editor.rs:1268:28
   4: helix_term::compositor::Compositor::handle_event
             at ./helix-term/src/compositor.rs:172:19
   5: helix_term::application::Application::handle_terminal_events
             at ./helix-term/src/application.rs:453:22
   6: helix_term::application::Application::event_loop_until_idle::{{closure}}
             at ./helix-term/src/application.rs:292:21
   7: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/future/mod.rs:91:19
   8: helix_term::application::Application::event_loop::{{closure}}
             at ./helix-term/src/application.rs:268:57
   9: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/future/mod.rs:91:19
  10: helix_term::application::Application::run::{{closure}}
             at ./helix-term/src/application.rs:838:38
  11: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/future/mod.rs:91:19
  12: hx::main_impl::{{closure}}
             at ./helix-term/src/main.rs:143:53

mode => self.command_mode(mode, &mut cx, key),

let view = cx.editor.tree.get_mut(focus);

I don't know how it works, but it's probably caused by this.
Maybe we need refresh something.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an issue for this now: #3626

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I check tree.view().count() == 1 and Refetching the view id.
It seems to ok with #3635

let current_mode = doc.mode();
match (last_mode, current_mode) {
(Mode::Normal, Mode::Insert) => {
// HAXX: if we just entered insert mode from normal, clear key buf
archseer marked this conversation as resolved.
Show resolved Hide resolved
// and record the command that got us into this mode.

// how we entered insert mode is important, and we should track that so
// we can repeat the side effect.
self.last_insert.0 = command.clone();
self.last_insert.1.clear();

commands::signature_help_impl(cxt, commands::SignatureHelpInvoked::Automatic);
}
(Mode::Insert, Mode::Normal) => {
// if exiting insert mode, remove completion
self.completion = None;

// TODO: Use an on_mode_change hook to remove signature help
cxt.jobs.callback(async {
let call: job::Callback = Box::new(|_editor, compositor| {
compositor.remove(SignatureHelp::ID);
});
Ok(call)
});
}
_ => (),
}
last_mode = current_mode;
};

match &key_result {
KeymapResult::Matched(command) => command.execute(cxt),
KeymapResult::Matched(command) => {
execute_command(command);
}
KeymapResult::Pending(node) => cxt.editor.autoinfo = Some(node.infobox()),
KeymapResult::MatchedSequence(commands) => {
for command in commands {
command.execute(cxt);
execute_command(command);
}
}
KeymapResult::NotFound | KeymapResult::Cancelled(_) => return Some(key_result),
Expand Down Expand Up @@ -1233,40 +1273,6 @@ impl Component for EditorView {
doc.append_changes_to_history(view.id);
}

// mode transitions
match (mode, doc.mode()) {
(Mode::Normal, Mode::Insert) => {
// HAXX: if we just entered insert mode from normal, clear key buf
// and record the command that got us into this mode.

// how we entered insert mode is important, and we should track that so
// we can repeat the side effect.
lucypero marked this conversation as resolved.
Show resolved Hide resolved

self.last_insert.0 = match self.keymaps.get(mode, key) {
KeymapResult::Matched(command) => command,
// FIXME: insert mode can only be entered through single KeyCodes
_ => unimplemented!(),
};
self.last_insert.1.clear();
commands::signature_help_impl(
&mut cx,
commands::SignatureHelpInvoked::Automatic,
);
}
(Mode::Insert, Mode::Normal) => {
// if exiting insert mode, remove completion
lucypero marked this conversation as resolved.
Show resolved Hide resolved
self.completion = None;
// TODO: Use an on_mode_change hook to remove signature help
context.jobs.callback(async {
let call: job::Callback = Box::new(|_editor, compositor| {
compositor.remove(SignatureHelp::ID);
});
Ok(call)
});
}
_ => (),
}

EventResult::Consumed(callback)
}

Expand Down