Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,21 @@ export function Prompt(props: PromptProps) {
const messageID = MessageID.ascending()
let inputText = store.prompt.input

// Extmark start/end are display-width offsets, NOT JS string indices.
// Convert them to char indices before slicing.
function visualOffsetToCharIndex(text: string, visualOffset: number): number {
let seen = 0
let idx = 0
while (idx < text.length) {
if (seen >= visualOffset) return idx
const cp = text.codePointAt(idx)!
const char = String.fromCodePoint(cp)
seen += char === "\n" ? 1 : Bun.stringWidth(char)
idx += char.length
}
return idx
}

// Expand pasted text inline before submitting
const allExtmarks = input.extmarks.getAllForTypeId(promptPartTypeId)
const sortedExtmarks = allExtmarks.sort((a: { start: number }, b: { start: number }) => b.start - a.start)
Expand All @@ -1101,8 +1116,10 @@ export function Prompt(props: PromptProps) {
if (partIndex !== undefined) {
const part = store.prompt.parts[partIndex]
if (part?.type === "text" && part.text) {
const before = inputText.slice(0, extmark.start)
const after = inputText.slice(extmark.end)
const charStart = visualOffsetToCharIndex(inputText, extmark.start)
const charEnd = visualOffsetToCharIndex(inputText, extmark.end)
const before = inputText.slice(0, charStart)
const after = inputText.slice(charEnd)
inputText = before + part.text + after
}
}
Expand Down
Loading