on master help sign in
Add option for automatic insertion of closing-parens/brackets/etc (#779)

* Add auto-pair editor option * Document auto-pair editor option * Make cargo fmt happy * Actually make cargo fmt happy * Rename auto-pair option to auto-pairs * Inline a few constants Co-authored-by: miaomai <cunso@tutanota.com>

commita958d34bfbcf45c01ce0d9c0d76e681fb863fc6a authorlurpahi <91169847+lurpahi@users.noreply.github.com> dateThu 23 Sep 2021, 18:28 (5 years ago) parent432bec1
3 files changed+11−4
book/src/configuration.md+1 helix-term/src/commands.rs+7−4 helix-view/src/editor.rs+3
book/src/configuration.md+1
@@ -18,6 +18,7 @@ To override global configuration parameters, create a `config.toml` file located
1818| `shell` | Shell to use when running external commands. | Unix: `["sh", "-c"]`<br/>Windows: `["cmd", "/C"]` |
1919| `line-number` | Line number display (`absolute`, `relative`) | `absolute` |
2020| `smart-case` | Enable smart case regex searching (case insensitive unless pattern contains upper case characters) | `true` |
21+| `auto-pairs` | Enable automatic insertion of pairs to parenthese, brackets, etc. | `true` |
2122
2223## LSP
2324
helix-term/src/commands.rs+7−4
@@ -3371,17 +3371,20 @@ pub mod insert {
33713371 }
33723372
33733373 use helix_core::auto_pairs;
3374− const HOOKS: &[Hook] = &[auto_pairs::hook, insert];
3375− const POST_HOOKS: &[PostHook] = &[completion, signature_help];
33763374
33773375 pub fn insert_char(cx: &mut Context, c: char) {
33783376 let (view, doc) = current!(cx.editor);
33793377
3378+ let hooks: &[Hook] = match cx.editor.config.auto_pairs {
3379+ true => &[auto_pairs::hook, insert],
3380+ false => &[insert],
3381+ };
3382+
33803383 let text = doc.text();
33813384 let selection = doc.selection(view.id).clone().cursors(text.slice(..));
33823385
33833386 // run through insert hooks, stopping on the first one that returns Some(t)
3384− for hook in HOOKS {
3387+ for hook in hooks {
33853388 if let Some(transaction) = hook(text, &selection, c) {
33863389 doc.apply(&transaction, view.id);
33873390 break;
@@ -3391,7 +3394,7 @@ pub mod insert {
33913394 // TODO: need a post insert hook too for certain triggers (autocomplete, signature help, etc)
33923395 // this could also generically look at Transaction, but it's a bit annoying to look at
33933396 // Operation instead of Change.
3394− for hook in POST_HOOKS {
3397+ for hook in &[completion, signature_help] {
33953398 hook(cx, c);
33963399 }
33973400 }
helix-view/src/editor.rs+3
@@ -41,6 +41,8 @@ pub struct Config {
4141 pub middle_click_paste: bool,
4242 /// Smart case: Case insensitive searching unless pattern contains upper case characters. Defaults to true.
4343 pub smart_case: bool,
44+ /// Automatic insertion of pairs to parentheses, brackets, etc. Defaults to true.
45+ pub auto_pairs: bool,
4446}
4547
4648#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
@@ -67,6 +69,7 @@ impl Default for Config {
6769 line_number: LineNumber::Absolute,
6870 middle_click_paste: true,
6971 smart_case: true,
72+ auto_pairs: true,
7073 }
7174 }
7275}