From 8a64ab7fdd40daf399d686b5e4a6f497833b4df9 Mon Sep 17 00:00:00 2001 From: Mikhail Zhuk Date: Sun, 2 Jun 2024 23:31:30 +0200 Subject: [PATCH] add param auto-pairs-overtype --- helix-core/src/auto_pairs.rs | 21 ++++++++++++++++----- helix-term/src/commands.rs | 3 ++- helix-view/src/editor.rs | 3 +++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/helix-core/src/auto_pairs.rs b/helix-core/src/auto_pairs.rs index 853290404..1646f1249 100644 --- a/helix-core/src/auto_pairs.rs +++ b/helix-core/src/auto_pairs.rs @@ -15,7 +15,6 @@ pub const DEFAULT_PAIRS: &[(char, char)] = &[ ('"', '"'), ('`', '`'), ]; - /// The type that represents the collection of auto pairs, /// keyed by both opener and closer. #[derive(Debug, Clone)] @@ -120,7 +119,13 @@ impl Default for AutoPairs { // middle of triple quotes, and more exotic pairs like Jinja's {% %} #[must_use] -pub fn hook(doc: &Rope, selection: &Selection, ch: char, pairs: &AutoPairs) -> Option { +pub fn hook( + doc: &Rope, + selection: &Selection, + ch: char, + pairs: &AutoPairs, + auto_pairs_overtype: bool, +) -> Option { log::trace!("autopairs hook selection: {:#?}", selection); if let Some(pair) = pairs.get(ch) { @@ -130,7 +135,7 @@ pub fn hook(doc: &Rope, selection: &Selection, ch: char, pairs: &AutoPairs) -> O return Some(handle_open(doc, selection, pair)); } else if pair.close == ch { // && char_at pos == close - return Some(handle_close(doc, selection, pair)); + return Some(handle_close(doc, selection, pair, auto_pairs_overtype)); } } @@ -301,7 +306,12 @@ fn handle_open(doc: &Rope, selection: &Selection, pair: &Pair) -> Transaction { t } -fn handle_close(doc: &Rope, selection: &Selection, pair: &Pair) -> Transaction { +fn handle_close( + doc: &Rope, + selection: &Selection, + pair: &Pair, + auto_pairs_overtype: bool, +) -> Transaction { let mut end_ranges = SmallVec::with_capacity(selection.len()); let mut offs = 0; @@ -309,8 +319,9 @@ fn handle_close(doc: &Rope, selection: &Selection, pair: &Pair) -> Transaction { let cursor = start_range.cursor(doc.slice(..)); let next_char = doc.get_char(cursor); let mut len_inserted = 0; + // let len_inserted = 1; - let change = if next_char == Some(pair.close) { + let change = if auto_pairs_overtype && next_char == Some(pair.close) { // return transaction that moves past close (cursor, cursor, None) // no-op } else { diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index b1c29378d..ab406004a 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -3847,10 +3847,11 @@ pub mod insert { let text = doc.text(); let selection = doc.selection(view.id); let auto_pairs = doc.auto_pairs(cx.editor); + let auto_pairs_overtype = cx.editor.config().auto_pairs_overtype; let transaction = auto_pairs .as_ref() - .and_then(|ap| auto_pairs::hook(text, selection, c, ap)) + .and_then(|ap| auto_pairs::hook(text, selection, c, ap, auto_pairs_overtype)) .or_else(|| insert(text, selection, c)); let (view, doc) = current!(cx.editor); diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 26dea3a21..50b6b0361 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -265,6 +265,8 @@ pub struct Config { /// etc. Optionally, this can be a list of 2-tuples to specify a /// global list of characters to pair. Defaults to true. pub auto_pairs: AutoPairConfig, + /// Automatic overtype closing of pairs. Defaults to true. + pub auto_pairs_overtype: bool, /// Automatic auto-completion, automatically pop up without user trigger. Defaults to true. pub auto_completion: bool, /// Automatic formatting on save. Defaults to true. @@ -946,6 +948,7 @@ impl Default for Config { gutters: GutterConfig::default(), middle_click_paste: true, auto_pairs: AutoPairConfig::default(), + auto_pairs_overtype: true, auto_completion: true, auto_format: true, auto_save: AutoSave::default(),