on master help sign in
Make post hooks trigger even when a different handler processes input.
ab4decfBlaž Hrastnik
TODO.md−5
@@ -42,13 +42,8 @@
42- [ ] lsp: formatting
42- [ ] lsp: formatting
43- [x] lsp: goto
43- [x] lsp: goto
44
44
45−- [ ] search: wrap around file
46- [ ] search: smart case by default: insensitive unless upper detected
45- [ ] search: smart case by default: insensitive unless upper detected
47
46
48−- decide if markdown should have vertical padding too
49−
50−- the hooks system should be better for pre/post insert.
51−
522
472
53- [ ] surround bindings (select + surround ( wraps selection in parens )
48- [ ] surround bindings (select + surround ( wraps selection in parens )
54- [ ] macro recording
49- [ ] macro recording
helix-term/src/commands.rs+16−12
@@ -1271,9 +1271,6 @@ pub mod insert {
1271 pub type Hook = fn(&Rope, &Selection, char) -> Option<Transaction>;
1271 pub type Hook = fn(&Rope, &Selection, char) -> Option<Transaction>;
1272 pub type PostHook = fn(&mut Context, char);
1272 pub type PostHook = fn(&mut Context, char);
1273
1273
1274− use helix_core::auto_pairs;
1275− const HOOKS: &[Hook] = &[auto_pairs::hook];
1276−
1277 fn completion(cx: &mut Context, ch: char) {
1274 fn completion(cx: &mut Context, ch: char) {
1278 // if ch matches completion char, trigger completion
1275 // if ch matches completion char, trigger completion
1279 let doc = cx.doc();
1276 let doc = cx.doc();
@@ -1302,7 +1299,6 @@ pub mod insert {
1302 }
1299 }
1303 }
1300 }
1304
1301
1305− // TODO: the pre-hook handles ( so post hook never gets called
1306 fn signature_help(cx: &mut Context, ch: char) {
1302 fn signature_help(cx: &mut Context, ch: char) {
1307 // if ch matches signature_help char, trigger
1303 // if ch matches signature_help char, trigger
1308 let doc = cx.doc();
1304 let doc = cx.doc();
@@ -1330,27 +1326,35 @@ pub mod insert {
1330 super::signature_help(cx);
1326 super::signature_help(cx);
1331 }
1327 }
1332 }
1328 }
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) }
1333 }
1331 }
1334
1332
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];
1335 const POST_HOOKS: &[PostHook] = &[completion, signature_help];
1342 const POST_HOOKS: &[PostHook] = &[completion, signature_help];
1336
1343
1337− // TODO: insert means add text just before cursor, on exit we should be on the last letter.
1338 pub fn insert_char(cx: &mut Context, c: char) {
1344 pub fn insert_char(cx: &mut Context, c: char) {
1339 let (view, doc) = cx.current();
1345 let (view, doc) = cx.current();
1340
1346
1347+ let text = doc.text();
1348+ let selection = doc.selection(view.id);
1349+
1341 // run through insert hooks, stopping on the first one that returns Some(t)
1350 // run through insert hooks, stopping on the first one that returns Some(t)
1342 for hook in HOOKS {
1351 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) {
1344 doc.apply(&transaction, view.id);
1353 doc.apply(&transaction, view.id);
1345− return;
1354+ break;
1346 }
1355 }
1347 }
1356 }
1348
1357
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−
1354 // TODO: need a post insert hook too for certain triggers (autocomplete, signature help, etc)
1358 // TODO: need a post insert hook too for certain triggers (autocomplete, signature help, etc)
1355 // this could also generically look at Transaction, but it's a bit annoying to look at
1359 // this could also generically look at Transaction, but it's a bit annoying to look at
1356 // Operation instead of Change.
1360 // Operation instead of Change.