on master help sign in
Auto indent change if selection is linewise (#7316)
9893a1fTudyx
helix-term/src/commands.rs+20−1
@@ -2342,10 +2342,25 @@ enum Operation {
2342 Change,
2342 Change,
2343}
2343}
2344
2344
2345+fn selection_is_linewise(selection: &Selection, text: &Rope) -> bool {
2346+ selection.ranges().iter().all(|range| {
2347+ let text = text.slice(..);
2348+ if range.slice(text).len_lines() < 2 {
2349+ return false;
2350+ }
2351+ // If the start of the selection is at the start of a line and the end at the end of a line.
2352+ let (start_line, end_line) = range.line_range(text);
2353+ let start = text.line_to_char(start_line);
2354+ let end = text.line_to_char((end_line + 1).min(text.len_lines()));
2355+ start == range.from() && end == range.to()
2356+ })
2357+}
2358+
2345fn delete_selection_impl(cx: &mut Context, op: Operation) {
2359fn delete_selection_impl(cx: &mut Context, op: Operation) {
2346 let (view, doc) = current!(cx.editor);
2360 let (view, doc) = current!(cx.editor);
2347
2361
2348 let selection = doc.selection(view.id);
2362 let selection = doc.selection(view.id);
2363+ let only_whole_lines = selection_is_linewise(selection, doc.text());
2349
2364
2350 if cx.register != Some('_') {
2365 if cx.register != Some('_') {
2351 // first yank the selection
2366 // first yank the selection
@@ -2366,7 +2381,11 @@ fn delete_selection_impl(cx: &mut Context, op: Operation) {
2366 exit_select_mode(cx);
2381 exit_select_mode(cx);
2367 }
2382 }
2368 Operation::Change => {
2383 Operation::Change => {
2384+ if only_whole_lines {
2385+ open_above(cx);
2386+ } else {
2369− enter_insert_mode(cx);
2387+ enter_insert_mode(cx);
2388+ }
2370 }
2389 }
2371 }
2390 }
2372}
2391}