From b43602f21b82357018233dd517036aa769756393 Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Tue, 2 Apr 2024 13:33:11 +0200 Subject: [PATCH] editor: indent from cursor position with a single selection (#10073) In 9970 @JosephTLyons noticed that tab + tab_prev action sequence leaves a buffer in the dirty state, whereas "similar" indent-outdent does not. I've tracked it down to the fact that tabs are always inserted at the start of the line, regardless of the cursor position, whereas tab-prevs act from cursor position. This PR adjust tab/tab-prev actions (and indent-outdent) to act from cursor position if possible. That way we can correctly report buffer dirty state for these event sequences. Fixes #9970 Release Notes: - Fixed buffer being marked as dirty when using tab/tab-prev actions. --- crates/editor/src/editor.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index c197e02fe1370ce361ea2d5e72309dd0ddf4b1e7..736ba74f8b0114faab6feb2cdc2fff119b439b49 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -4845,6 +4845,7 @@ impl Editor { } let mut delta_for_end_row = 0; + let has_multiple_rows = start_row + 1 != end_row; for row in start_row..end_row { let current_indent = snapshot.indent_size_for_line(row); let indent_delta = match (current_indent.kind, indent_kind) { @@ -4856,7 +4857,12 @@ impl Editor { (_, IndentKind::Tab) => IndentSize::tab(), }; - let row_start = Point::new(row, 0); + let start = if has_multiple_rows || current_indent.len < selection.start.column { + 0 + } else { + selection.start.column + }; + let row_start = Point::new(row, start); edits.push(( row_start..row_start, indent_delta.chars().collect::(), @@ -4902,7 +4908,7 @@ impl Editor { rows.start += 1; } } - + let has_multiple_rows = rows.len() > 1; for row in rows { let indent_size = snapshot.indent_size_for_line(row); if indent_size.len > 0 { @@ -4917,7 +4923,16 @@ impl Editor { } IndentKind::Tab => 1, }; - deletion_ranges.push(Point::new(row, 0)..Point::new(row, deletion_len)); + let start = if has_multiple_rows + || deletion_len > selection.start.column + || indent_size.len < selection.start.column + { + 0 + } else { + selection.start.column - deletion_len + }; + deletion_ranges + .push(Point::new(row, start)..Point::new(row, start + deletion_len)); last_outdent = Some(row); } }