on master help sign in
Move mode transition logic to handle_keymap_event() (#2634)

Co-authored-by: Blaž Hrastnik <blaz@mxxn.io> Co-authored-by: Blaž Hrastnik <blaz@mxxn.io>

commit404db2ebee477a9bfdb76e89ebf48adac9c1a57c authorLucy <11927498+lucypero@users.noreply.github.com> dateTue 30 Aug 2022, 22:44 (4 years ago) parent78189dd
1 file changed+42−36
helix-term/src/ui/editor.rs+42−36
helix-term/src/ui/editor.rs+42−36
@@ -771,15 +771,55 @@ impl EditorView {
771771 cxt: &mut commands::Context,
772772 event: KeyEvent,
773773 ) -> Option<KeymapResult> {
774+ let mut last_mode = mode;
774775 let key_result = self.keymaps.get(mode, event);
775776 cxt.editor.autoinfo = self.keymaps.sticky().map(|node| node.infobox());
776777
778+ // Track the currently open doc
779+ let view = view!(cxt.editor);
780+ let doc_id = view.doc;
781+
782+ let mut execute_command = |command: &commands::MappableCommand| {
783+ command.execute(cxt);
784+ let doc = cxt.editor.documents.get_mut(&doc_id).unwrap();
785+ let current_mode = doc.mode();
786+ match (last_mode, current_mode) {
787+ (Mode::Normal, Mode::Insert) => {
788+ // HAXX: if we just entered insert mode from normal, clear key buf
789+ // and record the command that got us into this mode.
790+
791+ // how we entered insert mode is important, and we should track that so
792+ // we can repeat the side effect.
793+ self.last_insert.0 = command.clone();
794+ self.last_insert.1.clear();
795+
796+ commands::signature_help_impl(cxt, commands::SignatureHelpInvoked::Automatic);
797+ }
798+ (Mode::Insert, Mode::Normal) => {
799+ // if exiting insert mode, remove completion
800+ self.completion = None;
801+
802+ // TODO: Use an on_mode_change hook to remove signature help
803+ cxt.jobs.callback(async {
804+ let call: job::Callback = Box::new(|_editor, compositor| {
805+ compositor.remove(SignatureHelp::ID);
806+ });
807+ Ok(call)
808+ });
809+ }
810+ _ => (),
811+ }
812+ last_mode = current_mode;
813+ };
814+
777815 match &key_result {
778− KeymapResult::Matched(command) => command.execute(cxt),
816+ KeymapResult::Matched(command) => {
817+ execute_command(command);
818+ }
779819 KeymapResult::Pending(node) => cxt.editor.autoinfo = Some(node.infobox()),
780820 KeymapResult::MatchedSequence(commands) => {
781821 for command in commands {
782− command.execute(cxt);
822+ execute_command(command);
783823 }
784824 }
785825 KeymapResult::NotFound | KeymapResult::Cancelled(_) => return Some(key_result),
@@ -1233,40 +1273,6 @@ impl Component for EditorView {
12331273 doc.append_changes_to_history(view.id);
12341274 }
12351275
1236− // mode transitions
1237− match (mode, doc.mode()) {
1238− (Mode::Normal, Mode::Insert) => {
1239− // HAXX: if we just entered insert mode from normal, clear key buf
1240− // and record the command that got us into this mode.
1241−
1242− // how we entered insert mode is important, and we should track that so
1243− // we can repeat the side effect.
1244−
1245− self.last_insert.0 = match self.keymaps.get(mode, key) {
1246− KeymapResult::Matched(command) => command,
1247− // FIXME: insert mode can only be entered through single KeyCodes
1248− _ => unimplemented!(),
1249− };
1250− self.last_insert.1.clear();
1251− commands::signature_help_impl(
1252− &mut cx,
1253− commands::SignatureHelpInvoked::Automatic,
1254− );
1255− }
1256− (Mode::Insert, Mode::Normal) => {
1257− // if exiting insert mode, remove completion
1258− self.completion = None;
1259− // TODO: Use an on_mode_change hook to remove signature help
1260− context.jobs.callback(async {
1261− let call: job::Callback = Box::new(|_editor, compositor| {
1262− compositor.remove(SignatureHelp::ID);
1263− });
1264− Ok(call)
1265− });
1266− }
1267− _ => (),
1268− }
1269−
12701276 EventResult::Consumed(callback)
12711277 }
12721278