feat: add delete surrounding tag functionality

This commit is contained in:
Nikita Revenco 2024-11-13 21:28:00 +00:00
parent 6e4d022e98
commit 2a61b113de

View File

@ -5768,8 +5768,6 @@ fn surround_replace(cx: &mut Context) {
}), }),
); );
// we don't need to touch this
// document.set_selection(view.id, *copy_selection);
document.apply(&transaction, view.id); document.apply(&transaction, view.id);
context.editor.mode = Mode::Normal; context.editor.mode = Mode::Normal;
}, },
@ -5846,19 +5844,45 @@ fn surround_delete(cx: &mut Context) {
let text = doc.text().slice(..); let text = doc.text().slice(..);
let selection = doc.selection(view.id); let selection = doc.selection(view.id);
let mut change_pos = if surround_ch.is_some_and(|ch| ch == 'x') {
match surround::get_surround_pos(doc.syntax(), text, selection, surround_ch, count) { let change_pos = match surround::get_surround_pos_tag(text, &selection, count) {
Ok(c) => c, Ok(c) => c,
Err(err) => { Err(err) => {
cx.editor.set_error(err.to_string()); cx.editor.set_error(err.to_string());
return; return;
} }
}; };
change_pos.sort_unstable(); // the changeset has to be sorted to allow nested surrounds let transaction = Transaction::change(
let transaction = doc.text(),
Transaction::change(doc.text(), change_pos.into_iter().map(|p| (p, p + 1, None))); change_pos.iter().flat_map(|(pos, _)| {
doc.apply(&transaction, view.id); let opening_range = pos.0;
exit_select_mode(cx); let closing_range = pos.1;
vec![
// add extra numbers to account for "<", ">" and "/" characters
(opening_range.from() - 1, opening_range.to() + 1, None),
(closing_range.from() - 2, closing_range.to() + 1, None),
]
}),
);
doc.apply(&transaction, view.id);
exit_select_mode(cx);
} else {
let mut change_pos =
match surround::get_surround_pos(doc.syntax(), text, selection, surround_ch, count)
{
Ok(c) => c,
Err(err) => {
cx.editor.set_error(err.to_string());
return;
}
};
change_pos.sort_unstable(); // the changeset has to be sorted to allow nested surrounds
let transaction =
Transaction::change(doc.text(), change_pos.into_iter().map(|p| (p, p + 1, None)));
doc.apply(&transaction, view.id);
exit_select_mode(cx);
}
}) })
} }