Skip to content

Commit 0a02849

Browse files
Houkimethomasskk
authored andcommitted
Auto pair-removal (helix-editor#2940)
* auto pair-removal Fixes helix-editor#1673 * autopairs removal: use doc autopairs * autopairs-removal: limit to one-char selections * use single_grapheme() to check if range is one char * fix errouneous deletes of " and other symmetric autopairs when at buffer start Co-authored-by: Houkime <>
1 parent a1f85b7 commit 0a02849

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

helix-term/src/commands.rs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3096,14 +3096,18 @@ pub mod insert {
30963096

30973097
pub fn delete_char_backward(cx: &mut Context) {
30983098
let count = cx.count();
3099-
let (view, doc) = current!(cx.editor);
3099+
let (view, doc) = current_ref!(cx.editor);
31003100
let text = doc.text().slice(..);
31013101
let indent_unit = doc.indent_unit();
31023102
let tab_size = doc.tab_width();
3103+
let auto_pairs = doc.auto_pairs(cx.editor);
31033104

31043105
let transaction =
31053106
Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
31063107
let pos = range.cursor(text);
3108+
if pos == 0 {
3109+
return (pos, pos, None);
3110+
}
31073111
let line_start_pos = text.line_to_char(range.cursor_line(text));
31083112
// consider to delete by indent level if all characters before `pos` are indent units.
31093113
let fragment = Cow::from(text.slice(line_start_pos..pos));
@@ -3151,14 +3155,36 @@ pub mod insert {
31513155
(start, pos, None) // delete!
31523156
}
31533157
} else {
3154-
// delete char
3155-
(
3156-
graphemes::nth_prev_grapheme_boundary(text, pos, count),
3157-
pos,
3158-
None,
3159-
)
3158+
match (
3159+
text.get_char(pos.saturating_sub(1)),
3160+
text.get_char(pos),
3161+
auto_pairs,
3162+
) {
3163+
(Some(_x), Some(_y), Some(ap))
3164+
if range.is_single_grapheme(text)
3165+
&& ap.get(_x).is_some()
3166+
&& ap.get(_x).unwrap().close == _y =>
3167+
// delete both autopaired characters
3168+
{
3169+
(
3170+
graphemes::nth_prev_grapheme_boundary(text, pos, count),
3171+
graphemes::nth_next_grapheme_boundary(text, pos, count),
3172+
None,
3173+
)
3174+
}
3175+
_ =>
3176+
// delete 1 char
3177+
{
3178+
(
3179+
graphemes::nth_prev_grapheme_boundary(text, pos, count),
3180+
pos,
3181+
None,
3182+
)
3183+
}
3184+
}
31603185
}
31613186
});
3187+
let (view, doc) = current!(cx.editor);
31623188
doc.apply(&transaction, view.id);
31633189

31643190
lsp::signature_help_impl(cx, SignatureHelpInvoked::Automatic);

0 commit comments

Comments
 (0)