fix(tui): keep pasted text intact when you copy it out of the prompt (#59209)

This commit is contained in:
Kit Langton
2026-10-26 09:57:10 -07:00
committed by GitHub
Unverified
parent ab4d1f3ab1
commit b9e594be04
2 changed files with 28 additions and 3 deletions
+10 -2
View File
@@ -3,9 +3,17 @@ import { promptOffsetIndex } from "./display"
export function expandPastedTextPlaceholders(
text: string,
pasted: readonly { text: string; source: { text: string } }[],
pasted: readonly { text: string; source: { text: string; start?: number } }[],
) {
return pasted.reduce((result, part) => result.replace(part.source.text, part.text), text)
let cursor = 0
let result = ""
for (const part of pasted.toSorted((a, b) => (a.source.start ?? 0) - (b.source.start ?? 0))) {
const index = text.indexOf(part.source.text, cursor)
if (index === -1) continue
result += text.slice(cursor, index) + part.text
cursor = index + part.source.text.length
}
return result + text.slice(cursor)
}
export function expandTrackedPastedText(
+18 -1
View File
@@ -1,6 +1,6 @@
import { expect, test } from "bun:test"
import { measureEditText } from "@opentui/core"
import { expandTrackedPastedText } from "../../src/prompt/part"
import { expandPastedTextPlaceholders, expandTrackedPastedText } from "../../src/prompt/part"
const widthMethod = "wcwidth"
const measure = (text: string) => measureEditText(text, { widthMethod })
@@ -21,3 +21,20 @@ test.each([
})
expect(expandTrackedPastedText(pieces.join(marker), ranges, widthMethod)).toBe(pieces.join(pasted))
})
test("expands clipboard placeholders literally in document order without re-matching expanded text", () => {
const first = "echo $$ && replace('$&', `$`cmd``, $'\\n') and literal [Pasted ~4 lines]"
const second = "second\npasted\nfour\nlines"
expect(
expandPastedTextPlaceholders("prefix [Pasted ~3 lines] middle [Pasted ~4 lines] suffix", [
{ text: first, source: { text: "[Pasted ~3 lines]", start: 7 } },
{ text: second, source: { text: "[Pasted ~4 lines]", start: 32 } },
]),
).toBe(`prefix ${first} middle ${second} suffix`)
expect(
expandPastedTextPlaceholders("prefix [Pasted ~3 lines] middle [Pasted ~3 lines] suffix", [
{ text: second, source: { text: "[Pasted ~3 lines]", start: 32 } },
{ text: first, source: { text: "[Pasted ~3 lines]", start: 7 } },
]),
).toBe(`prefix ${first} middle ${second} suffix`)
})