Auto indent change if selection is linewise (#7316)

pull/6197/head^2
Tudyx 1 year ago committed by GitHub
parent 8afc0282f2
commit 9893a1fbcc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2342,10 +2342,25 @@ enum Operation {
Change, Change,
} }
fn selection_is_linewise(selection: &Selection, text: &Rope) -> bool {
selection.ranges().iter().all(|range| {
let text = text.slice(..);
if range.slice(text).len_lines() < 2 {
return false;
}
// If the start of the selection is at the start of a line and the end at the end of a line.
let (start_line, end_line) = range.line_range(text);
let start = text.line_to_char(start_line);
let end = text.line_to_char((end_line + 1).min(text.len_lines()));
start == range.from() && end == range.to()
})
}
fn delete_selection_impl(cx: &mut Context, op: Operation) { fn delete_selection_impl(cx: &mut Context, op: Operation) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let selection = doc.selection(view.id); let selection = doc.selection(view.id);
let only_whole_lines = selection_is_linewise(selection, doc.text());
if cx.register != Some('_') { if cx.register != Some('_') {
// first yank the selection // first yank the selection
@ -2366,10 +2381,14 @@ fn delete_selection_impl(cx: &mut Context, op: Operation) {
exit_select_mode(cx); exit_select_mode(cx);
} }
Operation::Change => { Operation::Change => {
if only_whole_lines {
open_above(cx);
} else {
enter_insert_mode(cx); enter_insert_mode(cx);
} }
} }
} }
}
#[inline] #[inline]
fn delete_by_selection_insert_mode( fn delete_by_selection_insert_mode(

Loading…
Cancel
Save