on master help sign in
Make post hooks trigger even when a different handler processes input.

The default handler is now simply a fallback.

commitab4decfd6d18ee7377e20741d207e9e7b77758f4 authorBlaž Hrastnik <blaz@mxxn.io> dateMon 3 May 2021, 17:56 (5 years ago) parentb0bdcab
2 files changed+16−17
TODO.md−5 helix-term/src/commands.rs+16−12
TODO.md−5
@@ -42,13 +42,8 @@
4242- [ ] lsp: formatting
4343- [x] lsp: goto
4444
45−- [ ] search: wrap around file
4645- [ ] search: smart case by default: insensitive unless upper detected
4746
48−- decide if markdown should have vertical padding too
49−
50−- the hooks system should be better for pre/post insert.
51−
52472
5348- [ ] surround bindings (select + surround ( wraps selection in parens )
5449- [ ] macro recording
helix-term/src/commands.rs+16−12
@@ -1271,9 +1271,6 @@ pub mod insert {
12711271 pub type Hook = fn(&Rope, &Selection, char) -> Option<Transaction>;
12721272 pub type PostHook = fn(&mut Context, char);
12731273
1274− use helix_core::auto_pairs;
1275− const HOOKS: &[Hook] = &[auto_pairs::hook];
1276−
12771274 fn completion(cx: &mut Context, ch: char) {
12781275 // if ch matches completion char, trigger completion
12791276 let doc = cx.doc();
@@ -1302,7 +1299,6 @@ pub mod insert {
13021299 }
13031300 }
13041301
1305− // TODO: the pre-hook handles ( so post hook never gets called
13061302 fn signature_help(cx: &mut Context, ch: char) {
13071303 // if ch matches signature_help char, trigger
13081304 let doc = cx.doc();
@@ -1330,27 +1326,35 @@ pub mod insert {
13301326 super::signature_help(cx);
13311327 }
13321328 }
1329+
1330+ // 2021-05-03T17:54:36.326 hx::commands [INFO] SignatureHelp { signatures: [SignatureInformation { label: "fn open(&mut self, path: PathBuf, action: Action) -> Result<DocumentId, Error>", 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) }
13331331 }
13341332
1333+ // The default insert hook: simply insert the character
1334+ fn insert(doc: &Rope, selection: &Selection, ch: char) -> Option<Transaction> {
1335+ let t = Tendril::from_char(ch);
1336+ let transaction = Transaction::insert(doc, selection, t);
1337+ Some(transaction)
1338+ }
1339+
1340+ use helix_core::auto_pairs;
1341+ const HOOKS: &[Hook] = &[auto_pairs::hook, insert];
13351342 const POST_HOOKS: &[PostHook] = &[completion, signature_help];
13361343
1337− // TODO: insert means add text just before cursor, on exit we should be on the last letter.
13381344 pub fn insert_char(cx: &mut Context, c: char) {
13391345 let (view, doc) = cx.current();
13401346
1347+ let text = doc.text();
1348+ let selection = doc.selection(view.id);
1349+
13411350 // run through insert hooks, stopping on the first one that returns Some(t)
13421351 for hook in HOOKS {
1343− if let Some(transaction) = hook(doc.text(), doc.selection(view.id), c) {
1352+ if let Some(transaction) = hook(text, selection, c) {
13441353 doc.apply(&transaction, view.id);
1345− return;
1354+ break;
13461355 }
13471356 }
13481357
1349− let t = Tendril::from_char(c);
1350− let transaction = Transaction::insert(doc.text(), doc.selection(view.id), t);
1351−
1352− doc.apply(&transaction, view.id);
1353−
13541358 // TODO: need a post insert hook too for certain triggers (autocomplete, signature help, etc)
13551359 // this could also generically look at Transaction, but it's a bit annoying to look at
13561360 // Operation instead of Change.