From 8a5480c239e68aff3ef01d502f7d1e120d59a39b Mon Sep 17 00:00:00 2001 From: gongheng Date: Wed, 29 Jul 2026 16:29:16 +0800 Subject: [PATCH] Revert "fix(editor): persist file encoding across sessions" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 1d549851ab7fb497bdc516617ec72fc83f434fc4. 需求调整,回退 PR #491(bug-fix-365847)的编码历史持久化改动, 代码恢复到该 PR 合并前的状态。 Log: 回退编码历史持久化机制改动 Bug: https://pms.uniontech.com/bug-view-365847.html --- src/common/fileloadthread.cpp | 14 +------------- src/common/fileloadthread.h | 7 ------- src/editor/dtextedit.cpp | 25 ------------------------- src/editor/dtextedit.h | 8 -------- src/editor/editwrapper.cpp | 21 --------------------- src/widgets/window.cpp | 5 ----- 6 files changed, 1 insertion(+), 79 deletions(-) diff --git a/src/common/fileloadthread.cpp b/src/common/fileloadthread.cpp index 373ac211..519cc8a8 100644 --- a/src/common/fileloadthread.cpp +++ b/src/common/fileloadthread.cpp @@ -19,11 +19,6 @@ FileLoadThread::~FileLoadThread() { } -void FileLoadThread::setPreferredEncode(const QByteArray &encode) -{ - m_preferredEncode = encode; -} - void FileLoadThread::run() { QFile file(m_strFilePath); @@ -32,19 +27,12 @@ void FileLoadThread::run() QByteArray encode; QByteArray indata; - // 优先使用历史记录的编码格式,跳过自动探测 - if (!m_preferredEncode.isEmpty()) { - encode = m_preferredEncode; - } - // 判断文件大小是否超过40MB, 超过40MB的文件过大,需要调整读取策略,优先加载头部文件 static const int s_maxDirectReadLen = 40 * DATA_SIZE_1024 * DATA_SIZE_1024; if (file.size() > s_maxDirectReadLen) { // 先读取1MB数据 indata = file.read(DATA_SIZE_1024 * DATA_SIZE_1024); - if (encode.isEmpty()) { - encode = DetectCode::GetFileEncodingFormat(m_strFilePath, indata); - } + encode = DetectCode::GetFileEncodingFormat(m_strFilePath, indata); // 发送文件头信息,用于预先加载数据 QString textEncode = QString::fromLocal8Bit(encode); diff --git a/src/common/fileloadthread.h b/src/common/fileloadthread.h index 28074f40..9dd5c86d 100644 --- a/src/common/fileloadthread.h +++ b/src/common/fileloadthread.h @@ -16,12 +16,6 @@ class FileLoadThread : public QThread void run(); - /** - * @brief setPreferredEncode 设置优先使用的编码格式,用于跳过自动探测 - * @param encode 优先编码格式,为空时自动探测 - */ - void setPreferredEncode(const QByteArray &encode); - signals: // 预处理信号,优先处理文件头,防止出现加载时间过长的情况 void sigPreProcess(const QByteArray &encode, const QByteArray &content); @@ -29,7 +23,6 @@ class FileLoadThread : public QThread private: QString m_strFilePath; - QByteArray m_preferredEncode; }; #endif diff --git a/src/editor/dtextedit.cpp b/src/editor/dtextedit.cpp index dcb52599..6f1ec75d 100644 --- a/src/editor/dtextedit.cpp +++ b/src/editor/dtextedit.cpp @@ -4729,31 +4729,6 @@ QStringList TextEdit::readEncodeHistoryRecord() return filePathList; } -QString TextEdit::readEncodeHistoryRecord(const QString &filepath) -{ - QString history = m_settings->settings->option("advance.editor.browsing_encode_history")->value().toString(); - if (history.isEmpty()) { - return QString(); - } - - int nLeftPosition = history.indexOf("*[" + filepath + "]*"); - if (nLeftPosition == -1) { - return QString(); - } - - nLeftPosition = history.indexOf("]*", nLeftPosition); - if (nLeftPosition == -1) { - return QString(); - } - - int nRightPosition = history.indexOf("}*", nLeftPosition); - if (nRightPosition == -1) { - return QString(); - } - - return history.mid(nLeftPosition + 2, nRightPosition - nLeftPosition - 2); -} - void TextEdit::tellFindBarClose() { m_bIsFindClose = true; diff --git a/src/editor/dtextedit.h b/src/editor/dtextedit.h index a5eb2a05..957e0e98 100644 --- a/src/editor/dtextedit.h +++ b/src/editor/dtextedit.h @@ -119,8 +119,6 @@ class TextEdit : public DPlainTextEdit inline QString getFilePath() { return m_sFilePath;} // 设置文件路径 inline void setFilePath(const QString &file) { m_sFilePath = file;} - // 设置文件编码,用于记录编码历史 - inline void setTextEncode(const QString &encode) { m_textEncode = encode;} // 取得左侧的导航控件,包含行号、书签、折叠控件等 inline LeftAreaTextEdit *getLeftAreaWidget() { return m_pLeftAreaWidget;} @@ -415,12 +413,6 @@ class TextEdit : public DPlainTextEdit void setCursorStart(int pos); void writeEncodeHistoryRecord(); QStringList readEncodeHistoryRecord(); - /** - * @brief readEncodeHistoryRecord 读取指定文件路径的历史编码记录 - * @param filepath 文件路径 - * @return 返回该文件路径对应的历史编码,若无记录返回空字符串 - */ - QString readEncodeHistoryRecord(const QString &filepath); /** * @brief tellFindBarClose 通知查找框关闭 */ diff --git a/src/editor/editwrapper.cpp b/src/editor/editwrapper.cpp index 71cb7f15..b8acff1d 100644 --- a/src/editor/editwrapper.cpp +++ b/src/editor/editwrapper.cpp @@ -177,14 +177,6 @@ void EditWrapper::openFile(const QString &filepath, QString qstrTruePath, bool b } FileLoadThread *thread = new FileLoadThread(filepath); - - // 读取历史编码记录,优先使用历史编码格式,避免自动探测导致编码不一致 - m_pTextEdit->setFilePath(filepath); - QString historyEncode = m_pTextEdit->readEncodeHistoryRecord(filepath); - if (!historyEncode.isEmpty()) { - thread->setPreferredEncode(historyEncode.toLocal8Bit()); - } - // begin to load the file. connect(thread, &FileLoadThread::sigPreProcess, this, &EditWrapper::handleFilePreProcess); connect(thread, &FileLoadThread::sigLoadFinished, this, &EditWrapper::handleFileLoadFinished); @@ -544,10 +536,6 @@ bool EditWrapper::saveFile(QByteArray encode) m_tModifiedDateTime = fi.lastModified(); updateModifyStatus(false); m_bIsTemFile = false; - - // 保存成功后,记录文件编码历史,用于下次打开时优先使用 - m_pTextEdit->setTextEncode(m_sCurEncode); - m_pTextEdit->writeEncodeHistoryRecord(); } else { DMessageManager::instance()->sendMessage( this->window()->getStackedWgt()->currentWidget(), @@ -596,10 +584,6 @@ bool EditWrapper::forceSaveInvalidCharFile() m_tModifiedDateTime = fi.lastModified(); m_bIsTemFile = false; exitInvalidCharPreview(); - - // 保存成功后,记录文件编码历史 - m_pTextEdit->setTextEncode(m_sCurEncode); - m_pTextEdit->writeEncodeHistoryRecord(); } return ok; } @@ -756,11 +740,6 @@ bool EditWrapper::saveDraftFile(QString &newFilePath) updateSaveAsFileName(m_pTextEdit->getFilePath(), newFilePath); m_pTextEdit->document()->setModified(false); m_bIsTemFile = false; - - // 保存成功后,记录文件编码历史 - m_pTextEdit->setTextEncode(QString::fromUtf8(encode)); - m_pTextEdit->writeEncodeHistoryRecord(); - return true; } diff --git a/src/widgets/window.cpp b/src/widgets/window.cpp index ef837f6c..c6c3942d 100644 --- a/src/widgets/window.cpp +++ b/src/widgets/window.cpp @@ -1385,11 +1385,6 @@ QString Window::saveAsFileToDisk() } updateSaveAsFileName(wrapper->filePath(), newFilePath); - - // 另存为成功后,路径已更新为新路径,记录文件编码历史 - wrapper->textEditor()->setTextEncode(QString::fromUtf8(encode)); - wrapper->textEditor()->writeEncodeHistoryRecord(); - return newFilePath; }