rewrap.rs

  1use crate::{Vim, motion::Motion, object::Object, state::Mode};
  2use collections::HashMap;
  3use editor::{Bias, Editor, RewrapOptions, SelectionEffects, display_map::ToDisplayPoint};
  4use gpui::{Context, Window, actions};
  5use language::SelectionGoal;
  6
  7actions!(vim, [Rewrap]);
  8
  9pub(crate) fn register(editor: &mut Editor, cx: &mut Context<Vim>) {
 10    Vim::action(editor, cx, |vim, _: &Rewrap, window, cx| {
 11        vim.record_current_action(cx);
 12        Vim::take_count(cx);
 13        Vim::take_forced_motion(cx);
 14        vim.store_visual_marks(window, cx);
 15        vim.update_editor(window, cx, |vim, editor, window, cx| {
 16            editor.transact(window, cx, |editor, window, cx| {
 17                let mut positions = vim.save_selection_starts(editor, cx);
 18                editor.rewrap_impl(
 19                    RewrapOptions {
 20                        override_language_settings: true,
 21                        ..Default::default()
 22                    },
 23                    cx,
 24                );
 25                editor.change_selections(Default::default(), window, cx, |s| {
 26                    s.move_with(|map, selection| {
 27                        if let Some(anchor) = positions.remove(&selection.id) {
 28                            let mut point = anchor.to_display_point(map);
 29                            *point.column_mut() = 0;
 30                            selection.collapse_to(point, SelectionGoal::None);
 31                        }
 32                    });
 33                });
 34            });
 35        });
 36        if vim.mode.is_visual() {
 37            vim.switch_mode(Mode::Normal, true, window, cx)
 38        }
 39    });
 40}
 41
 42impl Vim {
 43    pub(crate) fn rewrap_motion(
 44        &mut self,
 45        motion: Motion,
 46        times: Option<usize>,
 47        forced_motion: bool,
 48        window: &mut Window,
 49        cx: &mut Context<Self>,
 50    ) {
 51        self.stop_recording(cx);
 52        self.update_editor(window, cx, |_, editor, window, cx| {
 53            let text_layout_details = editor.text_layout_details(window);
 54            editor.transact(window, cx, |editor, window, cx| {
 55                let mut selection_starts: HashMap<_, _> = Default::default();
 56                editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
 57                    s.move_with(|map, selection| {
 58                        let anchor = map.display_point_to_anchor(selection.head(), Bias::Right);
 59                        selection_starts.insert(selection.id, anchor);
 60                        motion.expand_selection(
 61                            map,
 62                            selection,
 63                            times,
 64                            &text_layout_details,
 65                            forced_motion,
 66                        );
 67                    });
 68                });
 69                editor.rewrap_impl(
 70                    RewrapOptions {
 71                        override_language_settings: true,
 72                        ..Default::default()
 73                    },
 74                    cx,
 75                );
 76                editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
 77                    s.move_with(|map, selection| {
 78                        let anchor = selection_starts.remove(&selection.id).unwrap();
 79                        let mut point = anchor.to_display_point(map);
 80                        *point.column_mut() = 0;
 81                        selection.collapse_to(point, SelectionGoal::None);
 82                    });
 83                });
 84            });
 85        });
 86    }
 87
 88    pub(crate) fn rewrap_object(
 89        &mut self,
 90        object: Object,
 91        around: bool,
 92        times: Option<usize>,
 93        window: &mut Window,
 94        cx: &mut Context<Self>,
 95    ) {
 96        self.stop_recording(cx);
 97        self.update_editor(window, cx, |_, editor, window, cx| {
 98            editor.transact(window, cx, |editor, window, cx| {
 99                let mut original_positions: HashMap<_, _> = Default::default();
100                editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
101                    s.move_with(|map, selection| {
102                        let anchor = map.display_point_to_anchor(selection.head(), Bias::Right);
103                        original_positions.insert(selection.id, anchor);
104                        object.expand_selection(map, selection, around, times);
105                    });
106                });
107                editor.rewrap_impl(
108                    RewrapOptions {
109                        override_language_settings: true,
110                        ..Default::default()
111                    },
112                    cx,
113                );
114                editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
115                    s.move_with(|map, selection| {
116                        let anchor = original_positions.remove(&selection.id).unwrap();
117                        let mut point = anchor.to_display_point(map);
118                        *point.column_mut() = 0;
119                        selection.collapse_to(point, SelectionGoal::None);
120                    });
121                });
122            });
123        });
124    }
125}
126
127#[cfg(test)]
128mod test {
129    use crate::test::NeovimBackedTestContext;
130
131    #[gpui::test]
132    async fn test_indent_gv(cx: &mut gpui::TestAppContext) {
133        let mut cx = NeovimBackedTestContext::new(cx).await;
134        cx.set_neovim_option("shiftwidth=4").await;
135
136        cx.set_shared_state("ˇhello\nworld\n").await;
137        cx.simulate_shared_keystrokes("v j > g v").await;
138        cx.shared_state()
139            .await
140            .assert_eq("«    hello\n ˇ»   world\n");
141    }
142}