Skip to content

Commit 7646989

Browse files
authored
Fixed Tab indenting the line instead of adding Tab or spaces (#403)
* Update editor.rs * Makes sure indent_required is calculated according to the selection type removed the redundant check for required_indent == 0, introduced a slightly different path for when no selection = None, and calculated the required tab width and indent position accordingly. * Undo typo undid an accidental move of a redraw call outside of a loop * Corrected indenting with rustfmt formatted the code with rustfmt to pass the failing CI checks
1 parent 85b07b4 commit 7646989

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

src/edit/editor.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -697,22 +697,32 @@ impl<'buffer> Edit<'buffer> for Editor<'buffer> {
697697
self.with_buffer(|buffer| {
698698
let line = &buffer.lines[line_i];
699699
let text = line.text();
700-
// Default to end of line if no non-whitespace found
701-
after_whitespace = text.len();
702-
for (count, (index, c)) in text.char_indices().enumerate() {
703-
if !c.is_whitespace() {
704-
after_whitespace = index;
705-
required_indent = tab_width - (count % tab_width);
706-
break;
700+
701+
if self.selection == Selection::None {
702+
//Selection::None counts whitespace from the cursor backwards
703+
let whitespace_length = match line.text()[0..self.cursor.index]
704+
.chars()
705+
.rev()
706+
.position(|c| !c.is_whitespace())
707+
{
708+
Some(length) => length,
709+
// The whole line is whitespace
710+
None => self.cursor.index,
711+
};
712+
required_indent = tab_width - (whitespace_length % tab_width);
713+
after_whitespace = self.cursor.index;
714+
} else {
715+
// Other selections count whitespace from the start of the line
716+
for (count, (index, c)) in text.char_indices().enumerate() {
717+
if !c.is_whitespace() {
718+
after_whitespace = index;
719+
required_indent = tab_width - (count % tab_width);
720+
break;
721+
}
707722
}
708723
}
709724
});
710725

711-
// No indent required (not possible?)
712-
if required_indent == 0 {
713-
required_indent = tab_width;
714-
}
715-
716726
self.insert_at(
717727
Cursor::new(line_i, after_whitespace),
718728
&" ".repeat(required_indent),
@@ -739,7 +749,6 @@ impl<'buffer> Edit<'buffer> for Editor<'buffer> {
739749
}
740750
}
741751
}
742-
743752
// Request redraw
744753
self.with_buffer_mut(|buffer| buffer.set_redraw(true));
745754
}

0 commit comments

Comments
 (0)