fix(editor): persist file encoding across sessions - #491
Conversation
Activate the existing but unused encoding history mechanism (browsing_encode_history + writeEncodeHistoryRecord/readEncodeHistoryRecord) to remember the user's chosen encoding per file path. When saving, record the encoding; when reopening, pass it as a preferred encode hint to FileLoadThread, skipping unreliable content-based detection that falls back to UTF-8 for ASCII-compatible encodings (GB18030, ISO-8859-1, CP1252, BIG5, etc.). Changes: - FileLoadThread: add setPreferredEncode/m_preferredEncode; use preferred encode to skip auto-detection in both large-file and normal paths - TextEdit: add readEncodeHistoryRecord(filepath) overload to look up a specific file's stored encoding; add setTextEncode setter - EditWrapper::openFile: read history encoding and set as preferred hint - EditWrapper::saveFile/saveTemFile/saveDraftFile/forceSaveInvalidCharFile: record encoding history after successful save - Window::saveAsFileToDisk: record encoding history after path is updated to the new file path (fixes path-key issue in saveAsFile) Log: 激活编码历史持久化机制,修复切换编码保存后重新打开编码丢失的问题 Bug: https://pms.uniontech.com/bug-view-365847.html
There was a problem hiding this comment.
Sorry @GongHeng2017, you have reached your weekly rate limit of 500000 diff characters.
Please try again later or upgrade to continue using Sourcery
Reviewer's GuidePersists user-selected file encodings by wiring the existing encode history mechanism into file open/save flows, and by allowing FileLoadThread to prefer a stored encoding over automatic detection. Sequence diagram for opening a file with preferred encoding historysequenceDiagram
actor User
participant Window
participant EditWrapper
participant TextEdit
participant FileLoadThread
participant DetectCode
User ->> Window: openFile
Window ->> EditWrapper: openFile(filepath, qstrTruePath, bNewWindow)
EditWrapper ->> TextEdit: setFilePath(filepath)
EditWrapper ->> TextEdit: readEncodeHistoryRecord(filepath)
TextEdit -->> EditWrapper: historyEncode
alt [historyEncode not empty]
EditWrapper ->> FileLoadThread: setPreferredEncode(historyEncode.toLocal8Bit)
end
EditWrapper ->> FileLoadThread: start
FileLoadThread ->> FileLoadThread: run
alt [m_preferredEncode not empty]
FileLoadThread ->> FileLoadThread: encode = m_preferredEncode
else [m_preferredEncode empty]
FileLoadThread ->> DetectCode: GetFileEncodingFormat(m_strFilePath, indata)
DetectCode -->> FileLoadThread: encode
end
Sequence diagram for saving a file and recording encoding historysequenceDiagram
actor User
participant Window
participant EditWrapper
participant TextEdit
User ->> EditWrapper: saveFile(encode)
EditWrapper ->> EditWrapper: write file to disk
EditWrapper ->> TextEdit: setTextEncode(m_sCurEncode)
EditWrapper ->> TextEdit: writeEncodeHistoryRecord
User ->> Window: saveAsFileToDisk
Window ->> EditWrapper: saveAsFile(newFilePath, encode)
Window ->> Window: updateSaveAsFileName(wrapper->filePath, newFilePath)
Window ->> TextEdit: setTextEncode(QString::fromUtf8(encode))
Window ->> TextEdit: writeEncodeHistoryRecord
User ->> EditWrapper: saveDraftFile(newFilePath)
EditWrapper ->> EditWrapper: write draft file
EditWrapper ->> TextEdit: setTextEncode(QString::fromUtf8(encode))
EditWrapper ->> TextEdit: writeEncodeHistoryRecord
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
deepin pr auto review★ 总体评分:100分■ 【总体评价】
■ 【详细分析】
■ 【改进建议代码示例】 // 建议改进 TextEdit::readEncodeHistoryRecord 的解析逻辑,使用更安全的 JSON 格式存储和读取
// 假设历史记录存储格式改为 JSON: {"file_path_1": "UTF-8", "file_path_2": "GBK"}
QString TextEdit::readEncodeHistoryRecord(const QString &filepath)
{
QString history = m_settings->settings->option("advance.editor.browsing_encode_history")->value().toString();
if (history.isEmpty()) {
return QString();
}
QJsonDocument doc = QJsonDocument::fromJson(history.toUtf8());
if (doc.isObject()) {
QJsonObject obj = doc.object();
if (obj.contains(filepath)) {
return obj.value(filepath).toString();
}
}
return QString();
}
// 对应的 writeEncodeHistoryRecord 也应修改为更新 JSON 对象 |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: GongHeng2017, max-lvs The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
/merge |
This reverts commit 1d54985. 需求调整,回退 PR #491(bug-fix-365847)的编码历史持久化改动, 代码恢复到该 PR 合并前的状态。 Log: 回退编码历史持久化机制改动 Bug: https://pms.uniontech.com/bug-view-365847.html
修复 BUG-365847:切换编码格式并保存后再打开,部分格式无法保存
问题
用户在文本编辑器中切换文件编码格式(如 GB18030、ISO-8859-1、CP1252 等)并保存后,关闭标签页重新打开,编码格式回退为 UTF-8,无法保持用户选择的编码。
根因
deepin-editor 打开文件时完全依赖
DetectCode::GetFileEncodingFormat对内容做编码探测。对于与 UTF-8/ASCII 字节重叠的编码(ASCII 内容保存为 GB18030 等),探测器无法区分,必然回退为 UTF-8。项目中已有编码历史持久化机制(隐藏配置项browsing_encode_history+writeEncodeHistoryRecord/readEncodeHistoryRecord),但该机制是死代码,从未接入保存/打开流程。修复方案
激活已有但未接入的编码历史持久化机制:
setPreferredEncode/m_preferredEncode,打开文件时优先使用历史编码,跳过不可靠的内容自动探测readEncodeHistoryRecord(filepath)重载,按文件路径查询历史编码;新增setTextEncodesetter审查说明
基于用户提供的 fix.patch,审查发现并修复了
saveAsFile(newFilePath, encode)中的路径键问题:原 patch 在writeEncodeHistoryRecord()时m_sFilePath仍为旧路径(调用方saveAsFileToDisk在updateSaveAsFileName之后才更新为新路径),导致历史记录键到错误路径。已将另存为的历史记录从saveAsFile移至saveAsFileToDisk中updateSaveAsFileName之后。PMS: https://pms.uniontech.com/bug-view-365847.html
Summary by Sourcery
Persist user-selected text encoding across file saves and reopen, avoiding incorrect fallback to UTF-8 by reusing stored encoding history when loading files.
Bug Fixes:
Enhancements: