add param auto-pairs-overtype

Mikhail Zhuk 6 months ago
parent 73deabaa40
commit 8a64ab7fdd

@ -15,7 +15,6 @@ pub const DEFAULT_PAIRS: &[(char, char)] = &[
('"', '"'), ('"', '"'),
('`', '`'), ('`', '`'),
]; ];
/// The type that represents the collection of auto pairs, /// The type that represents the collection of auto pairs,
/// keyed by both opener and closer. /// keyed by both opener and closer.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -120,7 +119,13 @@ impl Default for AutoPairs {
// middle of triple quotes, and more exotic pairs like Jinja's {% %} // middle of triple quotes, and more exotic pairs like Jinja's {% %}
#[must_use] #[must_use]
pub fn hook(doc: &Rope, selection: &Selection, ch: char, pairs: &AutoPairs) -> Option<Transaction> { pub fn hook(
doc: &Rope,
selection: &Selection,
ch: char,
pairs: &AutoPairs,
auto_pairs_overtype: bool,
) -> Option<Transaction> {
log::trace!("autopairs hook selection: {:#?}", selection); log::trace!("autopairs hook selection: {:#?}", selection);
if let Some(pair) = pairs.get(ch) { 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)); return Some(handle_open(doc, selection, pair));
} else if pair.close == ch { } else if pair.close == ch {
// && char_at pos == close // && 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 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 end_ranges = SmallVec::with_capacity(selection.len());
let mut offs = 0; 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 cursor = start_range.cursor(doc.slice(..));
let next_char = doc.get_char(cursor); let next_char = doc.get_char(cursor);
let mut len_inserted = 0; 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 // return transaction that moves past close
(cursor, cursor, None) // no-op (cursor, cursor, None) // no-op
} else { } else {

@ -3847,10 +3847,11 @@ pub mod insert {
let text = doc.text(); let text = doc.text();
let selection = doc.selection(view.id); let selection = doc.selection(view.id);
let auto_pairs = doc.auto_pairs(cx.editor); let auto_pairs = doc.auto_pairs(cx.editor);
let auto_pairs_overtype = cx.editor.config().auto_pairs_overtype;
let transaction = auto_pairs let transaction = auto_pairs
.as_ref() .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)); .or_else(|| insert(text, selection, c));
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);

@ -265,6 +265,8 @@ pub struct Config {
/// etc. Optionally, this can be a list of 2-tuples to specify a /// etc. Optionally, this can be a list of 2-tuples to specify a
/// global list of characters to pair. Defaults to true. /// global list of characters to pair. Defaults to true.
pub auto_pairs: AutoPairConfig, 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. /// Automatic auto-completion, automatically pop up without user trigger. Defaults to true.
pub auto_completion: bool, pub auto_completion: bool,
/// Automatic formatting on save. Defaults to true. /// Automatic formatting on save. Defaults to true.
@ -946,6 +948,7 @@ impl Default for Config {
gutters: GutterConfig::default(), gutters: GutterConfig::default(),
middle_click_paste: true, middle_click_paste: true,
auto_pairs: AutoPairConfig::default(), auto_pairs: AutoPairConfig::default(),
auto_pairs_overtype: true,
auto_completion: true, auto_completion: true,
auto_format: true, auto_format: true,
auto_save: AutoSave::default(), auto_save: AutoSave::default(),

Loading…
Cancel
Save