* Applies a simulated sed edit directly instead of running sed. * This is used by the permission dialog to ensure what the user previews * is exactly what gets written to the file.
(simulatedEdit: {
filePath: string;
newContent: string;
}, toolUseContext: SimulatedSedEditContext, parentMessage?: AssistantMessage)
| 358 | * is exactly what gets written to the file. |
| 359 | */ |
| 360 | async function applySedEdit(simulatedEdit: { |
| 361 | filePath: string; |
| 362 | newContent: string; |
| 363 | }, toolUseContext: SimulatedSedEditContext, parentMessage?: AssistantMessage): Promise<SimulatedSedEditResult> { |
| 364 | const { |
| 365 | filePath, |
| 366 | newContent |
| 367 | } = simulatedEdit; |
| 368 | const absoluteFilePath = expandPath(filePath); |
| 369 | const fs = getFsImplementation(); |
| 370 | |
| 371 | // Read original content for VS Code notification |
| 372 | const encoding = detectFileEncoding(absoluteFilePath); |
| 373 | let originalContent: string; |
| 374 | try { |
| 375 | originalContent = await fs.readFile(absoluteFilePath, { |
| 376 | encoding |
| 377 | }); |
| 378 | } catch (e) { |
| 379 | if (isENOENT(e)) { |
| 380 | return { |
| 381 | data: { |
| 382 | stdout: '', |
| 383 | stderr: `sed: ${filePath}: No such file or directory\nExit code 1`, |
| 384 | interrupted: false |
| 385 | } |
| 386 | }; |
| 387 | } |
| 388 | throw e; |
| 389 | } |
| 390 | |
| 391 | // Track file history before making changes (for undo support) |
| 392 | if (fileHistoryEnabled() && parentMessage) { |
| 393 | await fileHistoryTrackEdit(toolUseContext.updateFileHistoryState, absoluteFilePath, parentMessage.uuid); |
| 394 | } |
| 395 | |
| 396 | // Detect line endings and write new content |
| 397 | const endings = detectLineEndings(absoluteFilePath); |
| 398 | writeTextContent(absoluteFilePath, newContent, encoding, endings); |
| 399 | |
| 400 | // Notify VS Code about the file change |
| 401 | notifyVscodeFileUpdated(absoluteFilePath, originalContent, newContent); |
| 402 | |
| 403 | // Update read timestamp to invalidate stale writes |
| 404 | toolUseContext.readFileState.set(absoluteFilePath, { |
| 405 | content: newContent, |
| 406 | timestamp: getFileModificationTime(absoluteFilePath), |
| 407 | offset: undefined, |
| 408 | limit: undefined |
| 409 | }); |
| 410 | |
| 411 | // Return success result matching sed output format (sed produces no output on success) |
| 412 | return { |
| 413 | data: { |
| 414 | stdout: '', |
| 415 | stderr: '', |
| 416 | interrupted: false |
| 417 | } |
no test coverage detected