diff --git a/packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx b/packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx index ad55c5268..644ac2622 100644 --- a/packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx +++ b/packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx @@ -89,7 +89,7 @@ export function Prompt(props: PromptProps) { const fileStyleId = syntax().getStyleId("extmark.file")! const agentStyleId = syntax().getStyleId("extmark.agent")! const pasteStyleId = syntax().getStyleId("extmark.paste")! - let promptPartTypeId: number + let promptPartTypeId = 0 sdk.event.on(TuiEvent.PromptAppend.type, (evt) => { input.insertText(evt.properties.text) @@ -310,47 +310,44 @@ export function Prompt(props: PromptProps) { ] }) + const ref: PromptRef = { + get focused() { + return input.focused + }, + get current() { + return store.prompt + }, + focus() { + input.focus() + }, + blur() { + input.blur() + }, + set(prompt) { + input.setText(prompt.input) + setStore("prompt", prompt) + restoreExtmarksFromParts(prompt.parts) + input.gotoBufferEnd() + }, + reset() { + input.clear() + input.extmarks.clear() + setStore("prompt", { + input: "", + parts: [], + }) + setStore("extmarkToPartIndex", new Map()) + }, + submit() { + submit() + }, + } + createEffect(() => { if (props.visible !== false) input?.focus() if (props.visible === false) input?.blur() }) - onMount(() => { - promptPartTypeId = input.extmarks.registerType("prompt-part") - props.ref?.({ - get focused() { - return input.focused - }, - get current() { - return store.prompt - }, - focus() { - input.focus() - }, - blur() { - input.blur() - }, - set(prompt) { - input.setText(prompt.input) - setStore("prompt", prompt) - restoreExtmarksFromParts(prompt.parts) - input.gotoBufferEnd() - }, - reset() { - input.clear() - input.extmarks.clear() - setStore("prompt", { - input: "", - parts: [], - }) - setStore("extmarkToPartIndex", new Map()) - }, - submit() { - submit() - }, - }) - }) - function restoreExtmarksFromParts(parts: PromptInfo["parts"]) { input.extmarks.clear() setStore("extmarkToPartIndex", new Map()) @@ -920,6 +917,10 @@ export function Prompt(props: PromptProps) { }} ref={(r: TextareaRenderable) => { input = r + if (promptPartTypeId === 0) { + promptPartTypeId = input.extmarks.registerType("prompt-part") + } + props.ref?.(ref) setTimeout(() => { input.cursorColor = theme.text }, 0)