Files
zed/crates/vim/src/normal/yank.rs
T
Mohamad KhaniandClaude Fable 5 b72a46db68
Update All Top Ranking Issues / update_top_ranking_issues (push) Has been cancelled
release_nightly / notify_on_failure (push) Has been cancelled
release_nightly / check_style (push) Has been cancelled
release_nightly / run_tests_windows (push) Has been cancelled
release_nightly / clippy_windows (push) Has been cancelled
release_nightly / bundle_linux_aarch64 (push) Has been cancelled
release_nightly / bundle_linux_x86_64 (push) Has been cancelled
release_nightly / bundle_mac_aarch64 (push) Has been cancelled
release_nightly / bundle_mac_x86_64 (push) Has been cancelled
release_nightly / bundle_windows_aarch64 (push) Has been cancelled
release_nightly / bundle_windows_x86_64 (push) Has been cancelled
release_nightly / build_nix_mac_aarch64 (push) Has been cancelled
release_nightly / update_nightly_tag (push) Has been cancelled
Hotfix Review Monitor / check-hotfix-reviews (push) Has been cancelled
Stale PR Review Reminder / check-stale-prs (push) Has been cancelled
Update Weekly Top Ranking Issues / update_top_ranking_issues (push) Has been cancelled
Triage Project Sync (#84) / Sync triage project (push) Has been cancelled
release_nightly / build_nix_linux_x86_64 (push) Has been cancelled
Bump collab-staging Tag / update-collab-staging-tag (push) Has been cancelled
compliance_check / scheduled_compliance_check (push) Has been cancelled
logiguard fork: GPUI xdg-activation keyboard-focus serial fix
Single-commit orphan branch: full zed-industries/zed @ 8c74db0 source tree
with a 3-file patch applied (no upstream history).

Patch (crates/gpui_linux/src/linux/wayland/):
  - serial.rs: add SerialKind::KeyboardEnter
  - client.rs: store wl_keyboard.enter serial; add latest_serial_of()
  - window.rs: activate() uses keyboard-enter serial (Mutter focus gate)

Mutter honors window activation only when the token carries the keyboard-
focus serial from wl_keyboard.enter; GPUI used a stale mouse-press serial.
See docs/tray-window-focus-wayland.md in logiguard.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-14 02:22:17 +03:30

246 lines
8.0 KiB
Rust

use std::{ops::Range, time::Duration};
use crate::{
Vim, VimSettings,
motion::{Motion, MotionKind},
object::Object,
state::{Mode, Register},
};
use collections::HashMap;
use editor::{ClipboardSelection, Editor, HighlightKey, SelectionEffects};
use gpui::Context;
use gpui::Window;
use language::Point;
use settings::Settings;
impl Vim {
pub fn yank_motion(
&mut self,
motion: Motion,
times: Option<usize>,
forced_motion: bool,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.update_editor(cx, |vim, editor, cx| {
let text_layout_details = editor.text_layout_details(window, cx);
editor.transact(window, cx, |editor, window, cx| {
editor.set_clip_at_line_ends(false, cx);
let mut original_positions: HashMap<_, _> = Default::default();
let mut kind = None;
editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
s.move_with(&mut |map, selection| {
let original_position = (selection.head(), selection.goal);
kind = motion.expand_selection(
map,
selection,
times,
&text_layout_details,
forced_motion,
);
if kind == Some(MotionKind::Exclusive) {
original_positions
.insert(selection.id, (selection.start, selection.goal));
} else {
original_positions.insert(selection.id, original_position);
}
})
});
let Some(kind) = kind else { return };
vim.yank_selections_content(editor, kind, window, cx);
editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
s.move_with(&mut |_, selection| {
let (head, goal) = original_positions.remove(&selection.id).unwrap();
selection.collapse_to(head, goal);
});
});
});
});
self.exit_temporary_normal(window, cx);
}
pub fn yank_object(
&mut self,
object: Object,
around: bool,
times: Option<usize>,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.update_editor(cx, |vim, editor, cx| {
editor.transact(window, cx, |editor, window, cx| {
editor.set_clip_at_line_ends(false, cx);
let mut start_positions: HashMap<_, _> = Default::default();
editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
s.move_with(&mut |map, selection| {
object.expand_selection(map, selection, around, times);
let start_position = (selection.start, selection.goal);
start_positions.insert(selection.id, start_position);
});
});
let kind = match object.target_visual_mode(vim.mode, around) {
Mode::VisualLine => MotionKind::Linewise,
_ => MotionKind::Exclusive,
};
vim.yank_selections_content(editor, kind, window, cx);
editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
s.move_with(&mut |_, selection| {
let (head, goal) = start_positions.remove(&selection.id).unwrap();
selection.collapse_to(head, goal);
});
});
});
});
self.exit_temporary_normal(window, cx);
}
pub fn yank_selections_content(
&mut self,
editor: &mut Editor,
kind: MotionKind,
window: &mut Window,
cx: &mut Context<Editor>,
) {
self.copy_ranges(
editor,
kind,
true,
editor
.selections
.all_adjusted(&editor.display_snapshot(cx))
.iter()
.map(|s| s.range())
.collect(),
window,
cx,
)
}
pub fn copy_selections_content(
&mut self,
editor: &mut Editor,
kind: MotionKind,
window: &mut Window,
cx: &mut Context<Editor>,
) {
self.copy_ranges(
editor,
kind,
false,
editor
.selections
.all_adjusted(&editor.display_snapshot(cx))
.iter()
.map(|s| s.range())
.collect(),
window,
cx,
)
}
pub(crate) fn copy_ranges(
&mut self,
editor: &mut Editor,
kind: MotionKind,
is_yank: bool,
selections: Vec<Range<Point>>,
window: &mut Window,
cx: &mut Context<Editor>,
) {
let buffer = editor.buffer().read(cx).snapshot(cx);
self.set_mark(
"[".to_string(),
selections
.iter()
.map(|s| buffer.anchor_before(s.start))
.collect(),
editor.buffer(),
window,
cx,
);
self.set_mark(
"]".to_string(),
selections
.iter()
.map(|s| buffer.anchor_after(s.end))
.collect(),
editor.buffer(),
window,
cx,
);
let mut text = String::new();
let mut clipboard_selections = Vec::with_capacity(selections.len());
let mut ranges_to_highlight = Vec::new();
{
let mut is_first = true;
for selection in selections.iter() {
let start = selection.start;
let end = selection.end;
if is_first {
is_first = false;
} else {
text.push('\n');
}
let initial_len = text.len();
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 kind.linewise() {
text.push('\n');
}
clipboard_selections.push(ClipboardSelection::for_buffer(
text.len() - initial_len,
false,
start..end,
&buffer,
editor.project(),
cx,
));
}
}
let selected_register = self.selected_register.take();
Vim::update_globals(cx, |globals, cx| {
globals.write_registers(
Register {
text: text.into(),
clipboard_selections: Some(clipboard_selections),
},
selected_register,
is_yank,
kind,
cx,
)
});
let highlight_duration = VimSettings::get_global(cx).highlight_on_yank_duration;
if !is_yank || self.mode == Mode::Visual || highlight_duration == 0 {
return;
}
editor.highlight_background(
HighlightKey::HighlightOnYank,
&ranges_to_highlight,
|_, colors| colors.colors().vim_yank_background,
cx,
);
cx.spawn(async move |this, cx| {
cx.background_executor()
.timer(Duration::from_millis(highlight_duration))
.await;
this.update(cx, |editor, cx| {
editor.clear_background_highlights(HighlightKey::HighlightOnYank, cx)
})
.ok();
})
.detach();
}
}