Merge pull request #929 from zed-industries/non-uniform-batched-edits
Allow batched edits where each range is associated with different insertion text
This commit is contained in:
@@ -843,7 +843,7 @@ pub mod tests {
|
||||
|
||||
let ix = snapshot.buffer_snapshot.text().find("seven").unwrap();
|
||||
buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit(vec![ix..ix], "and ", cx);
|
||||
buffer.edit([(ix..ix, "and ")], cx);
|
||||
});
|
||||
|
||||
let snapshot = map.update(cx, |map, cx| map.snapshot(cx));
|
||||
@@ -878,11 +878,10 @@ pub mod tests {
|
||||
buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit(
|
||||
vec![
|
||||
Point::new(1, 0)..Point::new(1, 0),
|
||||
Point::new(1, 1)..Point::new(1, 1),
|
||||
Point::new(2, 1)..Point::new(2, 1),
|
||||
(Point::new(1, 0)..Point::new(1, 0), "\t"),
|
||||
(Point::new(1, 1)..Point::new(1, 1), "\t"),
|
||||
(Point::new(2, 1)..Point::new(2, 1), "\t"),
|
||||
],
|
||||
"\t",
|
||||
cx,
|
||||
)
|
||||
});
|
||||
|
||||
@@ -1154,7 +1154,7 @@ mod tests {
|
||||
|
||||
// Insert a line break, separating two block decorations into separate lines.
|
||||
let buffer_snapshot = buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit([Point::new(1, 1)..Point::new(1, 1)], "!!!\n", cx);
|
||||
buffer.edit([(Point::new(1, 1)..Point::new(1, 1), "!!!\n")], cx);
|
||||
buffer.snapshot(cx)
|
||||
});
|
||||
|
||||
|
||||
@@ -1248,10 +1248,9 @@ mod tests {
|
||||
let buffer_snapshot = buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit(
|
||||
vec![
|
||||
Point::new(0, 0)..Point::new(0, 1),
|
||||
Point::new(2, 3)..Point::new(2, 3),
|
||||
(Point::new(0, 0)..Point::new(0, 1), "123"),
|
||||
(Point::new(2, 3)..Point::new(2, 3), "123"),
|
||||
],
|
||||
"123",
|
||||
cx,
|
||||
);
|
||||
buffer.snapshot(cx)
|
||||
@@ -1274,7 +1273,7 @@ mod tests {
|
||||
);
|
||||
|
||||
let buffer_snapshot = buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit(vec![Point::new(2, 6)..Point::new(4, 3)], "456", cx);
|
||||
buffer.edit([(Point::new(2, 6)..Point::new(4, 3), "456")], cx);
|
||||
buffer.snapshot(cx)
|
||||
});
|
||||
let (snapshot4, _) = map.read(buffer_snapshot.clone(), subscription.consume().into_inner());
|
||||
@@ -1330,7 +1329,7 @@ mod tests {
|
||||
|
||||
// Edit within one of the folds.
|
||||
let buffer_snapshot = buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit(vec![0..1], "12345", cx);
|
||||
buffer.edit([(0..1, "12345")], cx);
|
||||
buffer.snapshot(cx)
|
||||
});
|
||||
let (snapshot, _) =
|
||||
@@ -1372,7 +1371,7 @@ mod tests {
|
||||
assert_eq!(snapshot.text(), "aa…cccc\nd…eeeee");
|
||||
|
||||
let buffer_snapshot = buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit(Some(Point::new(2, 2)..Point::new(3, 1)), "", cx);
|
||||
buffer.edit([(Point::new(2, 2)..Point::new(3, 1), "")], cx);
|
||||
buffer.snapshot(cx)
|
||||
});
|
||||
let (snapshot, _) = map.read(buffer_snapshot.clone(), subscription.consume().into_inner());
|
||||
|
||||
+148
-148
@@ -549,7 +549,7 @@ struct SnippetState {
|
||||
|
||||
pub struct RenameState {
|
||||
pub range: Range<Anchor>,
|
||||
pub old_name: String,
|
||||
pub old_name: Arc<str>,
|
||||
pub editor: ViewHandle<Editor>,
|
||||
block_id: BlockId,
|
||||
}
|
||||
@@ -1318,7 +1318,7 @@ impl Editor {
|
||||
pub fn replace_selections_with(
|
||||
&mut self,
|
||||
cx: &mut ViewContext<Self>,
|
||||
find_replacement: impl Fn(&DisplaySnapshot) -> DisplayPoint,
|
||||
mut find_replacement: impl FnMut(&DisplaySnapshot) -> DisplayPoint,
|
||||
) {
|
||||
let display_map = self.snapshot(cx);
|
||||
let cursor = find_replacement(&display_map);
|
||||
@@ -1336,7 +1336,7 @@ impl Editor {
|
||||
pub fn move_selections(
|
||||
&mut self,
|
||||
cx: &mut ViewContext<Self>,
|
||||
move_selection: impl Fn(&DisplaySnapshot, &mut Selection<DisplayPoint>),
|
||||
mut move_selection: impl FnMut(&DisplaySnapshot, &mut Selection<DisplayPoint>),
|
||||
) {
|
||||
let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
|
||||
let selections = self
|
||||
@@ -1354,7 +1354,7 @@ impl Editor {
|
||||
pub fn move_selection_heads(
|
||||
&mut self,
|
||||
cx: &mut ViewContext<Self>,
|
||||
update_head: impl Fn(
|
||||
mut update_head: impl FnMut(
|
||||
&DisplaySnapshot,
|
||||
DisplayPoint,
|
||||
SelectionGoal,
|
||||
@@ -1369,7 +1369,7 @@ impl Editor {
|
||||
pub fn move_cursors(
|
||||
&mut self,
|
||||
cx: &mut ViewContext<Self>,
|
||||
update_cursor_position: impl Fn(
|
||||
mut update_cursor_position: impl FnMut(
|
||||
&DisplaySnapshot,
|
||||
DisplayPoint,
|
||||
SelectionGoal,
|
||||
@@ -1863,133 +1863,91 @@ 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() {
|
||||
let start_point = selection.start.to_point(&buffer);
|
||||
let indent = buffer
|
||||
.indent_column_for_line(start_point.row)
|
||||
.min(start_point.column);
|
||||
let start = selection.start;
|
||||
let end = selection.end;
|
||||
selections
|
||||
.iter()
|
||||
.map(|selection| {
|
||||
let start_point = selection.start.to_point(&buffer);
|
||||
let indent = buffer
|
||||
.indent_column_for_line(start_point.row)
|
||||
.min(start_point.column);
|
||||
let start = selection.start;
|
||||
let end = selection.end;
|
||||
|
||||
let mut insert_extra_newline = false;
|
||||
if let Some(language) = buffer.language() {
|
||||
let leading_whitespace_len = buffer
|
||||
.reversed_chars_at(start)
|
||||
.take_while(|c| c.is_whitespace() && *c != '\n')
|
||||
.map(|c| c.len_utf8())
|
||||
.sum::<usize>();
|
||||
let mut insert_extra_newline = false;
|
||||
if let Some(language) = buffer.language() {
|
||||
let leading_whitespace_len = buffer
|
||||
.reversed_chars_at(start)
|
||||
.take_while(|c| c.is_whitespace() && *c != '\n')
|
||||
.map(|c| c.len_utf8())
|
||||
.sum::<usize>();
|
||||
|
||||
let trailing_whitespace_len = buffer
|
||||
.chars_at(end)
|
||||
.take_while(|c| c.is_whitespace() && *c != '\n')
|
||||
.map(|c| c.len_utf8())
|
||||
.sum::<usize>();
|
||||
let trailing_whitespace_len = buffer
|
||||
.chars_at(end)
|
||||
.take_while(|c| c.is_whitespace() && *c != '\n')
|
||||
.map(|c| c.len_utf8())
|
||||
.sum::<usize>();
|
||||
|
||||
insert_extra_newline = language.brackets().iter().any(|pair| {
|
||||
let pair_start = pair.start.trim_end();
|
||||
let pair_end = pair.end.trim_start();
|
||||
insert_extra_newline = language.brackets().iter().any(|pair| {
|
||||
let pair_start = pair.start.trim_end();
|
||||
let pair_end = pair.end.trim_start();
|
||||
|
||||
pair.newline
|
||||
&& buffer.contains_str_at(end + trailing_whitespace_len, pair_end)
|
||||
&& buffer.contains_str_at(
|
||||
(start - leading_whitespace_len)
|
||||
.saturating_sub(pair_start.len()),
|
||||
pair_start,
|
||||
)
|
||||
});
|
||||
}
|
||||
pair.newline
|
||||
&& buffer
|
||||
.contains_str_at(end + trailing_whitespace_len, pair_end)
|
||||
&& buffer.contains_str_at(
|
||||
(start - leading_whitespace_len)
|
||||
.saturating_sub(pair_start.len()),
|
||||
pair_start,
|
||||
)
|
||||
});
|
||||
}
|
||||
|
||||
old_selections.push((
|
||||
selection.id,
|
||||
buffer.anchor_after(end),
|
||||
start..end,
|
||||
indent,
|
||||
insert_extra_newline,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
this.buffer.update(cx, |buffer, cx| {
|
||||
let mut delta = 0_isize;
|
||||
let mut pending_edit: Option<PendingEdit> = None;
|
||||
for (_, _, range, indent, insert_extra_newline) in &old_selections {
|
||||
if pending_edit.as_ref().map_or(false, |pending| {
|
||||
pending.indent != *indent
|
||||
|| pending.insert_extra_newline != *insert_extra_newline
|
||||
}) {
|
||||
let pending = pending_edit.take().unwrap();
|
||||
let mut new_text = String::with_capacity(1 + pending.indent as usize);
|
||||
let mut new_text = String::with_capacity(1 + indent as usize);
|
||||
new_text.push('\n');
|
||||
new_text.extend(iter::repeat(' ').take(pending.indent as usize));
|
||||
if pending.insert_extra_newline {
|
||||
new_text.extend(iter::repeat(' ').take(indent as usize));
|
||||
if insert_extra_newline {
|
||||
new_text = new_text.repeat(2);
|
||||
}
|
||||
buffer.edit_with_autoindent(pending.ranges, new_text, cx);
|
||||
delta += pending.delta;
|
||||
}
|
||||
(
|
||||
(start..end, new_text),
|
||||
(insert_extra_newline, buffer.anchor_after(end)),
|
||||
)
|
||||
})
|
||||
.unzip()
|
||||
};
|
||||
|
||||
let start = (range.start as isize + delta) as usize;
|
||||
let end = (range.end as isize + delta) as usize;
|
||||
let mut text_len = *indent as usize + 1;
|
||||
if *insert_extra_newline {
|
||||
text_len *= 2;
|
||||
}
|
||||
|
||||
let pending = pending_edit.get_or_insert_with(Default::default);
|
||||
pending.delta += text_len as isize - (end - start) as isize;
|
||||
pending.indent = *indent;
|
||||
pending.insert_extra_newline = *insert_extra_newline;
|
||||
pending.ranges.push(start..end);
|
||||
}
|
||||
|
||||
let pending = pending_edit.unwrap();
|
||||
let mut new_text = String::with_capacity(1 + pending.indent as usize);
|
||||
new_text.push('\n');
|
||||
new_text.extend(iter::repeat(' ').take(pending.indent as usize));
|
||||
if pending.insert_extra_newline {
|
||||
new_text = new_text.repeat(2);
|
||||
}
|
||||
buffer.edit_with_autoindent(pending.ranges, new_text, cx);
|
||||
this.buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit_with_autoindent(edits, cx);
|
||||
|
||||
let buffer = buffer.read(cx);
|
||||
this.selections = this
|
||||
.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 {
|
||||
cursor.row -= 1;
|
||||
cursor.column = buffer.line_len(cursor.row);
|
||||
}
|
||||
let anchor = buffer.anchor_after(cursor);
|
||||
new_selection.start = anchor.clone();
|
||||
new_selection.end = anchor;
|
||||
new_selection
|
||||
},
|
||||
)
|
||||
.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);
|
||||
}
|
||||
let anchor = buffer.anchor_after(cursor);
|
||||
new_selection.start = anchor.clone();
|
||||
new_selection.end = anchor;
|
||||
new_selection
|
||||
})
|
||||
.collect();
|
||||
});
|
||||
|
||||
this.request_autoscroll(Autoscroll::Fit, cx);
|
||||
});
|
||||
|
||||
#[derive(Default)]
|
||||
struct PendingEdit {
|
||||
indent: u32,
|
||||
insert_extra_newline: bool,
|
||||
delta: isize,
|
||||
ranges: SmallVec<[Range<usize>; 32]>,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, text: &str, cx: &mut ViewContext<Self>) {
|
||||
let text: Arc<str> = text.into();
|
||||
self.transact(cx, |this, cx| {
|
||||
let old_selections = this.local_selections::<usize>(cx);
|
||||
let selection_anchors = this.buffer.update(cx, |buffer, cx| {
|
||||
@@ -2000,8 +1958,12 @@ impl Editor {
|
||||
.map(|s| (s.id, s.goal, snapshot.anchor_after(s.end)))
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
let edit_ranges = old_selections.iter().map(|s| s.start..s.end);
|
||||
buffer.edit_with_autoindent(edit_ranges, text, cx);
|
||||
buffer.edit_with_autoindent(
|
||||
old_selections
|
||||
.iter()
|
||||
.map(|s| (s.start..s.end, text.clone())),
|
||||
cx,
|
||||
);
|
||||
anchors
|
||||
});
|
||||
|
||||
@@ -2059,14 +2021,18 @@ impl Editor {
|
||||
drop(snapshot);
|
||||
|
||||
self.buffer.update(cx, |buffer, cx| {
|
||||
let pair_start: Arc<str> = pair.start.clone().into();
|
||||
buffer.edit(
|
||||
selections.iter().map(|s| s.start.clone()..s.start.clone()),
|
||||
&pair.start,
|
||||
selections
|
||||
.iter()
|
||||
.map(|s| (s.start.clone()..s.start.clone(), pair_start.clone())),
|
||||
cx,
|
||||
);
|
||||
let pair_end: Arc<str> = pair.end.clone().into();
|
||||
buffer.edit(
|
||||
selections.iter().map(|s| s.end.clone()..s.end.clone()),
|
||||
&pair.end,
|
||||
selections
|
||||
.iter()
|
||||
.map(|s| (s.end.clone()..s.end.clone(), pair_end.clone())),
|
||||
cx,
|
||||
);
|
||||
});
|
||||
@@ -2143,7 +2109,13 @@ impl Editor {
|
||||
})
|
||||
.collect::<SmallVec<[_; 32]>>();
|
||||
|
||||
buffer.edit(selection_ranges, &pair.end, cx);
|
||||
let pair_end: Arc<str> = pair.end.clone().into();
|
||||
buffer.edit(
|
||||
selection_ranges
|
||||
.iter()
|
||||
.map(|range| (range.clone(), pair_end.clone())),
|
||||
cx,
|
||||
);
|
||||
snapshot = buffer.snapshot(cx);
|
||||
|
||||
new_selections = Some(
|
||||
@@ -2402,7 +2374,8 @@ impl Editor {
|
||||
this.insert_snippet(&ranges, snippet, cx).log_err();
|
||||
} else {
|
||||
this.buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit_with_autoindent(ranges, text, cx);
|
||||
buffer
|
||||
.edit_with_autoindent(ranges.iter().map(|range| (range.clone(), text)), cx);
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -2759,7 +2732,14 @@ impl Editor {
|
||||
cx: &mut ViewContext<Self>,
|
||||
) -> Result<()> {
|
||||
let tabstops = self.buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit_with_autoindent(insertion_ranges.iter().cloned(), &snippet.text, cx);
|
||||
let snippet_text: Arc<str> = snippet.text.clone().into();
|
||||
buffer.edit_with_autoindent(
|
||||
insertion_ranges
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(|range| (range, snippet_text.clone())),
|
||||
cx,
|
||||
);
|
||||
|
||||
let snapshot = &*buffer.read(cx);
|
||||
let snippet = &snippet;
|
||||
@@ -2940,8 +2920,10 @@ impl Editor {
|
||||
.count();
|
||||
let chars_to_next_tab_stop = tab_size - (char_column as u32 % tab_size);
|
||||
buffer.edit(
|
||||
[selection.start..selection.start],
|
||||
" ".repeat(chars_to_next_tab_stop as usize),
|
||||
[(
|
||||
selection.start..selection.start,
|
||||
" ".repeat(chars_to_next_tab_stop as usize),
|
||||
)],
|
||||
cx,
|
||||
);
|
||||
selection.start.column += chars_to_next_tab_stop;
|
||||
@@ -2991,8 +2973,10 @@ impl Editor {
|
||||
let columns_to_next_tab_stop = tab_size - (indent_column % tab_size);
|
||||
let row_start = Point::new(row, 0);
|
||||
buffer.edit(
|
||||
[row_start..row_start],
|
||||
" ".repeat(columns_to_next_tab_stop as usize),
|
||||
[(
|
||||
row_start..row_start,
|
||||
" ".repeat(columns_to_next_tab_stop as usize),
|
||||
)],
|
||||
cx,
|
||||
);
|
||||
|
||||
@@ -3050,7 +3034,13 @@ impl Editor {
|
||||
|
||||
self.transact(cx, |this, cx| {
|
||||
this.buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit(deletion_ranges, "", cx);
|
||||
let empty_str: Arc<str> = "".into();
|
||||
buffer.edit(
|
||||
deletion_ranges
|
||||
.into_iter()
|
||||
.map(|range| (range, empty_str.clone())),
|
||||
cx,
|
||||
);
|
||||
});
|
||||
this.update_selections(
|
||||
this.local_selections::<usize>(cx),
|
||||
@@ -3112,7 +3102,13 @@ impl Editor {
|
||||
|
||||
self.transact(cx, |this, cx| {
|
||||
let buffer = this.buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit(edit_ranges, "", cx);
|
||||
let empty_str: Arc<str> = "".into();
|
||||
buffer.edit(
|
||||
edit_ranges
|
||||
.into_iter()
|
||||
.map(|range| (range, empty_str.clone())),
|
||||
cx,
|
||||
);
|
||||
buffer.snapshot(cx)
|
||||
});
|
||||
let new_selections = new_cursors
|
||||
@@ -3160,14 +3156,12 @@ impl Editor {
|
||||
.text_for_range(start..end)
|
||||
.chain(Some("\n"))
|
||||
.collect::<String>();
|
||||
edits.push((start, text, rows.len() as u32));
|
||||
edits.push((start..start, text));
|
||||
}
|
||||
|
||||
self.transact(cx, |this, cx| {
|
||||
this.buffer.update(cx, |buffer, cx| {
|
||||
for (point, text, _) in edits.into_iter().rev() {
|
||||
buffer.edit(Some(point..point), text, cx);
|
||||
}
|
||||
buffer.edit(edits, cx);
|
||||
});
|
||||
|
||||
this.request_autoscroll(Autoscroll::Fit, cx);
|
||||
@@ -3276,7 +3270,7 @@ impl Editor {
|
||||
this.unfold_ranges(unfold_ranges, true, cx);
|
||||
this.buffer.update(cx, |buffer, cx| {
|
||||
for (range, text) in edits {
|
||||
buffer.edit([range], text, cx);
|
||||
buffer.edit([(range, text)], cx);
|
||||
}
|
||||
});
|
||||
this.fold_ranges(refold_ranges, cx);
|
||||
@@ -3379,7 +3373,7 @@ impl Editor {
|
||||
this.unfold_ranges(unfold_ranges, true, cx);
|
||||
this.buffer.update(cx, |buffer, cx| {
|
||||
for (range, text) in edits {
|
||||
buffer.edit([range], text, cx);
|
||||
buffer.edit([(range, text)], cx);
|
||||
}
|
||||
});
|
||||
this.fold_ranges(refold_ranges, cx);
|
||||
@@ -3495,7 +3489,7 @@ impl Editor {
|
||||
};
|
||||
|
||||
delta += to_insert.len() as isize - range.len() as isize;
|
||||
buffer.edit([range], to_insert, cx);
|
||||
buffer.edit([(range, to_insert)], cx);
|
||||
selection.start += to_insert.len();
|
||||
selection.end = selection.start;
|
||||
});
|
||||
@@ -4210,11 +4204,11 @@ impl Editor {
|
||||
for selection in &mut selections {
|
||||
// Get the line comment prefix. Split its trailing whitespace into a separate string,
|
||||
// as that portion won't be used for detecting if a line is a comment.
|
||||
let full_comment_prefix = if let Some(prefix) = buffer
|
||||
let full_comment_prefix: Arc<str> = if let Some(prefix) = buffer
|
||||
.language_at(selection.start, cx)
|
||||
.and_then(|l| l.line_comment_prefix())
|
||||
{
|
||||
prefix.to_string()
|
||||
prefix.into()
|
||||
} else {
|
||||
return;
|
||||
};
|
||||
@@ -4281,15 +4275,22 @@ impl Editor {
|
||||
|
||||
if !edit_ranges.is_empty() {
|
||||
if all_selection_lines_are_comments {
|
||||
buffer.edit(edit_ranges.iter().cloned(), "", cx);
|
||||
let empty_str: Arc<str> = "".into();
|
||||
buffer.edit(
|
||||
edit_ranges
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(|range| (range, empty_str.clone())),
|
||||
cx,
|
||||
);
|
||||
} else {
|
||||
let min_column =
|
||||
edit_ranges.iter().map(|r| r.start.column).min().unwrap();
|
||||
let edit_ranges = edit_ranges.iter().map(|range| {
|
||||
let edits = edit_ranges.iter().map(|range| {
|
||||
let position = Point::new(range.start.row, min_column);
|
||||
position..position
|
||||
(position..position, full_comment_prefix.clone())
|
||||
});
|
||||
buffer.edit(edit_ranges, &full_comment_prefix, cx);
|
||||
buffer.edit(edits, cx);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4670,7 +4671,7 @@ impl Editor {
|
||||
let rename_end = rename_start + rename_buffer_range.len();
|
||||
let range = buffer.anchor_before(rename_start)..buffer.anchor_after(rename_end);
|
||||
let mut old_highlight_id = None;
|
||||
let old_name = buffer
|
||||
let old_name: Arc<str> = buffer
|
||||
.chunks(rename_start..rename_end, true)
|
||||
.map(|chunk| {
|
||||
if old_highlight_id.is_none() {
|
||||
@@ -4678,7 +4679,8 @@ impl Editor {
|
||||
}
|
||||
chunk.text
|
||||
})
|
||||
.collect();
|
||||
.collect::<String>()
|
||||
.into();
|
||||
|
||||
drop(buffer);
|
||||
|
||||
@@ -4692,7 +4694,7 @@ impl Editor {
|
||||
}
|
||||
editor
|
||||
.buffer
|
||||
.update(cx, |buffer, cx| buffer.edit([0..0], &old_name, cx));
|
||||
.update(cx, |buffer, cx| buffer.edit([(0..0, old_name.clone())], cx));
|
||||
editor.select_all(&SelectAll, cx);
|
||||
editor
|
||||
});
|
||||
@@ -5614,7 +5616,7 @@ impl Editor {
|
||||
self.buffer.read(cx).read(cx).text()
|
||||
}
|
||||
|
||||
pub fn set_text(&mut self, text: impl Into<String>, cx: &mut ViewContext<Self>) {
|
||||
pub fn set_text(&mut self, text: impl Into<Arc<str>>, cx: &mut ViewContext<Self>) {
|
||||
self.transact(cx, |this, cx| {
|
||||
this.buffer
|
||||
.read(cx)
|
||||
@@ -6619,8 +6621,8 @@ mod tests {
|
||||
// Simulate an edit in another editor
|
||||
buffer.update(cx, |buffer, cx| {
|
||||
buffer.start_transaction_at(now, cx);
|
||||
buffer.edit([0..1], "a", cx);
|
||||
buffer.edit([1..1], "b", cx);
|
||||
buffer.edit([(0..1, "a")], cx);
|
||||
buffer.edit([(1..1, "b")], cx);
|
||||
buffer.end_transaction_at(now, cx);
|
||||
});
|
||||
|
||||
@@ -6988,10 +6990,9 @@ mod tests {
|
||||
buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit(
|
||||
vec![
|
||||
Point::new(1, 0)..Point::new(1, 0),
|
||||
Point::new(1, 1)..Point::new(1, 1),
|
||||
(Point::new(1, 0)..Point::new(1, 0), "\t"),
|
||||
(Point::new(1, 1)..Point::new(1, 1), "\t"),
|
||||
],
|
||||
"\t",
|
||||
cx,
|
||||
);
|
||||
});
|
||||
@@ -7626,10 +7627,9 @@ mod tests {
|
||||
buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit(
|
||||
[
|
||||
Point::new(1, 2)..Point::new(3, 0),
|
||||
Point::new(4, 2)..Point::new(6, 0),
|
||||
(Point::new(1, 2)..Point::new(3, 0), ""),
|
||||
(Point::new(4, 2)..Point::new(6, 0), ""),
|
||||
],
|
||||
"",
|
||||
cx,
|
||||
);
|
||||
assert_eq!(
|
||||
@@ -7688,7 +7688,7 @@ mod tests {
|
||||
|
||||
// Edit the buffer directly, deleting ranges surrounding the editor's selections
|
||||
buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit([2..5, 10..13, 18..21], "", cx);
|
||||
buffer.edit([(2..5, ""), (10..13, ""), (18..21, "")], cx);
|
||||
assert_eq!(buffer.read(cx).text(), "a(), b(), c()".unindent());
|
||||
});
|
||||
|
||||
|
||||
@@ -255,38 +255,33 @@ impl MultiBuffer {
|
||||
self.subscriptions.subscribe()
|
||||
}
|
||||
|
||||
pub fn edit<I, S, T>(&mut self, ranges: I, new_text: T, cx: &mut ModelContext<Self>)
|
||||
pub fn edit<I, S, T>(&mut self, edits: I, cx: &mut ModelContext<Self>)
|
||||
where
|
||||
I: IntoIterator<Item = Range<S>>,
|
||||
I: IntoIterator<Item = (Range<S>, T)>,
|
||||
S: ToOffset,
|
||||
T: Into<String>,
|
||||
T: Into<Arc<str>>,
|
||||
{
|
||||
self.edit_internal(ranges, new_text, false, cx)
|
||||
self.edit_internal(edits, false, cx)
|
||||
}
|
||||
|
||||
pub fn edit_with_autoindent<I, S, T>(
|
||||
&mut self,
|
||||
ranges: I,
|
||||
new_text: T,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) where
|
||||
I: IntoIterator<Item = Range<S>>,
|
||||
pub fn edit_with_autoindent<I, S, T>(&mut self, edits: I, cx: &mut ModelContext<Self>)
|
||||
where
|
||||
I: IntoIterator<Item = (Range<S>, T)>,
|
||||
S: ToOffset,
|
||||
T: Into<String>,
|
||||
T: Into<Arc<str>>,
|
||||
{
|
||||
self.edit_internal(ranges, new_text, true, cx)
|
||||
self.edit_internal(edits, true, cx)
|
||||
}
|
||||
|
||||
pub fn edit_internal<I, S, T>(
|
||||
&mut self,
|
||||
ranges_iter: I,
|
||||
new_text: T,
|
||||
edits_iter: I,
|
||||
autoindent: bool,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) where
|
||||
I: IntoIterator<Item = Range<S>>,
|
||||
I: IntoIterator<Item = (Range<S>, T)>,
|
||||
S: ToOffset,
|
||||
T: Into<String>,
|
||||
T: Into<Arc<str>>,
|
||||
{
|
||||
if self.buffers.borrow().is_empty() {
|
||||
return;
|
||||
@@ -294,24 +289,29 @@ impl MultiBuffer {
|
||||
|
||||
if let Some(buffer) = self.as_singleton() {
|
||||
let snapshot = self.read(cx);
|
||||
let ranges = ranges_iter
|
||||
.into_iter()
|
||||
.map(|range| range.start.to_offset(&snapshot)..range.end.to_offset(&snapshot));
|
||||
let edits = edits_iter.into_iter().map(|(range, new_text)| {
|
||||
(
|
||||
range.start.to_offset(&snapshot)..range.end.to_offset(&snapshot),
|
||||
new_text,
|
||||
)
|
||||
});
|
||||
return buffer.update(cx, |buffer, cx| {
|
||||
let language_name = buffer.language().map(|language| language.name());
|
||||
let indent_size = cx.global::<Settings>().tab_size(language_name.as_deref());
|
||||
if autoindent {
|
||||
buffer.edit_with_autoindent(ranges, new_text, indent_size, cx);
|
||||
buffer.edit_with_autoindent(edits, indent_size, cx);
|
||||
} else {
|
||||
buffer.edit(ranges, new_text, cx);
|
||||
buffer.edit(edits, cx);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
let snapshot = self.read(cx);
|
||||
let mut buffer_edits: HashMap<usize, Vec<(Range<usize>, bool)>> = Default::default();
|
||||
let mut buffer_edits: HashMap<usize, Vec<(Range<usize>, Arc<str>, bool)>> =
|
||||
Default::default();
|
||||
let mut cursor = snapshot.excerpts.cursor::<usize>();
|
||||
for range in ranges_iter {
|
||||
for (range, new_text) in edits_iter {
|
||||
let new_text: Arc<str> = new_text.into();
|
||||
let start = range.start.to_offset(&snapshot);
|
||||
let end = range.end.to_offset(&snapshot);
|
||||
cursor.seek(&start, Bias::Right, &());
|
||||
@@ -335,7 +335,7 @@ impl MultiBuffer {
|
||||
buffer_edits
|
||||
.entry(start_excerpt.buffer_id)
|
||||
.or_insert(Vec::new())
|
||||
.push((buffer_start..buffer_end, true));
|
||||
.push((buffer_start..buffer_end, new_text, true));
|
||||
} else {
|
||||
let start_excerpt_range =
|
||||
buffer_start..start_excerpt.range.end.to_offset(&start_excerpt.buffer);
|
||||
@@ -344,11 +344,11 @@ impl MultiBuffer {
|
||||
buffer_edits
|
||||
.entry(start_excerpt.buffer_id)
|
||||
.or_insert(Vec::new())
|
||||
.push((start_excerpt_range, true));
|
||||
.push((start_excerpt_range, new_text.clone(), true));
|
||||
buffer_edits
|
||||
.entry(end_excerpt.buffer_id)
|
||||
.or_insert(Vec::new())
|
||||
.push((end_excerpt_range, false));
|
||||
.push((end_excerpt_range, new_text.clone(), false));
|
||||
|
||||
cursor.seek(&start, Bias::Right, &());
|
||||
cursor.next(&());
|
||||
@@ -359,25 +359,32 @@ impl MultiBuffer {
|
||||
buffer_edits
|
||||
.entry(excerpt.buffer_id)
|
||||
.or_insert(Vec::new())
|
||||
.push((excerpt.range.to_offset(&excerpt.buffer), false));
|
||||
.push((
|
||||
excerpt.range.to_offset(&excerpt.buffer),
|
||||
new_text.clone(),
|
||||
false,
|
||||
));
|
||||
cursor.next(&());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let new_text = new_text.into();
|
||||
for (buffer_id, mut edits) in buffer_edits {
|
||||
edits.sort_unstable_by_key(|(range, _)| range.start);
|
||||
edits.sort_unstable_by_key(|(range, _, _)| range.start);
|
||||
self.buffers.borrow()[&buffer_id]
|
||||
.buffer
|
||||
.update(cx, |buffer, cx| {
|
||||
let mut edits = edits.into_iter().peekable();
|
||||
let mut insertions = Vec::new();
|
||||
let mut deletions = Vec::new();
|
||||
while let Some((mut range, mut is_insertion)) = edits.next() {
|
||||
while let Some((next_range, next_is_insertion)) = edits.peek() {
|
||||
let empty_str: Arc<str> = "".into();
|
||||
while let Some((mut range, mut new_text, mut is_insertion)) = edits.next() {
|
||||
while let Some((next_range, next_new_text, next_is_insertion)) =
|
||||
edits.peek()
|
||||
{
|
||||
if range.end >= next_range.start {
|
||||
range.end = cmp::max(next_range.end, range.end);
|
||||
new_text = format!("{new_text}{next_new_text}").into();
|
||||
is_insertion |= *next_is_insertion;
|
||||
edits.next();
|
||||
} else {
|
||||
@@ -386,24 +393,26 @@ impl MultiBuffer {
|
||||
}
|
||||
|
||||
if is_insertion {
|
||||
insertions.push(
|
||||
insertions.push((
|
||||
buffer.anchor_before(range.start)..buffer.anchor_before(range.end),
|
||||
);
|
||||
new_text,
|
||||
));
|
||||
} else if !range.is_empty() {
|
||||
deletions.push(
|
||||
deletions.push((
|
||||
buffer.anchor_before(range.start)..buffer.anchor_before(range.end),
|
||||
);
|
||||
empty_str.clone(),
|
||||
));
|
||||
}
|
||||
}
|
||||
let language_name = buffer.language().map(|l| l.name());
|
||||
let indent_size = cx.global::<Settings>().tab_size(language_name.as_deref());
|
||||
|
||||
if autoindent {
|
||||
buffer.edit_with_autoindent(deletions, "", indent_size, cx);
|
||||
buffer.edit_with_autoindent(insertions, new_text.clone(), indent_size, cx);
|
||||
buffer.edit_with_autoindent(deletions, indent_size, cx);
|
||||
buffer.edit_with_autoindent(insertions, indent_size, cx);
|
||||
} else {
|
||||
buffer.edit(deletions, "", cx);
|
||||
buffer.edit(insertions, new_text.clone(), cx);
|
||||
buffer.edit(deletions, cx);
|
||||
buffer.edit(insertions, cx);
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -1249,28 +1258,34 @@ impl MultiBuffer {
|
||||
pub fn randomly_edit(
|
||||
&mut self,
|
||||
rng: &mut impl rand::Rng,
|
||||
count: usize,
|
||||
edit_count: usize,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) {
|
||||
use text::RandomCharIter;
|
||||
|
||||
let snapshot = self.read(cx);
|
||||
let mut old_ranges: Vec<Range<usize>> = Vec::new();
|
||||
for _ in 0..count {
|
||||
let last_end = old_ranges.last().map_or(0, |last_range| last_range.end + 1);
|
||||
if last_end > snapshot.len() {
|
||||
let mut edits: Vec<(Range<usize>, Arc<str>)> = Vec::new();
|
||||
let mut last_end = None;
|
||||
for _ in 0..edit_count {
|
||||
if last_end.map_or(false, |last_end| last_end >= snapshot.len()) {
|
||||
break;
|
||||
}
|
||||
let end_ix = snapshot.clip_offset(rng.gen_range(0..=last_end), Bias::Right);
|
||||
let start_ix = snapshot.clip_offset(rng.gen_range(0..=end_ix), Bias::Left);
|
||||
old_ranges.push(start_ix..end_ix);
|
||||
|
||||
let new_start = last_end.map_or(0, |last_end| last_end + 1);
|
||||
let end = snapshot.clip_offset(rng.gen_range(new_start..=snapshot.len()), Bias::Right);
|
||||
let start = snapshot.clip_offset(rng.gen_range(new_start..=end), Bias::Right);
|
||||
last_end = Some(end);
|
||||
let range = start..end;
|
||||
|
||||
let new_text_len = rng.gen_range(0..10);
|
||||
let new_text: String = RandomCharIter::new(&mut *rng).take(new_text_len).collect();
|
||||
|
||||
edits.push((range, new_text.into()));
|
||||
}
|
||||
let new_text_len = rng.gen_range(0..10);
|
||||
let new_text: String = RandomCharIter::new(&mut *rng).take(new_text_len).collect();
|
||||
log::info!("mutating multi-buffer at {:?}: {:?}", old_ranges, new_text);
|
||||
log::info!("mutating multi-buffer with {:?}", edits);
|
||||
drop(snapshot);
|
||||
|
||||
self.edit(old_ranges.iter().cloned(), new_text.as_str(), cx);
|
||||
self.edit(edits, cx);
|
||||
}
|
||||
|
||||
pub fn randomly_edit_excerpts(
|
||||
@@ -2950,7 +2965,7 @@ mod tests {
|
||||
.collect::<Vec<_>>()
|
||||
);
|
||||
|
||||
buffer.update(cx, |buffer, cx| buffer.edit([1..3], "XXX\n", cx));
|
||||
buffer.update(cx, |buffer, cx| buffer.edit([(1..3, "XXX\n")], cx));
|
||||
let snapshot = multibuffer.read(cx).snapshot(cx);
|
||||
|
||||
assert_eq!(snapshot.text(), buffer.read(cx).text());
|
||||
@@ -2973,11 +2988,11 @@ mod tests {
|
||||
let snapshot = multibuffer.read(cx).snapshot(cx);
|
||||
assert_eq!(snapshot.text(), "a");
|
||||
|
||||
guest_buffer.update(cx, |buffer, cx| buffer.edit([1..1], "b", cx));
|
||||
guest_buffer.update(cx, |buffer, cx| buffer.edit([(1..1, "b")], cx));
|
||||
let snapshot = multibuffer.read(cx).snapshot(cx);
|
||||
assert_eq!(snapshot.text(), "ab");
|
||||
|
||||
guest_buffer.update(cx, |buffer, cx| buffer.edit([2..2], "c", cx));
|
||||
guest_buffer.update(cx, |buffer, cx| buffer.edit([(2..2, "c")], cx));
|
||||
let snapshot = multibuffer.read(cx).snapshot(cx);
|
||||
assert_eq!(snapshot.text(), "abc");
|
||||
}
|
||||
@@ -3091,12 +3106,12 @@ mod tests {
|
||||
);
|
||||
|
||||
buffer_1.update(cx, |buffer, cx| {
|
||||
let text = "\n";
|
||||
buffer.edit(
|
||||
[
|
||||
Point::new(0, 0)..Point::new(0, 0),
|
||||
Point::new(2, 1)..Point::new(2, 3),
|
||||
(Point::new(0, 0)..Point::new(0, 0), text),
|
||||
(Point::new(2, 1)..Point::new(2, 3), text),
|
||||
],
|
||||
"\n",
|
||||
cx,
|
||||
);
|
||||
});
|
||||
@@ -3234,8 +3249,8 @@ mod tests {
|
||||
let multibuffer = cx.add_model(|cx| MultiBuffer::singleton(buffer.clone(), cx));
|
||||
let old_snapshot = multibuffer.read(cx).snapshot(cx);
|
||||
buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit([0..0], "X", cx);
|
||||
buffer.edit([5..5], "Y", cx);
|
||||
buffer.edit([(0..0, "X")], cx);
|
||||
buffer.edit([(5..5, "Y")], cx);
|
||||
});
|
||||
let new_snapshot = multibuffer.read(cx).snapshot(cx);
|
||||
|
||||
@@ -3268,12 +3283,12 @@ mod tests {
|
||||
assert_eq!(Anchor::max().to_offset(&old_snapshot), 10);
|
||||
|
||||
buffer_1.update(cx, |buffer, cx| {
|
||||
buffer.edit([0..0], "W", cx);
|
||||
buffer.edit([5..5], "X", cx);
|
||||
buffer.edit([(0..0, "W")], cx);
|
||||
buffer.edit([(5..5, "X")], cx);
|
||||
});
|
||||
buffer_2.update(cx, |buffer, cx| {
|
||||
buffer.edit([0..0], "Y", cx);
|
||||
buffer.edit([6..0], "Z", cx);
|
||||
buffer.edit([(0..0, "Y")], cx);
|
||||
buffer.edit([(6..0, "Z")], cx);
|
||||
});
|
||||
let new_snapshot = multibuffer.read(cx).snapshot(cx);
|
||||
|
||||
@@ -3302,7 +3317,7 @@ mod tests {
|
||||
|
||||
// Create an insertion id in buffer 1 that doesn't exist in buffer 2.
|
||||
// Add an excerpt from buffer 1 that spans this new insertion.
|
||||
buffer_1.update(cx, |buffer, cx| buffer.edit([4..4], "123", cx));
|
||||
buffer_1.update(cx, |buffer, cx| buffer.edit([(4..4, "123")], cx));
|
||||
let excerpt_id_1 = multibuffer.update(cx, |multibuffer, cx| {
|
||||
multibuffer
|
||||
.push_excerpts(buffer_1.clone(), [0..7], cx)
|
||||
@@ -3823,18 +3838,16 @@ mod tests {
|
||||
multibuffer.start_transaction_at(now, cx);
|
||||
multibuffer.edit(
|
||||
[
|
||||
Point::new(0, 0)..Point::new(0, 0),
|
||||
Point::new(1, 0)..Point::new(1, 0),
|
||||
(Point::new(0, 0)..Point::new(0, 0), "A"),
|
||||
(Point::new(1, 0)..Point::new(1, 0), "A"),
|
||||
],
|
||||
"A",
|
||||
cx,
|
||||
);
|
||||
multibuffer.edit(
|
||||
[
|
||||
Point::new(0, 1)..Point::new(0, 1),
|
||||
Point::new(1, 1)..Point::new(1, 1),
|
||||
(Point::new(0, 1)..Point::new(0, 1), "B"),
|
||||
(Point::new(1, 1)..Point::new(1, 1), "B"),
|
||||
],
|
||||
"B",
|
||||
cx,
|
||||
);
|
||||
multibuffer.end_transaction_at(now, cx);
|
||||
@@ -3843,19 +3856,19 @@ mod tests {
|
||||
// Edit buffer 1 through the multibuffer
|
||||
now += 2 * group_interval;
|
||||
multibuffer.start_transaction_at(now, cx);
|
||||
multibuffer.edit([2..2], "C", cx);
|
||||
multibuffer.edit([(2..2, "C")], cx);
|
||||
multibuffer.end_transaction_at(now, cx);
|
||||
assert_eq!(multibuffer.read(cx).text(), "ABC1234\nAB5678");
|
||||
|
||||
// Edit buffer 1 independently
|
||||
buffer_1.update(cx, |buffer_1, cx| {
|
||||
buffer_1.start_transaction_at(now);
|
||||
buffer_1.edit([3..3], "D", cx);
|
||||
buffer_1.edit([(3..3, "D")], cx);
|
||||
buffer_1.end_transaction_at(now, cx);
|
||||
|
||||
now += 2 * group_interval;
|
||||
buffer_1.start_transaction_at(now);
|
||||
buffer_1.edit([4..4], "E", cx);
|
||||
buffer_1.edit([(4..4, "E")], cx);
|
||||
buffer_1.end_transaction_at(now, cx);
|
||||
});
|
||||
assert_eq!(multibuffer.read(cx).text(), "ABCDE1234\nAB5678");
|
||||
|
||||
Reference in New Issue
Block a user