refactor: block start / end calculation

This commit is contained in:
Roy Qu 2022-12-10 18:48:48 +08:00
parent 210eb8b6dc
commit 5d0c49a0bc
13 changed files with 230 additions and 240 deletions

View File

@ -2,6 +2,8 @@ Red Panda C++ Version 2.6
- enhancement: Highlighter for makefiles - enhancement: Highlighter for makefiles
- fix: QSortFilterProxyModel not correctly cleared when exiting and project closed. (ASSERT fails in DEBUG mode.) - fix: QSortFilterProxyModel not correctly cleared when exiting and project closed. (ASSERT fails in DEBUG mode.)
- enhancement: Windows installers now use UNICODE encoding.
- fix: can't correctly show code suggestions after "template <"
Red Panda C++ Version 2.5 Red Panda C++ Version 2.5

View File

@ -729,7 +729,6 @@ void Editor::keyPressEvent(QKeyEvent *event)
return; return;
} else if (mLastIdCharPressed==pSettings->codeCompletion().minCharRequired()){ } else if (mLastIdCharPressed==pSettings->codeCompletion().minCharRequired()){
QString lastWord = getPreviousWordAtPositionForSuggestion(caretXY()); QString lastWord = getPreviousWordAtPositionForSuggestion(caretXY());
if (mParser && !lastWord.isEmpty()) { if (mParser && !lastWord.isEmpty()) {
if (lastWord == "using") { if (lastWord == "using") {
commandProcessor(QSynedit::EditCommand::ecChar,ch,nullptr); commandProcessor(QSynedit::EditCommand::ecChar,ch,nullptr);
@ -2350,7 +2349,7 @@ bool Editor::handleParentheseSkip()
if (document()->count()==0) if (document()->count()==0)
return false; return false;
if (highlighter()) { if (highlighter() && highlighter()->supportBraceLevel()) {
QSynedit::HighlighterState lastLineState = document()->ranges(document()->count()-1); QSynedit::HighlighterState lastLineState = document()->ranges(document()->count()-1);
if (lastLineState.parenthesisLevel==0) { if (lastLineState.parenthesisLevel==0) {
setCaretXY( QSynedit::BufferCoord{caretX() + 1, caretY()}); // skip over setCaretXY( QSynedit::BufferCoord{caretX() + 1, caretY()}); // skip over
@ -2401,7 +2400,7 @@ bool Editor::handleBracketSkip()
if (document()->count()==0) if (document()->count()==0)
return false; return false;
if (highlighter()) { if (highlighter() && highlighter()->supportBraceLevel()) {
QSynedit::HighlighterState lastLineState = document()->ranges(document()->count()-1); QSynedit::HighlighterState lastLineState = document()->ranges(document()->count()-1);
if (lastLineState.bracketLevel==0) { if (lastLineState.bracketLevel==0) {
setCaretXY( QSynedit::BufferCoord{caretX() + 1, caretY()}); // skip over setCaretXY( QSynedit::BufferCoord{caretX() + 1, caretY()}); // skip over
@ -2487,7 +2486,8 @@ bool Editor::handleBraceSkip()
if (document()->count()==0) if (document()->count()==0)
return false; return false;
if (highlighter()) {
if (highlighter() && highlighter()->supportBraceLevel()) {
QSynedit::HighlighterState lastLineState = document()->ranges(document()->count()-1); QSynedit::HighlighterState lastLineState = document()->ranges(document()->count()-1);
if (lastLineState.braceLevel==0) { if (lastLineState.braceLevel==0) {
bool oldInsertMode = insertMode(); bool oldInsertMode = insertMode();
@ -3150,7 +3150,6 @@ void Editor::showCompletion(const QString& preWord,bool autoComplete, CodeComple
pos, pos,
memberOperator, memberOperator,
memberExpression); memberExpression);
// qDebug()<<ownerExpression<<memberExpression;
word = memberExpression.join(""); word = memberExpression.join("");
mCompletionPopup->prepareSearch( mCompletionPopup->prepareSearch(
preWord, preWord,
@ -4421,7 +4420,10 @@ QString Editor::getPreviousWordAtPositionForSuggestion(const QSynedit::BufferCoo
bracketLevel++; bracketLevel++;
} else if (s[wordEnd] == '<' } else if (s[wordEnd] == '<'
|| s[wordEnd] == '[') { || s[wordEnd] == '[') {
bracketLevel--; if (bracketLevel>0)
bracketLevel--;
else
return "";
} else if (bracketLevel==0) { } else if (bracketLevel==0) {
//we can't differentiate multiple definition and function parameter define here , so we don't handle ',' //we can't differentiate multiple definition and function parameter define here , so we don't handle ','
if (s[wordEnd] == ',') { if (s[wordEnd] == ',') {
@ -4473,7 +4475,7 @@ QString Editor::getPreviousWordAtPositionForCompleteFunctionDefinition(const QSy
if (wordEnd >= s.length()) if (wordEnd >= s.length())
wordEnd = s.length()-1; wordEnd = s.length()-1;
while (wordEnd > 0 && (isIdentChar(s[wordEnd]) || s[wordEnd] == ':')) { while (wordEnd > 0 && (isIdentChar(s[wordEnd]) || s[wordEnd] == ':')) {
wordEnd--; wordEnd--;
} }
int bracketLevel=0; int bracketLevel=0;
while (wordEnd > 0) { while (wordEnd > 0) {
@ -4482,7 +4484,10 @@ QString Editor::getPreviousWordAtPositionForCompleteFunctionDefinition(const QSy
bracketLevel++; bracketLevel++;
} else if (s[wordEnd] == '<' } else if (s[wordEnd] == '<'
|| s[wordEnd] == '[') { || s[wordEnd] == '[') {
bracketLevel--; if (bracketLevel>0)
bracketLevel--;
else
break;
} else if (bracketLevel==0) { } else if (bracketLevel==0) {
if (s[wordEnd]=='*' || s[wordEnd]=='&' || s[wordEnd]==' ' || s[wordEnd]=='\t') { if (s[wordEnd]=='*' || s[wordEnd]=='&' || s[wordEnd]==' ' || s[wordEnd]=='\t') {
//do nothing //do nothing

View File

@ -49,7 +49,7 @@ CodeFoldingOptions::CodeFoldingOptions():
folderBarLinesColor(QColor("black")), folderBarLinesColor(QColor("black")),
indentGuidesColor("gray") indentGuidesColor("gray")
{ {
foldRegions.add(true,'{','}',SYNS_AttrSymbol);
} }

View File

@ -50,7 +50,7 @@ struct CodeFoldingOptions {
QColor collapsedLineColor; QColor collapsedLineColor;
QColor folderBarLinesColor; QColor folderBarLinesColor;
QColor indentGuidesColor; QColor indentGuidesColor;
CodeFoldingDefines foldRegions; //CodeFoldingDefines foldRegions;
CodeFoldingOptions(); CodeFoldingOptions();
}; };

View File

@ -3424,6 +3424,10 @@ int SynEdit::scanFrom(int Index, int canStopIndex)
iRange = mHighlighter->getState(); iRange = mHighlighter->getState();
if (Result > canStopIndex){ if (Result > canStopIndex){
if (mDocument->ranges(Result).state == iRange.state if (mDocument->ranges(Result).state == iRange.state
&& mDocument->ranges(Result).blockLevel == iRange.blockLevel
&& mDocument->ranges(Result).blockStarted == iRange.blockStarted
&& mDocument->ranges(Result).blockEnded == iRange.blockEnded
&& mDocument->ranges(Result).blockEndedLastLine == iRange.blockEndedLastLine
&& mDocument->ranges(Result).braceLevel == iRange.braceLevel && mDocument->ranges(Result).braceLevel == iRange.braceLevel
&& mDocument->ranges(Result).parenthesisLevel == iRange.parenthesisLevel && mDocument->ranges(Result).parenthesisLevel == iRange.parenthesisLevel
&& mDocument->ranges(Result).bracketLevel == iRange.bracketLevel && mDocument->ranges(Result).bracketLevel == iRange.bracketLevel
@ -3600,13 +3604,10 @@ void SynEdit::rescanForFoldRanges()
} }
} }
void SynEdit::scanForFoldRanges(PCodeFoldingRanges TopFoldRanges) void SynEdit::scanForFoldRanges(PCodeFoldingRanges topFoldRanges)
{ {
PCodeFoldingRanges parentFoldRanges = TopFoldRanges; PCodeFoldingRanges parentFoldRanges = topFoldRanges;
// Recursively scan for folds (all types) findSubFoldRange(topFoldRanges, parentFoldRanges,PCodeFoldingRange());
for (int i= 0 ; i< mCodeFolding.foldRegions.count() ; i++ ) {
findSubFoldRange(TopFoldRanges, i,parentFoldRanges,PCodeFoldingRange());
}
} }
//this func should only be used in findSubFoldRange //this func should only be used in findSubFoldRange
@ -3639,108 +3640,53 @@ int SynEdit::lineHasChar(int Line, int startChar, QChar character, const QString
return -1; return -1;
} }
void SynEdit::findSubFoldRange(PCodeFoldingRanges TopFoldRanges, int FoldIndex,PCodeFoldingRanges& parentFoldRanges, PCodeFoldingRange Parent) void SynEdit::findSubFoldRange(PCodeFoldingRanges topFoldRanges, PCodeFoldingRanges& parentFoldRanges, PCodeFoldingRange parent)
{ {
PCodeFoldingRange CollapsedFold; PCodeFoldingRange collapsedFold;
int Line = 0; int line = 0;
QString CurLine; QString curLine;
if (!mHighlighter) if (!mHighlighter)
return; return;
bool useBraces = ( mCodeFolding.foldRegions.get(FoldIndex)->openSymbol == "{"
&& mCodeFolding.foldRegions.get(FoldIndex)->closeSymbol == "}");
while (Line < mDocument->count()) { // index is valid for LinesToScan and fLines while (line < mDocument->count()) { // index is valid for LinesToScan and fLines
// If there is a collapsed fold over here, skip it // If there is a collapsed fold over here, skip it
CollapsedFold = collapsedFoldStartAtLine(Line + 1); // only collapsed folds remain collapsedFold = collapsedFoldStartAtLine(line + 1); // only collapsed folds remain
if (CollapsedFold) { if (collapsedFold) {
Line = CollapsedFold->toLine; line = collapsedFold->toLine;
continue; continue;
} }
//we just use braceLevel // Find an opening character on this line
if (useBraces) { curLine = mDocument->getString(line);
// Find an opening character on this line if (mDocument->blockEnded(line)>0) {
CurLine = mDocument->getString(Line); for (int i=0; i<mDocument->blockEnded(line);i++) {
if (mDocument->rightBraces(Line)>0) { // Stop the recursion if we find a closing char, and return to our parent
for (int i=0; i<mDocument->rightBraces(Line);i++) { if (parent) {
// Stop the recursion if we find a closing char, and return to our parent parent->toLine = line + 1;
if (Parent) { parent = parent->parent.lock();
Parent->toLine = Line + 1; if (!parent) {
Parent = Parent->parent.lock(); parentFoldRanges = topFoldRanges;
if (!Parent) { } else {
parentFoldRanges = TopFoldRanges; parentFoldRanges = parent->subFoldRanges;
} else {
parentFoldRanges = Parent->subFoldRanges;
}
} }
} }
} }
if (mDocument->leftBraces(Line)>0) {
for (int i=0; i<mDocument->leftBraces(Line);i++) {
// Add it to the top list of folds
Parent = parentFoldRanges->addByParts(
Parent,
TopFoldRanges,
Line + 1,
Line + 1);
parentFoldRanges = Parent->subFoldRanges;
}
}
} else {
// Find an opening character on this line
CurLine = mDocument->getString(Line);
mHighlighter->setState(mDocument->ranges(Line));
mHighlighter->setLine(CurLine,Line);
QString token;
int pos;
while (!mHighlighter->eol()) {
token = mHighlighter->getToken();
pos = mHighlighter->getTokenPos()+token.length();
PHighlighterAttribute attr = mHighlighter->getTokenAttribute();
// We've found a starting character and it have proper highlighting (ignore stuff inside comments...)
if (token == mCodeFolding.foldRegions.get(FoldIndex)->openSymbol && attr->name()==mCodeFolding.foldRegions.get(FoldIndex)->highlight) {
// And ignore lines with both opening and closing chars in them
if (lineHasChar(Line,pos,mCodeFolding.foldRegions.get(FoldIndex)->closeSymbol,
mCodeFolding.foldRegions.get(FoldIndex)->highlight)<0) {
// Add it to the top list of folds
Parent = parentFoldRanges->addByParts(
Parent,
TopFoldRanges,
Line + 1,
Line + 1);
parentFoldRanges = Parent->subFoldRanges;
// Skip until a newline
break;
}
} else if (token == mCodeFolding.foldRegions.get(FoldIndex)->closeSymbol && attr->name()==mCodeFolding.foldRegions.get(FoldIndex)->highlight) {
// And ignore lines with both opening and closing chars in them
if (lineHasChar(Line,pos,mCodeFolding.foldRegions.get(FoldIndex)->openSymbol,
mCodeFolding.foldRegions.get(FoldIndex)->highlight)<0) {
// Stop the recursion if we find a closing char, and return to our parent
if (Parent) {
Parent->toLine = Line + 1;
Parent = Parent->parent.lock();
if (!Parent) {
parentFoldRanges = TopFoldRanges;
} else {
parentFoldRanges = Parent->subFoldRanges;
}
}
// Skip until a newline
break;
}
}
mHighlighter->next();
}
} }
Line++; if (mDocument->blockStarted(line)>0) {
for (int i=0; i<mDocument->blockStarted(line);i++) {
// Add it to the top list of folds
parent = parentFoldRanges->addByParts(
parent,
topFoldRanges,
line + 1,
line + 1);
parentFoldRanges = parent->subFoldRanges;
}
}
line++;
} }
} }
PCodeFoldingRange SynEdit::collapsedFoldStartAtLine(int Line) PCodeFoldingRange SynEdit::collapsedFoldStartAtLine(int Line)
@ -5039,10 +4985,10 @@ void SynEdit::doGotoBlockStart(bool isSelection)
//todo: handle block other than {} //todo: handle block other than {}
if (document()->braceLevels(mCaretY-1)==0) { if (document()->braceLevels(mCaretY-1)==0) {
doGotoEditorStart(isSelection); doGotoEditorStart(isSelection);
} else if (document()->leftBraces(mCaretY-1)==0){ } else if (document()->blockStarted(mCaretY-1)==0){
int line=mCaretY-1; int line=mCaretY-1;
while (line>=1) { while (line>=1) {
if (document()->leftBraces(line-1)>document()->rightBraces(line-1)) { if (document()->blockStarted(line-1)>document()->blockEnded(line-1)) {
moveCaretVert(line+1-mCaretY, isSelection); moveCaretVert(line+1-mCaretY, isSelection);
moveCaretToLineStart(isSelection); moveCaretToLineStart(isSelection);
setTopLine(line-1); setTopLine(line-1);
@ -5059,12 +5005,12 @@ void SynEdit::doGotoBlockEnd(bool isSelection)
return; return;
HighlighterState state = document()->ranges(mCaretY-1); HighlighterState state = document()->ranges(mCaretY-1);
//todo: handle block other than {} //todo: handle block other than {}
if (document()->braceLevels(mCaretY-1)==0) { if (document()->blockLevel(mCaretY-1)==0) {
doGotoEditorEnd(isSelection); doGotoEditorEnd(isSelection);
} else if (document()->rightBraces(mCaretY-1)==0){ } else if (document()->blockEnded(mCaretY-1)==0){
int line=mCaretY+1; int line=mCaretY+1;
while (line<=document()->count()) { while (line<=document()->count()) {
if (document()->rightBraces(line-1)>document()->leftBraces(line-1)) { if (document()->blockEnded(line-1)>document()->blockStarted(line-1)) {
moveCaretVert(line-1-mCaretY, isSelection); moveCaretVert(line-1-mCaretY, isSelection);
moveCaretToLineStart(isSelection); moveCaretToLineStart(isSelection);
setTopLine(line-mLinesInWindow+1); setTopLine(line-mLinesInWindow+1);

View File

@ -513,9 +513,9 @@ private:
void foldOnListCleared(); void foldOnListCleared();
void rescanFolds(); // rescan for folds void rescanFolds(); // rescan for folds
void rescanForFoldRanges(); void rescanForFoldRanges();
void scanForFoldRanges(PCodeFoldingRanges TopFoldRanges); void scanForFoldRanges(PCodeFoldingRanges topFoldRanges);
int lineHasChar(int Line, int startChar, QChar character, const QString& highlighterAttrName); int lineHasChar(int Line, int startChar, QChar character, const QString& highlighterAttrName);
void findSubFoldRange(PCodeFoldingRanges TopFoldRanges,int FoldIndex,PCodeFoldingRanges& parentFoldRanges, PCodeFoldingRange Parent); void findSubFoldRange(PCodeFoldingRanges topFoldRanges,PCodeFoldingRanges& parentFoldRanges, PCodeFoldingRange Parent);
PCodeFoldingRange collapsedFoldStartAtLine(int Line); PCodeFoldingRange collapsedFoldStartAtLine(int Line);
void initializeCaret(); void initializeCaret();
PCodeFoldingRange foldStartAtLine(int Line) const; PCodeFoldingRange foldStartAtLine(int Line) const;

View File

@ -55,59 +55,71 @@ static void ListIndexOutOfBounds(int index) {
int Document::parenthesisLevels(int Index) int Document::parenthesisLevels(int index)
{ {
QMutexLocker locker(&mMutex); QMutexLocker locker(&mMutex);
if (Index>=0 && Index < mLines.size()) { if (index>=0 && index < mLines.size()) {
return mLines[Index]->fRange.parenthesisLevel; return mLines[index]->fRange.parenthesisLevel;
} else } else
return 0; return 0;
} }
int Document::bracketLevels(int Index) int Document::bracketLevels(int index)
{ {
QMutexLocker locker(&mMutex); QMutexLocker locker(&mMutex);
if (Index>=0 && Index < mLines.size()) { if (index>=0 && index < mLines.size()) {
return mLines[Index]->fRange.bracketLevel; return mLines[index]->fRange.bracketLevel;
} else } else
return 0; return 0;
} }
int Document::braceLevels(int Index) int Document::braceLevels(int index)
{ {
QMutexLocker locker(&mMutex); QMutexLocker locker(&mMutex);
if (Index>=0 && Index < mLines.size()) { if (index>=0 && index < mLines.size()) {
return mLines[Index]->fRange.braceLevel; return mLines[index]->fRange.braceLevel;
} else } else
return 0; return 0;
} }
int Document::lineColumns(int Index) int Document::lineColumns(int index)
{ {
QMutexLocker locker(&mMutex); QMutexLocker locker(&mMutex);
if (Index>=0 && Index < mLines.size()) { if (index>=0 && index < mLines.size()) {
if (mLines[Index]->fColumns == -1) { if (mLines[index]->fColumns == -1) {
return calculateLineColumns(Index); return calculateLineColumns(index);
} else } else
return mLines[Index]->fColumns; return mLines[index]->fColumns;
} else } else
return 0; return 0;
} }
int Document::leftBraces(int Index) int Document::blockLevel(int index)
{ {
QMutexLocker locker(&mMutex); QMutexLocker locker(&mMutex);
if (Index>=0 && Index < mLines.size()) { if (index>=0 && index < mLines.size()) {
return mLines[Index]->fRange.leftBraces; return mLines[index]->fRange.blockLevel;
} else } else
return 0; return 0;
} }
int Document::rightBraces(int Index) int Document::blockStarted(int index)
{ {
QMutexLocker locker(&mMutex); QMutexLocker locker(&mMutex);
if (Index>=0 && Index < mLines.size()) { if (index>=0 && index < mLines.size()) {
return mLines[Index]->fRange.rightBraces; return mLines[index]->fRange.blockStarted;
} else
return 0;
}
int Document::blockEnded(int index)
{
QMutexLocker locker(&mMutex);
if (index>=0 && index < mLines.size()) {
int result = mLines[index]->fRange.blockEnded;
// if (index+1 < mLines.size())
// result += mLines[index+1]->fRange.blockEndedLastLine;
return result;
} else } else
return 0; return 0;
} }
@ -146,13 +158,13 @@ QString Document::lineBreak() const
return "\n"; return "\n";
} }
HighlighterState Document::ranges(int Index) HighlighterState Document::ranges(int index)
{ {
QMutexLocker locker(&mMutex); QMutexLocker locker(&mMutex);
if (Index>=0 && Index < mLines.size()) { if (index>=0 && index < mLines.size()) {
return mLines[Index]->fRange; return mLines[index]->fRange;
} else { } else {
ListIndexOutOfBounds(Index); ListIndexOutOfBounds(index);
} }
return HighlighterState(); return HighlighterState();
} }
@ -189,14 +201,14 @@ void Document::setAppendNewLineAtEOF(bool appendNewLineAtEOF)
mAppendNewLineAtEOF = appendNewLineAtEOF; mAppendNewLineAtEOF = appendNewLineAtEOF;
} }
void Document::setRange(int Index, const HighlighterState& ARange) void Document::setRange(int Index, const HighlighterState& range)
{ {
QMutexLocker locker(&mMutex); QMutexLocker locker(&mMutex);
if (Index<0 || Index>=mLines.count()) { if (Index<0 || Index>=mLines.count()) {
ListIndexOutOfBounds(Index); ListIndexOutOfBounds(Index);
} }
beginUpdate(); beginUpdate();
mLines[Index]->fRange = ARange; mLines[Index]->fRange = range;
endUpdate(); endUpdate();
} }
@ -285,10 +297,10 @@ int Document::add(const QString &s)
return Result; return Result;
} }
void Document::addStrings(const QStringList &Strings) void Document::addStrings(const QStringList &strings)
{ {
QMutexLocker locker(&mMutex); QMutexLocker locker(&mMutex);
if (Strings.count() > 0) { if (strings.count() > 0) {
mIndexOfLongestLine = -1; mIndexOfLongestLine = -1;
beginUpdate(); beginUpdate();
auto action = finally([this]{ auto action = finally([this]{
@ -296,10 +308,10 @@ void Document::addStrings(const QStringList &Strings)
}); });
int FirstAdded = mLines.count(); int FirstAdded = mLines.count();
for (const QString& s:Strings) { for (const QString& s:strings) {
addItem(s); addItem(s);
} }
emit inserted(FirstAdded,Strings.count()); emit inserted(FirstAdded,strings.count());
} }
} }
@ -324,80 +336,80 @@ void Document::clear()
internalClear(); internalClear();
} }
void Document::deleteLines(int Index, int NumLines) void Document::deleteLines(int index, int numLines)
{ {
QMutexLocker locker(&mMutex); QMutexLocker locker(&mMutex);
if (NumLines<=0) if (numLines<=0)
return; return;
if ((Index < 0) || (Index >= mLines.count())) { if ((index < 0) || (index >= mLines.count())) {
ListIndexOutOfBounds(Index); ListIndexOutOfBounds(index);
} }
beginUpdate(); beginUpdate();
auto action = finally([this]{ auto action = finally([this]{
endUpdate(); endUpdate();
}); });
if (mIndexOfLongestLine>=Index) { if (mIndexOfLongestLine>=index) {
if (mIndexOfLongestLine <Index+NumLines) { if (mIndexOfLongestLine <index+numLines) {
mIndexOfLongestLine = -1; mIndexOfLongestLine = -1;
} else { } else {
mIndexOfLongestLine -= NumLines; mIndexOfLongestLine -= numLines;
} }
} }
int LinesAfter = mLines.count() - (Index + NumLines); int LinesAfter = mLines.count() - (index + numLines);
if (LinesAfter < 0) { if (LinesAfter < 0) {
NumLines = mLines.count() - Index; numLines = mLines.count() - index;
} }
mLines.remove(Index,NumLines); mLines.remove(index,numLines);
emit deleted(Index,NumLines); emit deleted(index,numLines);
} }
void Document::exchange(int Index1, int Index2) void Document::exchange(int index1, int index2)
{ {
QMutexLocker locker(&mMutex); QMutexLocker locker(&mMutex);
if ((Index1 < 0) || (Index1 >= mLines.count())) { if ((index1 < 0) || (index1 >= mLines.count())) {
ListIndexOutOfBounds(Index1); ListIndexOutOfBounds(index1);
} }
if ((Index2 < 0) || (Index2 >= mLines.count())) { if ((index2 < 0) || (index2 >= mLines.count())) {
ListIndexOutOfBounds(Index2); ListIndexOutOfBounds(index2);
} }
beginUpdate(); beginUpdate();
PDocumentLine temp = mLines[Index1]; PDocumentLine temp = mLines[index1];
mLines[Index1]=mLines[Index2]; mLines[index1]=mLines[index2];
mLines[Index2]=temp; mLines[index2]=temp;
//mList.swapItemsAt(Index1,Index2); //mList.swapItemsAt(Index1,Index2);
if (mIndexOfLongestLine == Index1) { if (mIndexOfLongestLine == index1) {
mIndexOfLongestLine = Index2; mIndexOfLongestLine = index2;
} else if (mIndexOfLongestLine == Index2) { } else if (mIndexOfLongestLine == index2) {
mIndexOfLongestLine = Index1; mIndexOfLongestLine = index1;
} }
endUpdate(); endUpdate();
} }
void Document::insert(int Index, const QString &s) void Document::insert(int index, const QString &s)
{ {
QMutexLocker locker(&mMutex); QMutexLocker locker(&mMutex);
if ((Index < 0) || (Index > mLines.count())) { if ((index < 0) || (index > mLines.count())) {
ListIndexOutOfBounds(Index); ListIndexOutOfBounds(index);
} }
beginUpdate(); beginUpdate();
insertItem(Index, s); insertItem(index, s);
emit inserted(Index,1); emit inserted(index,1);
endUpdate(); endUpdate();
} }
void Document::deleteAt(int Index) void Document::deleteAt(int index)
{ {
QMutexLocker locker(&mMutex); QMutexLocker locker(&mMutex);
if ((Index < 0) || (Index >= mLines.count())) { if ((index < 0) || (index >= mLines.count())) {
ListIndexOutOfBounds(Index); ListIndexOutOfBounds(index);
} }
beginUpdate(); beginUpdate();
if (mIndexOfLongestLine == Index) if (mIndexOfLongestLine == index)
mIndexOfLongestLine = -1; mIndexOfLongestLine = -1;
else if (mIndexOfLongestLine>Index) else if (mIndexOfLongestLine>index)
mIndexOfLongestLine -= 1; mIndexOfLongestLine -= 1;
mLines.removeAt(Index); mLines.removeAt(index);
emit deleted(Index,1); emit deleted(index,1);
endUpdate(); endUpdate();
} }
@ -415,26 +427,26 @@ QString Document::getTextStr() const
return result; return result;
} }
void Document::putString(int Index, const QString &s, bool notify) { void Document::putString(int index, const QString &s, bool notify) {
QMutexLocker locker(&mMutex); QMutexLocker locker(&mMutex);
if (Index == mLines.count()) { if (index == mLines.count()) {
add(s); add(s);
} else { } else {
if (Index<0 || Index>=mLines.count()) { if (index<0 || index>=mLines.count()) {
ListIndexOutOfBounds(Index); ListIndexOutOfBounds(index);
} }
beginUpdate(); beginUpdate();
int oldColumns = mLines[Index]->fColumns; int oldColumns = mLines[index]->fColumns;
mLines[Index]->fString = s; mLines[index]->fString = s;
calculateLineColumns(Index); calculateLineColumns(index);
if (mIndexOfLongestLine == Index && oldColumns>mLines[Index]->fColumns ) if (mIndexOfLongestLine == index && oldColumns>mLines[index]->fColumns )
mIndexOfLongestLine = -1; mIndexOfLongestLine = -1;
else if (mIndexOfLongestLine>=0 else if (mIndexOfLongestLine>=0
&& mIndexOfLongestLine<mLines.count() && mIndexOfLongestLine<mLines.count()
&& mLines[Index]->fColumns > mLines[mIndexOfLongestLine]->fColumns) && mLines[index]->fColumns > mLines[mIndexOfLongestLine]->fColumns)
mIndexOfLongestLine = Index; mIndexOfLongestLine = index;
if (notify) if (notify)
emit putted(Index,1); emit putted(index,1);
endUpdate(); endUpdate();
} }
} }
@ -455,13 +467,13 @@ int Document::calculateLineColumns(int Index)
return line->fColumns; return line->fColumns;
} }
void Document::insertLines(int Index, int NumLines) void Document::insertLines(int index, int numLines)
{ {
QMutexLocker locker(&mMutex); QMutexLocker locker(&mMutex);
if (Index<0 || Index>mLines.count()) { if (index<0 || index>mLines.count()) {
ListIndexOutOfBounds(Index); ListIndexOutOfBounds(index);
} }
if (NumLines<=0) if (numLines<=0)
return; return;
beginUpdate(); beginUpdate();
auto action = finally([this]{ auto action = finally([this]{
@ -469,12 +481,12 @@ void Document::insertLines(int Index, int NumLines)
}); });
mIndexOfLongestLine = -1; mIndexOfLongestLine = -1;
PDocumentLine line; PDocumentLine line;
mLines.insert(Index,NumLines,line); mLines.insert(index,numLines,line);
for (int i=Index;i<Index+NumLines;i++) { for (int i=index;i<index+numLines;i++) {
line = std::make_shared<DocumentLine>(); line = std::make_shared<DocumentLine>();
mLines[i]=line; mLines[i]=line;
} }
emit inserted(Index,NumLines); emit inserted(index,numLines);
} }

View File

@ -55,38 +55,39 @@ class Document : public QObject
public: public:
explicit Document(const QFont& font, const QFont& nonAsciiFont, QObject* parent=nullptr); explicit Document(const QFont& font, const QFont& nonAsciiFont, QObject* parent=nullptr);
int parenthesisLevels(int Index); int parenthesisLevels(int index);
int bracketLevels(int Index); int bracketLevels(int index);
int braceLevels(int Index); int braceLevels(int index);
int lineColumns(int Index); int lineColumns(int index);
int leftBraces(int Index); int blockLevel(int index);
int rightBraces(int Index); int blockStarted(int index);
int blockEnded(int index);
int lengthOfLongestLine(); int lengthOfLongestLine();
QString lineBreak() const; QString lineBreak() const;
HighlighterState ranges(int Index); HighlighterState ranges(int index);
void setRange(int Index, const HighlighterState& ARange); void setRange(int index, const HighlighterState& range);
QString getString(int Index); QString getString(int index);
int count(); int count();
QString text(); QString text();
void setText(const QString& text); void setText(const QString& text);
void setContents(const QStringList& text); void setContents(const QStringList& text);
QStringList contents(); QStringList contents();
void putString(int Index, const QString& s, bool notify=true); void putString(int index, const QString& s, bool notify=true);
void beginUpdate(); void beginUpdate();
void endUpdate(); void endUpdate();
int add(const QString& s); int add(const QString& s);
void addStrings(const QStringList& Strings); void addStrings(const QStringList& strings);
int getTextLength(); int getTextLength();
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 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, void saveToFile(QFile& file, const QByteArray& encoding,

View File

@ -265,11 +265,15 @@ int HighlighterState::getLastIndent()
HighlighterState::HighlighterState(): HighlighterState::HighlighterState():
state(0), state(0),
blockLevel(0),
blockStarted(0),
blockEnded(0),
blockEndedLastLine(0),
braceLevel(0), braceLevel(0),
bracketLevel(0), bracketLevel(0),
parenthesisLevel(0), parenthesisLevel(0),
leftBraces(0), // leftBraces(0),
rightBraces(0), // rightBraces(0),
firstIndentThisLine(0) firstIndentThisLine(0)
{ {
} }

View File

@ -36,11 +36,15 @@ enum SynIndentType {
struct HighlighterState { struct HighlighterState {
int state; // current syntax parsing state int state; // current syntax parsing state
int blockLevel; // needed by block folding
int blockStarted; // needed by block folding
int blockEnded; // needed by block folding;
int blockEndedLastLine; //needed by block folding;
int braceLevel; // current braces embedding level (needed by rainbow color) int braceLevel; // current braces embedding level (needed by rainbow color)
int bracketLevel; // current brackets embedding level (needed by rainbow color) int bracketLevel; // current brackets embedding level (needed by rainbow color)
int parenthesisLevel; // current parenthesis embedding level (needed by rainbow color) int parenthesisLevel; // current parenthesis embedding level (needed by rainbow color)
int leftBraces; // unpairing left braces in the current line ( needed by block folding) // int leftBraces; // unpairing left braces in the current line ( needed by block folding)
int rightBraces; // unparing right braces in the current line (needed by block folding) // int rightBraces; // unparing right braces in the current line (needed by block folding)
QVector<int> indents; // indents stack (needed by auto indent) QVector<int> indents; // indents stack (needed by auto indent)
int firstIndentThisLine; /* index of first indent that appended to the indents int firstIndentThisLine; /* index of first indent that appended to the indents
* stack at this line ( need by auto indent) */ * stack at this line ( need by auto indent) */

View File

@ -372,12 +372,15 @@ void CppHighlighter::braceCloseProc()
} }
mRange.braceLevel -= 1; mRange.braceLevel -= 1;
if (mRange.braceLevel<0) mRange.blockLevel -= 1;
if (mRange.braceLevel<0) {
mRange.braceLevel = 0; mRange.braceLevel = 0;
if (mRange.leftBraces>0) { mRange.blockLevel = 0;
mRange.leftBraces--; }
if (mRange.blockStarted>0) {
mRange.blockStarted--;
} else { } else {
mRange.rightBraces++ ; mRange.blockEnded++ ;
} }
popIndents(sitBrace); popIndents(sitBrace);
} }
@ -391,7 +394,8 @@ void CppHighlighter::braceOpenProc()
mAsmStart = true; mAsmStart = true;
} }
mRange.braceLevel += 1; mRange.braceLevel += 1;
mRange.leftBraces++; mRange.blockLevel += 1;
mRange.blockStarted++;
if (mRange.getLastIndent() == sitStatement) { if (mRange.getLastIndent() == sitStatement) {
// if last indent is started by 'if' 'for' etc // if last indent is started by 'if' 'for' etc
// just replace it // just replace it
@ -1576,8 +1580,9 @@ void CppHighlighter::setLine(const QString &newLine, int lineNumber)
mLineSize = mLine.size(); mLineSize = mLine.size();
mLineNumber = lineNumber; mLineNumber = lineNumber;
mRun = 0; mRun = 0;
mRange.leftBraces = 0; mRange.blockStarted = 0;
mRange.rightBraces = 0; mRange.blockEnded = 0;
mRange.blockEndedLastLine = 0;
mRange.firstIndentThisLine = mRange.indents.length(); mRange.firstIndentThisLine = mRange.indents.length();
mRange.matchingIndents.clear(); mRange.matchingIndents.clear();
next(); next();
@ -1592,8 +1597,9 @@ void CppHighlighter::setState(const HighlighterState& rangeState)
{ {
mRange = rangeState; mRange = rangeState;
// current line's left / right parenthesis count should be reset before parsing each line // current line's left / right parenthesis count should be reset before parsing each line
mRange.leftBraces = 0; mRange.blockStarted = 0;
mRange.rightBraces = 0; mRange.blockEnded = 0;
mRange.blockEndedLastLine = 0;
mRange.firstIndentThisLine = mRange.indents.length(); mRange.firstIndentThisLine = mRange.indents.length();
mRange.matchingIndents.clear(); mRange.matchingIndents.clear();
} }
@ -1604,8 +1610,10 @@ void CppHighlighter::resetState()
mRange.braceLevel = 0; mRange.braceLevel = 0;
mRange.bracketLevel = 0; mRange.bracketLevel = 0;
mRange.parenthesisLevel = 0; mRange.parenthesisLevel = 0;
mRange.leftBraces = 0; mRange.blockLevel = 0;
mRange.rightBraces = 0; mRange.blockStarted = 0;
mRange.blockEnded = 0;
mRange.blockEndedLastLine = 0;
mRange.indents.clear(); mRange.indents.clear();
mRange.firstIndentThisLine = 0; mRange.firstIndentThisLine = 0;
mRange.matchingIndents.clear(); mRange.matchingIndents.clear();

View File

@ -12,8 +12,6 @@ void CustomHighlighterV1::resetState()
mRange.braceLevel = 0; mRange.braceLevel = 0;
mRange.bracketLevel = 0; mRange.bracketLevel = 0;
mRange.parenthesisLevel = 0; mRange.parenthesisLevel = 0;
mRange.leftBraces = 0;
mRange.rightBraces = 0;
mRange.indents.clear(); mRange.indents.clear();
mRange.firstIndentThisLine = 0; mRange.firstIndentThisLine = 0;
mRange.matchingIndents.clear(); mRange.matchingIndents.clear();

View File

@ -312,12 +312,15 @@ void GLSLHighlighter::braceCloseProc()
} }
mRange.braceLevel -= 1; mRange.braceLevel -= 1;
if (mRange.braceLevel<0) mRange.blockLevel -= 1;
if (mRange.braceLevel<0) {
mRange.braceLevel = 0; mRange.braceLevel = 0;
if (mRange.leftBraces>0) { mRange.blockLevel = 0;
mRange.leftBraces--; }
if (mRange.blockStarted>0) {
mRange.blockStarted--;
} else { } else {
mRange.rightBraces++ ; mRange.blockEnded++ ;
} }
popIndents(sitBrace); popIndents(sitBrace);
} }
@ -331,7 +334,8 @@ void GLSLHighlighter::braceOpenProc()
mAsmStart = true; mAsmStart = true;
} }
mRange.braceLevel += 1; mRange.braceLevel += 1;
mRange.leftBraces++; mRange.blockLevel += 1;
mRange.blockStarted += 1;
if (mRange.getLastIndent() == sitStatement) { if (mRange.getLastIndent() == sitStatement) {
// if last indent is started by 'if' 'for' etc // if last indent is started by 'if' 'for' etc
// just replace it // just replace it
@ -1411,8 +1415,10 @@ void GLSLHighlighter::setLine(const QString &newLine, int lineNumber)
mLine = mLineString.data(); mLine = mLineString.data();
mLineNumber = lineNumber; mLineNumber = lineNumber;
mRun = 0; mRun = 0;
mRange.leftBraces = 0; mRange.blockLevel = 0;
mRange.rightBraces = 0; mRange.blockStarted = 0;
mRange.blockEnded = 0;
mRange.blockEndedLastLine = 0;
mRange.firstIndentThisLine = mRange.indents.length(); mRange.firstIndentThisLine = mRange.indents.length();
mRange.matchingIndents.clear(); mRange.matchingIndents.clear();
next(); next();
@ -1427,8 +1433,10 @@ void GLSLHighlighter::setState(const HighlighterState& rangeState)
{ {
mRange = rangeState; mRange = rangeState;
// current line's left / right parenthesis count should be reset before parsing each line // current line's left / right parenthesis count should be reset before parsing each line
mRange.leftBraces = 0; mRange.blockLevel = 0;
mRange.rightBraces = 0; mRange.blockStarted = 0;
mRange.blockEnded = 0;
mRange.blockEndedLastLine = 0;
mRange.firstIndentThisLine = mRange.indents.length(); mRange.firstIndentThisLine = mRange.indents.length();
mRange.matchingIndents.clear(); mRange.matchingIndents.clear();
} }
@ -1439,8 +1447,10 @@ void GLSLHighlighter::resetState()
mRange.braceLevel = 0; mRange.braceLevel = 0;
mRange.bracketLevel = 0; mRange.bracketLevel = 0;
mRange.parenthesisLevel = 0; mRange.parenthesisLevel = 0;
mRange.leftBraces = 0; mRange.blockLevel = 0;
mRange.rightBraces = 0; mRange.blockStarted = 0;
mRange.blockEnded = 0;
mRange.blockEndedLastLine = 0;
mRange.indents.clear(); mRange.indents.clear();
mRange.firstIndentThisLine = 0; mRange.firstIndentThisLine = 0;
mRange.matchingIndents.clear(); mRange.matchingIndents.clear();