History

Merge pull request #329 from zed-industries/fix-newline-in-multibuffer

Fix cursor position when inserting newlines on a repeated excerpt

Max Brunsfeld committed 4 years agov0.11

Commit 5a889b02 parents 89ead1c c1682012 files · +30 −25Browse files

Unified · Split

2 files
  1. crates/editor/Cargo.toml +1 −1
  2. crates/editor/src/editor.rs +29 −24
crates/editor/Cargo.toml+1−1
@@ -1,7 +1,7 @@
11 [package]
22 name = "editor"
33 version = "0.1.0"
4−edition = "2018"
4+edition = "2021"
55
66 [lib]
77 path = "src/editor.rs"
crates/editor/src/editor.rs+29−24
@@ -1216,7 +1216,6 @@ impl Editor { · 1215 unmodified lines
12161216 }
12171217 }
12181218
1219− let mut new_selections = Vec::with_capacity(old_selections.len());
12201219 self.buffer.update(cx, |buffer, cx| {
12211220 let mut delta = 0_isize;
12221221 let mut pending_edit: Option<PendingEdit> = None;
@@ -1259,31 +1258,27 @@ impl Editor { · 36 unmodified lines
12591258 }
12601259 buffer.edit_with_autoindent(pending.ranges, new_text, cx);
12611260
1262− let mut delta = 0_isize;
1263− new_selections.extend(old_selections.into_iter().map(
1264− |(id, range, indent, insert_extra_newline)| {
1265− let start = (range.start as isize + delta) as usize;
1266− let end = (range.end as isize + delta) as usize;
1267− let text_before_cursor_len = indent as usize + 1;
1268− let cursor = start + text_before_cursor_len;
1269− let text_len = if insert_extra_newline {
1270− text_before_cursor_len * 2
1271− } else {
1272− text_before_cursor_len
1273− };
1274− delta += text_len as isize - (end - start) as isize;
1275− Selection {
1276− id,
1277− start: cursor,
1278− end: cursor,
1279− reversed: false,
1280− goal: SelectionGoal::None,
1261+ let buffer = buffer.read(cx);
1262+ self.selections = self
1263+ .selections
1264+ .iter()
1265+ .cloned()
1266+ .zip(old_selections)
1267+ .map(|(mut new_selection, (_, _, _, insert_extra_newline))| {
1268+ if insert_extra_newline {
1269+ let mut cursor = new_selection.start.to_point(&buffer);
1270+ cursor.row -= 1;
1271+ cursor.column = buffer.line_len(cursor.row);
1272+ let anchor = buffer.anchor_after(cursor);
1273+ new_selection.start = anchor.clone();
1274+ new_selection.end = anchor;
12811275 }
1282− },
1283− ))
1276+ new_selection
1277+ })
1278+ .collect();
12841279 });
12851280
1286− self.update_selections(new_selections, Some(Autoscroll::Fit), cx);
1281+ self.request_autoscroll(Autoscroll::Fit, cx);
12871282 self.end_transaction(cx);
12881283
12891284 #[derive(Default)]
@@ -6186,7 +6181,17 @@ mod tests { · 4896 unmodified lines
61866181 DisplayPoint::new(1, 2)..DisplayPoint::new(1, 2),
61876182 DisplayPoint::new(2, 5)..DisplayPoint::new(2, 5),
61886183 ]
6189− )
6184+ );
6185+
6186+ view.newline(&Newline, cx);
6187+ assert_eq!(view.text(cx), "aaaa\nbX\nbbX\nb\nbX\nbbX\nb\ncccc");
6188+ assert_eq!(
6189+ view.selected_display_ranges(cx),
6190+ &[
6191+ DisplayPoint::new(2, 0)..DisplayPoint::new(2, 0),
6192+ DisplayPoint::new(6, 0)..DisplayPoint::new(6, 0),
6193+ ]
6194+ );
61906195 });
61916196 }
61926197