fix: reduce redundant file I/O and fix BOM preservation for non-UTF-8 encodings

- Extract isUtf8CompatibleEncoding to iconvHelper.ts, removing duplicate
  definitions in fileUtils.ts and fileSystemService.ts
- Add readFileWithEncodingInfo() returning content + encoding + bom in a
  single I/O pass; update edit.ts and write-file.ts to use it instead of
  separate readTextFile/detectFileEncoding/detectFileBOM calls
- Add readTextFileWithInfo() to FileSystemService interface; implement in
  StandardFileSystemService and AcpFileSystemService (delegates to fallback)
- Fix FileReadResult.bom to be true for all Unicode BOM variants (UTF-8/
  16/32), not just UTF-8; add getBOMBytesForEncoding() and update
  writeTextFile to re-prepend the correct BOM bytes on write-back so
  UTF-16/32 BOM files are no longer silently corrupted
- Add tests for readFileWithEncodingInfo, readTextFileWithInfo, and
  UTF-16LE BOM write-back preservation
This commit is contained in:
LaZzyMan 2026-03-04 16:03:17 +08:00
parent a5eb1733fa
commit 109b3d41ab
9 changed files with 328 additions and 59 deletions

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
import type { FileSystemService } from '@qwen-code/qwen-code-core';
import type { FileSystemService , FileReadResult } from '@qwen-code/qwen-code-core';
import type * as acp from '../acp.js';
import { ACP_ERROR_CODES } from '../errorCodes.js';
@ -54,6 +54,12 @@ export class AcpFileSystemService implements FileSystemService {
return response.content;
}
async readTextFileWithInfo(filePath: string): Promise<FileReadResult> {
// ACP protocol does not expose encoding metadata; delegate to the local
// fallback which performs a single-pass read with encoding detection.
return this.fallback.readTextFileWithInfo(filePath);
}
async writeTextFile(
filePath: string,
content: string,