Repository navigation
Detect file encoding from byte order mark - #235
dronov-dmitry wants to merge 4 commits into
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. |
- Extend FileEncodingTest from 10 to 19 cases: fromCharset reconstruction, truncated BOMs, BOM-only files, fallback encode, round-trip for all supported encodings - New EditorFileEncodingTest opens UTF-8, UTF-16LE, UTF-16BE, UTF-32LE and no-BOM documents in the editor and verifies decoded text and byte exact re-save with the original BOM - Recovery metadata JSON round-trip now covers encoding and hasBom
EditorActivity requests WRITE/READ_EXTERNAL_STORAGE in onCreate, so on a fresh install a system permission dialog covers the activity and UI tests fail with NoActivityResumedException. Use GrantPermissionRule (already present in EditorActivityTest) to grant the permissions before each class
|
Hi @maxistar — following up on your review and the test checklist above. Your review (recovery draft encoding) — addressed in 84ad11c: Tests added in 111c03a Unit tests (
Opening files in various character encodings (
Recovery metadata JSON round-trip now also asserts Also in 11be531: UI tests were failing on a fresh install because Verification: Ready for another look. |
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— 19 unit tests: BOM detection for all 5 encodings (incl. UTF-32 vs UTF-16 disambiguation and truncated BOMs),fromCharset()reconstruction, BOM stripping, BOM-only files, encode/decode round-trips with Cyrillic textEditorFileEncodingTest— 5 instrumented tests: opens UTF-8+BOM, UTF-16LE+BOM, UTF-16BE+BOM, UTF-32LE+BOM and no-BOM files, checks the decoded text (no stray BOM char) and saves back byte-exact with the original BOMEditorRecoveryTest— UTF-16LE+BOM → edit → recreate/restore → save recovery round-tripRecoveryRepositoryTest— recovery metadata JSON round-trip coversencodingandhasBom./gradlew test— OK./gradlew compileDebugJavaWithJavac— OK./gradlew lint— no errors./gradlew :app:connectedDebugAndroidTest— 53/53 passed, 1 skipped (API-level gate), on Redmi 7 / Android 11GrantPermissionRuleso they no longer fail behind the system permission dialog on fresh installs