Skip to content

Commit 7d6af5d

Browse files
committed
fix(editor): restore CRLF line ending conversion when saving files
Add setEndlineFormat() to TextFileSaver to convert \n to \r\n for Windows format files. All 5 save paths now pass the current endline format to the saver. 修复TextFileSaver重构后丢失CRLF转换逻辑的问题,Windows格式 文件保存后重新打开格式正确显示为Windows。 Log: 修复Windows格式文件保存后换行符丢失的问题 PMS: BUG-342025 Influence: 修复后Windows(CRLF)格式文件保存并重新打开后格式正确显示为Windows,不再回退为Unix。
1 parent 303ace6 commit 7d6af5d

3 files changed

Lines changed: 24 additions & 9 deletions

File tree

src/common/text_file_saver.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2025 UnionTech Software Technology Co., Ltd.
1+
// SPDX-FileCopyrightText: 2025 - 2026 UnionTech Software Technology Co., Ltd.
22
//
33
// SPDX-License-Identifier: GPL-3.0-or-later
44

@@ -57,6 +57,11 @@ void TextFileSaver::setEncoding(const QByteArray &toEncode)
5757
m_toEncode = toEncode;
5858
}
5959

60+
void TextFileSaver::setEndlineFormat(bool useCRLF)
61+
{
62+
m_useCRLF = useCRLF;
63+
}
64+
6065
/**
6166
* @brief Saves the document to the preset file path
6267
* @return true if the file was saved successfully, false otherwise
@@ -150,7 +155,10 @@ bool TextFileSaver::saveToFile(QFileDevice &file)
150155
return false;
151156
}
152157
qDebug() << "memory sufficient";
153-
const QString content = m_document->toPlainText();
158+
QString content = m_document->toPlainText();
159+
if (m_useCRLF) {
160+
content.replace(QStringLiteral("\n"), QStringLiteral("\r\n"));
161+
}
154162
const ushort *data = content.utf16();
155163
const int length = content.length();
156164

src/common/text_file_saver.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2025 UnionTech Software Technology Co., Ltd.
1+
// SPDX-FileCopyrightText: 2025 - 2026 UnionTech Software Technology Co., Ltd.
22
//
33
// SPDX-License-Identifier: GPL-3.0-or-later
44

@@ -19,6 +19,7 @@ class TextFileSaver
1919

2020
void setFilePath(const QString &filePath);
2121
void setEncoding(const QByteArray &toEncode);
22+
void setEndlineFormat(bool useCRLF);
2223
bool save();
2324
bool saveAs(const QString &newFilePath);
2425
QString errorString() const;
@@ -32,6 +33,7 @@ class TextFileSaver
3233
QString m_filePath;
3334
QByteArray m_fromEncode; // default is UTF-16 (QString)
3435
QByteArray m_toEncode; // default is UTF-8
36+
bool m_useCRLF = false;
3537
QString m_errorString;
3638
static const int MAX_FILENAME_LENGTH = 245;
3739
};

src/editor/editwrapper.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2017 - 2023 UnionTech Software Technology Co., Ltd.
1+
// SPDX-FileCopyrightText: 2017 - 2026 UnionTech Software Technology Co., Ltd.
22
//
33
// SPDX-License-Identifier: GPL-3.0-or-later
44

@@ -262,7 +262,8 @@ bool EditWrapper::saveAsFile(const QString &newFilePath, const QByteArray &encod
262262
TextFileSaver saver(m_pTextEdit->document());
263263
saver.setFilePath(newFilePath);
264264
saver.setEncoding(encodeName);
265-
265+
saver.setEndlineFormat(m_pBottomBar->getEndlineFormat() == BottomBar::EndlineFormat::Windows);
266+
266267
bool saveSuccess = saver.save();
267268
if (!saveSuccess) {
268269
qWarning() << "Failed to save file:" << newFilePath << "Error:" << saver.errorString();
@@ -310,7 +311,8 @@ bool EditWrapper::saveAsFile()
310311
TextFileSaver saver(m_pTextEdit->document());
311312
saver.setFilePath(newFilePath);
312313
saver.setEncoding(m_sFirstEncode.toUtf8());
313-
314+
saver.setEndlineFormat(m_pBottomBar->getEndlineFormat() == BottomBar::EndlineFormat::Windows);
315+
314316
if (!saver.save()) {
315317
qWarning() << "EditWrapper saveAsFile, Failed to save file:" << saver.errorString();
316318
return false;
@@ -603,7 +605,8 @@ bool EditWrapper::saveFile(QByteArray encode)
603605
TextFileSaver saver(m_pTextEdit->document());
604606
saver.setFilePath(qstrFilePath);
605607
saver.setEncoding(m_sCurEncode.toUtf8());
606-
608+
saver.setEndlineFormat(m_pBottomBar->getEndlineFormat() == BottomBar::EndlineFormat::Windows);
609+
607610
bool ok = saver.save();
608611
if (ok) {
609612
qDebug() << "EditWrapper saveFile, ok is true";
@@ -674,7 +677,8 @@ bool EditWrapper::saveTemFile(QString qstrDir)
674677
TextFileSaver saver(m_pTextEdit->document());
675678
saver.setFilePath(qstrDir);
676679
saver.setEncoding(m_sCurEncode.toUtf8());
677-
680+
saver.setEndlineFormat(m_pBottomBar->getEndlineFormat() == BottomBar::EndlineFormat::Windows);
681+
678682
bool ok = saver.save();
679683
if (ok) {
680684
qDebug() << "EditWrapper saveTemFile, ok is true";
@@ -782,7 +786,8 @@ bool EditWrapper::saveDraftFile(QString &newFilePath)
782786
TextFileSaver saver(m_pTextEdit->document());
783787
saver.setFilePath(newFilePath);
784788
saver.setEncoding(encode);
785-
789+
saver.setEndlineFormat(m_pBottomBar->getEndlineFormat() == BottomBar::EndlineFormat::Windows);
790+
786791
if (!saver.save()) {
787792
qDebug() << "EditWrapper saveDraftFile, saver.save() failed";
788793
return false;

0 commit comments

Comments
 (0)