Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Merge branch 'main' into vim-visual-selection

Conrad Irwinfb90eada7045
diff --cc crates/editor/src/element.rs
index e2cd15f7eeb,13856cc8efa..9fdd210b74d
--- a/crates/editor/src/element.rs
+++ b/crates/editor/src/element.rs
@@@ -3019,150 -2995,6 +3013,154 @@@ mod tests
assert_eq!(layouts.len(), 6);
}
+ #[gpui::test]
+ async fn test_vim_visual_selections(cx: &mut TestAppContext) {
+ init_test(cx, |_| {});
+
- let (_, editor) = cx.add_window(|cx| {
- let buffer = MultiBuffer::build_simple(&(sample_text(6, 6, 'a') + "\n"), cx);
- Editor::new(EditorMode::Full, buffer, None, None, cx)
- });
++ let editor = cx
++ .add_window(|cx| {
++ let buffer = MultiBuffer::build_simple(&(sample_text(6, 6, 'a') + "\n"), cx);
++ Editor::new(EditorMode::Full, buffer, None, None, cx)
++ })
++ .root(cx);
+ let mut element = EditorElement::new(editor.read_with(cx, |editor, cx| editor.style(cx)));
+ let (_, state) = editor.update(cx, |editor, cx| {
+ editor.cursor_shape = CursorShape::Block;
+ editor.change_selections(None, cx, |s| {
+ s.select_ranges([
+ Point::new(0, 0)..Point::new(1, 0),
+ Point::new(3, 2)..Point::new(3, 3),
+ Point::new(5, 6)..Point::new(6, 0),
+ ]);
+ });
+ let mut new_parents = Default::default();
+ let mut notify_views_if_parents_change = Default::default();
+ let mut layout_cx = LayoutContext::new(
+ cx,
+ &mut new_parents,
+ &mut notify_views_if_parents_change,
+ false,
+ );
+ element.layout(
+ SizeConstraint::new(vec2f(500., 500.), vec2f(500., 500.)),
+ editor,
+ &mut layout_cx,
+ )
+ });
+ assert_eq!(state.selections.len(), 1);
+ let local_selections = &state.selections[0].1;
+ assert_eq!(local_selections.len(), 3);
+ // moves cursor back one line
+ assert_eq!(local_selections[0].head, DisplayPoint::new(0, 6));
+ assert_eq!(
+ local_selections[0].range,
+ DisplayPoint::new(0, 0)..DisplayPoint::new(1, 0)
+ );
+
+ // moves cursor back one column
+ assert_eq!(
+ local_selections[1].range,
+ DisplayPoint::new(3, 2)..DisplayPoint::new(3, 3)
+ );
+ assert_eq!(local_selections[1].head, DisplayPoint::new(3, 2));
+
+ // leaves cursor on the max point
+ assert_eq!(
+ local_selections[2].range,
+ DisplayPoint::new(5, 6)..DisplayPoint::new(6, 0)
+ );
+ assert_eq!(local_selections[2].head, DisplayPoint::new(6, 0));
+
+ // active lines does not include 1 (even though the range of the selection does)
+ assert_eq!(
+ state.active_rows.keys().cloned().collect::<Vec<u32>>(),
+ vec![0, 3, 5, 6]
+ );
+
+ // multi-buffer support
+ // in DisplayPoint co-ordinates, this is what we're dealing with:
+ // 0: [[file
+ // 1: header]]
+ // 2: aaaaaa
+ // 3: bbbbbb
+ // 4: cccccc
+ // 5:
+ // 6: ...
+ // 7: ffffff
+ // 8: gggggg
+ // 9: hhhhhh
+ // 10:
+ // 11: [[file
+ // 12: header]]
+ // 13: bbbbbb
+ // 14: cccccc
+ // 15: dddddd
- let (_, editor) = cx.add_window(|cx| {
- let buffer = MultiBuffer::build_multi(
- [
- (
- &(sample_text(8, 6, 'a') + "\n"),
- vec![
- Point::new(0, 0)..Point::new(3, 0),
- Point::new(4, 0)..Point::new(7, 0),
- ],
- ),
- (
- &(sample_text(8, 6, 'a') + "\n"),
- vec![Point::new(1, 0)..Point::new(3, 0)],
- ),
- ],
- cx,
- );
- Editor::new(EditorMode::Full, buffer, None, None, cx)
- });
++ let editor = cx
++ .add_window(|cx| {
++ let buffer = MultiBuffer::build_multi(
++ [
++ (
++ &(sample_text(8, 6, 'a') + "\n"),
++ vec![
++ Point::new(0, 0)..Point::new(3, 0),
++ Point::new(4, 0)..Point::new(7, 0),
++ ],
++ ),
++ (
++ &(sample_text(8, 6, 'a') + "\n"),
++ vec![Point::new(1, 0)..Point::new(3, 0)],
++ ),
++ ],
++ cx,
++ );
++ Editor::new(EditorMode::Full, buffer, None, None, cx)
++ })
++ .root(cx);
+ let mut element = EditorElement::new(editor.read_with(cx, |editor, cx| editor.style(cx)));
+ let (_, state) = editor.update(cx, |editor, cx| {
+ editor.cursor_shape = CursorShape::Block;
+ editor.change_selections(None, cx, |s| {
+ s.select_display_ranges([
+ DisplayPoint::new(4, 0)..DisplayPoint::new(7, 0),
+ DisplayPoint::new(10, 0)..DisplayPoint::new(13, 0),
+ ]);
+ });
+ let mut new_parents = Default::default();
+ let mut notify_views_if_parents_change = Default::default();
+ let mut layout_cx = LayoutContext::new(
+ cx,
+ &mut new_parents,
+ &mut notify_views_if_parents_change,
+ false,
+ );
+ element.layout(
+ SizeConstraint::new(vec2f(500., 500.), vec2f(500., 500.)),
+ editor,
+ &mut layout_cx,
+ )
+ });
+
+ assert_eq!(state.selections.len(), 1);
+ let local_selections = &state.selections[0].1;
+ assert_eq!(local_selections.len(), 2);
+
+ // moves cursor on excerpt boundary back a line
+ // and doesn't allow selection to bleed through
+ assert_eq!(
+ local_selections[0].range,
+ DisplayPoint::new(4, 0)..DisplayPoint::new(6, 0)
+ );
+ assert_eq!(local_selections[0].head, DisplayPoint::new(5, 0));
+
+ // moves cursor on buffer boundary back two lines
+ // and doesn't allow selection to bleed through
+ assert_eq!(
+ local_selections[1].range,
+ DisplayPoint::new(10, 0)..DisplayPoint::new(11, 0)
+ );
+ assert_eq!(local_selections[1].head, DisplayPoint::new(10, 0));
+ }
+
#[gpui::test]
fn test_layout_with_placeholder_text_and_blocks(cx: &mut TestAppContext) {
init_test(cx, |_| {});
diff --cc crates/vim/src/test/vim_test_context.rs
index ca1020cf753,5eaaef900e4..ab5d7382c70
--- a/crates/vim/src/test/vim_test_context.rs
+++ b/crates/vim/src/test/vim_test_context.rs
@@@ -85,14 -85,13 +85,14 @@@ impl<'a> VimTestContext<'a>
}
pub fn set_state(&mut self, text: &str, mode: Mode) -> ContextHandle {
- let window_id = self.window_id;
+ let window = self.window;
+ let context_handle = self.cx.set_state(text);
- self.update_window(window_id, |cx| {
+ window.update(self.cx.cx.cx, |cx| {
Vim::update(cx, |vim, cx| {
- vim.switch_mode(mode, false, cx);
+ vim.switch_mode(mode, true, cx);
})
});
- self.cx.set_state(text)
+ context_handle
}
#[track_caller]