add anchor to selection fixup info in newline

This commit is contained in:
Keith Simmons 2022-04-28 13:10:31 -07:00
parent 4c860dc82f
commit 42b900774e

View file

@ -1861,11 +1861,12 @@ impl Editor {
pub fn newline(&mut self, _: &Newline, cx: &mut ViewContext<Self>) {
self.transact(cx, |this, cx| {
let mut old_selections = SmallVec::<[_; 32]>::new();
{
let (edits, selection_fixup_info): (Vec<_>, Vec<_>) = {
let selections = this.local_selections::<usize>(cx);
let buffer = this.buffer.read(cx).snapshot(cx);
for selection in selections.iter() {
selections
.iter()
.map(|selection| {
let start_point = selection.start.to_point(&buffer);
let indent = buffer
.indent_column_for_line(start_point.row)
@ -1892,7 +1893,8 @@ impl Editor {
let pair_end = pair.end.trim_start();
pair.newline
&& buffer.contains_str_at(end + trailing_whitespace_len, pair_end)
&& buffer
.contains_str_at(end + trailing_whitespace_len, pair_end)
&& buffer.contains_str_at(
(start - leading_whitespace_len)
.saturating_sub(pair_start.len()),
@ -1901,29 +1903,21 @@ impl Editor {
});
}
old_selections.push((
selection.id,
buffer.anchor_after(end),
start..end,
indent,
insert_extra_newline,
));
}
}
this.buffer.update(cx, |buffer, cx| {
let edits =
old_selections
.iter()
.map(|(_, _, range, indent, insert_extra_newline)| {
let mut new_text = String::with_capacity(1 + *indent as usize);
let mut new_text = String::with_capacity(1 + indent as usize);
new_text.push('\n');
new_text.extend(iter::repeat(' ').take(*indent as usize));
if *insert_extra_newline {
new_text.extend(iter::repeat(' ').take(indent as usize));
if insert_extra_newline {
new_text = new_text.repeat(2);
}
(range.clone(), new_text)
});
(
(start..end, new_text),
(insert_extra_newline, buffer.anchor_after(end)),
)
})
.unzip()
};
this.buffer.update(cx, |buffer, cx| {
buffer.edit_with_autoindent_batched(edits, cx);
let buffer = buffer.read(cx);
@ -1931,11 +1925,10 @@ impl Editor {
.selections
.iter()
.cloned()
.zip(old_selections)
.map(
|(mut new_selection, (_, end_anchor, _, _, insert_extra_newline))| {
let mut cursor = end_anchor.to_point(&buffer);
if insert_extra_newline {
.zip(selection_fixup_info)
.map(|(mut new_selection, (extra_newline_inserted, end))| {
let mut cursor = end.to_point(&buffer);
if extra_newline_inserted {
cursor.row -= 1;
cursor.column = buffer.line_len(cursor.row);
}
@ -1943,8 +1936,7 @@ impl Editor {
new_selection.start = anchor.clone();
new_selection.end = anchor;
new_selection
},
)
})
.collect();
});