🎨 Rename and simplify some autoindent stuff
This commit is contained in:
parent
7a26fa18c7
commit
868c460620
10 changed files with 112 additions and 94 deletions
|
@ -231,8 +231,15 @@ struct SyntaxTree {
|
|||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum AutoindentMode {
|
||||
Block { start_columns: Vec<u32> },
|
||||
Independent,
|
||||
/// Indent each line of inserted text.
|
||||
EachLine,
|
||||
/// Apply the same indentation adjustment to all of the lines
|
||||
/// in a given insertion.
|
||||
Block {
|
||||
/// The original indentation level of the first line of each
|
||||
/// insertion, if it has been copied.
|
||||
original_indent_columns: Vec<u32>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
|
@ -252,7 +259,7 @@ struct AutoindentRequestEntry {
|
|||
/// only be adjusted if the suggested indentation level has *changed*
|
||||
/// since the edit was made.
|
||||
first_line_is_new: bool,
|
||||
start_column: Option<u32>,
|
||||
original_indent_column: Option<u32>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -827,7 +834,7 @@ impl Buffer {
|
|||
let old_row = position.to_point(&request.before_edit).row;
|
||||
old_to_new_rows.insert(old_row, new_row);
|
||||
}
|
||||
row_ranges.push((new_row..new_end_row, entry.start_column));
|
||||
row_ranges.push((new_row..new_end_row, entry.original_indent_column));
|
||||
}
|
||||
|
||||
// Build a map containing the suggested indentation for each of the edited lines
|
||||
|
@ -905,20 +912,22 @@ impl Buffer {
|
|||
// For each block of inserted text, adjust the indentation of the remaining
|
||||
// lines of the block by the same amount as the first line was adjusted.
|
||||
if request.is_block_mode {
|
||||
for (row_range, start_column) in
|
||||
row_ranges.into_iter().filter_map(|(range, start_column)| {
|
||||
if range.len() > 1 {
|
||||
Some((range, start_column?))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
for (row_range, original_indent_column) in
|
||||
row_ranges
|
||||
.into_iter()
|
||||
.filter_map(|(range, original_indent_column)| {
|
||||
if range.len() > 1 {
|
||||
Some((range, original_indent_column?))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
{
|
||||
let new_indent = indent_sizes
|
||||
.get(&row_range.start)
|
||||
.copied()
|
||||
.unwrap_or_else(|| snapshot.indent_size_for_line(row_range.start));
|
||||
let delta = new_indent.len as i64 - start_column as i64;
|
||||
let delta = new_indent.len as i64 - original_indent_column as i64;
|
||||
if delta != 0 {
|
||||
for row in row_range.skip(1) {
|
||||
indent_sizes.entry(row).or_insert_with(|| {
|
||||
|
@ -1224,8 +1233,10 @@ impl Buffer {
|
|||
IndentSize::spaces(settings.tab_size(language_name.as_deref()).get())
|
||||
};
|
||||
let (start_columns, is_block_mode) = match mode {
|
||||
AutoindentMode::Block { start_columns } => (start_columns, true),
|
||||
AutoindentMode::Independent => (Default::default(), false),
|
||||
AutoindentMode::Block {
|
||||
original_indent_columns: start_columns,
|
||||
} => (start_columns, true),
|
||||
AutoindentMode::EachLine => (Default::default(), false),
|
||||
};
|
||||
|
||||
let mut delta = 0isize;
|
||||
|
@ -1260,9 +1271,7 @@ impl Buffer {
|
|||
|
||||
// Avoid auto-indenting before the insertion.
|
||||
if is_block_mode {
|
||||
start_column = start_columns
|
||||
.get(ix)
|
||||
.map(|start| start + indent_size_for_text(new_text.chars()).len);
|
||||
start_column = start_columns.get(ix).copied();
|
||||
if new_text[range_of_insertion_to_indent.clone()].ends_with('\n') {
|
||||
range_of_insertion_to_indent.end -= 1;
|
||||
}
|
||||
|
@ -1270,7 +1279,7 @@ impl Buffer {
|
|||
|
||||
AutoindentRequestEntry {
|
||||
first_line_is_new,
|
||||
start_column,
|
||||
original_indent_column: start_column,
|
||||
range: self.anchor_before(new_start + range_of_insertion_to_indent.start)
|
||||
..self.anchor_after(new_start + range_of_insertion_to_indent.end),
|
||||
}
|
||||
|
|
|
@ -35,7 +35,7 @@ fn test_line_endings(cx: &mut gpui::MutableAppContext) {
|
|||
buffer.check_invariants();
|
||||
buffer.edit(
|
||||
[(buffer.len()..buffer.len(), "\r\nfour")],
|
||||
Some(AutoindentMode::Independent),
|
||||
Some(AutoindentMode::EachLine),
|
||||
cx,
|
||||
);
|
||||
buffer.edit([(0..0, "zero\r\n")], None, cx);
|
||||
|
@ -630,12 +630,12 @@ fn test_autoindent_with_soft_tabs(cx: &mut MutableAppContext) {
|
|||
let text = "fn a() {}";
|
||||
let mut buffer = Buffer::new(0, text, cx).with_language(Arc::new(rust_lang()), cx);
|
||||
|
||||
buffer.edit([(8..8, "\n\n")], Some(AutoindentMode::Independent), cx);
|
||||
buffer.edit([(8..8, "\n\n")], Some(AutoindentMode::EachLine), cx);
|
||||
assert_eq!(buffer.text(), "fn a() {\n \n}");
|
||||
|
||||
buffer.edit(
|
||||
[(Point::new(1, 4)..Point::new(1, 4), "b()\n")],
|
||||
Some(AutoindentMode::Independent),
|
||||
Some(AutoindentMode::EachLine),
|
||||
cx,
|
||||
);
|
||||
assert_eq!(buffer.text(), "fn a() {\n b()\n \n}");
|
||||
|
@ -644,7 +644,7 @@ fn test_autoindent_with_soft_tabs(cx: &mut MutableAppContext) {
|
|||
// to be indented.
|
||||
buffer.edit(
|
||||
[(Point::new(2, 4)..Point::new(2, 4), ".c")],
|
||||
Some(AutoindentMode::Independent),
|
||||
Some(AutoindentMode::EachLine),
|
||||
cx,
|
||||
);
|
||||
assert_eq!(buffer.text(), "fn a() {\n b()\n .c\n}");
|
||||
|
@ -653,7 +653,7 @@ fn test_autoindent_with_soft_tabs(cx: &mut MutableAppContext) {
|
|||
// causing the line to be outdented.
|
||||
buffer.edit(
|
||||
[(Point::new(2, 8)..Point::new(2, 9), "")],
|
||||
Some(AutoindentMode::Independent),
|
||||
Some(AutoindentMode::EachLine),
|
||||
cx,
|
||||
);
|
||||
assert_eq!(buffer.text(), "fn a() {\n b()\n c\n}");
|
||||
|
@ -672,12 +672,12 @@ fn test_autoindent_with_hard_tabs(cx: &mut MutableAppContext) {
|
|||
let text = "fn a() {}";
|
||||
let mut buffer = Buffer::new(0, text, cx).with_language(Arc::new(rust_lang()), cx);
|
||||
|
||||
buffer.edit([(8..8, "\n\n")], Some(AutoindentMode::Independent), cx);
|
||||
buffer.edit([(8..8, "\n\n")], Some(AutoindentMode::EachLine), cx);
|
||||
assert_eq!(buffer.text(), "fn a() {\n\t\n}");
|
||||
|
||||
buffer.edit(
|
||||
[(Point::new(1, 1)..Point::new(1, 1), "b()\n")],
|
||||
Some(AutoindentMode::Independent),
|
||||
Some(AutoindentMode::EachLine),
|
||||
cx,
|
||||
);
|
||||
assert_eq!(buffer.text(), "fn a() {\n\tb()\n\t\n}");
|
||||
|
@ -686,7 +686,7 @@ fn test_autoindent_with_hard_tabs(cx: &mut MutableAppContext) {
|
|||
// to be indented.
|
||||
buffer.edit(
|
||||
[(Point::new(2, 1)..Point::new(2, 1), ".c")],
|
||||
Some(AutoindentMode::Independent),
|
||||
Some(AutoindentMode::EachLine),
|
||||
cx,
|
||||
);
|
||||
assert_eq!(buffer.text(), "fn a() {\n\tb()\n\t\t.c\n}");
|
||||
|
@ -695,7 +695,7 @@ fn test_autoindent_with_hard_tabs(cx: &mut MutableAppContext) {
|
|||
// causing the line to be outdented.
|
||||
buffer.edit(
|
||||
[(Point::new(2, 2)..Point::new(2, 3), "")],
|
||||
Some(AutoindentMode::Independent),
|
||||
Some(AutoindentMode::EachLine),
|
||||
cx,
|
||||
);
|
||||
assert_eq!(buffer.text(), "fn a() {\n\tb()\n\tc\n}");
|
||||
|
@ -727,7 +727,7 @@ fn test_autoindent_does_not_adjust_lines_with_unchanged_suggestion(cx: &mut Muta
|
|||
(empty(Point::new(1, 1)), "()"),
|
||||
(empty(Point::new(2, 1)), "()"),
|
||||
],
|
||||
Some(AutoindentMode::Independent),
|
||||
Some(AutoindentMode::EachLine),
|
||||
cx,
|
||||
);
|
||||
assert_eq!(
|
||||
|
@ -748,7 +748,7 @@ fn test_autoindent_does_not_adjust_lines_with_unchanged_suggestion(cx: &mut Muta
|
|||
(empty(Point::new(1, 1)), "\n.f\n.g"),
|
||||
(empty(Point::new(2, 1)), "\n.f\n.g"),
|
||||
],
|
||||
Some(AutoindentMode::Independent),
|
||||
Some(AutoindentMode::EachLine),
|
||||
cx,
|
||||
);
|
||||
assert_eq!(
|
||||
|
@ -783,7 +783,7 @@ fn test_autoindent_does_not_adjust_lines_with_unchanged_suggestion(cx: &mut Muta
|
|||
// Delete a closing curly brace changes the suggested indent for the line.
|
||||
buffer.edit(
|
||||
[(Point::new(3, 4)..Point::new(3, 5), "")],
|
||||
Some(AutoindentMode::Independent),
|
||||
Some(AutoindentMode::EachLine),
|
||||
cx,
|
||||
);
|
||||
assert_eq!(
|
||||
|
@ -803,7 +803,7 @@ fn test_autoindent_does_not_adjust_lines_with_unchanged_suggestion(cx: &mut Muta
|
|||
// Manually editing the leading whitespace
|
||||
buffer.edit(
|
||||
[(Point::new(3, 0)..Point::new(3, 12), "")],
|
||||
Some(AutoindentMode::Independent),
|
||||
Some(AutoindentMode::EachLine),
|
||||
cx,
|
||||
);
|
||||
assert_eq!(
|
||||
|
@ -833,7 +833,7 @@ fn test_autoindent_adjusts_lines_when_only_text_changes(cx: &mut MutableAppConte
|
|||
|
||||
let mut buffer = Buffer::new(0, text, cx).with_language(Arc::new(rust_lang()), cx);
|
||||
|
||||
buffer.edit([(5..5, "\nb")], Some(AutoindentMode::Independent), cx);
|
||||
buffer.edit([(5..5, "\nb")], Some(AutoindentMode::EachLine), cx);
|
||||
assert_eq!(
|
||||
buffer.text(),
|
||||
"
|
||||
|
@ -847,7 +847,7 @@ fn test_autoindent_adjusts_lines_when_only_text_changes(cx: &mut MutableAppConte
|
|||
// is now at the beginning of the line.
|
||||
buffer.edit(
|
||||
[(Point::new(1, 4)..Point::new(1, 5), "")],
|
||||
Some(AutoindentMode::Independent),
|
||||
Some(AutoindentMode::EachLine),
|
||||
cx,
|
||||
);
|
||||
assert_eq!(
|
||||
|
@ -871,7 +871,7 @@ fn test_autoindent_with_edit_at_end_of_buffer(cx: &mut MutableAppContext) {
|
|||
let mut buffer = Buffer::new(0, text, cx).with_language(Arc::new(rust_lang()), cx);
|
||||
buffer.edit(
|
||||
[(0..1, "\n"), (2..3, "\n")],
|
||||
Some(AutoindentMode::Independent),
|
||||
Some(AutoindentMode::EachLine),
|
||||
cx,
|
||||
);
|
||||
assert_eq!(buffer.text(), "\n\n\n");
|
||||
|
@ -896,7 +896,7 @@ fn test_autoindent_multi_line_insertion(cx: &mut MutableAppContext) {
|
|||
let mut buffer = Buffer::new(0, text, cx).with_language(Arc::new(rust_lang()), cx);
|
||||
buffer.edit(
|
||||
[(Point::new(3, 0)..Point::new(3, 0), "e(\n f()\n);\n")],
|
||||
Some(AutoindentMode::Independent),
|
||||
Some(AutoindentMode::EachLine),
|
||||
cx,
|
||||
);
|
||||
assert_eq!(
|
||||
|
@ -945,7 +945,7 @@ fn test_autoindent_block_mode(cx: &mut MutableAppContext) {
|
|||
buffer.edit(
|
||||
[(Point::new(2, 0)..Point::new(2, 0), inserted_text.clone())],
|
||||
Some(AutoindentMode::Block {
|
||||
start_columns: vec![0],
|
||||
original_indent_columns: vec![0],
|
||||
}),
|
||||
cx,
|
||||
);
|
||||
|
@ -970,7 +970,7 @@ fn test_autoindent_block_mode(cx: &mut MutableAppContext) {
|
|||
buffer.edit(
|
||||
[(Point::new(2, 8)..Point::new(2, 8), inserted_text.clone())],
|
||||
Some(AutoindentMode::Block {
|
||||
start_columns: vec![0],
|
||||
original_indent_columns: vec![0],
|
||||
}),
|
||||
cx,
|
||||
);
|
||||
|
@ -1017,7 +1017,7 @@ fn test_autoindent_language_without_indents_query(cx: &mut MutableAppContext) {
|
|||
);
|
||||
buffer.edit(
|
||||
[(Point::new(3, 0)..Point::new(3, 0), "\n")],
|
||||
Some(AutoindentMode::Independent),
|
||||
Some(AutoindentMode::EachLine),
|
||||
cx,
|
||||
);
|
||||
assert_eq!(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue