Skip to main content

eclipse_core/
paste.rs

1//! Big pastes, and the one rule about what "big" means.
2//!
3//! A person pasting three hundred lines into a composer did not ask for three hundred lines in the
4//! composer: they asked for the text to go *somewhere* the agent can read it. So a large paste is
5//! held aside and the line carries a chip instead — `[Pasted #1: 12 line(s), 4210 chars]` — and the
6//! text goes back in on submit. The model never sees a chip.
7//!
8//! The rule lives here rather than in either surface because two surfaces disagreeing about what
9//! counts as big is a bug waiting to be filed: the terminal's composer and the dashboard's both
10//! read [`PASTE_INLINE_LIMIT`], and the dashboard's is rendered into the page from this constant.
11
12/// A paste that fits on one line and is no longer than this stays inline: a path, a model name, a
13/// sentence quoted from a page. Anything longer, or with a newline in it, becomes a chip.
14pub const PASTE_INLINE_LIMIT: usize = 160;
15
16/// One held-back paste: the chip that stands in for it, and the text itself.
17#[derive(Debug, Clone, PartialEq, Eq)]
18pub struct Paste {
19    pub chip: String,
20    pub text: String,
21}
22
23/// How many lines the text has. One trailing newline does not make it two lines — a copy from a
24/// terminal almost always carries one, and "2 line(s)" for one line reads like a bug.
25pub fn lines(text: &str) -> usize {
26    text.trim_end_matches('\n').lines().count().max(1)
27}
28
29/// Whether this paste can go straight into the line it was dropped into.
30pub fn fits_inline(text: &str) -> bool {
31    lines(text) == 1 && text.chars().count() <= PASTE_INLINE_LIMIT
32}
33
34/// The chip that stands in for a paste, numbered from 1 the way the composer numbers them.
35pub fn chip_for(text: &str, index: usize) -> String {
36    format!(
37        "[Pasted #{index}: {} line(s), {} chars]",
38        lines(text),
39        text.chars().count()
40    )
41}
42
43/// The line with every chip it contains replaced by its text, left to right, once each.
44///
45/// `#[must_use]` on purpose: forgetting to call this sends a chip to the model, which reads as a
46/// placeholder the user typed and answers about nothing.
47#[must_use]
48pub fn expand(line: &str, pastes: &[Paste]) -> String {
49    let mut out = line.to_string();
50    for paste in pastes {
51        if out.contains(&paste.chip) {
52            out = out.replacen(&paste.chip, &paste.text, 1);
53        }
54    }
55    out
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn one_short_line_stays_inline_and_everything_else_does_not() {
64        assert!(fits_inline("~/src/eclipse"));
65        assert!(
66            fits_inline(&"x".repeat(PASTE_INLINE_LIMIT)),
67            "the limit itself fits"
68        );
69        assert!(!fits_inline(&"x".repeat(PASTE_INLINE_LIMIT + 1)));
70        assert!(
71            !fits_inline("two\nlines"),
72            "a newline is a paste, not a sentence"
73        );
74        // A tab is not a newline: a short tab-separated row is still one line, and it fits.
75        assert!(fits_inline("with\ttab"));
76        // A copy out of a terminal almost always carries a trailing newline; that is one line.
77        assert!(
78            fits_inline("one line\n"),
79            "a trailing newline does not make it two"
80        );
81        assert!(!fits_inline("one\ntwo\n"), "but a real second line does");
82    }
83
84    #[test]
85    fn a_chip_counts_what_it_stands_for() {
86        let text = "a\nb\nc";
87        assert_eq!(chip_for(text, 1), "[Pasted #1: 3 line(s), 5 chars]");
88        assert_eq!(chip_for(text, 2), "[Pasted #2: 3 line(s), 5 chars]");
89        assert_eq!(lines("single"), 1);
90        assert_eq!(lines(""), 1, "an empty paste is still one line of nothing");
91    }
92
93    /// The property that matters: the model never sees a chip. Two pastes, out of order on the
94    /// line, each replaced exactly once.
95    #[test]
96    fn expanding_puts_every_byte_back() {
97        let big = "one\ntwo\nthree".repeat(40);
98        let other = "second paste\nwith two lines";
99        let pastes = vec![
100            Paste {
101                chip: chip_for(&big, 1),
102                text: big.clone(),
103            },
104            Paste {
105                chip: chip_for(other, 2),
106                text: other.to_string(),
107            },
108        ];
109        let line = format!("compare {} with {}", pastes[1].chip, pastes[0].chip);
110        let out = expand(&line, &pastes);
111        assert_eq!(out, format!("compare {other} with {big}"));
112        assert!(
113            !out.contains("[Pasted"),
114            "nothing is left standing in for text"
115        );
116        // A paste whose chip was deleted from the line is simply not in the result.
117        assert_eq!(expand("nothing here", &pastes), "nothing here");
118    }
119}