Skip to main content

editor/
paste.rs

1//! Application policy for clipboard paste. File drops keep their own path.
2
3use std::path::Path;
4
5use gpui::{App, ClipboardItem, Entity, Global};
6
7use crate::{Editor, Mode};
8
9/// The destination snapshot; the editor entity is being updated during the call.
10#[derive(Clone, Copy, Debug)]
11pub struct PasteContext<'a> {
12    pub mode: Mode,
13    /// Whether the selection lies within one fence, including source mode.
14    pub in_fence: bool,
15    pub base: Option<&'a Path>,
16}
17
18/// Content applied through normal editing and undo.
19#[derive(Clone, Debug, PartialEq, Eq)]
20pub enum PasteContent {
21    Literal(String),
22    /// Uses normal text-paste rules: parsed with the editor's marks in rich
23    /// text, literal in a fence or source mode, with normal URL handling.
24    Markdown(String),
25}
26
27/// Called once before default clipboard handling. `None` keeps that behavior.
28/// The entity identifies the editor; do not read or update it during the call.
29/// Use the snapshot and `cx.image_store()` when calling `ImageStore::keep`.
30pub type PasteHandler = fn(
31    item: &ClipboardItem,
32    editor: &Entity<Editor>,
33    destination: PasteContext<'_>,
34    cx: &mut App,
35) -> Option<PasteContent>;
36
37pub(crate) struct Installed(pub PasteHandler);
38
39impl Global for Installed {}