- fix: add mutex lock to prevent editor crash in rare conditions

This commit is contained in:
royqh1979@gmail.com 2021-10-17 21:09:50 +08:00
parent d015235404
commit fee2115093
9 changed files with 140 additions and 101 deletions

View File

@ -1,5 +1,6 @@
Version 0.6.8 Version 0.6.8
- enhancement: add link to cppreference in the help menu - enhancement: add link to cppreference in the help menu
- fix: add mutex lock to prevent editor crash in rare conditions
Version 0.6.7 Version 0.6.7
- fix: messages send to the gdb process's standard error are not received - fix: messages send to the gdb process's standard error are not received

View File

@ -121,7 +121,7 @@ PSearchResultTreeItem CppRefacter::findOccurenceInFile(
editor.lines()->setContents(buffer); editor.lines()->setContents(buffer);
} else { } else {
QByteArray encoding; QByteArray encoding;
editor.lines()->LoadFromFile(filename,ENCODING_AUTO_DETECT,encoding); editor.lines()->loadFromFile(filename,ENCODING_AUTO_DETECT,encoding);
} }
editor.setHighlighter(HighlighterManager().getCppHighlighter()); editor.setHighlighter(HighlighterManager().getCppHighlighter());
int posY = 0; int posY = 0;
@ -182,7 +182,7 @@ void CppRefacter::renameSymbolInFile(const QString &filename, const PStatement &
editor.lines()->setContents(buffer); editor.lines()->setContents(buffer);
} else { } else {
QByteArray encoding; QByteArray encoding;
editor.lines()->LoadFromFile(filename,ENCODING_AUTO_DETECT,encoding); editor.lines()->loadFromFile(filename,ENCODING_AUTO_DETECT,encoding);
} }
QStringList newContents; QStringList newContents;
editor.setHighlighter(HighlighterManager().getCppHighlighter()); editor.setHighlighter(HighlighterManager().getCppHighlighter());
@ -235,7 +235,7 @@ void CppRefacter::renameSymbolInFile(const QString &filename, const PStatement &
} else { } else {
QByteArray realEncoding; QByteArray realEncoding;
QFile file(filename); QFile file(filename);
editor.lines()->SaveToFile(file,ENCODING_AUTO_DETECT, realEncoding); editor.lines()->saveToFile(file,ENCODING_AUTO_DETECT, realEncoding);
} }
} }

View File

