WIP just missing insert line above and below
This commit is contained in:
parent
c268099554
commit
833a7b6e76
4 changed files with 648 additions and 35 deletions
|
@ -1,15 +1,60 @@
|
|||
mod change;
|
||||
mod delete;
|
||||
|
||||
use crate::{motion::Motion, state::Operator, Vim};
|
||||
use crate::{
|
||||
motion::Motion,
|
||||
state::{Mode, Operator},
|
||||
Vim,
|
||||
};
|
||||
use change::init as change_init;
|
||||
use gpui::{actions, MutableAppContext};
|
||||
use gpui::{actions, MutableAppContext, ViewContext};
|
||||
use language::SelectionGoal;
|
||||
use workspace::Workspace;
|
||||
|
||||
use self::{change::change_over, delete::delete_over};
|
||||
|
||||
actions!(vim, [InsertLineAbove, InsertLineBelow, InsertAfter]);
|
||||
actions!(
|
||||
vim,
|
||||
[
|
||||
InsertAfter,
|
||||
InsertFirstNonWhitespace,
|
||||
InsertEndOfLine,
|
||||
InsertLineAbove,
|
||||
InsertLineBelow,
|
||||
DeleteLeft,
|
||||
DeleteRight,
|
||||
ChangeToEndOfLine,
|
||||
DeleteToEndOfLine,
|
||||
]
|
||||
);
|
||||
|
||||
pub fn init(cx: &mut MutableAppContext) {
|
||||
cx.add_action(insert_after);
|
||||
cx.add_action(insert_first_non_whitespace);
|
||||
cx.add_action(insert_end_of_line);
|
||||
cx.add_action(insert_line_above);
|
||||
cx.add_action(insert_line_below);
|
||||
cx.add_action(|_: &mut Workspace, _: &DeleteLeft, cx| {
|
||||
Vim::update(cx, |vim, cx| {
|
||||
delete_over(vim, Motion::Left, cx);
|
||||
})
|
||||
});
|
||||
cx.add_action(|_: &mut Workspace, _: &DeleteRight, cx| {
|
||||
Vim::update(cx, |vim, cx| {
|
||||
delete_over(vim, Motion::Right, cx);
|
||||
})
|
||||
});
|
||||
cx.add_action(|_: &mut Workspace, _: &ChangeToEndOfLine, cx| {
|
||||
Vim::update(cx, |vim, cx| {
|
||||
change_over(vim, Motion::EndOfLine, cx);
|
||||
})
|
||||
});
|
||||
cx.add_action(|_: &mut Workspace, _: &DeleteToEndOfLine, cx| {
|
||||
Vim::update(cx, |vim, cx| {
|
||||
delete_over(vim, Motion::EndOfLine, cx);
|
||||
})
|
||||
});
|
||||
|
||||
change_init(cx);
|
||||
}
|
||||
|
||||
|
@ -33,6 +78,88 @@ fn move_cursor(vim: &mut Vim, motion: Motion, cx: &mut MutableAppContext) {
|
|||
});
|
||||
}
|
||||
|
||||
fn insert_after(_: &mut Workspace, _: &InsertAfter, cx: &mut ViewContext<Workspace>) {
|
||||
Vim::update(cx, |vim, cx| {
|
||||
vim.switch_mode(Mode::Insert, cx);
|
||||
vim.update_active_editor(cx, |editor, cx| {
|
||||
editor.move_cursors(cx, |map, cursor, goal| {
|
||||
Motion::Right.move_point(map, cursor, goal)
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
fn insert_first_non_whitespace(
|
||||
_: &mut Workspace,
|
||||
_: &InsertFirstNonWhitespace,
|
||||
cx: &mut ViewContext<Workspace>,
|
||||
) {
|
||||
Vim::update(cx, |vim, cx| {
|
||||
vim.switch_mode(Mode::Insert, cx);
|
||||
vim.update_active_editor(cx, |editor, cx| {
|
||||
editor.move_cursors(cx, |map, cursor, goal| {
|
||||
Motion::FirstNonWhitespace.move_point(map, cursor, goal)
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
fn insert_end_of_line(_: &mut Workspace, _: &InsertEndOfLine, cx: &mut ViewContext<Workspace>) {
|
||||
Vim::update(cx, |vim, cx| {
|
||||
vim.switch_mode(Mode::Insert, cx);
|
||||
vim.update_active_editor(cx, |editor, cx| {
|
||||
editor.move_cursors(cx, |map, cursor, goal| {
|
||||
Motion::EndOfLine.move_point(map, cursor, goal)
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
fn insert_line_above(_: &mut Workspace, _: &InsertLineAbove, cx: &mut ViewContext<Workspace>) {
|
||||
Vim::update(cx, |vim, cx| {
|
||||
vim.switch_mode(Mode::Insert, cx);
|
||||
vim.update_active_editor(cx, |editor, cx| {
|
||||
editor.transact(cx, |editor, cx| {
|
||||
editor.move_cursors(cx, |map, cursor, goal| {
|
||||
let (indent, _) = map.line_indent(cursor.row());
|
||||
let (cursor, _) = Motion::EndOfLine.move_point(map, cursor, goal);
|
||||
(cursor, SelectionGoal::Column(indent))
|
||||
});
|
||||
editor.insert("\n", cx);
|
||||
editor.move_cursors(cx, |_, mut cursor, goal| {
|
||||
if let SelectionGoal::Column(column) = goal {
|
||||
*cursor.column_mut() = column;
|
||||
}
|
||||
(cursor, SelectionGoal::None)
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
fn insert_line_below(_: &mut Workspace, _: &InsertLineAbove, cx: &mut ViewContext<Workspace>) {
|
||||
Vim::update(cx, |vim, cx| {
|
||||
vim.switch_mode(Mode::Insert, cx);
|
||||
vim.update_active_editor(cx, |editor, cx| {
|
||||
editor.transact(cx, |editor, cx| {
|
||||
editor.move_cursors(cx, |map, cursor, goal| {
|
||||
let (indent, _) = map.line_indent(cursor.row());
|
||||
let (cursor, _) = Motion::StartOfLine.move_point(map, cursor, goal);
|
||||
(cursor, SelectionGoal::Column(indent))
|
||||
});
|
||||
editor.insert("\n", cx);
|
||||
editor.move_cursors(cx, |_, mut cursor, goal| {
|
||||
*cursor.row_mut() -= 1;
|
||||
if let SelectionGoal::Column(column) = goal {
|
||||
*cursor.column_mut() = column;
|
||||
}
|
||||
(cursor, SelectionGoal::None)
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use indoc::indoc;
|
||||
|
@ -63,18 +190,18 @@ mod test {
|
|||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_l(cx: &mut gpui::TestAppContext) {
|
||||
async fn test_backspace(cx: &mut gpui::TestAppContext) {
|
||||
let cx = VimTestContext::new(cx, true).await;
|
||||
let mut cx = cx.binding(["l"]);
|
||||
cx.assert("The q|uick", "The qu|ick");
|
||||
cx.assert("The quic|k", "The quic|k");
|
||||
let mut cx = cx.binding(["backspace"]);
|
||||
cx.assert("The q|uick", "The |quick");
|
||||
cx.assert("|The quick", "|The quick");
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
The quic|k
|
||||
brown"},
|
||||
The quick
|
||||
|brown"},
|
||||
indoc! {"
|
||||
The quic|k
|
||||
brown"},
|
||||
The quick
|
||||
|brown"},
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -146,6 +273,22 @@ mod test {
|
|||
);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_l(cx: &mut gpui::TestAppContext) {
|
||||
let cx = VimTestContext::new(cx, true).await;
|
||||
let mut cx = cx.binding(["l"]);
|
||||
cx.assert("The q|uick", "The qu|ick");
|
||||
cx.assert("The quic|k", "The quic|k");
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
The quic|k
|
||||
brown"},
|
||||
indoc! {"
|
||||
The quic|k
|
||||
brown"},
|
||||
);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_jump_to_line_boundaries(cx: &mut gpui::TestAppContext) {
|
||||
let cx = VimTestContext::new(cx, true).await;
|
||||
|
@ -242,7 +385,7 @@ mod test {
|
|||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_next_word_start(cx: &mut gpui::TestAppContext) {
|
||||
async fn test_w(cx: &mut gpui::TestAppContext) {
|
||||
let mut cx = VimTestContext::new(cx, true).await;
|
||||
let (_, cursor_offsets) = marked_text(indoc! {"
|
||||
The |quick|-|brown
|
||||
|
@ -289,7 +432,7 @@ mod test {
|
|||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_next_word_end(cx: &mut gpui::TestAppContext) {
|
||||
async fn test_e(cx: &mut gpui::TestAppContext) {
|
||||
let mut cx = VimTestContext::new(cx, true).await;
|
||||
let (_, cursor_offsets) = marked_text(indoc! {"
|
||||
Th|e quic|k|-brow|n
|
||||
|
@ -335,7 +478,7 @@ mod test {
|
|||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_previous_word_start(cx: &mut gpui::TestAppContext) {
|
||||
async fn test_b(cx: &mut gpui::TestAppContext) {
|
||||
let mut cx = VimTestContext::new(cx, true).await;
|
||||
let (_, cursor_offsets) = marked_text(indoc! {"
|
||||
||The |quick|-|brown
|
||||
|
@ -397,7 +540,7 @@ mod test {
|
|||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_move_to_start(cx: &mut gpui::TestAppContext) {
|
||||
async fn test_gg(cx: &mut gpui::TestAppContext) {
|
||||
let cx = VimTestContext::new(cx, true).await;
|
||||
let mut cx = cx.binding(["g", "g"]);
|
||||
cx.assert(
|
||||
|
@ -449,4 +592,418 @@ mod test {
|
|||
over the lazy dog"},
|
||||
);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_a(cx: &mut gpui::TestAppContext) {
|
||||
let cx = VimTestContext::new(cx, true).await;
|
||||
let mut cx = cx.binding(["a"]).mode_after(Mode::Insert);
|
||||
|
||||
cx.assert("The q|uick", "The qu|ick");
|
||||
cx.assert("The quic|k", "The quick|");
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_insert_end_of_line(cx: &mut gpui::TestAppContext) {
|
||||
let cx = VimTestContext::new(cx, true).await;
|
||||
let mut cx = cx.binding(["shift-A"]).mode_after(Mode::Insert);
|
||||
cx.assert("The q|uick", "The quick|");
|
||||
cx.assert("The q|uick ", "The quick |");
|
||||
cx.assert("|", "|");
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
The q|uick
|
||||
brown fox"},
|
||||
indoc! {"
|
||||
The quick|
|
||||
brown fox"},
|
||||
);
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
|
|
||||
The quick"},
|
||||
indoc! {"
|
||||
|
|
||||
The quick"},
|
||||
);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_jump_to_first_non_whitespace(cx: &mut gpui::TestAppContext) {
|
||||
let cx = VimTestContext::new(cx, true).await;
|
||||
let mut cx = cx.binding(["shift-^"]);
|
||||
cx.assert("The q|uick", "|The quick");
|
||||
cx.assert(" The q|uick", " |The quick");
|
||||
cx.assert("|", "|");
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
The q|uick
|
||||
brown fox"},
|
||||
indoc! {"
|
||||
|The quick
|
||||
brown fox"},
|
||||
);
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
|
|
||||
The quick"},
|
||||
indoc! {"
|
||||
|
|
||||
The quick"},
|
||||
);
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
|
|
||||
The quick"},
|
||||
indoc! {"
|
||||
|
|
||||
The quick"},
|
||||
);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_insert_first_non_whitespace(cx: &mut gpui::TestAppContext) {
|
||||
let cx = VimTestContext::new(cx, true).await;
|
||||
let mut cx = cx.binding(["shift-I"]).mode_after(Mode::Insert);
|
||||
cx.assert("The q|uick", "|The quick");
|
||||
cx.assert(" The q|uick", " |The quick");
|
||||
cx.assert("|", "|");
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
The q|uick
|
||||
brown fox"},
|
||||
indoc! {"
|
||||
|The quick
|
||||
brown fox"},
|
||||
);
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
|
|
||||
The quick"},
|
||||
indoc! {"
|
||||
|
|
||||
The quick"},
|
||||
);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_delete_to_end_of_line(cx: &mut gpui::TestAppContext) {
|
||||
let cx = VimTestContext::new(cx, true).await;
|
||||
let mut cx = cx.binding(["shift-D"]);
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
The q|uick
|
||||
brown fox"},
|
||||
indoc! {"
|
||||
The |q
|
||||
brown fox"},
|
||||
);
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
The quick
|
||||
|
|
||||
brown fox"},
|
||||
indoc! {"
|
||||
The quick
|
||||
|
|
||||
brown fox"},
|
||||
);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_x(cx: &mut gpui::TestAppContext) {
|
||||
let cx = VimTestContext::new(cx, true).await;
|
||||
let mut cx = cx.binding(["x"]);
|
||||
cx.assert("|Test", "|est");
|
||||
cx.assert("Te|st", "Te|t");
|
||||
cx.assert("Tes|t", "Te|s");
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
Tes|t
|
||||
test"},
|
||||
indoc! {"
|
||||
Te|s
|
||||
test"},
|
||||
);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_delete_left(cx: &mut gpui::TestAppContext) {
|
||||
let cx = VimTestContext::new(cx, true).await;
|
||||
let mut cx = cx.binding(["shift-X"]);
|
||||
cx.assert("Te|st", "T|st");
|
||||
cx.assert("T|est", "|est");
|
||||
cx.assert("|Test", "|Test");
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
Test
|
||||
|test"},
|
||||
indoc! {"
|
||||
Test
|
||||
|test"},
|
||||
);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_o(cx: &mut gpui::TestAppContext) {
|
||||
let cx = VimTestContext::new(cx, true).await;
|
||||
let mut cx = cx.binding(["o"]).mode_after(Mode::Insert);
|
||||
|
||||
cx.assert(
|
||||
"|",
|
||||
indoc! {"
|
||||
|
||||
|"},
|
||||
);
|
||||
cx.assert(
|
||||
"The |quick",
|
||||
indoc! {"
|
||||
The quick
|
||||
|"},
|
||||
);
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
The quick
|
||||
brown |fox
|
||||
jumps over"},
|
||||
indoc! {"
|
||||
The quick
|
||||
brown fox
|
||||
|
|
||||
jumps over"},
|
||||
);
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
The quick
|
||||
brown fox
|
||||
jumps |over"},
|
||||
indoc! {"
|
||||
The quick
|
||||
brown fox
|
||||
jumps over
|
||||
|"},
|
||||
);
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
The q|uick
|
||||
brown fox
|
||||
jumps over"},
|
||||
indoc! {"
|
||||
The quick
|
||||
|
|
||||
brown fox
|
||||
jumps over"},
|
||||
);
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
The quick
|
||||
|
|
||||
brown fox"},
|
||||
indoc! {"
|
||||
The quick
|
||||
|
||||
|
|
||||
brown fox"},
|
||||
);
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
fn test() {
|
||||
println!(|);
|
||||
}"},
|
||||
indoc! {"
|
||||
fn test() {
|
||||
println!();
|
||||
|
|
||||
}"},
|
||||
);
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
fn test(|) {
|
||||
println!();
|
||||
}"},
|
||||
indoc! {"
|
||||
fn test() {
|
||||
|
|
||||
println!();
|
||||
}"},
|
||||
);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_insert_line_above(cx: &mut gpui::TestAppContext) {
|
||||
let cx = VimTestContext::new(cx, true).await;
|
||||
let mut cx = cx.binding(["shift-O"]).mode_after(Mode::Insert);
|
||||
|
||||
cx.assert(
|
||||
"|",
|
||||
indoc! {"
|
||||
|
|
||||
"},
|
||||
);
|
||||
cx.assert(
|
||||
"The |quick",
|
||||
indoc! {"
|
||||
|
|
||||
The quick"},
|
||||
);
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
The quick
|
||||
brown |fox
|
||||
jumps over"},
|
||||
indoc! {"
|
||||
The quick
|
||||
|
|
||||
brown fox
|
||||
jumps over"},
|
||||
);
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
The quick
|
||||
brown fox
|
||||
jumps |over"},
|
||||
indoc! {"
|
||||
The quick
|
||||
brown fox
|
||||
|
|
||||
jumps over"},
|
||||
);
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
The q|uick
|
||||
brown fox
|
||||
jumps over"},
|
||||
indoc! {"
|
||||
|
|
||||
The quick
|
||||
brown fox
|
||||
jumps over"},
|
||||
);
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
The quick
|
||||
|
|
||||
brown fox"},
|
||||
indoc! {"
|
||||
The quick
|
||||
|
|
||||
|
||||
brown fox"},
|
||||
);
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
fn test() {
|
||||
println!(|);
|
||||
}"},
|
||||
indoc! {"
|
||||
fn test() {
|
||||
|
|
||||
println!();
|
||||
}"},
|
||||
);
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
fn test(|) {
|
||||
println!();
|
||||
}"},
|
||||
indoc! {"
|
||||
|
|
||||
fn test() {
|
||||
println!();
|
||||
}"},
|
||||
);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_dd(cx: &mut gpui::TestAppContext) {
|
||||
let cx = VimTestContext::new(cx, true).await;
|
||||
let mut cx = cx.binding(["d", "d"]);
|
||||
|
||||
cx.assert("|", "|");
|
||||
cx.assert("The |quick", "|");
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
The quick
|
||||
brown |fox
|
||||
jumps over"},
|
||||
indoc! {"
|
||||
The quick
|
||||
jumps |over"},
|
||||
);
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
The quick
|
||||
brown fox
|
||||
jumps |over"},
|
||||
indoc! {"
|
||||
The quick
|
||||
brown |fox"},
|
||||
);
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
The q|uick
|
||||
brown fox
|
||||
jumps over"},
|
||||
indoc! {"
|
||||
brown| fox
|
||||
jumps over"},
|
||||
);
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
The quick
|
||||
|
|
||||
brown fox"},
|
||||
indoc! {"
|
||||
The quick
|
||||
|brown fox"},
|
||||
);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_cc(cx: &mut gpui::TestAppContext) {
|
||||
let cx = VimTestContext::new(cx, true).await;
|
||||
let mut cx = cx.binding(["c", "c"]).mode_after(Mode::Insert);
|
||||
|
||||
cx.assert("|", "|");
|
||||
cx.assert("The |quick", "|");
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
The quick
|
||||
brown |fox
|
||||
jumps over"},
|
||||
indoc! {"
|
||||
The quick
|
||||
|
|
||||
jumps over"},
|
||||
);
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
The quick
|
||||
brown fox
|
||||
jumps |over"},
|
||||
indoc! {"
|
||||
The quick
|
||||
brown fox
|
||||
|"},
|
||||
);
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
The q|uick
|
||||
brown fox
|
||||
jumps over"},
|
||||
indoc! {"
|
||||
|
|
||||
brown fox
|
||||
jumps over"},
|
||||
);
|
||||
cx.assert(
|
||||
indoc! {"
|
||||
The quick
|
||||
|
|
||||
brown fox"},
|
||||
indoc! {"
|
||||
The quick
|
||||
|
|
||||
brown fox"},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue