( currentPrompt: string, pastedContents?: Record<number, PastedContent>, )
| 136 | |
| 137 | // sync IO: called from sync context (React components, sync command handlers) |
| 138 | export function editPromptInEditor( |
| 139 | currentPrompt: string, |
| 140 | pastedContents?: Record<number, PastedContent>, |
| 141 | ): EditorResult { |
| 142 | const fs = getFsImplementation() |
| 143 | const tempFile = generateTempFilePath() |
| 144 | |
| 145 | try { |
| 146 | // Expand any pasted text references before editing |
| 147 | const expandedPrompt = pastedContents |
| 148 | ? expandPastedTextRefs(currentPrompt, pastedContents) |
| 149 | : currentPrompt |
| 150 | |
| 151 | // Write expanded prompt to temp file |
| 152 | writeFileSync_DEPRECATED(tempFile, expandedPrompt, { |
| 153 | encoding: 'utf-8', |
| 154 | flush: true, |
| 155 | }) |
| 156 | |
| 157 | // Delegate to editFileInEditor |
| 158 | const result = editFileInEditor(tempFile) |
| 159 | |
| 160 | if (result.content === null) { |
| 161 | return result |
| 162 | } |
| 163 | |
| 164 | // Trim a single trailing newline if present (common editor behavior) |
| 165 | let finalContent = result.content |
| 166 | if (finalContent.endsWith('\n') && !finalContent.endsWith('\n\n')) { |
| 167 | finalContent = finalContent.slice(0, -1) |
| 168 | } |
| 169 | |
| 170 | // Re-collapse pasted content if it wasn't edited |
| 171 | if (pastedContents) { |
| 172 | finalContent = recollapsePastedContent( |
| 173 | finalContent, |
| 174 | currentPrompt, |
| 175 | pastedContents, |
| 176 | ) |
| 177 | } |
| 178 | |
| 179 | return { content: finalContent } |
| 180 | } finally { |
| 181 | // Clean up temp file |
| 182 | try { |
| 183 | fs.unlinkSync(tempFile) |
| 184 | } catch { |
| 185 | // Ignore cleanup errors |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 |
no test coverage detected