vim: Add Separator and RemoveIndent in Join Lines, fix gJ use space join (#22496)
Closes #22492 Release Notes: - Added Join Lines Separator And RemoveIndent --------- Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
This commit is contained in:
parent
677868ba1a
commit
4deab8a0b9
6 changed files with 98 additions and 26 deletions
|
@ -197,6 +197,7 @@
|
||||||
"d": ["vim::PushOperator", "Delete"],
|
"d": ["vim::PushOperator", "Delete"],
|
||||||
"shift-d": "vim::DeleteToEndOfLine",
|
"shift-d": "vim::DeleteToEndOfLine",
|
||||||
"shift-j": "vim::JoinLines",
|
"shift-j": "vim::JoinLines",
|
||||||
|
"g shift-j": "vim::JoinLinesNoWhitespace",
|
||||||
"y": ["vim::PushOperator", "Yank"],
|
"y": ["vim::PushOperator", "Yank"],
|
||||||
"shift-y": "vim::YankLine",
|
"shift-y": "vim::YankLine",
|
||||||
"i": "vim::InsertBefore",
|
"i": "vim::InsertBefore",
|
||||||
|
@ -278,6 +279,7 @@
|
||||||
"g shift-i": "vim::VisualInsertFirstNonWhiteSpace",
|
"g shift-i": "vim::VisualInsertFirstNonWhiteSpace",
|
||||||
"g shift-a": "vim::VisualInsertEndOfLine",
|
"g shift-a": "vim::VisualInsertEndOfLine",
|
||||||
"shift-j": "vim::JoinLines",
|
"shift-j": "vim::JoinLines",
|
||||||
|
"g shift-j": "vim::JoinLinesNoWhitespace",
|
||||||
"r": ["vim::PushOperator", "Replace"],
|
"r": ["vim::PushOperator", "Replace"],
|
||||||
"ctrl-c": ["vim::SwitchMode", "Normal"],
|
"ctrl-c": ["vim::SwitchMode", "Normal"],
|
||||||
"escape": ["vim::SwitchMode", "Normal"],
|
"escape": ["vim::SwitchMode", "Normal"],
|
||||||
|
|
|
@ -204,7 +204,7 @@ impl_actions!(
|
||||||
ToggleCodeActions,
|
ToggleCodeActions,
|
||||||
ToggleComments,
|
ToggleComments,
|
||||||
UnfoldAt,
|
UnfoldAt,
|
||||||
FoldAtLevel
|
FoldAtLevel,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -5851,7 +5851,7 @@ impl Editor {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn join_lines(&mut self, _: &JoinLines, cx: &mut ViewContext<Self>) {
|
pub fn join_lines_impl(&mut self, insert_whitespace: bool, cx: &mut ViewContext<Self>) {
|
||||||
if self.read_only(cx) {
|
if self.read_only(cx) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -5893,11 +5893,12 @@ impl Editor {
|
||||||
let indent = snapshot.indent_size_for_line(next_line_row);
|
let indent = snapshot.indent_size_for_line(next_line_row);
|
||||||
let start_of_next_line = Point::new(next_line_row.0, indent.len);
|
let start_of_next_line = Point::new(next_line_row.0, indent.len);
|
||||||
|
|
||||||
let replace = if snapshot.line_len(next_line_row) > indent.len {
|
let replace =
|
||||||
" "
|
if snapshot.line_len(next_line_row) > indent.len && insert_whitespace {
|
||||||
} else {
|
" "
|
||||||
""
|
} else {
|
||||||
};
|
""
|
||||||
|
};
|
||||||
|
|
||||||
this.buffer.update(cx, |buffer, cx| {
|
this.buffer.update(cx, |buffer, cx| {
|
||||||
buffer.edit([(end_of_line..start_of_next_line, replace)], None, cx)
|
buffer.edit([(end_of_line..start_of_next_line, replace)], None, cx)
|
||||||
|
@ -5911,6 +5912,10 @@ impl Editor {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn join_lines(&mut self, _: &JoinLines, cx: &mut ViewContext<Self>) {
|
||||||
|
self.join_lines_impl(true, cx);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn sort_lines_case_sensitive(
|
pub fn sort_lines_case_sensitive(
|
||||||
&mut self,
|
&mut self,
|
||||||
_: &SortLinesCaseSensitive,
|
_: &SortLinesCaseSensitive,
|
||||||
|
|
|
@ -44,6 +44,8 @@ actions!(
|
||||||
InsertLineAbove,
|
InsertLineAbove,
|
||||||
InsertLineBelow,
|
InsertLineBelow,
|
||||||
InsertAtPrevious,
|
InsertAtPrevious,
|
||||||
|
JoinLines,
|
||||||
|
JoinLinesNoWhitespace,
|
||||||
DeleteLeft,
|
DeleteLeft,
|
||||||
DeleteRight,
|
DeleteRight,
|
||||||
ChangeToEndOfLine,
|
ChangeToEndOfLine,
|
||||||
|
@ -53,7 +55,6 @@ actions!(
|
||||||
ChangeCase,
|
ChangeCase,
|
||||||
ConvertToUpperCase,
|
ConvertToUpperCase,
|
||||||
ConvertToLowerCase,
|
ConvertToLowerCase,
|
||||||
JoinLines,
|
|
||||||
ToggleComments,
|
ToggleComments,
|
||||||
Undo,
|
Undo,
|
||||||
Redo,
|
Redo,
|
||||||
|
@ -108,25 +109,11 @@ pub(crate) fn register(editor: &mut Editor, cx: &mut ViewContext<Vim>) {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
Vim::action(editor, cx, |vim, _: &JoinLines, cx| {
|
Vim::action(editor, cx, |vim, _: &JoinLines, cx| {
|
||||||
vim.record_current_action(cx);
|
vim.join_lines_impl(true, cx);
|
||||||
let mut times = Vim::take_count(cx).unwrap_or(1);
|
});
|
||||||
if vim.mode.is_visual() {
|
|
||||||
times = 1;
|
|
||||||
} else if times > 1 {
|
|
||||||
// 2J joins two lines together (same as J or 1J)
|
|
||||||
times -= 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
vim.update_editor(cx, |_, editor, cx| {
|
Vim::action(editor, cx, |vim, _: &JoinLinesNoWhitespace, cx| {
|
||||||
editor.transact(cx, |editor, cx| {
|
vim.join_lines_impl(false, cx);
|
||||||
for _ in 0..times {
|
|
||||||
editor.join_lines(&Default::default(), cx)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
});
|
|
||||||
if vim.mode.is_visual() {
|
|
||||||
vim.switch_mode(Mode::Normal, true, cx)
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
Vim::action(editor, cx, |vim, _: &Undo, cx| {
|
Vim::action(editor, cx, |vim, _: &Undo, cx| {
|
||||||
|
@ -401,6 +388,28 @@ impl Vim {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn join_lines_impl(&mut self, insert_whitespace: bool, cx: &mut ViewContext<Self>) {
|
||||||
|
self.record_current_action(cx);
|
||||||
|
let mut times = Vim::take_count(cx).unwrap_or(1);
|
||||||
|
if self.mode.is_visual() {
|
||||||
|
times = 1;
|
||||||
|
} else if times > 1 {
|
||||||
|
// 2J joins two lines together (same as J or 1J)
|
||||||
|
times -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.update_editor(cx, |_, editor, cx| {
|
||||||
|
editor.transact(cx, |editor, cx| {
|
||||||
|
for _ in 0..times {
|
||||||
|
editor.join_lines_impl(insert_whitespace, cx)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
});
|
||||||
|
if self.mode.is_visual() {
|
||||||
|
self.switch_mode(Mode::Normal, true, cx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn yank_line(&mut self, _: &YankLine, cx: &mut ViewContext<Self>) {
|
fn yank_line(&mut self, _: &YankLine, cx: &mut ViewContext<Self>) {
|
||||||
let count = Vim::take_count(cx);
|
let count = Vim::take_count(cx);
|
||||||
self.yank_motion(motion::Motion::CurrentLine, count, cx)
|
self.yank_motion(motion::Motion::CurrentLine, count, cx)
|
||||||
|
|
|
@ -367,6 +367,46 @@ async fn test_join_lines(cx: &mut gpui::TestAppContext) {
|
||||||
two three fourˇ five
|
two three fourˇ five
|
||||||
six
|
six
|
||||||
"});
|
"});
|
||||||
|
|
||||||
|
cx.set_shared_state(indoc! {"
|
||||||
|
ˇone
|
||||||
|
two
|
||||||
|
three
|
||||||
|
four
|
||||||
|
five
|
||||||
|
six
|
||||||
|
"})
|
||||||
|
.await;
|
||||||
|
cx.simulate_shared_keystrokes("g shift-j").await;
|
||||||
|
cx.shared_state().await.assert_eq(indoc! {"
|
||||||
|
oneˇtwo
|
||||||
|
three
|
||||||
|
four
|
||||||
|
five
|
||||||
|
six
|
||||||
|
"});
|
||||||
|
cx.simulate_shared_keystrokes("3 g shift-j").await;
|
||||||
|
cx.shared_state().await.assert_eq(indoc! {"
|
||||||
|
onetwothreeˇfour
|
||||||
|
five
|
||||||
|
six
|
||||||
|
"});
|
||||||
|
|
||||||
|
cx.set_shared_state(indoc! {"
|
||||||
|
ˇone
|
||||||
|
two
|
||||||
|
three
|
||||||
|
four
|
||||||
|
five
|
||||||
|
six
|
||||||
|
"})
|
||||||
|
.await;
|
||||||
|
cx.simulate_shared_keystrokes("j v 3 j g shift-j").await;
|
||||||
|
cx.shared_state().await.assert_eq(indoc! {"
|
||||||
|
one
|
||||||
|
twothreefourˇfive
|
||||||
|
six
|
||||||
|
"});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
|
|
|
@ -11,3 +11,19 @@
|
||||||
{"Key":"j"}
|
{"Key":"j"}
|
||||||
{"Key":"shift-j"}
|
{"Key":"shift-j"}
|
||||||
{"Get":{"state":"one\ntwo three fourˇ five\nsix\n","mode":"Normal"}}
|
{"Get":{"state":"one\ntwo three fourˇ five\nsix\n","mode":"Normal"}}
|
||||||
|
{"Put":{"state":"ˇone\ntwo\nthree\nfour\nfive\nsix\n"}}
|
||||||
|
{"Key":"g"}
|
||||||
|
{"Key":"shift-j"}
|
||||||
|
{"Get":{"state":"oneˇtwo\nthree\nfour\nfive\nsix\n","mode":"Normal"}}
|
||||||
|
{"Key":"3"}
|
||||||
|
{"Key":"g"}
|
||||||
|
{"Key":"shift-j"}
|
||||||
|
{"Get":{"state":"onetwothreeˇfour\nfive\nsix\n","mode":"Normal"}}
|
||||||
|
{"Put":{"state":"ˇone\ntwo\nthree\nfour\nfive\nsix\n"}}
|
||||||
|
{"Key":"j"}
|
||||||
|
{"Key":"v"}
|
||||||
|
{"Key":"3"}
|
||||||
|
{"Key":"j"}
|
||||||
|
{"Key":"g"}
|
||||||
|
{"Key":"shift-j"}
|
||||||
|
{"Get":{"state":"one\ntwothreefourˇfive\nsix\n","mode":"Normal"}}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue