Skip to content

Commit f5a9042

Browse files
committed
Refactor: use writeFileWithEncodingPreservation save the file to save encoding
1 parent 7402ad0 commit f5a9042

File tree

4 files changed

+414
-9
lines changed

4 files changed

+414
-9
lines changed

src/integrations/editor/DiffViewProvider.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { XMLBuilder } from "fast-xml-parser"
77
import delay from "delay"
88

99
import { createDirectoriesForFile } from "../../utils/fs"
10-
import { readFileWithEncodingDetection } from "../../utils/encoding"
10+
import { readFileWithEncodingDetection, writeFileWithEncodingPreservation } from "../../utils/encoding"
1111
import { arePathsEqual, getReadablePath } from "../../utils/path"
1212
import { formatResponse } from "../../core/prompts/responses"
1313
import { diagnosticsToProblemsString, getNewDiagnostics } from "../diagnostics"
@@ -655,9 +655,9 @@ export class DiffViewProvider {
655655
// Get diagnostics before editing the file
656656
this.preDiagnostics = vscode.languages.getDiagnostics()
657657

658-
// Write the content directly to the file
658+
// Write the content directly to the file with encoding preservation
659659
await createDirectoriesForFile(absolutePath)
660-
await fs.writeFile(absolutePath, content, "utf-8")
660+
await writeFileWithEncodingPreservation(absolutePath, content)
661661

662662
// Open the document to ensure diagnostics are loaded
663663
// When openFile is false (PREVENT_FOCUS_DISRUPTION enabled), we only open in memory

src/integrations/editor/__tests__/DiffViewProvider.spec.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ vi.mock("../../../utils/fs", () => ({
2727
createDirectoriesForFile: vi.fn().mockResolvedValue([]),
2828
}))
2929

30+
// Mock encoding utilities
31+
vi.mock("../../../utils/encoding", () => ({
32+
readFileWithEncodingDetection: vi.fn().mockResolvedValue("file content"),
33+
writeFileWithEncodingPreservation: vi.fn().mockResolvedValue(undefined),
34+
}))
35+
3036
// Mock path
3137
vi.mock("path", async () => {
3238
const actual = await vi.importActual("path");
@@ -382,8 +388,8 @@ describe("DiffViewProvider", () => {
382388
const result = await diffViewProvider.saveDirectly("test.ts", "new content", true, true, 2000)
383389

384390
// Verify file was written
385-
const fs = await import("fs/promises")
386-
expect(fs.writeFile).toHaveBeenCalledWith(`${mockCwd}/test.ts`, "new content", "utf-8")
391+
const { writeFileWithEncodingPreservation } = await import("../../../utils/encoding")
392+
expect(writeFileWithEncodingPreservation).toHaveBeenCalledWith(`${mockCwd}/test.ts`, "new content")
387393

388394
// Verify file was opened without focus
389395
expect(vscode.window.showTextDocument).toHaveBeenCalledWith(
@@ -405,8 +411,8 @@ describe("DiffViewProvider", () => {
405411
await diffViewProvider.saveDirectly("test.ts", "new content", false, true, 1000)
406412

407413
// Verify file was written
408-
const fs = await import("fs/promises")
409-
expect(fs.writeFile).toHaveBeenCalledWith(`${mockCwd}/test.ts`, "new content", "utf-8")
414+
const { writeFileWithEncodingPreservation } = await import("../../../utils/encoding")
415+
expect(writeFileWithEncodingPreservation).toHaveBeenCalledWith(`${mockCwd}/test.ts`, "new content")
410416

411417
// Verify file was NOT opened
412418
expect(vscode.window.showTextDocument).not.toHaveBeenCalled()
@@ -420,8 +426,8 @@ describe("DiffViewProvider", () => {
420426
await diffViewProvider.saveDirectly("test.ts", "new content", true, false, 1000)
421427

422428
// Verify file was written
423-
const fs = await import("fs/promises")
424-
expect(fs.writeFile).toHaveBeenCalledWith(`${mockCwd}/test.ts`, "new content", "utf-8")
429+
const { writeFileWithEncodingPreservation } = await import("../../../utils/encoding")
430+
expect(writeFileWithEncodingPreservation).toHaveBeenCalledWith(`${mockCwd}/test.ts`, "new content")
425431

426432
// Verify delay was NOT called
427433
expect(mockDelay).not.toHaveBeenCalled()

0 commit comments

Comments
 (0)