From 71999cce43b3750362cdea534c12cbf320d2677b Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Mon, 22 Mar 2021 12:18:48 +0900 Subject: Implement auto-pairs behavior for open and close. --- helix-term/src/commands.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'helix-term') diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 54fea453..f7e137e0 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1009,9 +1009,23 @@ pub fn goto_reference(cx: &mut Context) { // NOTE: Transactions in this module get appended to history when we switch back to normal mode. pub mod insert { use super::*; + pub type Hook = fn(&Rope, &Selection, char) -> Option; + + use helix_core::auto_pairs; + const HOOKS: &[Hook] = &[auto_pairs::hook]; + // 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 doc = cx.doc(); + + // 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(), c) { + doc.apply(&transaction); + return; + } + } + let c = Tendril::from_char(c); let transaction = Transaction::insert(doc.text(), doc.selection(), c); @@ -1019,6 +1033,7 @@ pub mod insert { } pub fn insert_tab(cx: &mut Context) { + // TODO: tab should insert either \t or indent width spaces insert_char(cx, '\t'); } -- cgit v1.2.3-70-g09d2