Detect file encoding from byte order mark - #235
Conversation
maxistar
left a comment
There was a problem hiding this comment.
Thanks, this is a very valuable improvement and the BOM detection/round-trip implementation looks good. I found one data-integrity edge case: after restoring an edited UTF-16/UTF-32 recovery draft, documentEncoding is null, so the next save may rewrite the file using the configured default encoding and drop its BOM. The recovery metadata already has encoding and hasBom; could we persist both and reconstruct documentEncoding in restoreDraft()? A recovery test covering UTF-16LE+BOM → edit → recreate/restore → save would be ideal.
…raft() - Add FileEncoding.fromCharset() to reconstruct encoding from metadata - restoreDraft() now restores documentEncoding from draft metadata - createRecoverySnapshot() uses documentEncoding.hasBom() instead of hardcoded false - Fix RecoveryMetadata JSON null handling for encoding field - Add UTF-16LE+BOM round-trip recovery test
|
Thanks for detailed description of the changes! To ensure everything works reliably, it would be great to write unit tests covering all these edge cases you've listed. Also, please make sure to include tests for opening files in various character encodings to prevent any regressions there. |
Summary
Fixes opening files that are not UTF-8: the editor used the configured default encoding (UTF-8) for every file, so e.g. a UTF-16LE file was shown as garbage. Now the file encoding is detected from its byte order mark (BOM) and the file is opened (and saved) in the correct encoding.
How it works
utils/FileEncodingdetects the BOM:openNamedFileDirect,openNamedFileLegacyDirect,applyExternalDocument) the detected encoding is used and the BOM is stripped from the text.Verification
FileEncodingTest(10 cases: detection, BOM stripping, encode round-trip) — all pass../gradlew compileDebugJavaWithJavac— OK./gradlew testDebugUnitTest— OK./gradlew lint— no errors