🎨 Rename and simplify some autoindent stuff
This commit is contained in:
parent
7a26fa18c7
commit
868c460620
10 changed files with 112 additions and 94 deletions
|
@ -1476,7 +1476,7 @@ impl Editor {
|
|||
T: Into<Arc<str>>,
|
||||
{
|
||||
self.buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit(edits, Some(AutoindentMode::Independent), cx)
|
||||
buffer.edit(edits, Some(AutoindentMode::EachLine), cx)
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -1927,7 +1927,7 @@ impl Editor {
|
|||
old_selections
|
||||
.iter()
|
||||
.map(|s| (s.start..s.end, text.clone())),
|
||||
Some(AutoindentMode::Independent),
|
||||
Some(AutoindentMode::EachLine),
|
||||
cx,
|
||||
);
|
||||
anchors
|
||||
|
@ -2369,7 +2369,7 @@ impl Editor {
|
|||
this.buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit(
|
||||
ranges.iter().map(|range| (range.clone(), text)),
|
||||
Some(AutoindentMode::Independent),
|
||||
Some(AutoindentMode::EachLine),
|
||||
cx,
|
||||
);
|
||||
});
|
||||
|
@ -2737,7 +2737,7 @@ impl Editor {
|
|||
.iter()
|
||||
.cloned()
|
||||
.map(|range| (range, snippet_text.clone())),
|
||||
Some(AutoindentMode::Independent),
|
||||
Some(AutoindentMode::EachLine),
|
||||
cx,
|
||||
);
|
||||
|
||||
|
@ -3513,10 +3513,7 @@ impl Editor {
|
|||
clipboard_selections.push(ClipboardSelection {
|
||||
len,
|
||||
is_entire_line,
|
||||
first_line_indent: cmp::min(
|
||||
selection.start.column,
|
||||
buffer.indent_size_for_line(selection.start.row).len,
|
||||
),
|
||||
first_line_indent: buffer.indent_size_for_line(selection.start.row).len,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -3554,10 +3551,7 @@ impl Editor {
|
|||
clipboard_selections.push(ClipboardSelection {
|
||||
len,
|
||||
is_entire_line,
|
||||
first_line_indent: cmp::min(
|
||||
start.column,
|
||||
buffer.indent_size_for_line(start.row).len,
|
||||
),
|
||||
first_line_indent: buffer.indent_size_for_line(start.row).len,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -3626,7 +3620,13 @@ impl Editor {
|
|||
start_columns.push(start_column);
|
||||
}
|
||||
drop(snapshot);
|
||||
buffer.edit(edits, Some(AutoindentMode::Block { start_columns }), cx);
|
||||
buffer.edit(
|
||||
edits,
|
||||
Some(AutoindentMode::Block {
|
||||
original_indent_columns: start_columns,
|
||||
}),
|
||||
cx,
|
||||
);
|
||||
});
|
||||
|
||||
let selections = this.selections.all::<usize>(cx);
|
||||
|
|
|
@ -331,8 +331,10 @@ impl MultiBuffer {
|
|||
});
|
||||
}
|
||||
|
||||
let indent_start_columns = match &mut autoindent_mode {
|
||||
Some(AutoindentMode::Block { start_columns }) => mem::take(start_columns),
|
||||
let original_indent_columns = match &mut autoindent_mode {
|
||||
Some(AutoindentMode::Block {
|
||||
original_indent_columns,
|
||||
}) => mem::take(original_indent_columns),
|
||||
_ => Default::default(),
|
||||
};
|
||||
|
||||
|
@ -341,7 +343,7 @@ impl MultiBuffer {
|
|||
let mut cursor = snapshot.excerpts.cursor::<usize>();
|
||||
for (ix, (range, new_text)) in edits.enumerate() {
|
||||
let new_text: Arc<str> = new_text.into();
|
||||
let start_column = indent_start_columns.get(ix).copied().unwrap_or(0);
|
||||
let original_indent_column = original_indent_columns.get(ix).copied().unwrap_or(0);
|
||||
cursor.seek(&range.start, Bias::Right, &());
|
||||
if cursor.item().is_none() && range.start == *cursor.start() {
|
||||
cursor.prev(&());
|
||||
|
@ -372,7 +374,12 @@ impl MultiBuffer {
|
|||
buffer_edits
|
||||
.entry(start_excerpt.buffer_id)
|
||||
.or_insert(Vec::new())
|
||||
.push((buffer_start..buffer_end, new_text, true, start_column));
|
||||
.push((
|
||||
buffer_start..buffer_end,
|
||||
new_text,
|
||||
true,
|
||||
original_indent_column,
|
||||
));
|
||||
} else {
|
||||
let start_excerpt_range = buffer_start
|
||||
..start_excerpt
|
||||
|
@ -389,11 +396,21 @@ impl MultiBuffer {
|
|||
buffer_edits
|
||||
.entry(start_excerpt.buffer_id)
|
||||
.or_insert(Vec::new())
|
||||
.push((start_excerpt_range, new_text.clone(), true, start_column));
|
||||
.push((
|
||||
start_excerpt_range,
|
||||
new_text.clone(),
|
||||
true,
|
||||
original_indent_column,
|
||||
));
|
||||
buffer_edits
|
||||
.entry(end_excerpt.buffer_id)
|
||||
.or_insert(Vec::new())
|
||||
.push((end_excerpt_range, new_text.clone(), false, start_column));
|
||||
.push((
|
||||
end_excerpt_range,
|
||||
new_text.clone(),
|
||||
false,
|
||||
original_indent_column,
|
||||
));
|
||||
|
||||
cursor.seek(&range.start, Bias::Right, &());
|
||||
cursor.next(&());
|
||||
|
@ -408,7 +425,7 @@ impl MultiBuffer {
|
|||
excerpt.range.context.to_offset(&excerpt.buffer),
|
||||
new_text.clone(),
|
||||
false,
|
||||
start_column,
|
||||
original_indent_column,
|
||||
));
|
||||
cursor.next(&());
|
||||
}
|
||||
|
@ -422,11 +439,15 @@ impl MultiBuffer {
|
|||
.update(cx, |buffer, cx| {
|
||||
let mut edits = edits.into_iter().peekable();
|
||||
let mut insertions = Vec::new();
|
||||
let mut insertion_start_columns = Vec::new();
|
||||
let mut original_indent_columns = Vec::new();
|
||||
let mut deletions = Vec::new();
|
||||
let empty_str: Arc<str> = "".into();
|
||||
while let Some((mut range, new_text, mut is_insertion, start_column)) =
|
||||
edits.next()
|
||||
while let Some((
|
||||
mut range,
|
||||
new_text,
|
||||
mut is_insertion,
|
||||
original_indent_column,
|
||||
)) = edits.next()
|
||||
{
|
||||
while let Some((next_range, _, next_is_insertion, _)) = edits.peek() {
|
||||
if range.end >= next_range.start {
|
||||
|
@ -439,7 +460,7 @@ impl MultiBuffer {
|
|||
}
|
||||
|
||||
if is_insertion {
|
||||
insertion_start_columns.push(start_column);
|
||||
original_indent_columns.push(original_indent_column);
|
||||
insertions.push((
|
||||
buffer.anchor_before(range.start)..buffer.anchor_before(range.end),
|
||||
new_text.clone(),
|
||||
|
@ -455,7 +476,7 @@ impl MultiBuffer {
|
|||
let deletion_autoindent_mode =
|
||||
if let Some(AutoindentMode::Block { .. }) = autoindent_mode {
|
||||
Some(AutoindentMode::Block {
|
||||
start_columns: Default::default(),
|
||||
original_indent_columns: Default::default(),
|
||||
})
|
||||
} else {
|
||||
None
|
||||
|
@ -463,7 +484,7 @@ impl MultiBuffer {
|
|||
let insertion_autoindent_mode =
|
||||
if let Some(AutoindentMode::Block { .. }) = autoindent_mode {
|
||||
Some(AutoindentMode::Block {
|
||||
start_columns: insertion_start_columns,
|
||||
original_indent_columns,
|
||||
})
|
||||
} else {
|
||||
None
|
||||
|
|
|
@ -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,10 +912,12 @@ 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)| {
|
||||
for (row_range, original_indent_column) in
|
||||
row_ranges
|
||||
.into_iter()
|
||||
.filter_map(|(range, original_indent_column)| {
|
||||
if range.len() > 1 {
|
||||
Some((range, start_column?))
|
||||
Some((range, original_indent_column?))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@ -918,7 +927,7 @@ impl Buffer {
|
|||
.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!(
|
||||
|
|
|
@ -278,7 +278,7 @@ fn paste(_: &mut Workspace, _: &Paste, cx: &mut ViewContext<Workspace>) {
|
|||
}
|
||||
}
|
||||
drop(snapshot);
|
||||
buffer.edit(edits, Some(AutoindentMode::Independent), cx);
|
||||
buffer.edit(edits, Some(AutoindentMode::EachLine), cx);
|
||||
});
|
||||
|
||||
editor.change_selections(Some(Autoscroll::Fit), cx, |s| {
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
use editor::{ClipboardSelection, Editor};
|
||||
use gpui::{ClipboardItem, MutableAppContext};
|
||||
use std::cmp;
|
||||
|
||||
pub fn copy_selections_content(editor: &mut Editor, linewise: bool, cx: &mut MutableAppContext) {
|
||||
let selections = editor.selections.all_adjusted(cx);
|
||||
|
@ -18,10 +17,7 @@ pub fn copy_selections_content(editor: &mut Editor, linewise: bool, cx: &mut Mut
|
|||
clipboard_selections.push(ClipboardSelection {
|
||||
len: text.len() - initial_len,
|
||||
is_entire_line: linewise,
|
||||
first_line_indent: cmp::min(
|
||||
start.column,
|
||||
buffer.indent_size_for_line(start.row).len,
|
||||
),
|
||||
first_line_indent: buffer.indent_size_for_line(start.row).len,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -254,7 +254,7 @@ pub fn paste(_: &mut Workspace, _: &VisualPaste, cx: &mut ViewContext<Workspace>
|
|||
}
|
||||
}
|
||||
drop(snapshot);
|
||||
buffer.edit(edits, Some(AutoindentMode::Independent), cx);
|
||||
buffer.edit(edits, Some(AutoindentMode::EachLine), cx);
|
||||
});
|
||||
|
||||
editor.change_selections(Some(Autoscroll::Fit), cx, |s| {
|
||||
|
|
|
@ -269,21 +269,17 @@ mod tests {
|
|||
|
||||
// indent inside braces
|
||||
let ix = buffer.len() - 1;
|
||||
buffer.edit([(ix..ix, "\n\n")], Some(AutoindentMode::Independent), cx);
|
||||
buffer.edit([(ix..ix, "\n\n")], Some(AutoindentMode::EachLine), cx);
|
||||
assert_eq!(buffer.text(), "int main() {\n \n}");
|
||||
|
||||
// indent body of single-statement if statement
|
||||
let ix = buffer.len() - 2;
|
||||
buffer.edit(
|
||||
[(ix..ix, "if (a)\nb;")],
|
||||
Some(AutoindentMode::Independent),
|
||||
cx,
|
||||
);
|
||||
buffer.edit([(ix..ix, "if (a)\nb;")], Some(AutoindentMode::EachLine), cx);
|
||||
assert_eq!(buffer.text(), "int main() {\n if (a)\n b;\n}");
|
||||
|
||||
// indent inside field expression
|
||||
let ix = buffer.len() - 3;
|
||||
buffer.edit([(ix..ix, "\n.c")], Some(AutoindentMode::Independent), cx);
|
||||
buffer.edit([(ix..ix, "\n.c")], Some(AutoindentMode::EachLine), cx);
|
||||
assert_eq!(buffer.text(), "int main() {\n if (a)\n b\n .c;\n}");
|
||||
|
||||
buffer
|
||||
|
|
|
@ -163,7 +163,7 @@ mod tests {
|
|||
let mut buffer = Buffer::new(0, "", cx).with_language(Arc::new(language), cx);
|
||||
let append = |buffer: &mut Buffer, text: &str, cx: &mut ModelContext<Buffer>| {
|
||||
let ix = buffer.len();
|
||||
buffer.edit([(ix..ix, text)], Some(AutoindentMode::Independent), cx);
|
||||
buffer.edit([(ix..ix, text)], Some(AutoindentMode::EachLine), cx);
|
||||
};
|
||||
|
||||
// indent after "def():"
|
||||
|
@ -209,7 +209,7 @@ mod tests {
|
|||
let argument_ix = buffer.text().find("1").unwrap();
|
||||
buffer.edit(
|
||||
[(argument_ix..argument_ix + 1, "")],
|
||||
Some(AutoindentMode::Independent),
|
||||
Some(AutoindentMode::EachLine),
|
||||
cx,
|
||||
);
|
||||
assert_eq!(
|
||||
|
@ -228,7 +228,7 @@ mod tests {
|
|||
let end_whitespace_ix = buffer.len() - 4;
|
||||
buffer.edit(
|
||||
[(end_whitespace_ix..buffer.len(), "")],
|
||||
Some(AutoindentMode::Independent),
|
||||
Some(AutoindentMode::EachLine),
|
||||
cx,
|
||||
);
|
||||
assert_eq!(
|
||||
|
|
|
@ -444,29 +444,29 @@ mod tests {
|
|||
// indent between braces
|
||||
buffer.set_text("fn a() {}", cx);
|
||||
let ix = buffer.len() - 1;
|
||||
buffer.edit([(ix..ix, "\n\n")], Some(AutoindentMode::Independent), cx);
|
||||
buffer.edit([(ix..ix, "\n\n")], Some(AutoindentMode::EachLine), cx);
|
||||
assert_eq!(buffer.text(), "fn a() {\n \n}");
|
||||
|
||||
// indent between braces, even after empty lines
|
||||
buffer.set_text("fn a() {\n\n\n}", cx);
|
||||
let ix = buffer.len() - 2;
|
||||
buffer.edit([(ix..ix, "\n")], Some(AutoindentMode::Independent), cx);
|
||||
buffer.edit([(ix..ix, "\n")], Some(AutoindentMode::EachLine), cx);
|
||||
assert_eq!(buffer.text(), "fn a() {\n\n\n \n}");
|
||||
|
||||
// indent a line that continues a field expression
|
||||
buffer.set_text("fn a() {\n \n}", cx);
|
||||
let ix = buffer.len() - 2;
|
||||
buffer.edit([(ix..ix, "b\n.c")], Some(AutoindentMode::Independent), cx);
|
||||
buffer.edit([(ix..ix, "b\n.c")], Some(AutoindentMode::EachLine), cx);
|
||||
assert_eq!(buffer.text(), "fn a() {\n b\n .c\n}");
|
||||
|
||||
// indent further lines that continue the field expression, even after empty lines
|
||||
let ix = buffer.len() - 2;
|
||||
buffer.edit([(ix..ix, "\n\n.d")], Some(AutoindentMode::Independent), cx);
|
||||
buffer.edit([(ix..ix, "\n\n.d")], Some(AutoindentMode::EachLine), cx);
|
||||
assert_eq!(buffer.text(), "fn a() {\n b\n .c\n \n .d\n}");
|
||||
|
||||
// dedent the line after the field expression
|
||||
let ix = buffer.len() - 2;
|
||||
buffer.edit([(ix..ix, ";\ne")], Some(AutoindentMode::Independent), cx);
|
||||
buffer.edit([(ix..ix, ";\ne")], Some(AutoindentMode::EachLine), cx);
|
||||
assert_eq!(
|
||||
buffer.text(),
|
||||
"fn a() {\n b\n .c\n \n .d;\n e\n}"
|
||||
|
@ -475,21 +475,17 @@ mod tests {
|
|||
// indent inside a struct within a call
|
||||
buffer.set_text("const a: B = c(D {});", cx);
|
||||
let ix = buffer.len() - 3;
|
||||
buffer.edit([(ix..ix, "\n\n")], Some(AutoindentMode::Independent), cx);
|
||||
buffer.edit([(ix..ix, "\n\n")], Some(AutoindentMode::EachLine), cx);
|
||||
assert_eq!(buffer.text(), "const a: B = c(D {\n \n});");
|
||||
|
||||
// indent further inside a nested call
|
||||
let ix = buffer.len() - 4;
|
||||
buffer.edit(
|
||||
[(ix..ix, "e: f(\n\n)")],
|
||||
Some(AutoindentMode::Independent),
|
||||
cx,
|
||||
);
|
||||
buffer.edit([(ix..ix, "e: f(\n\n)")], Some(AutoindentMode::EachLine), cx);
|
||||
assert_eq!(buffer.text(), "const a: B = c(D {\n e: f(\n \n )\n});");
|
||||
|
||||
// keep that indent after an empty line
|
||||
let ix = buffer.len() - 8;
|
||||
buffer.edit([(ix..ix, "\n")], Some(AutoindentMode::Independent), cx);
|
||||
buffer.edit([(ix..ix, "\n")], Some(AutoindentMode::EachLine), cx);
|
||||
assert_eq!(
|
||||
buffer.text(),
|
||||
"const a: B = c(D {\n e: f(\n \n \n )\n});"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue