- fix: Correctly set file's real encoding to ASCII after saving

This commit is contained in:
Roy Qu 2022-10-26 16:23:34 +08:00
parent fde8a868fa
commit 5bd8fedb2b
2 changed files with 9 additions and 9 deletions

View File

@ -231,9 +231,12 @@ void Editor::saveFile(QString filename) {
QByteArray encoding = mFileEncoding; QByteArray encoding = mFileEncoding;
if (mEncodingOption!=ENCODING_AUTO_DETECT || mFileEncoding==ENCODING_ASCII) if (mEncodingOption!=ENCODING_AUTO_DETECT || mFileEncoding==ENCODING_ASCII)
encoding = mEncodingOption; encoding = mEncodingOption;
this->document()->saveToFile(file,encoding, this->document()->saveToFile(file,encoding,
pSettings->editor().defaultEncoding(), pSettings->editor().defaultEncoding(),
mFileEncoding); mFileEncoding);
if (isVisible())
pMainWindow->updateForEncodingInfo(this);
emit fileSaved(filename, inProject()); emit fileSaved(filename, inProject());
} }
@ -258,7 +261,7 @@ bool Editor::save(bool force, bool doReparse) {
pMainWindow->fileSystemWatcher()->addPath(mFilename); pMainWindow->fileSystemWatcher()->addPath(mFilename);
setModified(false); setModified(false);
mIsNew = false; mIsNew = false;
this->updateCaption(); updateCaption();
} catch (SaveException& exception) { } catch (SaveException& exception) {
if (!force) { if (!force) {
QMessageBox::critical(pMainWindow,tr("Error"), QMessageBox::critical(pMainWindow,tr("Error"),
@ -347,7 +350,6 @@ bool Editor::saveAs(const QString &name, bool fromProject){
saveFile(mFilename); saveFile(mFilename);
mIsNew = false; mIsNew = false;
setModified(false); setModified(false);
this->updateCaption();
} catch (SaveException& exception) { } catch (SaveException& exception) {
QMessageBox::critical(pMainWindow,tr("Error"), QMessageBox::critical(pMainWindow,tr("Error"),
exception.reason()); exception.reason());
@ -390,7 +392,6 @@ bool Editor::saveAs(const QString &name, bool fromProject){
if (!shouldOpenInReadonly()) { if (!shouldOpenInReadonly()) {
setReadOnly(false); setReadOnly(false);
} }
pMainWindow->updateForEncodingInfo(this);
updateCaption(); updateCaption();
emit renamed(oldName, newName , firstSave); emit renamed(oldName, newName , firstSave);

View File

@ -657,8 +657,6 @@ void Document::saveToFile(QFile &file, const QByteArray& encoding,
throw FileError(tr("Can't open file '%1' for save!").arg(file.fileName())); throw FileError(tr("Can't open file '%1' for save!").arg(file.fileName()));
if (mLines.isEmpty()) if (mLines.isEmpty())
return; return;
bool allAscii = true;
QTextCodec* codec; QTextCodec* codec;
realEncoding = encoding; realEncoding = encoding;
if (realEncoding == ENCODING_UTF8_BOM) { if (realEncoding == ENCODING_UTF8_BOM) {
@ -675,6 +673,7 @@ void Document::saveToFile(QFile &file, const QByteArray& encoding,
} else { } else {
codec = QTextCodec::codecForName(realEncoding); codec = QTextCodec::codecForName(realEncoding);
} }
bool allAscii = true;
for (PDocumentLine& line:mLines) { for (PDocumentLine& line:mLines) {
if (allAscii) { if (allAscii) {
allAscii = isTextAllAscii(line->fString); allAscii = isTextAllAscii(line->fString);
@ -686,10 +685,10 @@ void Document::saveToFile(QFile &file, const QByteArray& encoding,
} }
file.write(lineBreak().toLatin1()); file.write(lineBreak().toLatin1());
} }
if (encoding == ENCODING_AUTO_DETECT) { if (allAscii) {
if (allAscii) realEncoding = ENCODING_ASCII;
realEncoding = ENCODING_ASCII; } else if (encoding == ENCODING_AUTO_DETECT) {
else if (codec->name() == "System") { if (codec->name() == "System") {
realEncoding = pCharsetInfoManager->getDefaultSystemEncoding(); realEncoding = pCharsetInfoManager->getDefaultSystemEncoding();
} else { } else {
realEncoding = codec->name(); realEncoding = codec->name();