2021-03-22 04:18:48 +01:00
|
|
|
use crate::{Range, Rope, Selection, Tendril, Transaction};
|
|
|
|
use smallvec::SmallVec;
|
|
|
|
|
2021-03-22 09:58:49 +01:00
|
|
|
// Heavily based on https://github.com/codemirror/closebrackets/
|
|
|
|
|
2021-05-06 15:59:59 +02:00
|
|
|
pub const PAIRS: &[(char, char)] = &[
|
2021-03-22 04:18:48 +01:00
|
|
|
('(', ')'),
|
|
|
|
('{', '}'),
|
|
|
|
('[', ']'),
|
|
|
|
('\'', '\''),
|
|
|
|
('"', '"'),
|
|
|
|
('`', '`'),
|
|
|
|
];
|
|
|
|
|
|
|
|
const CLOSE_BEFORE: &str = ")]}'\":;> \n"; // includes space and newline
|
|
|
|
|
|
|
|
// insert hook:
|
|
|
|
// Fn(doc, selection, char) => Option<Transaction>
|
|
|
|
// problem is, we want to do this per range, so we can call default handler for some ranges
|
|
|
|
// so maybe ret Vec<Option<Change>>
|
|
|
|
// but we also need to be able to return transactions...
|
|
|
|
//
|
|
|
|
// to simplify, maybe return Option<Transaction> and just reimplement the default
|
|
|
|
|
|
|
|
// TODO: delete implementation where it erases the whole bracket (|) -> |
|
|
|
|
|
2021-03-24 06:52:13 +01:00
|
|
|
#[must_use]
|
2021-03-22 04:18:48 +01:00
|
|
|
pub fn hook(doc: &Rope, selection: &Selection, ch: char) -> Option<Transaction> {
|
|
|
|
for &(open, close) in PAIRS {
|
|
|
|
if open == ch {
|
2021-03-24 10:17:00 +01:00
|
|
|
if open == close {
|
|
|
|
return handle_same(doc, selection, open);
|
2021-03-22 04:18:48 +01:00
|
|
|
} else {
|
2021-03-24 10:17:00 +01:00
|
|
|
return Some(handle_open(doc, selection, open, close, CLOSE_BEFORE));
|
|
|
|
}
|
2021-03-22 04:18:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if close == ch {
|
|
|
|
// && char_at pos == close
|
|
|
|
return Some(handle_close(doc, selection, open, close));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: special handling for lifetimes in rust: if preceeded by & or < don't auto close '
|
|
|
|
// for example "&'a mut", or "fn<'a>"
|
|
|
|
|
|
|
|
fn next_char(doc: &Rope, pos: usize) -> Option<char> {
|
|
|
|
if pos >= doc.len_chars() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(doc.char(pos))
|
|
|
|
}
|
2021-03-22 10:06:52 +01:00
|
|
|
// TODO: selections should be extended if range, moved if point.
|
2021-03-22 04:18:48 +01:00
|
|
|
|
|
|
|
// TODO: if not cursor but selection, wrap on both sides of selection (surround)
|
|
|
|
fn handle_open(
|
|
|
|
doc: &Rope,
|
|
|
|
selection: &Selection,
|
|
|
|
open: char,
|
|
|
|
close: char,
|
|
|
|
close_before: &str,
|
|
|
|
) -> Transaction {
|
|
|
|
let mut ranges = SmallVec::with_capacity(selection.len());
|
|
|
|
|
2021-05-15 03:26:41 +02:00
|
|
|
let mut offs = 0;
|
|
|
|
|
2021-06-11 14:57:07 +02:00
|
|
|
let transaction = Transaction::change_by_selection(doc, selection, |range| {
|
2021-03-22 04:18:48 +01:00
|
|
|
let pos = range.head;
|
|
|
|
let next = next_char(doc, pos);
|
|
|
|
|
2021-05-15 03:26:41 +02:00
|
|
|
let head = pos + offs + open.len_utf8();
|
2021-04-22 16:26:57 +02:00
|
|
|
// if selection, retain anchor, if cursor, move over
|
|
|
|
ranges.push(Range::new(
|
2021-05-15 03:26:41 +02:00
|
|
|
if range.is_empty() {
|
|
|
|
head
|
|
|
|
} else {
|
|
|
|
range.anchor + offs
|
|
|
|
},
|
2021-04-22 16:26:57 +02:00
|
|
|
head,
|
|
|
|
));
|
2021-03-22 04:18:48 +01:00
|
|
|
|
|
|
|
match next {
|
|
|
|
Some(ch) if !close_before.contains(ch) => {
|
|
|
|
// TODO: else return (use default handler that inserts open)
|
|
|
|
(pos, pos, Some(Tendril::from_char(open)))
|
|
|
|
}
|
|
|
|
// None | Some(ch) if close_before.contains(ch) => {}
|
|
|
|
_ => {
|
|
|
|
// insert open & close
|
|
|
|
let mut pair = Tendril::with_capacity(2);
|
|
|
|
pair.push_char(open);
|
|
|
|
pair.push_char(close);
|
|
|
|
|
2021-05-15 03:26:41 +02:00
|
|
|
offs += 2;
|
|
|
|
|
2021-03-22 04:18:48 +01:00
|
|
|
(pos, pos, Some(pair))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
transaction.with_selection(Selection::new(ranges, selection.primary_index()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_close(doc: &Rope, selection: &Selection, _open: char, close: char) -> Transaction {
|
|
|
|
let mut ranges = SmallVec::with_capacity(selection.len());
|
|
|
|
|
2021-05-15 03:26:41 +02:00
|
|
|
let mut offs = 0;
|
|
|
|
|
2021-06-11 14:57:07 +02:00
|
|
|
let transaction = Transaction::change_by_selection(doc, selection, |range| {
|
2021-03-22 04:18:48 +01:00
|
|
|
let pos = range.head;
|
|
|
|
let next = next_char(doc, pos);
|
|
|
|
|
2021-05-15 03:26:41 +02:00
|
|
|
let head = pos + offs + close.len_utf8();
|
2021-04-22 16:26:57 +02:00
|
|
|
// if selection, retain anchor, if cursor, move over
|
|
|
|
ranges.push(Range::new(
|
2021-05-15 03:26:41 +02:00
|
|
|
if range.is_empty() {
|
|
|
|
head
|
|
|
|
} else {
|
|
|
|
range.anchor + offs
|
|
|
|
},
|
2021-04-22 16:26:57 +02:00
|
|
|
head,
|
|
|
|
));
|
2021-03-22 04:18:48 +01:00
|
|
|
|
|
|
|
if next == Some(close) {
|
|
|
|
// return transaction that moves past close
|
|
|
|
(pos, pos, None) // no-op
|
|
|
|
} else {
|
2021-05-15 03:26:41 +02:00
|
|
|
offs += close.len_utf8();
|
|
|
|
|
2021-03-22 04:18:48 +01:00
|
|
|
// TODO: else return (use default handler that inserts close)
|
|
|
|
(pos, pos, Some(Tendril::from_char(close)))
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
transaction.with_selection(Selection::new(ranges, selection.primary_index()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle cases where open and close is the same, or in triples ("""docstring""")
|
2021-03-24 10:17:00 +01:00
|
|
|
fn handle_same(doc: &Rope, selection: &Selection, token: char) -> Option<Transaction> {
|
2021-03-22 04:18:48 +01:00
|
|
|
// if not cursor but selection, wrap
|
|
|
|
// let next = next char
|
2021-03-24 10:17:00 +01:00
|
|
|
|
|
|
|
// if next == bracket {
|
|
|
|
// // if start of syntax node, insert token twice (new pair because node is complete)
|
|
|
|
// // elseif colsedBracketAt
|
|
|
|
// // is_triple == allow triple && next 3 is equal
|
|
|
|
// // cursor jump over
|
|
|
|
// }
|
|
|
|
//} else if allow_triple && followed by triple {
|
|
|
|
//}
|
|
|
|
//} else if next != word char && prev != bracket && prev != word char {
|
|
|
|
// // condition checks for cases like I' where you don't want I'' (or I'm)
|
|
|
|
// insert pair ("")
|
|
|
|
//}
|
|
|
|
None
|
2021-03-22 04:18:48 +01:00
|
|
|
}
|