@ -163,9 +163,9 @@ Editor::~Editor() {
void Editor::loadFile(const QString& filename) { void Editor::loadFile(const QString& filename) {
if (filename.isEmpty()) { if (filename.isEmpty()) {
this->lines()->LoadFromFile(mFilename,mEncodingOption,mFileEncoding); this->lines()->loadFromFile(mFilename,mEncodingOption,mFileEncoding);
} else { } else {
this->lines()->LoadFromFile(filename,mEncodingOption,mFileEncoding); this->lines()->loadFromFile(filename,mEncodingOption,mFileEncoding);
} }
//this->setModified(false); //this->setModified(false);
updateCaption(); updateCaption();
@ -192,7 +192,7 @@ void Editor::loadFile(const QString& filename) {
void Editor::saveFile(const QString &filename) { void Editor::saveFile(const QString &filename) {
QFile file(filename); QFile file(filename);
this->lines()->SaveToFile(file,mEncodingOption,mFileEncoding); this->lines()->saveToFile(file,mEncodingOption,mFileEncoding);
pMainWindow->updateForEncodingInfo(); pMainWindow->updateForEncodingInfo();
} }

View File

@ -2032,6 +2032,8 @@ void MainWindow::openShell(const QString &folder, const QString &shellCommand)
void MainWindow::onAutoSaveTimeout() void MainWindow::onAutoSaveTimeout()
{ {
if (mQuitting)
return;
if (!pSettings->editor().enableAutoSave()) if (!pSettings->editor().enableAutoSave())
return; return;
int updateCount = 0; int updateCount = 0;
@ -2050,6 +2052,15 @@ void MainWindow::onAutoSaveTimeout()
} }
break; break;
case astAllProjectFiles: case astAllProjectFiles:
if (!mProject)
return;
for (int i=0;i<mEditorList->pageCount();i++) {
Editor *e = (*mEditorList)[i];
if (!e->inProject())
return;
doAutoSave(e);
updateCount++;
}
//todo: auto save project files //todo: auto save project files
break; break;
} }

View File

@ -536,13 +536,11 @@ BufferCoord SynEdit::getMatchingBracketEx(BufferCoord APoint)
QStringList SynEdit::contents() QStringList SynEdit::contents()
{ {
//QMutexLocker locker(&mMutex);
return lines()->contents(); return lines()->contents();
} }
QString SynEdit::text() QString SynEdit::text()
{ {
//QMutexLocker locker(&mMutex);
return lines()->text(); return lines()->text();
} }
@ -1750,7 +1748,7 @@ void SynEdit::doDuplicateLine()
{ {
if (!mReadOnly && (mLines->count() > 0)) { if (!mReadOnly && (mLines->count() > 0)) {
doOnPaintTransient(SynTransientType::ttBefore); doOnPaintTransient(SynTransientType::ttBefore);
mLines->Insert(mCaretY, lineText()); mLines->insert(mCaretY, lineText());
doLinesInserted(mCaretY + 1, 1); doLinesInserted(mCaretY + 1, 1);
mUndoList->AddChange(SynChangeReason::crLineBreak, mUndoList->AddChange(SynChangeReason::crLineBreak,
caretXY(), caretXY(), "", SynSelectionMode::smNormal); caretXY(), caretXY(), "", SynSelectionMode::smNormal);
@ -1774,7 +1772,7 @@ void SynEdit::doMoveSelUp()
doLinesDeleted(OrigBlockBegin.Line - 1, 1); // before start, 1 based doLinesDeleted(OrigBlockBegin.Line - 1, 1); // before start, 1 based
// Insert line below selection // Insert line below selection
mLines->Insert(OrigBlockEnd.Line - 1, s); mLines->insert(OrigBlockEnd.Line - 1, s);
doLinesInserted(OrigBlockEnd.Line, 1); doLinesInserted(OrigBlockEnd.Line, 1);
// Restore caret and selection // Restore caret and selection
@ -1820,7 +1818,7 @@ void SynEdit::doMoveSelDown()
doLinesDeleted(OrigBlockEnd.Line, 1); // before start, 1 based doLinesDeleted(OrigBlockEnd.Line, 1); // before start, 1 based
// Insert line above selection // Insert line above selection
mLines->Insert(OrigBlockBegin.Line - 1, s); mLines->insert(OrigBlockBegin.Line - 1, s);
doLinesInserted(OrigBlockBegin.Line, 1); doLinesInserted(OrigBlockBegin.Line, 1);
// Restore caret and selection // Restore caret and selection
@ -1895,7 +1893,7 @@ void SynEdit::insertLine(bool moveCaret)
if (Len > 0) { if (Len > 0) {
if (Len >= mCaretX) { if (Len >= mCaretX) {
if (mCaretX <= 1) { if (mCaretX <= 1) {
mLines->Insert(mCaretY - 1, ""); mLines->insert(mCaretY - 1, "");
nLinesInserted++; nLinesInserted++;
mUndoList->AddChange(SynChangeReason::crLineBreak, caretXY(), caretXY(), Temp2, mUndoList->AddChange(SynChangeReason::crLineBreak, caretXY(), caretXY(), Temp2,
SynSelectionMode::smNormal); SynSelectionMode::smNormal);
@ -1925,7 +1923,7 @@ void SynEdit::insertLine(bool moveCaret)
} }
} }
QString indentSpacesForRightLineText = GetLeftSpacing(indentSpaces,true); QString indentSpacesForRightLineText = GetLeftSpacing(indentSpaces,true);
mLines->Insert(mCaretY, indentSpacesForRightLineText+rightLineText); mLines->insert(mCaretY, indentSpacesForRightLineText+rightLineText);
nLinesInserted++; nLinesInserted++;
//SpaceCount1 = mLines->getString(mCaretY).length(); //??? //SpaceCount1 = mLines->getString(mCaretY).length(); //???
@ -1936,7 +1934,7 @@ void SynEdit::insertLine(bool moveCaret)
indentSpaces = indentSpacesOfLeftLineText; indentSpaces = indentSpacesOfLeftLineText;
indentSpaces += mTabWidth; indentSpaces += mTabWidth;
indentSpacesForRightLineText = GetLeftSpacing(indentSpaces,true); indentSpacesForRightLineText = GetLeftSpacing(indentSpaces,true);
mLines->Insert(mCaretY, indentSpacesForRightLineText); mLines->insert(mCaretY, indentSpacesForRightLineText);
nLinesInserted++; nLinesInserted++;
mUndoList->AddChange(SynChangeReason::crLineBreak, caretXY(), caretXY(), "", mUndoList->AddChange(SynChangeReason::crLineBreak, caretXY(), caretXY(), "",
SynSelectionMode::smNormal); SynSelectionMode::smNormal);
@ -1954,7 +1952,7 @@ void SynEdit::insertLine(bool moveCaret)
SpaceCount2 = leftSpaces(Temp); SpaceCount2 = leftSpaces(Temp);
} while ((BackCounter != 0) && (Temp == "")); } while ((BackCounter != 0) && (Temp == ""));
} }
mLines->Insert(mCaretY, ""); mLines->insert(mCaretY, "");
nLinesInserted++; nLinesInserted++;
BufferCoord Caret = caretXY(); BufferCoord Caret = caretXY();
if (moveCaret) { if (moveCaret) {
@ -1988,7 +1986,7 @@ void SynEdit::insertLine(bool moveCaret)
BackCounter--; BackCounter--;
} }
} }
mLines->Insert(mCaretY - 1, ""); mLines->insert(mCaretY - 1, "");
nLinesInserted++; nLinesInserted++;
mUndoList->AddChange(SynChangeReason::crLineBreak, caretXY(), caretXY(), "", mUndoList->AddChange(SynChangeReason::crLineBreak, caretXY(), caretXY(), "",
SynSelectionMode::smNormal); SynSelectionMode::smNormal);
@ -4690,7 +4688,7 @@ int SynEdit::insertTextByNormalMode(const QString &Value)
if (P<Value.length()) { if (P<Value.length()) {
Str = sLeftSide + Value.mid(0, P - Start); Str = sLeftSide + Value.mid(0, P - Start);
properSetLine(mCaretY - 1, Str); properSetLine(mCaretY - 1, Str);
mLines->InsertLines(mCaretY, CountLines(Value,P)); mLines->insertLines(mCaretY, CountLines(Value,P));
} else { } else {
Str = sLeftSide + Value + sRightSide; Str = sLeftSide + Value + sRightSide;
properSetLine(mCaretY - 1, Str); properSetLine(mCaretY - 1, Str);
@ -4810,7 +4808,7 @@ int SynEdit::insertTextByLineMode(const QString &Value)
else else
Str = ""; Str = "";
if ((mCaretY == mLines->count()) || mInserting) { if ((mCaretY == mLines->count()) || mInserting) {
mLines->Insert(mCaretY - 1, ""); mLines->insert(mCaretY - 1, "");
Result++; Result++;
} }
properSetLine(mCaretY - 1, Str); properSetLine(mCaretY - 1, Str);
@ -4886,7 +4884,6 @@ void SynEdit::onCommandProcessed(SynEditorCommand , QChar , void *)
void SynEdit::ExecuteCommand(SynEditorCommand Command, QChar AChar, void *pData) void SynEdit::ExecuteCommand(SynEditorCommand Command, QChar AChar, void *pData)
{ {
QMutexLocker locker(&mMutex);
incPaintLock(); incPaintLock();
auto action=finally([this] { auto action=finally([this] {
decPaintLock(); decPaintLock();
@ -5924,8 +5921,6 @@ void SynEdit::setSelLength(int Value)
void SynEdit::setSelText(const QString &text) void SynEdit::setSelText(const QString &text)
{ {
QMutexLocker locker(&mMutex);
doSetSelText(text); doSetSelText(text);
} }

View File

@ -5,7 +5,6 @@
#include <QCursor> #include <QCursor>
#include <QDateTime> #include <QDateTime>
#include <QFrame> #include <QFrame>
#include <QMutex>
#include <QStringList> #include <QStringList>
#include <QTimer> #include <QTimer>
#include <QWidget> #include <QWidget>
@ -678,8 +677,6 @@ private:
QString mInputPreeditString; QString mInputPreeditString;
QRecursiveMutex mMutex;
friend class SynEditTextPainter; friend class SynEditTextPainter;
// QWidget interface // QWidget interface

View File

@ -3,6 +3,7 @@
#include <QFile> #include <QFile>
#include <QTextCodec> #include <QTextCodec>
#include <QTextStream> #include <QTextStream>
#include <QMutexLocker>
#include <stdexcept> #include <stdexcept>
#include "SynEdit.h" #include "SynEdit.h"
#include "../utils.h" #include "../utils.h"
@ -25,24 +26,27 @@ static void ListIndexOutOfBounds(int index) {
int SynEditStringList::parenthesisLevels(int Index) const int SynEditStringList::parenthesisLevels(int Index)
{ {
QMutexLocker locker(&mMutex);
if (Index>=0 && Index < mList.size()) { if (Index>=0 && Index < mList.size()) {
return mList[Index]->fRange.parenthesisLevel; return mList[Index]->fRange.parenthesisLevel;
} else } else
return 0; return 0;
} }
int SynEditStringList::bracketLevels(int Index) const int SynEditStringList::bracketLevels(int Index)
{ {
QMutexLocker locker(&mMutex);
if (Index>=0 && Index < mList.size()) { if (Index>=0 && Index < mList.size()) {
return mList[Index]->fRange.bracketLevel; return mList[Index]->fRange.bracketLevel;
} else } else
return 0; return 0;
} }
int SynEditStringList::braceLevels(int Index) const int SynEditStringList::braceLevels(int Index)
{ {
QMutexLocker locker(&mMutex);
if (Index>=0 && Index < mList.size()) { if (Index>=0 && Index < mList.size()) {
return mList[Index]->fRange.braceLevel; return mList[Index]->fRange.braceLevel;
} else } else
@ -62,6 +66,7 @@ int SynEditStringList::braceLevels(int Index) const
int SynEditStringList::lineColumns(int Index) int SynEditStringList::lineColumns(int Index)
{ {
QMutexLocker locker(&mMutex);
if (Index>=0 && Index < mList.size()) { if (Index>=0 && Index < mList.size()) {
if (mList[Index]->fColumns == -1) { if (mList[Index]->fColumns == -1) {
return calculateLineColumns(Index); return calculateLineColumns(Index);
@ -71,16 +76,18 @@ int SynEditStringList::lineColumns(int Index)
return 0; return 0;
} }
int SynEditStringList::leftBraces(int Index) const int SynEditStringList::leftBraces(int Index)
{ {
QMutexLocker locker(&mMutex);
if (Index>=0 && Index < mList.size()) { if (Index>=0 && Index < mList.size()) {
return mList[Index]->fLeftBraces; return mList[Index]->fLeftBraces;
} else } else
return 0; return 0;
} }
int SynEditStringList::rightBraces(int Index) const int SynEditStringList::rightBraces(int Index)
{ {
QMutexLocker locker(&mMutex);
if (Index>=0 && Index < mList.size()) { if (Index>=0 && Index < mList.size()) {
return mList[Index]->fRightBraces; return mList[Index]->fRightBraces;
} else } else
@ -88,6 +95,7 @@ int SynEditStringList::rightBraces(int Index) const
} }
int SynEditStringList::lengthOfLongestLine() { int SynEditStringList::lengthOfLongestLine() {
QMutexLocker locker(&mMutex);
if (mIndexOfLongestLine < 0) { if (mIndexOfLongestLine < 0) {
int MaxLen = -1; int MaxLen = -1;
mIndexOfLongestLine = -1; mIndexOfLongestLine = -1;
@ -120,8 +128,9 @@ QString SynEditStringList::lineBreak() const
return "\n"; return "\n";
} }
const SynRangeState& SynEditStringList::ranges(int Index) const const SynRangeState& SynEditStringList::ranges(int Index)
{ {
QMutexLocker locker(&mMutex);
if (Index>=0 && Index < mList.size()) { if (Index>=0 && Index < mList.size()) {
return mList[Index]->fRange; return mList[Index]->fRange;
} else { } else {
@ -130,7 +139,7 @@ const SynRangeState& SynEditStringList::ranges(int Index) const
return {0}; return {0};
} }
void SynEditStringList::InsertItem(int Index, const QString &s) void SynEditStringList::insertItem(int Index, const QString &s)
{ {
beginUpdate(); beginUpdate();
PSynEditStringRec line = std::make_shared<SynEditStringRec>(); PSynEditStringRec line = std::make_shared<SynEditStringRec>();
@ -150,23 +159,21 @@ void SynEditStringList::addItem(const QString &s)
endUpdate(); endUpdate();
} }
ConvertTabsProcEx SynEditStringList::getConvertTabsProc() const bool SynEditStringList::getAppendNewLineAtEOF()
{
return mConvertTabsProc;
}
bool SynEditStringList::getAppendNewLineAtEOF() const
{ {
QMutexLocker locker(&mMutex);
return mAppendNewLineAtEOF; return mAppendNewLineAtEOF;
} }
void SynEditStringList::setAppendNewLineAtEOF(bool appendNewLineAtEOF) void SynEditStringList::setAppendNewLineAtEOF(bool appendNewLineAtEOF)
{ {
QMutexLocker locker(&mMutex);
mAppendNewLineAtEOF = appendNewLineAtEOF; mAppendNewLineAtEOF = appendNewLineAtEOF;
} }
void SynEditStringList::setRange(int Index, const SynRangeState& ARange, int ALeftBraces, int ARightBraces) void SynEditStringList::setRange(int Index, const SynRangeState& ARange, int ALeftBraces, int ARightBraces)
{ {
QMutexLocker locker(&mMutex);
if (Index<0 || Index>=mList.count()) { if (Index<0 || Index>=mList.count()) {
ListIndexOutOfBounds(Index); ListIndexOutOfBounds(Index);
} }
@ -177,44 +184,50 @@ void SynEditStringList::setRange(int Index, const SynRangeState& ARange, int ALe
endUpdate(); endUpdate();
} }
QString SynEditStringList::getString(int Index) const QString SynEditStringList::getString(int Index)
{ {
QMutexLocker locker(&mMutex);
if (Index<0 || Index>=mList.count()) { if (Index<0 || Index>=mList.count()) {
return QString(); return QString();
} }
return mList[Index]->fString; return mList[Index]->fString;
} }
int SynEditStringList::count() const int SynEditStringList::count()
{ {
QMutexLocker locker(&mMutex);
return mList.count(); return mList.count();
} }
void *SynEditStringList::getObject(int Index) const void *SynEditStringList::getObject(int Index)
{ {
QMutexLocker locker(&mMutex);
if (Index<0 || Index>=mList.count()) { if (Index<0 || Index>=mList.count()) {
return nullptr; return nullptr;
} }
return mList[Index]->fObject; return mList[Index]->fObject;
} }
QString SynEditStringList::text() const QString SynEditStringList::text()
{ {
QMutexLocker locker(&mMutex);
return getTextStr(); return getTextStr();
} }
void SynEditStringList::setText(const QString &text) void SynEditStringList::setText(const QString &text)
{ {
PutTextStr(text); QMutexLocker locker(&mMutex);
putTextStr(text);
} }
void SynEditStringList::setContents(const QStringList &text) void SynEditStringList::setContents(const QStringList &text)
{ {
QMutexLocker locker(&mMutex);
beginUpdate(); beginUpdate();
auto action = finally([this]{ auto action = finally([this]{
endUpdate(); endUpdate();
}); });
clear(); internalClear();
if (text.count() > 0) { if (text.count() > 0) {
mIndexOfLongestLine = -1; mIndexOfLongestLine = -1;
int FirstAdded = mList.count(); int FirstAdded = mList.count();
@ -226,8 +239,9 @@ void SynEditStringList::setContents(const QStringList &text)
} }
} }
QStringList SynEditStringList::contents() const QStringList SynEditStringList::contents()
{ {
QMutexLocker locker(&mMutex);
QStringList Result; QStringList Result;
SynEditStringRecList list = mList; SynEditStringRecList list = mList;
foreach (const PSynEditStringRec& line, list) { foreach (const PSynEditStringRec& line, list) {
@ -239,7 +253,7 @@ QStringList SynEditStringList::contents() const
void SynEditStringList::beginUpdate() void SynEditStringList::beginUpdate()
{ {
if (mUpdateCount == 0) { if (mUpdateCount == 0) {
SetUpdateState(true); setUpdateState(true);
} }
mUpdateCount++; mUpdateCount++;
} }
@ -248,16 +262,17 @@ void SynEditStringList::endUpdate()
{ {
mUpdateCount--; mUpdateCount--;
if (mUpdateCount == 0) { if (mUpdateCount == 0) {
SetUpdateState(false); setUpdateState(false);
} }
} }
int SynEditStringList::add(const QString &s) int SynEditStringList::add(const QString &s)
{ {
QMutexLocker locker(&mMutex);
beginUpdate(); beginUpdate();
int Result = mList.count(); int Result = mList.count();
InsertItem(Result, s); insertItem(Result, s);
emit inserted(Result,1); emit inserted(Result,1);
endUpdate(); endUpdate();
return Result; return Result;
@ -265,6 +280,7 @@ int SynEditStringList::add(const QString &s)
void SynEditStringList::addStrings(const QStringList &Strings) void SynEditStringList::addStrings(const QStringList &Strings)
{ {
QMutexLocker locker(&mMutex);
if (Strings.count() > 0) { if (Strings.count() > 0) {
mIndexOfLongestLine = -1; mIndexOfLongestLine = -1;
beginUpdate(); beginUpdate();
@ -282,6 +298,7 @@ void SynEditStringList::addStrings(const QStringList &Strings)
int SynEditStringList::getTextLength() int SynEditStringList::getTextLength()
{ {
QMutexLocker locker(&mMutex);
int Result = 0; int Result = 0;
foreach (const PSynEditStringRec& line, mList ) { foreach (const PSynEditStringRec& line, mList ) {
Result += line->fString.length(); Result += line->fString.length();
@ -296,18 +313,13 @@ int SynEditStringList::getTextLength()
void SynEditStringList::clear() void SynEditStringList::clear()
{ {
if (!mList.isEmpty()) { QMutexLocker locker(&mMutex);
beginUpdate(); internalClear();
int oldCount = mList.count();
mIndexOfLongestLine = -1;
mList.clear();
emit deleted(0,oldCount);
endUpdate();
}
} }
void SynEditStringList::deleteLines(int Index, int NumLines) void SynEditStringList::deleteLines(int Index, int NumLines)
{ {
QMutexLocker locker(&mMutex);
if (NumLines<=0) if (NumLines<=0)
return; return;
if ((Index < 0) || (Index >= mList.count())) { if ((Index < 0) || (Index >= mList.count())) {
@ -328,8 +340,9 @@ void SynEditStringList::deleteLines(int Index, int NumLines)
emit deleted(Index,NumLines); emit deleted(Index,NumLines);
} }
void SynEditStringList::Exchange(int Index1, int Index2) void SynEditStringList::exchange(int Index1, int Index2)
{ {
QMutexLocker locker(&mMutex);
if ((Index1 < 0) || (Index1 >= mList.count())) { if ((Index1 < 0) || (Index1 >= mList.count())) {
ListIndexOutOfBounds(Index1); ListIndexOutOfBounds(Index1);
} }
@ -346,19 +359,21 @@ void SynEditStringList::Exchange(int Index1, int Index2)
endUpdate(); endUpdate();
} }
void SynEditStringList::Insert(int Index, const QString &s) void SynEditStringList::insert(int Index, const QString &s)
{ {
QMutexLocker locker(&mMutex);
if ((Index < 0) || (Index > mList.count())) { if ((Index < 0) || (Index > mList.count())) {
ListIndexOutOfBounds(Index); ListIndexOutOfBounds(Index);
} }
beginUpdate(); beginUpdate();
InsertItem(Index, s); insertItem(Index, s);
emit inserted(Index,1); emit inserted(Index,1);
endUpdate(); endUpdate();
} }
void SynEditStringList::deleteAt(int Index) void SynEditStringList::deleteAt(int Index)
{ {
QMutexLocker locker(&mMutex);
if ((Index < 0) || (Index >= mList.count())) { if ((Index < 0) || (Index >= mList.count())) {
ListIndexOutOfBounds(Index); ListIndexOutOfBounds(Index);
} }
@ -385,6 +400,7 @@ QString SynEditStringList::getTextStr() const
} }
void SynEditStringList::putString(int Index, const QString &s) { void SynEditStringList::putString(int Index, const QString &s) {
QMutexLocker locker(&mMutex);
if (Index == mList.count()) { if (Index == mList.count()) {
add(s); add(s);
} else { } else {
@ -402,6 +418,7 @@ void SynEditStringList::putString(int Index, const QString &s) {
void SynEditStringList::putObject(int Index, void *AObject) void SynEditStringList::putObject(int Index, void *AObject)
{ {
QMutexLocker locker(&mMutex);
if (Index<0 || Index>=mList.count()) { if (Index<0 || Index>=mList.count()) {
ListIndexOutOfBounds(Index); ListIndexOutOfBounds(Index);
} }
@ -410,7 +427,7 @@ void SynEditStringList::putObject(int Index, void *AObject)
endUpdate(); endUpdate();
} }
void SynEditStringList::SetUpdateState(bool Updating) void SynEditStringList::setUpdateState(bool Updating)
{ {
if (Updating) if (Updating)
emit changing(); emit changing();
@ -426,8 +443,9 @@ int SynEditStringList::calculateLineColumns(int Index)
return line->fColumns; return line->fColumns;
} }
void SynEditStringList::InsertLines(int Index, int NumLines) void SynEditStringList::insertLines(int Index, int NumLines)
{ {
QMutexLocker locker(&mMutex);
if (Index<0 || Index>mList.count()) { if (Index<0 || Index>mList.count()) {
ListIndexOutOfBounds(Index); ListIndexOutOfBounds(Index);
} }
@ -446,8 +464,9 @@ void SynEditStringList::InsertLines(int Index, int NumLines)
emit inserted(Index,NumLines); emit inserted(Index,NumLines);
} }
void SynEditStringList::InsertStrings(int Index, const QStringList &NewStrings) void SynEditStringList::insertStrings(int Index, const QStringList &NewStrings)
{ {
QMutexLocker locker(&mMutex);
if (Index<0 || Index>mList.count()) { if (Index<0 || Index>mList.count()) {
ListIndexOutOfBounds(Index); ListIndexOutOfBounds(Index);
} }
@ -467,19 +486,21 @@ void SynEditStringList::InsertStrings(int Index, const QStringList &NewStrings)
emit inserted(Index,NewStrings.length()); emit inserted(Index,NewStrings.length());
} }
void SynEditStringList::InsertText(int Index, const QString &NewText) void SynEditStringList::insertText(int Index, const QString &NewText)
{ {
QMutexLocker locker(&mMutex);
if (Index<0 || Index>=mList.count()) { if (Index<0 || Index>=mList.count()) {
ListIndexOutOfBounds(Index); ListIndexOutOfBounds(Index);
} }
if (NewText.isEmpty()) if (NewText.isEmpty())
return; return;
QStringList lines = TextToLines(NewText); QStringList lines = TextToLines(NewText);
InsertStrings(Index,lines); insertStrings(Index,lines);
} }
void SynEditStringList::LoadFromFile(const QString& filename, const QByteArray& encoding, QByteArray& realEncoding) void SynEditStringList::loadFromFile(const QString& filename, const QByteArray& encoding, QByteArray& realEncoding)
{ {
QMutexLocker locker(&mMutex);
QFile file(filename); QFile file(filename);
if (!file.open(QFile::ReadOnly )) if (!file.open(QFile::ReadOnly ))
throw FileError(tr("Can't open file '%1' for read!").arg(file.fileName())); throw FileError(tr("Can't open file '%1' for read!").arg(file.fileName()));
@ -514,7 +535,7 @@ void SynEditStringList::LoadFromFile(const QString& filename, const QByteArray&
} else if (line.endsWith("\r")) { } else if (line.endsWith("\r")) {
mFileEndingType = FileEndingType::Mac; mFileEndingType = FileEndingType::Mac;
} }
clear(); internalClear();
while (true) { while (true) {
if (allAscii) { if (allAscii) {
allAscii = isTextAllAscii(line); allAscii = isTextAllAscii(line);
@ -558,7 +579,7 @@ void SynEditStringList::LoadFromFile(const QString& filename, const QByteArray&
textStream.setCodec(realEncoding); textStream.setCodec(realEncoding);
} }
QString line; QString line;
clear(); internalClear();
while (textStream.readLineInto(&line)) { while (textStream.readLineInto(&line)) {
addItem(TrimRight(line)); addItem(TrimRight(line));
} }
@ -567,8 +588,9 @@ void SynEditStringList::LoadFromFile(const QString& filename, const QByteArray&
void SynEditStringList::SaveToFile(QFile &file, const QByteArray& encoding, QByteArray& realEncoding) void SynEditStringList::saveToFile(QFile &file, const QByteArray& encoding, QByteArray& realEncoding)
{ {
QMutexLocker locker(&mMutex);
if (!file.open(QFile::WriteOnly | QFile::Truncate)) if (!file.open(QFile::WriteOnly | QFile::Truncate))
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 (mList.isEmpty()) if (mList.isEmpty())
@ -611,13 +633,13 @@ void SynEditStringList::SaveToFile(QFile &file, const QByteArray& encoding, QByt
} }
} }
void SynEditStringList::PutTextStr(const QString &text) void SynEditStringList::putTextStr(const QString &text)
{ {
beginUpdate(); beginUpdate();
auto action = finally([this]{ auto action = finally([this]{
endUpdate(); endUpdate();
}); });
clear(); internalClear();
int pos = 0; int pos = 0;
int start; int start;
while (pos < text.length()) { while (pos < text.length()) {
@ -638,23 +660,39 @@ void SynEditStringList::PutTextStr(const QString &text)
} }
} }
FileEndingType SynEditStringList::getFileEndingType() const void SynEditStringList::internalClear()
{ {
if (!mList.isEmpty()) {
beginUpdate();
int oldCount = mList.count();
mIndexOfLongestLine = -1;
mList.clear();
emit deleted(0,oldCount);
endUpdate();
}
}
FileEndingType SynEditStringList::getFileEndingType()
{
QMutexLocker locker(&mMutex);
return mFileEndingType; return mFileEndingType;
} }
void SynEditStringList::setFileEndingType(const FileEndingType &fileEndingType) void SynEditStringList::setFileEndingType(const FileEndingType &fileEndingType)
{ {
QMutexLocker locker(&mMutex);
mFileEndingType = fileEndingType; mFileEndingType = fileEndingType;
} }
bool SynEditStringList::empty() bool SynEditStringList::empty()
{ {
return count()==0; QMutexLocker locker(&mMutex);
return mList.count()==0;
} }
void SynEditStringList::resetColumns() void SynEditStringList::resetColumns()
{ {
QMutexLocker locker(&mMutex);
mIndexOfLongestLine = -1; mIndexOfLongestLine = -1;
if (mList.count() > 0 ) { if (mList.count() > 0 ) {
for (int i=0;i<mList.size();i++) { for (int i=0;i<mList.size();i++) {
@ -665,6 +703,7 @@ void SynEditStringList::resetColumns()
void SynEditStringList::invalidAllLineColumns() void SynEditStringList::invalidAllLineColumns()
{ {
QMutexLocker locker(&mMutex);
mIndexOfLongestLine = -1; mIndexOfLongestLine = -1;
for (PSynEditStringRec& line:mList) { for (PSynEditStringRec& line:mList) {
line->fColumns = -1; line->fColumns = -1;

View File

@ -3,6 +3,7 @@
#include <QStringList> #include <QStringList>
#include "highlighter/base.h" #include "highlighter/base.h"
#include <QMutex>
#include <QVector> #include <QVector>
#include <memory> #include <memory>
#include "MiscProcs.h" #include "MiscProcs.h"
@ -50,23 +51,23 @@ class SynEditStringList : public QObject
public: public:
explicit SynEditStringList(SynEdit* pEdit,QObject* parent=nullptr); explicit SynEditStringList(SynEdit* pEdit,QObject* parent=nullptr);
int parenthesisLevels(int Index) const; int parenthesisLevels(int Index);
int bracketLevels(int Index) const; int bracketLevels(int Index);
int braceLevels(int Index) const; int braceLevels(int Index);
int lineColumns(int Index); int lineColumns(int Index);
int leftBraces(int Index) const; int leftBraces(int Index);
int rightBraces(int Index) const; int rightBraces(int Index);
int lengthOfLongestLine(); int lengthOfLongestLine();
QString lineBreak() const; QString lineBreak() const;
const SynRangeState& ranges(int Index) const; const SynRangeState& ranges(int Index);
void setRange(int Index, const SynRangeState& ARange, int leftBraces, int rightBraces); void setRange(int Index, const SynRangeState& ARange, int leftBraces, int rightBraces);
QString getString(int Index) const; QString getString(int Index);
int count() const ; int count();
void* getObject(int Index) const; void* getObject(int Index);
QString text() const; QString text();
void setText(const QString& text); void setText(const QString& text);
void setContents(const QStringList& text); void setContents(const QStringList& text);
QStringList contents() const; QStringList contents();
void putString(int Index, const QString& s); void putString(int Index, const QString& s);
void putObject(int Index, void * AObject); void putObject(int Index, void * AObject);
@ -81,20 +82,18 @@ public:
void clear(); void clear();
void deleteAt(int Index); void deleteAt(int Index);
void deleteLines(int Index, int NumLines); void deleteLines(int Index, int NumLines);
void Exchange(int Index1, int Index2); void exchange(int Index1, int Index2);
void Insert(int Index, const QString& s); void insert(int Index, const QString& s);
void InsertLines(int Index, int NumLines); void insertLines(int Index, int NumLines);
void InsertStrings(int Index, const QStringList& NewStrings); void insertStrings(int Index, const QStringList& NewStrings);
void InsertText(int Index,const QString& NewText); void insertText(int Index,const QString& NewText);
void LoadFromFile(const QString& filename, const QByteArray& encoding, QByteArray& realEncoding); void loadFromFile(const QString& filename, const QByteArray& encoding, QByteArray& realEncoding);
void SaveToFile(QFile& file, const QByteArray& encoding, QByteArray& realEncoding); void saveToFile(QFile& file, const QByteArray& encoding, QByteArray& realEncoding);
bool getAppendNewLineAtEOF() const; bool getAppendNewLineAtEOF();
void setAppendNewLineAtEOF(bool appendNewLineAtEOF); void setAppendNewLineAtEOF(bool appendNewLineAtEOF);
ConvertTabsProcEx getConvertTabsProc() const; FileEndingType getFileEndingType();
FileEndingType getFileEndingType() const;
void setFileEndingType(const FileEndingType &fileEndingType); void setFileEndingType(const FileEndingType &fileEndingType);
bool empty(); bool empty();
@ -112,10 +111,11 @@ signals:
void putted(int index, int count); void putted(int index, int count);
protected: protected:
QString getTextStr() const; QString getTextStr() const;
void SetUpdateState(bool Updating); void setUpdateState(bool Updating);
void InsertItem(int Index, const QString& s); void insertItem(int Index, const QString& s);
void addItem(const QString& s); void addItem(const QString& s);
void PutTextStr(const QString& text); void putTextStr(const QString& text);
void internalClear();
private: private:
SynEditStringRecList mList; SynEditStringRecList mList;
@ -125,9 +125,9 @@ private:
//int mCapacity; //int mCapacity;
FileEndingType mFileEndingType; FileEndingType mFileEndingType;
bool mAppendNewLineAtEOF; bool mAppendNewLineAtEOF;
ConvertTabsProcEx mConvertTabsProc;
int mIndexOfLongestLine; int mIndexOfLongestLine;
int mUpdateCount; int mUpdateCount;
QRecursiveMutex mMutex;
int calculateLineColumns(int Index); int calculateLineColumns(int Index);
}; };

View File

@ -3,10 +3,6 @@
#include <QObject> #include <QObject>
class AppTheme {
QPalette
};
class ThemeManager : public QObject class ThemeManager : public QObject
{ {
Q_OBJECT Q_OBJECT