Add an editor: diff clipboard with selection
action (#33283)
https://github.com/user-attachments/assets/d472fbdd-7736-4bd7-8a90-8cca356b2815 This PR adds `editor: diff clipboard with selection` - good for spotting the differences in eerily-similar code, which is when refactoring code, as you need to see what needs to be passed in in order to maintain previous behavior of both snippets. 1. Copy some text from anywhere 2. Highlight some text in Zed 3. Run `editor: diff clipboard with selection` Like JetBrains' IDEs and VS Code with the `PartialDiff` package, if the selection is empty, we take the entire buffer as the selection. Caveats: - We do not know the language of the text in the clipboard. I went ahead and just assumed that in most cases, it will be the same language as the selected text, which does mean we will highlight the old text incorrectly if they are copying from a different language, but I think in most cases, it will be the same, and the alternative of always having no syntax highlighting is worse. PyCharm seems to do the same thing. Release Notes: - Added an `editor: diff clipboard with selection` action --------- Co-authored-by: Junkui Zhang <364772080@qq.com> Co-authored-by: Ben Kunkle <ben@zed.dev>
This commit is contained in:
parent
3e27fa1d92
commit
500ceaabcd
8 changed files with 671 additions and 56 deletions
|
@ -213,6 +213,7 @@ use workspace::{
|
|||
notifications::{DetachAndPromptErr, NotificationId, NotifyTaskExt},
|
||||
searchable::SearchEvent,
|
||||
};
|
||||
use zed_actions;
|
||||
|
||||
use crate::{
|
||||
code_context_menus::CompletionsMenuSource,
|
||||
|
@ -12154,6 +12155,41 @@ impl Editor {
|
|||
});
|
||||
}
|
||||
|
||||
pub fn diff_clipboard_with_selection(
|
||||
&mut self,
|
||||
_: &DiffClipboardWithSelection,
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
let selections = self.selections.all::<usize>(cx);
|
||||
|
||||
if selections.is_empty() {
|
||||
log::warn!("There should always be at least one selection in Zed. This is a bug.");
|
||||
return;
|
||||
};
|
||||
|
||||
let clipboard_text = match cx.read_from_clipboard() {
|
||||
Some(item) => match item.entries().first() {
|
||||
Some(ClipboardEntry::String(text)) => Some(text.text().to_string()),
|
||||
_ => None,
|
||||
},
|
||||
None => None,
|
||||
};
|
||||
|
||||
let Some(clipboard_text) = clipboard_text else {
|
||||
log::warn!("Clipboard doesn't contain text.");
|
||||
return;
|
||||
};
|
||||
|
||||
window.dispatch_action(
|
||||
Box::new(DiffClipboardWithSelectionData {
|
||||
clipboard_text,
|
||||
editor: cx.entity(),
|
||||
}),
|
||||
cx,
|
||||
);
|
||||
}
|
||||
|
||||
pub fn paste(&mut self, _: &Paste, window: &mut Window, cx: &mut Context<Self>) {
|
||||
self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
|
||||
if let Some(item) = cx.read_from_clipboard() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue