Add insert_tab test, deduplicate implementation
@@ -21,29 +21,26 @@ pub fn execute<L: BufferDataListener>(
) -> Option<RopeDelta> {
let indent = buffer.indent_unit();
let mut edits = Vec::new();
+
+ let mut create_edit = |offset| {
+ let indent = if indent.starts_with('\t') {
+ indent.to_string()
+ } else {
+ let (_, col) = buffer.offset_to_line_col(offset, self.tab_width);
+ " ".repeat(indent.len() - col % indent.len())
+ };
+ edits.push((Selection::caret(offset), indent));
+ };
+
for region in self.selection.regions() {
if region.is_caret() {
- if indent.starts_with('\t') {
- edits.push((Selection::caret(region.start), indent.to_string()));
- } else {
- let (_, col) =
- buffer.offset_to_line_col(region.start, self.tab_width);
- let indent = " ".repeat(indent.len() - col % indent.len());
- edits.push((Selection::caret(region.start), indent));
- }
+ create_edit(region.start);
} else {
let start_line = buffer.line_of_offset(region.min());
let end_line = buffer.line_of_offset(region.max());
for line in start_line..end_line + 1 {
let offset = buffer.first_non_blank_character_on_line(line);
- if indent.starts_with('\t') {
- edits.push((Selection::caret(offset), indent.to_string()));
- } else {
- let (_, col) =
- buffer.offset_to_line_col(offset, self.tab_width);
- let indent = " ".repeat(indent.len() - col % indent.len());
- edits.push((Selection::caret(offset), indent));
- }
+ create_edit(offset);
}
}
}
@@ -73,7 +70,6 @@ fn insert_tab_inserts_spaces() {
assert_eq!(" <$0>", editor.state());
}
-
#[test]
fn insert_tab_inserts_at_multiple_places() {
let mut editor = MockEditor::new(
@@ -89,4 +85,24 @@ fn insert_tab_inserts_at_multiple_places() {
editor.state()
);
}
+
+ #[test]
+ fn insert_tab_aligns_to_tab_width() {
+ let mut editor = MockEditor::new(
+ r#"<$0>
+ <$1>
+ <$2>
+ <$3>"#,
+ );
+
+ editor.command(EditCommandKind::InsertTab);
+
+ assert_eq!(
+ r#" <$0>
+ <$1>
+ <$2>
+ <$3>"#,
+ editor.state()
+ );
+ }
}
