From ab4decfd6d18ee7377e20741d207e9e7b77758f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Mon, 3 May 2021 17:56:02 +0900 Subject: [PATCH] Make post hooks trigger even when a different handler processes input. The default handler is now simply a fallback. --- TODO.md | 5 ----- helix-term/src/commands.rs | 28 ++++++++++++++++------------ 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/TODO.md b/TODO.md index 5755c0f30..822cfa025 100644 --- a/TODO.md +++ b/TODO.md @@ -42,13 +42,8 @@ - [ ] lsp: formatting - [x] lsp: goto -- [ ] search: wrap around file - [ ] search: smart case by default: insensitive unless upper detected -- decide if markdown should have vertical padding too - -- the hooks system should be better for pre/post insert. - 2 - [ ] surround bindings (select + surround ( wraps selection in parens ) - [ ] macro recording diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index b77e205f3..bd65688db 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1271,9 +1271,6 @@ pub mod insert { pub type Hook = fn(&Rope, &Selection, char) -> Option; pub type PostHook = fn(&mut Context, char); - use helix_core::auto_pairs; - const HOOKS: &[Hook] = &[auto_pairs::hook]; - fn completion(cx: &mut Context, ch: char) { // if ch matches completion char, trigger completion let doc = cx.doc(); @@ -1302,7 +1299,6 @@ pub mod insert { } } - // TODO: the pre-hook handles ( so post hook never gets called fn signature_help(cx: &mut Context, ch: char) { // if ch matches signature_help char, trigger let doc = cx.doc(); @@ -1330,27 +1326,35 @@ pub mod insert { super::signature_help(cx); } } + + // 2021-05-03T17:54:36.326 hx::commands [INFO] SignatureHelp { signatures: [SignatureInformation { label: "fn open(&mut self, path: PathBuf, action: Action) -> Result", documentation: None, parameters: Some([ParameterInformation { label: Simple("path: PathBuf"), documentation: None }, ParameterInformation { label: Simple("action: Action"), documentation: None }]), active_parameter: Some(0) }], active_signature: None, active_parameter: Some(0) } } + // The default insert hook: simply insert the character + fn insert(doc: &Rope, selection: &Selection, ch: char) -> Option { + let t = Tendril::from_char(ch); + let transaction = Transaction::insert(doc, selection, t); + Some(transaction) + } + + use helix_core::auto_pairs; + const HOOKS: &[Hook] = &[auto_pairs::hook, insert]; const POST_HOOKS: &[PostHook] = &[completion, signature_help]; - // TODO: insert means add text just before cursor, on exit we should be on the last letter. pub fn insert_char(cx: &mut Context, c: char) { let (view, doc) = cx.current(); + let text = doc.text(); + let selection = doc.selection(view.id); + // run through insert hooks, stopping on the first one that returns Some(t) for hook in HOOKS { - if let Some(transaction) = hook(doc.text(), doc.selection(view.id), c) { + if let Some(transaction) = hook(text, selection, c) { doc.apply(&transaction, view.id); - return; + break; } } - let t = Tendril::from_char(c); - let transaction = Transaction::insert(doc.text(), doc.selection(view.id), t); - - doc.apply(&transaction, view.id); - // TODO: need a post insert hook too for certain triggers (autocomplete, signature help, etc) // this could also generically look at Transaction, but it's a bit annoying to look at // Operation instead of Change.