### Summary This PR adds support for count and object motions to the toggle comments action in Vim mode. The relevant issue is [#14337](https://github.com/zed-industries/zed/issues/14337). For example, `2 g c j` will toggle comments three lines downward. `g c g g` will toggle comments from the current cursor position up to the start of the file. Notably missing from this PR are `g c b` (toggle comments for the current block) as well as `g c p` (toggle comments for the current paragraph). These seem to be non-standard. The new module `normal/toggle_comments.rs` has been copied almost verbatim from `normal/indent.rs`. Maybe that ought to be abstracted over but I feel I lack the overview. Release Notes: - vim: Added support for count and object motion to the toggle comments action ([#14337](https://github.com/zed-industries/zed/issues/14337)).
58 lines
2.4 KiB
Rust
58 lines
2.4 KiB
Rust
use crate::{motion::Motion, object::Object, Vim};
|
|
use collections::HashMap;
|
|
use editor::{display_map::ToDisplayPoint, Bias};
|
|
use gpui::WindowContext;
|
|
use language::SelectionGoal;
|
|
|
|
pub fn toggle_comments_motion(
|
|
vim: &mut Vim,
|
|
motion: Motion,
|
|
times: Option<usize>,
|
|
cx: &mut WindowContext,
|
|
) {
|
|
vim.stop_recording();
|
|
vim.update_active_editor(cx, |_, editor, cx| {
|
|
let text_layout_details = editor.text_layout_details(cx);
|
|
editor.transact(cx, |editor, cx| {
|
|
let mut selection_starts: HashMap<_, _> = Default::default();
|
|
editor.change_selections(None, cx, |s| {
|
|
s.move_with(|map, selection| {
|
|
let anchor = map.display_point_to_anchor(selection.head(), Bias::Right);
|
|
selection_starts.insert(selection.id, anchor);
|
|
motion.expand_selection(map, selection, times, false, &text_layout_details);
|
|
});
|
|
});
|
|
editor.toggle_comments(&Default::default(), cx);
|
|
editor.change_selections(None, cx, |s| {
|
|
s.move_with(|map, selection| {
|
|
let anchor = selection_starts.remove(&selection.id).unwrap();
|
|
selection.collapse_to(anchor.to_display_point(map), SelectionGoal::None);
|
|
});
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
pub fn toggle_comments_object(vim: &mut Vim, object: Object, around: bool, cx: &mut WindowContext) {
|
|
vim.stop_recording();
|
|
vim.update_active_editor(cx, |_, editor, cx| {
|
|
editor.transact(cx, |editor, cx| {
|
|
let mut original_positions: HashMap<_, _> = Default::default();
|
|
editor.change_selections(None, cx, |s| {
|
|
s.move_with(|map, selection| {
|
|
let anchor = map.display_point_to_anchor(selection.head(), Bias::Right);
|
|
original_positions.insert(selection.id, anchor);
|
|
object.expand_selection(map, selection, around);
|
|
});
|
|
});
|
|
editor.toggle_comments(&Default::default(), cx);
|
|
editor.change_selections(None, cx, |s| {
|
|
s.move_with(|map, selection| {
|
|
let anchor = original_positions.remove(&selection.id).unwrap();
|
|
selection.collapse_to(anchor.to_display_point(map), SelectionGoal::None);
|
|
});
|
|
});
|
|
});
|
|
});
|
|
}
|