on master help sign in
Auto pair-removal (#2940)

* auto pair-removal Fixes https://github.com/helix-editor/helix/issues/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 <>

commit52bb1103f8137e5f7c42de6f79c13e2bfbcf7490 authorHoukime <32805122+Houkime@users.noreply.github.com> dateFri 22 Jul 2022, 01:23 (4 years ago) parent19b7864
1 file changed+33−7
helix-term/src/commands.rs+33−7
helix-term/src/commands.rs+33−7
@@ -3056,14 +3056,18 @@ pub mod insert {
30563056
30573057 pub fn delete_char_backward(cx: &mut Context) {
30583058 let count = cx.count();
3059− let (view, doc) = current!(cx.editor);
3059+ let (view, doc) = current_ref!(cx.editor);
30603060 let text = doc.text().slice(..);
30613061 let indent_unit = doc.indent_unit();
30623062 let tab_size = doc.tab_width();
3063+ let auto_pairs = doc.auto_pairs(cx.editor);
30633064
30643065 let transaction =
30653066 Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
30663067 let pos = range.cursor(text);
3068+ if pos == 0 {
3069+ return (pos, pos, None);
3070+ }
30673071 let line_start_pos = text.line_to_char(range.cursor_line(text));
30683072 // consider to delete by indent level if all characters before `pos` are indent units.
30693073 let fragment = Cow::from(text.slice(line_start_pos..pos));
@@ -3111,14 +3115,36 @@ pub mod insert {
31113115 (start, pos, None) // delete!
31123116 }
31133117 } else {
3114− // delete char
3115− (
3116− graphemes::nth_prev_grapheme_boundary(text, pos, count),
3117− pos,
3118− None,
3119− )
3118+ match (
3119+ text.get_char(pos.saturating_sub(1)),
3120+ text.get_char(pos),
3121+ auto_pairs,
3122+ ) {
3123+ (Some(_x), Some(_y), Some(ap))
3124+ if range.is_single_grapheme(text)
3125+ && ap.get(_x).is_some()
3126+ && ap.get(_x).unwrap().close == _y =>
3127+ // delete both autopaired characters
3128+ {
3129+ (
3130+ graphemes::nth_prev_grapheme_boundary(text, pos, count),
3131+ graphemes::nth_next_grapheme_boundary(text, pos, count),
3132+ None,
3133+ )
3134+ }
3135+ _ =>
3136+ // delete 1 char
3137+ {
3138+ (
3139+ graphemes::nth_prev_grapheme_boundary(text, pos, count),
3140+ pos,
3141+ None,
3142+ )
3143+ }
3144+ }
31203145 }
31213146 });
3147+ let (view, doc) = current!(cx.editor);
31223148 doc.apply(&transaction, view.id);
31233149
31243150 lsp::signature_help_impl(cx, SignatureHelpInvoked::Automatic);