Skip to content

Commit 3cfe63a

Browse files
authored
fix(tui): restore pasted text when undo brings back its placeholder (#63265)
1 parent 89e66ae commit 3cfe63a

2 files changed

Lines changed: 52 additions & 9 deletions

File tree

‎packages/tui/src/component/prompt/index.tsx‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,7 @@ export function Prompt(props: PromptProps) {
799799
virtual: true,
800800
styleId,
801801
typeId: promptPartTypeId,
802+
data: ref.type === "pasted" ? { pasted: prompt.pasted[ref.index].text, label: mention.text } : undefined,
802803
})
803804
setStore("extmarkToPart", (map: Map<number, PromptPartRef>) => {
804805
const newMap = new Map(map)
@@ -821,6 +822,19 @@ export function Prompt(props: PromptProps) {
821822
const pasted: PromptInfo["pasted"] = []
822823

823824
for (const extmark of allExtmarks) {
825+
// Undo restores the extmark after its entry in the live prompt has been removed.
826+
if (typeof extmark.data?.pasted === "string" && typeof extmark.data?.label === "string") {
827+
if (input.getTextRange(extmark.start, extmark.end) !== extmark.data.label) {
828+
input.extmarks.delete(extmark.id)
829+
continue
830+
}
831+
newMap.set(extmark.id, { type: "pasted", index: pasted.length })
832+
pasted.push({
833+
text: extmark.data.pasted,
834+
source: { start: extmark.start, end: extmark.end, text: extmark.data.label },
835+
})
836+
continue
837+
}
824838
const ref = draft.extmarkToPart.get(extmark.id)
825839
if (!ref) continue
826840
if (ref.type === "file") {
@@ -850,15 +864,7 @@ export function Prompt(props: PromptProps) {
850864
const index = skills.length
851865
skills.push(part)
852866
newMap.set(extmark.id, { type: "skill", index })
853-
continue
854867
}
855-
const part = draft.prompt.pasted[ref.index]
856-
if (!part) continue
857-
part.source.start = extmark.start
858-
part.source.end = extmark.end
859-
const index = pasted.length
860-
pasted.push(part)
861-
newMap.set(extmark.id, { type: "pasted", index })
862868
}
863869

864870
const nextFiles = preserveMentionlessPromptAttachments(draft.prompt.files, files)
@@ -1405,6 +1411,7 @@ export function Prompt(props: PromptProps) {
14051411
virtual: true,
14061412
styleId: pasteStyleId,
14071413
typeId: promptPartTypeId,
1414+
data: { pasted: text, label: virtualText },
14081415
})
14091416

14101417
setStore(
@@ -1426,7 +1433,6 @@ export function Prompt(props: PromptProps) {
14261433
const part = store.prompt.pasted[ref.index]
14271434
if (!part) return false
14281435

1429-
input.extmarks.delete(extmarkId)
14301436
input.setSelection(extmark.start, extmark.end)
14311437
input.insertText(part.text)
14321438
return true

‎packages/tui/test/prompt-paste.test.tsx‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { expect, test } from "bun:test"
22
import { takeDraft } from "../src/component/prompt/draft-stash"
33
import { createAppFixture } from "./fixture/app"
4+
import { agent, model, session } from "./fixture/local"
5+
import { directory, json } from "./fixture/tui-client"
46

57
test("the prompt keeps pasted control characters and drops escape sequences", async () => {
68
// Other app tests can leave a home draft in the process-wide stash.
@@ -14,3 +16,38 @@ test("the prompt keeps pasted control characters and drops escape sequences", as
1416
await setup.waitForFrame((frame) => frame.includes("xa^Lbc^@d"))
1517
expect(setup.renderer.currentFocusedEditor?.plainText).toBe("xa\fbc\0d")
1618
})
19+
20+
test.each(["whole", "fragment"])("undo submits the restored %s paste chip", async (kind) => {
21+
const sessionID = `ses_paste_${crypto.randomUUID()}`
22+
const prompted = Promise.withResolvers<string>()
23+
await using setup = await createAppFixture({
24+
args: { sessionID },
25+
fetch: async (url, request) => {
26+
const location = { directory }
27+
if (url.pathname === "/api/agent") return json({ location, data: [agent("build")] })
28+
if (url.pathname === "/api/model") return json({ location, data: [model("Paste Model")] })
29+
if (url.pathname === `/api/session/${sessionID}`) return json({ data: session(sessionID) })
30+
if (url.pathname.endsWith("/message") || url.pathname.endsWith("/inbox")) return json({ data: [], cursor: {} })
31+
if (url.pathname.endsWith("/prompt")) {
32+
prompted.resolve((await request.json()).text)
33+
return json({ data: {} })
34+
}
35+
},
36+
})
37+
await setup.ready
38+
await setup.waitForFrame((frame) => frame.includes("Paste Model"))
39+
const pasted = "one\ntwo\nthree\nfour"
40+
const text = () => setup.renderer.currentFocusedEditor?.plainText
41+
await setup.mockInput.pasteBracketedText(pasted)
42+
await setup.waitFor(() => text() === "[Pasted ~4 lines] ")
43+
if (kind === "fragment") {
44+
setup.mockInput.pressBackspace() // Remove the space after the chip.
45+
setup.mockInput.pressKey("w", { ctrl: true }) // Leave a fragment of the chip.
46+
}
47+
setup.mockInput.pressKey("u", { ctrl: true })
48+
await setup.waitFor(() => text() === "")
49+
setup.mockInput.pressKey("z", { super: true })
50+
await setup.waitFor(() => text() === (kind === "whole" ? "[Pasted ~4 lines] " : "[Pasted ~4 "))
51+
setup.mockInput.pressEnter()
52+
expect(await prompted.promise).toBe(kind === "whole" ? `${pasted} ` : "[Pasted ~4 ")
53+
})

0 commit comments

Comments
 (0)