editor: indent from cursor position with a single selection (#10073)

Piotr Osiewicz created

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.

Change summary

crates/editor/src/editor.rs | 21 ++++++++++++++++++---
1 file changed, 18 insertions(+), 3 deletions(-)

Detailed changes

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::<String>(),
@@ -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);
                     }
                 }