Multicursor vim registers (#13025)
Release Notes: - vim: Added support for multicursor registers (#11687) - vim: Added support for the `"/` register
This commit is contained in:
parent
068b1c235c
commit
a5af5b2883
17 changed files with 333 additions and 250 deletions
|
@ -1,8 +1,8 @@
|
|||
use crate::{
|
||||
motion::{self, Motion},
|
||||
normal::yank::copy_selections_content,
|
||||
object::Object,
|
||||
state::Mode,
|
||||
utils::copy_selections_content,
|
||||
Vim,
|
||||
};
|
||||
use editor::{
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::{motion::Motion, object::Object, utils::copy_selections_content, Vim};
|
||||
use crate::{motion::Motion, normal::yank::copy_selections_content, object::Object, Vim};
|
||||
use collections::{HashMap, HashSet};
|
||||
use editor::{
|
||||
display_map::{DisplaySnapshot, ToDisplayPoint},
|
||||
|
|
|
@ -1,19 +1,15 @@
|
|||
use std::cmp;
|
||||
|
||||
use editor::{
|
||||
display_map::ToDisplayPoint, movement, scroll::Autoscroll, ClipboardSelection, DisplayPoint,
|
||||
RowExt,
|
||||
};
|
||||
use gpui::{impl_actions, AppContext, ViewContext};
|
||||
use editor::{display_map::ToDisplayPoint, movement, scroll::Autoscroll, DisplayPoint, RowExt};
|
||||
use gpui::{impl_actions, ViewContext};
|
||||
use language::{Bias, SelectionGoal};
|
||||
use serde::Deserialize;
|
||||
use settings::Settings;
|
||||
use workspace::Workspace;
|
||||
|
||||
use crate::{
|
||||
state::Mode,
|
||||
utils::{copy_selections_content, SYSTEM_CLIPBOARD},
|
||||
UseSystemClipboard, Vim, VimSettings,
|
||||
normal::yank::copy_selections_content,
|
||||
state::{Mode, Register},
|
||||
Vim,
|
||||
};
|
||||
|
||||
#[derive(Clone, Deserialize, PartialEq)]
|
||||
|
@ -31,16 +27,6 @@ pub(crate) fn register(workspace: &mut Workspace, _: &mut ViewContext<Workspace>
|
|||
workspace.register_action(paste);
|
||||
}
|
||||
|
||||
fn system_clipboard_is_newer(vim: &Vim, cx: &mut AppContext) -> bool {
|
||||
cx.read_from_clipboard().is_some_and(|item| {
|
||||
if let Some(last_state) = vim.workspace_state.registers.get(&SYSTEM_CLIPBOARD) {
|
||||
last_state != item.text()
|
||||
} else {
|
||||
true
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn paste(_: &mut Workspace, action: &Paste, cx: &mut ViewContext<Workspace>) {
|
||||
Vim::update(cx, |vim, cx| {
|
||||
vim.record_current_action(cx);
|
||||
|
@ -50,40 +36,19 @@ fn paste(_: &mut Workspace, action: &Paste, cx: &mut ViewContext<Workspace>) {
|
|||
editor.transact(cx, |editor, cx| {
|
||||
editor.set_clip_at_line_ends(false, cx);
|
||||
|
||||
let (clipboard_text, clipboard_selections): (String, Option<_>) = if let Some(
|
||||
register,
|
||||
) =
|
||||
vim.update_state(|state| state.selected_register.take())
|
||||
{
|
||||
(
|
||||
vim.read_register(register, Some(editor), cx)
|
||||
.unwrap_or_default(),
|
||||
None,
|
||||
)
|
||||
} else if VimSettings::get_global(cx).use_system_clipboard
|
||||
== UseSystemClipboard::Never
|
||||
|| VimSettings::get_global(cx).use_system_clipboard
|
||||
== UseSystemClipboard::OnYank
|
||||
&& !system_clipboard_is_newer(vim, cx)
|
||||
{
|
||||
(vim.read_register('"', None, cx).unwrap_or_default(), None)
|
||||
} else {
|
||||
if let Some(item) = cx.read_from_clipboard() {
|
||||
let clipboard_selections = item
|
||||
.metadata::<Vec<ClipboardSelection>>()
|
||||
.filter(|clipboard_selections| {
|
||||
clipboard_selections.len() > 1
|
||||
&& vim.state().mode != Mode::VisualLine
|
||||
});
|
||||
(item.text().clone(), clipboard_selections)
|
||||
} else {
|
||||
("".into(), None)
|
||||
}
|
||||
};
|
||||
let selected_register = vim.update_state(|state| state.selected_register.take());
|
||||
|
||||
if clipboard_text.is_empty() {
|
||||
let Some(Register {
|
||||
text,
|
||||
clipboard_selections,
|
||||
}) = vim
|
||||
.read_register(selected_register, Some(editor), cx)
|
||||
.filter(|reg| !reg.text.is_empty())
|
||||
else {
|
||||
return;
|
||||
}
|
||||
};
|
||||
let clipboard_selections = clipboard_selections
|
||||
.filter(|sel| sel.len() > 1 && vim.state().mode != Mode::VisualLine);
|
||||
|
||||
if !action.preserve_clipboard && vim.state().mode.is_visual() {
|
||||
copy_selections_content(vim, editor, vim.state().mode == Mode::VisualLine, cx);
|
||||
|
@ -135,14 +100,14 @@ fn paste(_: &mut Workspace, action: &Paste, cx: &mut ViewContext<Workspace>) {
|
|||
if let Some(clipboard_selections) = &clipboard_selections {
|
||||
if let Some(clipboard_selection) = clipboard_selections.get(ix) {
|
||||
let end_offset = start_offset + clipboard_selection.len;
|
||||
let text = clipboard_text[start_offset..end_offset].to_string();
|
||||
let text = text[start_offset..end_offset].to_string();
|
||||
start_offset = end_offset + 1;
|
||||
(text, Some(clipboard_selection.first_line_indent))
|
||||
} else {
|
||||
("".to_string(), first_selection_indent_column)
|
||||
}
|
||||
} else {
|
||||
(clipboard_text.to_string(), first_selection_indent_column)
|
||||
(text.to_string(), first_selection_indent_column)
|
||||
};
|
||||
let line_mode = to_insert.ends_with('\n');
|
||||
let is_multiline = to_insert.contains('\n');
|
||||
|
@ -679,6 +644,7 @@ mod test {
|
|||
cx.shared_register('a').await.assert_eq("jumps ");
|
||||
cx.simulate_shared_keystrokes("\" shift-a d i w").await;
|
||||
cx.shared_register('a').await.assert_eq("jumps over");
|
||||
cx.shared_register('"').await.assert_eq("jumps over");
|
||||
cx.simulate_shared_keystrokes("\" a p").await;
|
||||
cx.shared_state().await.assert_eq(indoc! {"
|
||||
The quick brown
|
||||
|
@ -719,12 +685,50 @@ mod test {
|
|||
cx.shared_clipboard().await.assert_eq("lazy dog");
|
||||
cx.shared_register('"').await.assert_eq("lazy dog");
|
||||
|
||||
cx.simulate_shared_keystrokes("/ d o g enter").await;
|
||||
cx.shared_register('/').await.assert_eq("dog");
|
||||
cx.simulate_shared_keystrokes("\" / shift-p").await;
|
||||
cx.shared_state().await.assert_eq(indoc! {"
|
||||
The quick brown
|
||||
doˇg"});
|
||||
|
||||
// not testing nvim as it doesn't have a filename
|
||||
cx.simulate_keystrokes("\" % p");
|
||||
cx.assert_state(
|
||||
indoc! {"
|
||||
The quick brown
|
||||
dir/file.rˇs"},
|
||||
dogdir/file.rˇs"},
|
||||
Mode::Normal,
|
||||
);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_multicursor_paste(cx: &mut gpui::TestAppContext) {
|
||||
let mut cx = VimTestContext::new(cx, true).await;
|
||||
|
||||
cx.update_global(|store: &mut SettingsStore, cx| {
|
||||
store.update_user_settings::<VimSettings>(cx, |s| {
|
||||
s.use_system_clipboard = Some(UseSystemClipboard::Never)
|
||||
});
|
||||
});
|
||||
|
||||
cx.set_state(
|
||||
indoc! {"
|
||||
ˇfish one
|
||||
fish two
|
||||
fish red
|
||||
fish blue
|
||||
"},
|
||||
Mode::Normal,
|
||||
);
|
||||
cx.simulate_keystrokes("4 g l w escape d i w 0 shift-p");
|
||||
cx.assert_state(
|
||||
indoc! {"
|
||||
onˇefish•
|
||||
twˇofish•
|
||||
reˇdfish•
|
||||
bluˇefish•
|
||||
"},
|
||||
Mode::Normal,
|
||||
);
|
||||
}
|
||||
|
|
|
@ -165,6 +165,9 @@ fn search_submit(workspace: &mut Workspace, _: &SearchSubmit, cx: &mut ViewConte
|
|||
{
|
||||
count = count.saturating_sub(1)
|
||||
}
|
||||
vim.workspace_state
|
||||
.registers
|
||||
.insert('/', search_bar.query(cx).into());
|
||||
state.count = 1;
|
||||
search_bar.select_match(direction, count, cx);
|
||||
search_bar.focus_editor(&Default::default(), cx);
|
||||
|
|
|
@ -3,7 +3,7 @@ use gpui::{actions, ViewContext, WindowContext};
|
|||
use language::Point;
|
||||
use workspace::Workspace;
|
||||
|
||||
use crate::{motion::Motion, utils::copy_selections_content, Mode, Vim};
|
||||
use crate::{motion::Motion, normal::yank::copy_selections_content, Mode, Vim};
|
||||
|
||||
actions!(vim, [Substitute, SubstituteLine]);
|
||||
|
||||
|
|
|
@ -1,6 +1,17 @@
|
|||
use crate::{motion::Motion, object::Object, utils::yank_selections_content, Vim};
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::{
|
||||
motion::Motion,
|
||||
object::Object,
|
||||
state::{Mode, Register},
|
||||
Vim,
|
||||
};
|
||||
use collections::HashMap;
|
||||
use editor::{ClipboardSelection, Editor};
|
||||
use gpui::WindowContext;
|
||||
use language::Point;
|
||||
use multi_buffer::MultiBufferRow;
|
||||
use ui::ViewContext;
|
||||
|
||||
pub fn yank_motion(vim: &mut Vim, motion: Motion, times: Option<usize>, cx: &mut WindowContext) {
|
||||
vim.update_active_editor(cx, |vim, editor, cx| {
|
||||
|
@ -48,3 +59,130 @@ pub fn yank_object(vim: &mut Vim, object: Object, around: bool, cx: &mut WindowC
|
|||
});
|
||||
});
|
||||
}
|
||||
|
||||
pub fn yank_selections_content(
|
||||
vim: &mut Vim,
|
||||
editor: &mut Editor,
|
||||
linewise: bool,
|
||||
cx: &mut ViewContext<Editor>,
|
||||
) {
|
||||
copy_selections_content_internal(vim, editor, linewise, true, cx);
|
||||
}
|
||||
|
||||
pub fn copy_selections_content(
|
||||
vim: &mut Vim,
|
||||
editor: &mut Editor,
|
||||
linewise: bool,
|
||||
cx: &mut ViewContext<Editor>,
|
||||
) {
|
||||
copy_selections_content_internal(vim, editor, linewise, false, cx);
|
||||
}
|
||||
|
||||
struct HighlightOnYank;
|
||||
|
||||
fn copy_selections_content_internal(
|
||||
vim: &mut Vim,
|
||||
editor: &mut Editor,
|
||||
linewise: bool,
|
||||
is_yank: bool,
|
||||
cx: &mut ViewContext<Editor>,
|
||||
) {
|
||||
let selections = editor.selections.all_adjusted(cx);
|
||||
let buffer = editor.buffer().read(cx).snapshot(cx);
|
||||
let mut text = String::new();
|
||||
let mut clipboard_selections = Vec::with_capacity(selections.len());
|
||||
let mut ranges_to_highlight = Vec::new();
|
||||
|
||||
vim.update_state(|state| {
|
||||
state.marks.insert(
|
||||
"[".to_string(),
|
||||
selections
|
||||
.iter()
|
||||
.map(|s| buffer.anchor_before(s.start))
|
||||
.collect(),
|
||||
);
|
||||
state.marks.insert(
|
||||
"]".to_string(),
|
||||
selections
|
||||
.iter()
|
||||
.map(|s| buffer.anchor_after(s.end))
|
||||
.collect(),
|
||||
)
|
||||
});
|
||||
|
||||
{
|
||||
let mut is_first = true;
|
||||
for selection in selections.iter() {
|
||||
let mut start = selection.start;
|
||||
let end = selection.end;
|
||||
if is_first {
|
||||
is_first = false;
|
||||
} else {
|
||||
text.push_str("\n");
|
||||
}
|
||||
let initial_len = text.len();
|
||||
|
||||
// if the file does not end with \n, and our line-mode selection ends on
|
||||
// that line, we will have expanded the start of the selection to ensure it
|
||||
// contains a newline (so that delete works as expected). We undo that change
|
||||
// here.
|
||||
let is_last_line = linewise
|
||||
&& end.row == buffer.max_buffer_row().0
|
||||
&& buffer.max_point().column > 0
|
||||
&& start.row < buffer.max_buffer_row().0
|
||||
&& start == Point::new(start.row, buffer.line_len(MultiBufferRow(start.row)));
|
||||
|
||||
if is_last_line {
|
||||
start = Point::new(start.row + 1, 0);
|
||||
}
|
||||
|
||||
let start_anchor = buffer.anchor_after(start);
|
||||
let end_anchor = buffer.anchor_before(end);
|
||||
ranges_to_highlight.push(start_anchor..end_anchor);
|
||||
|
||||
for chunk in buffer.text_for_range(start..end) {
|
||||
text.push_str(chunk);
|
||||
}
|
||||
if is_last_line {
|
||||
text.push_str("\n");
|
||||
}
|
||||
clipboard_selections.push(ClipboardSelection {
|
||||
len: text.len() - initial_len,
|
||||
is_entire_line: linewise,
|
||||
first_line_indent: buffer.indent_size_for_line(MultiBufferRow(start.row)).len,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
let selected_register = vim.update_state(|state| state.selected_register.take());
|
||||
vim.write_registers(
|
||||
Register {
|
||||
text: text.into(),
|
||||
clipboard_selections: Some(clipboard_selections),
|
||||
},
|
||||
selected_register,
|
||||
is_yank,
|
||||
linewise,
|
||||
cx,
|
||||
);
|
||||
|
||||
if !is_yank || vim.state().mode == Mode::Visual {
|
||||
return;
|
||||
}
|
||||
|
||||
editor.highlight_background::<HighlightOnYank>(
|
||||
&ranges_to_highlight,
|
||||
|colors| colors.editor_document_highlight_read_background,
|
||||
cx,
|
||||
);
|
||||
cx.spawn(|this, mut cx| async move {
|
||||
cx.background_executor()
|
||||
.timer(Duration::from_millis(200))
|
||||
.await;
|
||||
this.update(&mut cx, |editor, cx| {
|
||||
editor.clear_background_highlights::<HighlightOnYank>(cx)
|
||||
})
|
||||
.ok();
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue