2022-07-08 17:57:02 +00:00
|
|
|
use std::borrow::Cow;
|
|
|
|
|
2022-05-19 00:41:26 +00:00
|
|
|
use collections::HashMap;
|
2022-07-08 17:57:02 +00:00
|
|
|
use editor::{display_map::ToDisplayPoint, Autoscroll, Bias, ClipboardSelection};
|
2022-05-03 17:29:57 +00:00
|
|
|
use gpui::{actions, MutableAppContext, ViewContext};
|
2022-07-28 21:03:31 +00:00
|
|
|
use language::{AutoindentMode, SelectionGoal};
|
2022-05-03 17:29:57 +00:00
|
|
|
use workspace::Workspace;
|
|
|
|
|
2022-05-20 00:42:30 +00:00
|
|
|
use crate::{motion::Motion, state::Mode, utils::copy_selections_content, Vim};
|
2022-05-03 17:29:57 +00:00
|
|
|
|
2022-07-05 04:10:20 +00:00
|
|
|
actions!(vim, [VisualDelete, VisualChange, VisualYank, VisualPaste]);
|
2022-05-03 17:29:57 +00:00
|
|
|
|
|
|
|
pub fn init(cx: &mut MutableAppContext) {
|
|
|
|
cx.add_action(change);
|
|
|
|
cx.add_action(delete);
|
2022-05-23 16:23:25 +00:00
|
|
|
cx.add_action(yank);
|
2022-07-08 17:57:02 +00:00
|
|
|
cx.add_action(paste);
|
2022-05-03 17:29:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn visual_motion(motion: Motion, cx: &mut MutableAppContext) {
|
|
|
|
Vim::update(cx, |vim, cx| {
|
|
|
|
vim.update_active_editor(cx, |editor, cx| {
|
2022-05-12 21:18:46 +00:00
|
|
|
editor.change_selections(Some(Autoscroll::Fit), cx, |s| {
|
2022-05-06 04:09:26 +00:00
|
|
|
s.move_with(|map, selection| {
|
|
|
|
let (new_head, goal) = motion.move_point(map, selection.head(), selection.goal);
|
|
|
|
let was_reversed = selection.reversed;
|
|
|
|
selection.set_head(new_head, goal);
|
2022-05-03 17:29:57 +00:00
|
|
|
|
2022-05-06 04:09:26 +00:00
|
|
|
if was_reversed && !selection.reversed {
|
|
|
|
// Head was at the start of the selection, and now is at the end. We need to move the start
|
|
|
|
// back by one if possible in order to compensate for this change.
|
|
|
|
*selection.start.column_mut() = selection.start.column().saturating_sub(1);
|
|
|
|
selection.start = map.clip_point(selection.start, Bias::Left);
|
|
|
|
} else if !was_reversed && selection.reversed {
|
|
|
|
// Head was at the end of the selection, and now is at the start. We need to move the end
|
|
|
|
// forward by one if possible in order to compensate for this change.
|
|
|
|
*selection.end.column_mut() = selection.end.column() + 1;
|
2022-05-20 00:42:30 +00:00
|
|
|
selection.end = map.clip_point(selection.end, Bias::Right);
|
2022-05-06 04:09:26 +00:00
|
|
|
}
|
|
|
|
});
|
2022-05-03 17:29:57 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn change(_: &mut Workspace, _: &VisualChange, cx: &mut ViewContext<Workspace>) {
|
|
|
|
Vim::update(cx, |vim, cx| {
|
|
|
|
vim.update_active_editor(cx, |editor, cx| {
|
|
|
|
editor.set_clip_at_line_ends(false, cx);
|
2022-05-24 20:35:57 +00:00
|
|
|
// Compute edits and resulting anchor selections. If in line mode, adjust
|
|
|
|
// the anchor location and additional newline
|
|
|
|
let mut edits = Vec::new();
|
|
|
|
let mut new_selections = Vec::new();
|
|
|
|
let line_mode = editor.selections.line_mode;
|
|
|
|
editor.change_selections(None, cx, |s| {
|
2022-05-06 04:09:26 +00:00
|
|
|
s.move_with(|map, selection| {
|
|
|
|
if !selection.reversed {
|
2022-05-23 16:23:25 +00:00
|
|
|
// Head is at the end of the selection. Adjust the end position to
|
|
|
|
// to include the character under the cursor.
|
2022-05-06 04:09:26 +00:00
|
|
|
*selection.end.column_mut() = selection.end.column() + 1;
|
2022-05-26 18:28:05 +00:00
|
|
|
selection.end = map.clip_point(selection.end, Bias::Right);
|
2022-05-06 04:09:26 +00:00
|
|
|
}
|
2022-05-03 17:29:57 +00:00
|
|
|
|
2022-05-24 20:35:57 +00:00
|
|
|
if line_mode {
|
|
|
|
let range = selection.map(|p| p.to_point(map)).range();
|
|
|
|
let expanded_range = map.expand_to_line(range);
|
|
|
|
// If we are at the last line, the anchor needs to be after the newline so that
|
|
|
|
// it is on a line of its own. Otherwise, the anchor may be after the newline
|
|
|
|
let anchor = if expanded_range.end == map.buffer_snapshot.max_point() {
|
|
|
|
map.buffer_snapshot.anchor_after(expanded_range.end)
|
|
|
|
} else {
|
|
|
|
map.buffer_snapshot.anchor_before(expanded_range.start)
|
|
|
|
};
|
2022-05-18 18:10:24 +00:00
|
|
|
|
2022-05-24 20:35:57 +00:00
|
|
|
edits.push((expanded_range, "\n"));
|
|
|
|
new_selections.push(selection.map(|_| anchor.clone()));
|
|
|
|
} else {
|
|
|
|
let range = selection.map(|p| p.to_point(map)).range();
|
|
|
|
let anchor = map.buffer_snapshot.anchor_after(range.end);
|
|
|
|
edits.push((range, ""));
|
|
|
|
new_selections.push(selection.map(|_| anchor.clone()));
|
2022-05-06 04:09:26 +00:00
|
|
|
}
|
2022-05-26 18:28:05 +00:00
|
|
|
selection.goal = SelectionGoal::None;
|
2022-05-06 04:09:26 +00:00
|
|
|
});
|
2022-05-03 17:29:57 +00:00
|
|
|
});
|
2022-05-24 20:35:57 +00:00
|
|
|
copy_selections_content(editor, editor.selections.line_mode, cx);
|
|
|
|
editor.edit_with_autoindent(edits, cx);
|
2022-05-18 18:10:24 +00:00
|
|
|
editor.change_selections(Some(Autoscroll::Fit), cx, |s| {
|
2022-05-24 20:35:57 +00:00
|
|
|
s.select_anchors(new_selections);
|
2022-05-18 18:10:24 +00:00
|
|
|
});
|
|
|
|
});
|
2022-07-18 20:33:55 +00:00
|
|
|
vim.switch_mode(Mode::Insert, false, cx);
|
2022-05-18 18:10:24 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-24 20:35:57 +00:00
|
|
|
pub fn delete(_: &mut Workspace, _: &VisualDelete, cx: &mut ViewContext<Workspace>) {
|
2022-05-18 18:10:24 +00:00
|
|
|
Vim::update(cx, |vim, cx| {
|
|
|
|
vim.update_active_editor(cx, |editor, cx| {
|
|
|
|
editor.set_clip_at_line_ends(false, cx);
|
2022-05-19 00:41:26 +00:00
|
|
|
let mut original_columns: HashMap<_, _> = Default::default();
|
2022-05-24 20:35:57 +00:00
|
|
|
let line_mode = editor.selections.line_mode;
|
2022-05-18 18:10:24 +00:00
|
|
|
editor.change_selections(Some(Autoscroll::Fit), cx, |s| {
|
|
|
|
s.move_with(|map, selection| {
|
2022-05-24 20:35:57 +00:00
|
|
|
if line_mode {
|
|
|
|
original_columns
|
|
|
|
.insert(selection.id, selection.head().to_point(&map).column);
|
|
|
|
} else if !selection.reversed {
|
|
|
|
// Head is at the end of the selection. Adjust the end position to
|
|
|
|
// to include the character under the cursor.
|
|
|
|
*selection.end.column_mut() = selection.end.column() + 1;
|
2022-05-23 16:23:25 +00:00
|
|
|
selection.end = map.clip_point(selection.end, Bias::Right);
|
2022-05-18 18:10:24 +00:00
|
|
|
}
|
2022-05-26 18:28:05 +00:00
|
|
|
selection.goal = SelectionGoal::None;
|
2022-05-18 18:10:24 +00:00
|
|
|
});
|
|
|
|
});
|
2022-05-24 20:35:57 +00:00
|
|
|
copy_selections_content(editor, line_mode, cx);
|
2022-05-18 18:10:24 +00:00
|
|
|
editor.insert("", cx);
|
|
|
|
|
|
|
|
// Fixup cursor position after the deletion
|
2022-05-10 18:09:49 +00:00
|
|
|
editor.set_clip_at_line_ends(true, cx);
|
2022-05-12 21:18:46 +00:00
|
|
|
editor.change_selections(Some(Autoscroll::Fit), cx, |s| {
|
2022-05-06 04:09:26 +00:00
|
|
|
s.move_with(|map, selection| {
|
2022-05-24 20:35:57 +00:00
|
|
|
let mut cursor = selection.head().to_point(map);
|
|
|
|
|
2022-05-19 00:41:26 +00:00
|
|
|
if let Some(column) = original_columns.get(&selection.id) {
|
2022-05-24 20:35:57 +00:00
|
|
|
cursor.column = *column
|
2022-05-19 00:41:26 +00:00
|
|
|
}
|
2022-05-24 20:35:57 +00:00
|
|
|
let cursor = map.clip_point(cursor.to_display_point(map), Bias::Left);
|
2022-05-06 04:09:26 +00:00
|
|
|
selection.collapse_to(cursor, selection.goal)
|
|
|
|
});
|
2022-05-10 18:09:49 +00:00
|
|
|
});
|
2022-05-03 17:29:57 +00:00
|
|
|
});
|
2022-07-18 20:33:55 +00:00
|
|
|
vim.switch_mode(Mode::Normal, false, cx);
|
2022-05-03 17:29:57 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-23 16:23:25 +00:00
|
|
|
pub fn yank(_: &mut Workspace, _: &VisualYank, cx: &mut ViewContext<Workspace>) {
|
|
|
|
Vim::update(cx, |vim, cx| {
|
|
|
|
vim.update_active_editor(cx, |editor, cx| {
|
|
|
|
editor.set_clip_at_line_ends(false, cx);
|
2022-05-24 20:35:57 +00:00
|
|
|
let line_mode = editor.selections.line_mode;
|
2022-07-08 17:57:02 +00:00
|
|
|
if !line_mode {
|
2022-05-26 18:28:05 +00:00
|
|
|
editor.change_selections(None, cx, |s| {
|
|
|
|
s.move_with(|map, selection| {
|
|
|
|
if !selection.reversed {
|
|
|
|
// Head is at the end of the selection. Adjust the end position to
|
|
|
|
// to include the character under the cursor.
|
|
|
|
*selection.end.column_mut() = selection.end.column() + 1;
|
|
|
|
selection.end = map.clip_point(selection.end, Bias::Right);
|
|
|
|
}
|
|
|
|
});
|
2022-05-23 16:23:25 +00:00
|
|
|
});
|
2022-05-26 18:28:05 +00:00
|
|
|
}
|
2022-05-24 20:35:57 +00:00
|
|
|
copy_selections_content(editor, line_mode, cx);
|
|
|
|
editor.change_selections(None, cx, |s| {
|
|
|
|
s.move_with(|_, selection| {
|
|
|
|
selection.collapse_to(selection.start, SelectionGoal::None)
|
|
|
|
});
|
|
|
|
});
|
2022-05-23 16:23:25 +00:00
|
|
|
});
|
2022-07-18 20:33:55 +00:00
|
|
|
vim.switch_mode(Mode::Normal, false, cx);
|
2022-05-23 16:23:25 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-08 17:57:02 +00:00
|
|
|
pub fn paste(_: &mut Workspace, _: &VisualPaste, cx: &mut ViewContext<Workspace>) {
|
|
|
|
Vim::update(cx, |vim, cx| {
|
|
|
|
vim.update_active_editor(cx, |editor, cx| {
|
|
|
|
editor.transact(cx, |editor, cx| {
|
|
|
|
if let Some(item) = cx.as_mut().read_from_clipboard() {
|
|
|
|
copy_selections_content(editor, editor.selections.line_mode, cx);
|
|
|
|
let mut clipboard_text = Cow::Borrowed(item.text());
|
|
|
|
if let Some(mut clipboard_selections) =
|
|
|
|
item.metadata::<Vec<ClipboardSelection>>()
|
|
|
|
{
|
|
|
|
let (display_map, selections) = editor.selections.all_adjusted_display(cx);
|
|
|
|
let all_selections_were_entire_line =
|
|
|
|
clipboard_selections.iter().all(|s| s.is_entire_line);
|
|
|
|
if clipboard_selections.len() != selections.len() {
|
|
|
|
let mut newline_separated_text = String::new();
|
|
|
|
let mut clipboard_selections =
|
|
|
|
clipboard_selections.drain(..).peekable();
|
|
|
|
let mut ix = 0;
|
|
|
|
while let Some(clipboard_selection) = clipboard_selections.next() {
|
|
|
|
newline_separated_text
|
|
|
|
.push_str(&clipboard_text[ix..ix + clipboard_selection.len]);
|
|
|
|
ix += clipboard_selection.len;
|
|
|
|
if clipboard_selections.peek().is_some() {
|
|
|
|
newline_separated_text.push('\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
clipboard_text = Cow::Owned(newline_separated_text);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut new_selections = Vec::new();
|
|
|
|
editor.buffer().update(cx, |buffer, cx| {
|
|
|
|
let snapshot = buffer.snapshot(cx);
|
|
|
|
let mut start_offset = 0;
|
|
|
|
let mut edits = Vec::new();
|
|
|
|
for (ix, selection) in selections.iter().enumerate() {
|
|
|
|
let to_insert;
|
|
|
|
let linewise;
|
|
|
|
if let Some(clipboard_selection) = clipboard_selections.get(ix) {
|
|
|
|
let end_offset = start_offset + clipboard_selection.len;
|
|
|
|
to_insert = &clipboard_text[start_offset..end_offset];
|
|
|
|
linewise = clipboard_selection.is_entire_line;
|
|
|
|
start_offset = end_offset;
|
|
|
|
} else {
|
|
|
|
to_insert = clipboard_text.as_str();
|
|
|
|
linewise = all_selections_were_entire_line;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut selection = selection.clone();
|
|
|
|
if !selection.reversed {
|
|
|
|
let mut adjusted = selection.end;
|
|
|
|
// Head is at the end of the selection. Adjust the end position to
|
|
|
|
// to include the character under the cursor.
|
|
|
|
*adjusted.column_mut() = adjusted.column() + 1;
|
|
|
|
adjusted = display_map.clip_point(adjusted, Bias::Right);
|
|
|
|
// If the selection is empty, move both the start and end forward one
|
|
|
|
// character
|
|
|
|
if selection.is_empty() {
|
|
|
|
selection.start = adjusted;
|
|
|
|
selection.end = adjusted;
|
|
|
|
} else {
|
|
|
|
selection.end = adjusted;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let range = selection.map(|p| p.to_point(&display_map)).range();
|
|
|
|
|
|
|
|
let new_position = if linewise {
|
|
|
|
edits.push((range.start..range.start, "\n"));
|
|
|
|
let mut new_position = range.start.clone();
|
|
|
|
new_position.column = 0;
|
|
|
|
new_position.row += 1;
|
|
|
|
new_position
|
|
|
|
} else {
|
|
|
|
range.start.clone()
|
|
|
|
};
|
|
|
|
|
|
|
|
new_selections.push(selection.map(|_| new_position.clone()));
|
|
|
|
|
|
|
|
if linewise && to_insert.ends_with('\n') {
|
|
|
|
edits.push((
|
|
|
|
range.clone(),
|
|
|
|
&to_insert[0..to_insert.len().saturating_sub(1)],
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
edits.push((range.clone(), to_insert));
|
|
|
|
}
|
|
|
|
|
|
|
|
if linewise {
|
|
|
|
edits.push((range.end..range.end, "\n"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
drop(snapshot);
|
2022-07-29 05:34:56 +00:00
|
|
|
buffer.edit(edits, Some(AutoindentMode::EachLine), cx);
|
2022-07-08 17:57:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
editor.change_selections(Some(Autoscroll::Fit), cx, |s| {
|
|
|
|
s.select(new_selections)
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
editor.insert(&clipboard_text, cx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2022-07-18 20:33:55 +00:00
|
|
|
vim.switch_mode(Mode::Normal, false, cx);
|
2022-07-08 17:57:02 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-03 17:29:57 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use indoc::indoc;
|
|
|
|
|
|
|
|
use crate::{state::Mode, vim_test_context::VimTestContext};
|
|
|
|
|
|
|
|
#[gpui::test]
|
|
|
|
async fn test_enter_visual_mode(cx: &mut gpui::TestAppContext) {
|
|
|
|
let cx = VimTestContext::new(cx, true).await;
|
2022-05-24 20:35:57 +00:00
|
|
|
let mut cx = cx
|
|
|
|
.binding(["v", "w", "j"])
|
|
|
|
.mode_after(Mode::Visual { line: false });
|
2022-05-03 17:29:57 +00:00
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
2022-08-04 00:52:34 +00:00
|
|
|
The ˇquick brown
|
2022-05-03 17:29:57 +00:00
|
|
|
fox jumps over
|
|
|
|
the lazy dog"},
|
|
|
|
indoc! {"
|
2022-08-04 00:52:34 +00:00
|
|
|
The «quick brown
|
|
|
|
fox jumps ˇ»over
|
2022-05-03 17:29:57 +00:00
|
|
|
the lazy dog"},
|
|
|
|
);
|
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
|
|
|
fox jumps over
|
2022-08-04 00:52:34 +00:00
|
|
|
the ˇlazy dog"},
|
2022-05-03 17:29:57 +00:00
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
|
|
|
fox jumps over
|
2022-08-04 00:52:34 +00:00
|
|
|
the «lazy ˇ»dog"},
|
2022-05-03 17:29:57 +00:00
|
|
|
);
|
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
2022-08-04 00:52:34 +00:00
|
|
|
fox jumps ˇover
|
2022-05-03 17:29:57 +00:00
|
|
|
the lazy dog"},
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
2022-08-04 00:52:34 +00:00
|
|
|
fox jumps «over
|
|
|
|
ˇ»the lazy dog"},
|
2022-05-03 17:29:57 +00:00
|
|
|
);
|
2022-05-24 20:35:57 +00:00
|
|
|
let mut cx = cx
|
|
|
|
.binding(["v", "b", "k"])
|
|
|
|
.mode_after(Mode::Visual { line: false });
|
2022-05-03 17:29:57 +00:00
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
2022-08-04 00:52:34 +00:00
|
|
|
The ˇquick brown
|
2022-05-03 17:29:57 +00:00
|
|
|
fox jumps over
|
|
|
|
the lazy dog"},
|
|
|
|
indoc! {"
|
2022-08-04 00:52:34 +00:00
|
|
|
«ˇThe q»uick brown
|
2022-05-03 17:29:57 +00:00
|
|
|
fox jumps over
|
|
|
|
the lazy dog"},
|
|
|
|
);
|
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
|
|
|
fox jumps over
|
2022-08-04 00:52:34 +00:00
|
|
|
the ˇlazy dog"},
|
2022-05-03 17:29:57 +00:00
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
2022-08-04 00:52:34 +00:00
|
|
|
«ˇfox jumps over
|
|
|
|
the l»azy dog"},
|
2022-05-03 17:29:57 +00:00
|
|
|
);
|
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
2022-08-04 00:52:34 +00:00
|
|
|
fox jumps ˇover
|
2022-05-03 17:29:57 +00:00
|
|
|
the lazy dog"},
|
|
|
|
indoc! {"
|
2022-08-04 00:52:34 +00:00
|
|
|
The «ˇquick brown
|
|
|
|
fox jumps o»ver
|
2022-05-03 17:29:57 +00:00
|
|
|
the lazy dog"},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[gpui::test]
|
|
|
|
async fn test_visual_delete(cx: &mut gpui::TestAppContext) {
|
|
|
|
let cx = VimTestContext::new(cx, true).await;
|
|
|
|
let mut cx = cx.binding(["v", "w", "x"]);
|
2022-08-04 00:52:34 +00:00
|
|
|
cx.assert("The quick ˇbrown", "The quickˇ ");
|
2022-05-03 17:29:57 +00:00
|
|
|
let mut cx = cx.binding(["v", "w", "j", "x"]);
|
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
2022-08-04 00:52:34 +00:00
|
|
|
The ˇquick brown
|
2022-05-03 17:29:57 +00:00
|
|
|
fox jumps over
|
|
|
|
the lazy dog"},
|
|
|
|
indoc! {"
|
2022-08-04 00:52:34 +00:00
|
|
|
The ˇver
|
2022-05-03 17:29:57 +00:00
|
|
|
the lazy dog"},
|
|
|
|
);
|
2022-05-20 01:20:41 +00:00
|
|
|
// Test pasting code copied on delete
|
|
|
|
cx.simulate_keystrokes(["j", "p"]);
|
|
|
|
cx.assert_editor_state(indoc! {"
|
|
|
|
The ver
|
2022-08-04 00:52:34 +00:00
|
|
|
the lˇquick brown
|
2022-05-26 18:28:05 +00:00
|
|
|
fox jumps oazy dog"});
|
2022-05-20 01:20:41 +00:00
|
|
|
|
2022-05-03 17:29:57 +00:00
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
|
|
|
fox jumps over
|
2022-08-04 00:52:34 +00:00
|
|
|
the ˇlazy dog"},
|
2022-05-03 17:29:57 +00:00
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
|
|
|
fox jumps over
|
2022-08-04 00:52:34 +00:00
|
|
|
the ˇog"},
|
2022-05-03 17:29:57 +00:00
|
|
|
);
|
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
2022-08-04 00:52:34 +00:00
|
|
|
fox jumps ˇover
|
2022-05-03 17:29:57 +00:00
|
|
|
the lazy dog"},
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
2022-08-04 00:52:34 +00:00
|
|
|
fox jumps ˇhe lazy dog"},
|
2022-05-03 17:29:57 +00:00
|
|
|
);
|
|
|
|
let mut cx = cx.binding(["v", "b", "k", "x"]);
|
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
2022-08-04 00:52:34 +00:00
|
|
|
The ˇquick brown
|
2022-05-03 17:29:57 +00:00
|
|
|
fox jumps over
|
|
|
|
the lazy dog"},
|
|
|
|
indoc! {"
|
2022-08-04 00:52:34 +00:00
|
|
|
ˇuick brown
|
2022-05-03 17:29:57 +00:00
|
|
|
fox jumps over
|
|
|
|
the lazy dog"},
|
|
|
|
);
|
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
|
|
|
fox jumps over
|
2022-08-04 00:52:34 +00:00
|
|
|
the ˇlazy dog"},
|
2022-05-03 17:29:57 +00:00
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
2022-08-04 00:52:34 +00:00
|
|
|
ˇazy dog"},
|
2022-05-03 17:29:57 +00:00
|
|
|
);
|
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
2022-08-04 00:52:34 +00:00
|
|
|
fox jumps ˇover
|
2022-05-03 17:29:57 +00:00
|
|
|
the lazy dog"},
|
|
|
|
indoc! {"
|
2022-08-04 00:52:34 +00:00
|
|
|
The ˇver
|
2022-05-03 17:29:57 +00:00
|
|
|
the lazy dog"},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-05-20 01:20:41 +00:00
|
|
|
#[gpui::test]
|
|
|
|
async fn test_visual_line_delete(cx: &mut gpui::TestAppContext) {
|
|
|
|
let cx = VimTestContext::new(cx, true).await;
|
2022-07-27 10:19:01 +00:00
|
|
|
let mut cx = cx.binding(["shift-v", "x"]);
|
2022-05-20 01:20:41 +00:00
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
2022-08-04 00:52:34 +00:00
|
|
|
The quˇick brown
|
2022-05-20 01:20:41 +00:00
|
|
|
fox jumps over
|
|
|
|
the lazy dog"},
|
|
|
|
indoc! {"
|
2022-08-04 00:52:34 +00:00
|
|
|
fox juˇmps over
|
2022-05-20 01:20:41 +00:00
|
|
|
the lazy dog"},
|
|
|
|
);
|
|
|
|
// Test pasting code copied on delete
|
|
|
|
cx.simulate_keystroke("p");
|
|
|
|
cx.assert_editor_state(indoc! {"
|
|
|
|
fox jumps over
|
2022-08-04 00:52:34 +00:00
|
|
|
ˇThe quick brown
|
2022-05-20 01:20:41 +00:00
|
|
|
the lazy dog"});
|
|
|
|
|
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
2022-08-04 00:52:34 +00:00
|
|
|
fox juˇmps over
|
2022-05-20 01:20:41 +00:00
|
|
|
the lazy dog"},
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
2022-08-04 00:52:34 +00:00
|
|
|
the laˇzy dog"},
|
2022-05-20 01:20:41 +00:00
|
|
|
);
|
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
|
|
|
fox jumps over
|
2022-08-04 00:52:34 +00:00
|
|
|
the laˇzy dog"},
|
2022-05-20 01:20:41 +00:00
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
2022-08-04 00:52:34 +00:00
|
|
|
fox juˇmps over"},
|
2022-05-20 01:20:41 +00:00
|
|
|
);
|
2022-07-27 10:19:01 +00:00
|
|
|
let mut cx = cx.binding(["shift-v", "j", "x"]);
|
2022-05-20 01:20:41 +00:00
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
2022-08-04 00:52:34 +00:00
|
|
|
The quˇick brown
|
2022-05-20 01:20:41 +00:00
|
|
|
fox jumps over
|
|
|
|
the lazy dog"},
|
2022-08-04 00:52:34 +00:00
|
|
|
"the laˇzy dog",
|
2022-05-20 01:20:41 +00:00
|
|
|
);
|
|
|
|
// Test pasting code copied on delete
|
|
|
|
cx.simulate_keystroke("p");
|
|
|
|
cx.assert_editor_state(indoc! {"
|
|
|
|
the lazy dog
|
2022-08-04 00:52:34 +00:00
|
|
|
ˇThe quick brown
|
2022-05-20 01:20:41 +00:00
|
|
|
fox jumps over"});
|
|
|
|
|
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
2022-08-04 00:52:34 +00:00
|
|
|
fox juˇmps over
|
2022-05-20 01:20:41 +00:00
|
|
|
the lazy dog"},
|
2022-08-04 00:52:34 +00:00
|
|
|
"The quˇick brown",
|
2022-05-20 01:20:41 +00:00
|
|
|
);
|
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
|
|
|
fox jumps over
|
2022-08-04 00:52:34 +00:00
|
|
|
the laˇzy dog"},
|
2022-05-20 01:20:41 +00:00
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
2022-08-04 00:52:34 +00:00
|
|
|
fox juˇmps over"},
|
2022-05-20 01:20:41 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-05-03 17:29:57 +00:00
|
|
|
#[gpui::test]
|
|
|
|
async fn test_visual_change(cx: &mut gpui::TestAppContext) {
|
|
|
|
let cx = VimTestContext::new(cx, true).await;
|
2022-05-10 18:09:49 +00:00
|
|
|
let mut cx = cx.binding(["v", "w", "c"]).mode_after(Mode::Insert);
|
2022-08-04 00:52:34 +00:00
|
|
|
cx.assert("The quick ˇbrown", "The quick ˇ");
|
2022-05-03 17:29:57 +00:00
|
|
|
let mut cx = cx.binding(["v", "w", "j", "c"]).mode_after(Mode::Insert);
|
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
2022-08-04 00:52:34 +00:00
|
|
|
The ˇquick brown
|
2022-05-03 17:29:57 +00:00
|
|
|
fox jumps over
|
|
|
|
the lazy dog"},
|
|
|
|
indoc! {"
|
2022-08-04 00:52:34 +00:00
|
|
|
The ˇver
|
2022-05-03 17:29:57 +00:00
|
|
|
the lazy dog"},
|
|
|
|
);
|
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
|
|
|
fox jumps over
|
2022-08-04 00:52:34 +00:00
|
|
|
the ˇlazy dog"},
|
2022-05-03 17:29:57 +00:00
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
|
|
|
fox jumps over
|
2022-08-04 00:52:34 +00:00
|
|
|
the ˇog"},
|
2022-05-03 17:29:57 +00:00
|
|
|
);
|
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
2022-08-04 00:52:34 +00:00
|
|
|
fox jumps ˇover
|
2022-05-03 17:29:57 +00:00
|
|
|
the lazy dog"},
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
2022-08-04 00:52:34 +00:00
|
|
|
fox jumps ˇhe lazy dog"},
|
2022-05-03 17:29:57 +00:00
|
|
|
);
|
|
|
|
let mut cx = cx.binding(["v", "b", "k", "c"]).mode_after(Mode::Insert);
|
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
2022-08-04 00:52:34 +00:00
|
|
|
The ˇquick brown
|
2022-05-03 17:29:57 +00:00
|
|
|
fox jumps over
|
|
|
|
the lazy dog"},
|
|
|
|
indoc! {"
|
2022-08-04 00:52:34 +00:00
|
|
|
ˇuick brown
|
2022-05-03 17:29:57 +00:00
|
|
|
fox jumps over
|
|
|
|
the lazy dog"},
|
|
|
|
);
|
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
|
|
|
fox jumps over
|
2022-08-04 00:52:34 +00:00
|
|
|
the ˇlazy dog"},
|
2022-05-03 17:29:57 +00:00
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
2022-08-04 00:52:34 +00:00
|
|
|
ˇazy dog"},
|
2022-05-03 17:29:57 +00:00
|
|
|
);
|
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
2022-08-04 00:52:34 +00:00
|
|
|
fox jumps ˇover
|
2022-05-03 17:29:57 +00:00
|
|
|
the lazy dog"},
|
|
|
|
indoc! {"
|
2022-08-04 00:52:34 +00:00
|
|
|
The ˇver
|
2022-05-03 17:29:57 +00:00
|
|
|
the lazy dog"},
|
|
|
|
);
|
|
|
|
}
|
2022-05-20 01:20:41 +00:00
|
|
|
|
|
|
|
#[gpui::test]
|
|
|
|
async fn test_visual_line_change(cx: &mut gpui::TestAppContext) {
|
|
|
|
let cx = VimTestContext::new(cx, true).await;
|
2022-07-27 10:19:01 +00:00
|
|
|
let mut cx = cx.binding(["shift-v", "c"]).mode_after(Mode::Insert);
|
2022-05-20 01:20:41 +00:00
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
2022-08-04 00:52:34 +00:00
|
|
|
The quˇick brown
|
2022-05-20 01:20:41 +00:00
|
|
|
fox jumps over
|
|
|
|
the lazy dog"},
|
|
|
|
indoc! {"
|
2022-08-04 00:52:34 +00:00
|
|
|
ˇ
|
2022-05-20 01:20:41 +00:00
|
|
|
fox jumps over
|
|
|
|
the lazy dog"},
|
|
|
|
);
|
|
|
|
// Test pasting code copied on change
|
|
|
|
cx.simulate_keystrokes(["escape", "j", "p"]);
|
|
|
|
cx.assert_editor_state(indoc! {"
|
|
|
|
|
|
|
|
fox jumps over
|
2022-08-04 00:52:34 +00:00
|
|
|
ˇThe quick brown
|
2022-05-20 01:20:41 +00:00
|
|
|
the lazy dog"});
|
|
|
|
|
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
2022-08-04 00:52:34 +00:00
|
|
|
fox juˇmps over
|
2022-05-20 01:20:41 +00:00
|
|
|
the lazy dog"},
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
2022-08-04 00:52:34 +00:00
|
|
|
ˇ
|
2022-05-20 01:20:41 +00:00
|
|
|
the lazy dog"},
|
|
|
|
);
|
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
|
|
|
fox jumps over
|
2022-08-04 00:52:34 +00:00
|
|
|
the laˇzy dog"},
|
2022-05-20 01:20:41 +00:00
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
|
|
|
fox jumps over
|
2022-08-04 00:52:34 +00:00
|
|
|
ˇ"},
|
2022-05-20 01:20:41 +00:00
|
|
|
);
|
2022-07-27 10:19:01 +00:00
|
|
|
let mut cx = cx.binding(["shift-v", "j", "c"]).mode_after(Mode::Insert);
|
2022-05-20 01:20:41 +00:00
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
2022-08-04 00:52:34 +00:00
|
|
|
The quˇick brown
|
2022-05-20 01:20:41 +00:00
|
|
|
fox jumps over
|
|
|
|
the lazy dog"},
|
|
|
|
indoc! {"
|
2022-08-04 00:52:34 +00:00
|
|
|
ˇ
|
2022-05-20 01:20:41 +00:00
|
|
|
the lazy dog"},
|
|
|
|
);
|
|
|
|
// Test pasting code copied on delete
|
|
|
|
cx.simulate_keystrokes(["escape", "j", "p"]);
|
|
|
|
cx.assert_editor_state(indoc! {"
|
|
|
|
|
|
|
|
the lazy dog
|
2022-08-04 00:52:34 +00:00
|
|
|
ˇThe quick brown
|
2022-05-20 01:20:41 +00:00
|
|
|
fox jumps over"});
|
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
2022-08-04 00:52:34 +00:00
|
|
|
fox juˇmps over
|
2022-05-20 01:20:41 +00:00
|
|
|
the lazy dog"},
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
2022-08-04 00:52:34 +00:00
|
|
|
ˇ"},
|
2022-05-20 01:20:41 +00:00
|
|
|
);
|
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
|
|
|
fox jumps over
|
2022-08-04 00:52:34 +00:00
|
|
|
the laˇzy dog"},
|
2022-05-20 01:20:41 +00:00
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
|
|
|
fox jumps over
|
2022-08-04 00:52:34 +00:00
|
|
|
ˇ"},
|
2022-05-20 01:20:41 +00:00
|
|
|
);
|
|
|
|
}
|
2022-05-23 16:23:25 +00:00
|
|
|
|
|
|
|
#[gpui::test]
|
|
|
|
async fn test_visual_yank(cx: &mut gpui::TestAppContext) {
|
|
|
|
let cx = VimTestContext::new(cx, true).await;
|
|
|
|
let mut cx = cx.binding(["v", "w", "y"]);
|
2022-08-04 00:52:34 +00:00
|
|
|
cx.assert("The quick ˇbrown", "The quick ˇbrown");
|
2022-05-23 16:23:25 +00:00
|
|
|
cx.assert_clipboard_content(Some("brown"));
|
|
|
|
let mut cx = cx.binding(["v", "w", "j", "y"]);
|
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
2022-08-04 00:52:34 +00:00
|
|
|
The ˇquick brown
|
2022-05-23 16:23:25 +00:00
|
|
|
fox jumps over
|
|
|
|
the lazy dog"},
|
|
|
|
indoc! {"
|
2022-08-04 00:52:34 +00:00
|
|
|
The ˇquick brown
|
2022-05-23 16:23:25 +00:00
|
|
|
fox jumps over
|
|
|
|
the lazy dog"},
|
|
|
|
);
|
|
|
|
cx.assert_clipboard_content(Some(indoc! {"
|
|
|
|
quick brown
|
2022-05-24 20:35:57 +00:00
|
|
|
fox jumps o"}));
|
2022-05-23 16:23:25 +00:00
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
|
|
|
fox jumps over
|
2022-08-04 00:52:34 +00:00
|
|
|
the ˇlazy dog"},
|
2022-05-23 16:23:25 +00:00
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
|
|
|
fox jumps over
|
2022-08-04 00:52:34 +00:00
|
|
|
the ˇlazy dog"},
|
2022-05-23 16:23:25 +00:00
|
|
|
);
|
|
|
|
cx.assert_clipboard_content(Some("lazy d"));
|
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
2022-08-04 00:52:34 +00:00
|
|
|
fox jumps ˇover
|
2022-05-23 16:23:25 +00:00
|
|
|
the lazy dog"},
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
2022-08-04 00:52:34 +00:00
|
|
|
fox jumps ˇover
|
2022-05-23 16:23:25 +00:00
|
|
|
the lazy dog"},
|
|
|
|
);
|
|
|
|
cx.assert_clipboard_content(Some(indoc! {"
|
|
|
|
over
|
|
|
|
t"}));
|
|
|
|
let mut cx = cx.binding(["v", "b", "k", "y"]);
|
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
2022-08-04 00:52:34 +00:00
|
|
|
The ˇquick brown
|
2022-05-23 16:23:25 +00:00
|
|
|
fox jumps over
|
|
|
|
the lazy dog"},
|
|
|
|
indoc! {"
|
2022-08-04 00:52:34 +00:00
|
|
|
ˇThe quick brown
|
2022-05-23 16:23:25 +00:00
|
|
|
fox jumps over
|
|
|
|
the lazy dog"},
|
|
|
|
);
|
|
|
|
cx.assert_clipboard_content(Some("The q"));
|
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
|
|
|
fox jumps over
|
2022-08-04 00:52:34 +00:00
|
|
|
the ˇlazy dog"},
|
2022-05-23 16:23:25 +00:00
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
2022-08-04 00:52:34 +00:00
|
|
|
ˇfox jumps over
|
2022-05-24 20:35:57 +00:00
|
|
|
the lazy dog"},
|
2022-05-23 16:23:25 +00:00
|
|
|
);
|
|
|
|
cx.assert_clipboard_content(Some(indoc! {"
|
|
|
|
fox jumps over
|
|
|
|
the l"}));
|
|
|
|
cx.assert(
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
2022-08-04 00:52:34 +00:00
|
|
|
fox jumps ˇover
|
2022-05-23 16:23:25 +00:00
|
|
|
the lazy dog"},
|
|
|
|
indoc! {"
|
2022-08-04 00:52:34 +00:00
|
|
|
The ˇquick brown
|
2022-05-24 20:35:57 +00:00
|
|
|
fox jumps over
|
2022-05-23 16:23:25 +00:00
|
|
|
the lazy dog"},
|
|
|
|
);
|
|
|
|
cx.assert_clipboard_content(Some(indoc! {"
|
|
|
|
quick brown
|
|
|
|
fox jumps o"}));
|
|
|
|
}
|
2022-07-08 17:57:02 +00:00
|
|
|
|
|
|
|
#[gpui::test]
|
|
|
|
async fn test_visual_paste(cx: &mut gpui::TestAppContext) {
|
|
|
|
let mut cx = VimTestContext::new(cx, true).await;
|
|
|
|
cx.set_state(
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
2022-08-04 00:52:34 +00:00
|
|
|
fox «jumpˇ»s over
|
2022-07-08 17:57:02 +00:00
|
|
|
the lazy dog"},
|
|
|
|
Mode::Visual { line: false },
|
|
|
|
);
|
|
|
|
cx.simulate_keystroke("y");
|
|
|
|
cx.set_state(
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
2022-08-04 00:52:34 +00:00
|
|
|
fox jumpˇs over
|
2022-07-08 17:57:02 +00:00
|
|
|
the lazy dog"},
|
|
|
|
Mode::Normal,
|
|
|
|
);
|
|
|
|
cx.simulate_keystroke("p");
|
|
|
|
cx.assert_state(
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
2022-08-04 00:52:34 +00:00
|
|
|
fox jumpsˇjumps over
|
2022-07-08 17:57:02 +00:00
|
|
|
the lazy dog"},
|
|
|
|
Mode::Normal,
|
|
|
|
);
|
|
|
|
|
|
|
|
cx.set_state(
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
2022-08-04 00:52:34 +00:00
|
|
|
fox juˇmps over
|
2022-07-08 17:57:02 +00:00
|
|
|
the lazy dog"},
|
|
|
|
Mode::Visual { line: true },
|
|
|
|
);
|
|
|
|
cx.simulate_keystroke("d");
|
|
|
|
cx.assert_state(
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
2022-08-04 00:52:34 +00:00
|
|
|
the laˇzy dog"},
|
2022-07-08 17:57:02 +00:00
|
|
|
Mode::Normal,
|
|
|
|
);
|
|
|
|
cx.set_state(
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
2022-08-04 00:52:34 +00:00
|
|
|
the «lazˇ»y dog"},
|
2022-07-08 17:57:02 +00:00
|
|
|
Mode::Visual { line: false },
|
|
|
|
);
|
|
|
|
cx.simulate_keystroke("p");
|
|
|
|
cx.assert_state(
|
|
|
|
indoc! {"
|
|
|
|
The quick brown
|
|
|
|
the
|
2022-08-04 00:52:34 +00:00
|
|
|
ˇfox jumps over
|
2022-07-08 17:57:02 +00:00
|
|
|
dog"},
|
|
|
|
Mode::Normal,
|
|
|
|
);
|
|
|
|
}
|
2022-05-03 17:29:57 +00:00
|
|
|
}
|