From c8704fdfbb20e583afd769c3465a7a99599874d2 Mon Sep 17 00:00:00 2001 From: "royqh1979@gmail.com" Date: Thu, 28 Oct 2021 08:56:13 +0800 Subject: [PATCH] work save --- RedPandaIDE/qsynedit/SynEdit.cpp | 47 +++++++++++++++---- RedPandaIDE/qsynedit/SynEdit.h | 2 + RedPandaIDE/qsynedit/TextBuffer.cpp | 8 ++-- RedPandaIDE/qsynedit/TextBuffer.h | 4 +- RedPandaIDE/qsynedit/highlighter/base.cpp | 10 ----- RedPandaIDE/qsynedit/highlighter/base.h | 9 ++-- RedPandaIDE/qsynedit/highlighter/cpp.cpp | 55 +++++++++++++---------- RedPandaIDE/qsynedit/highlighter/cpp.h | 3 -- 8 files changed, 82 insertions(+), 56 deletions(-) diff --git a/RedPandaIDE/qsynedit/SynEdit.cpp b/RedPandaIDE/qsynedit/SynEdit.cpp index 8e74d48e..3623f4f9 100644 --- a/RedPandaIDE/qsynedit/SynEdit.cpp +++ b/RedPandaIDE/qsynedit/SynEdit.cpp @@ -1364,6 +1364,39 @@ void SynEdit::setWordBlock(BufferCoord Value) setCaretAndSelection(v_WordEnd, v_WordStart, v_WordEnd); } +int SynEdit::calcIndentSpaces(int line, const QString& lineText) +{ + if (!mHighlighter) + return 0; + line = std::min(line, line = mLines->count()+1); + if (line<=1) + return 0; + int startLine = line-1; + int indentSpaces = 0; + QString s; + while (startLine>=1) { + s = mLines->getString(startLine-1); + if (!s.trimmed().isEmpty()) { + break; + } + startLine -- ; + } + if (startLine>=1) { + SynRangeState range = mLines->ranges(startLine-1); + indentSpaces = leftSpaces(s); + int left = range.leftBraces+range.leftBrackets+range.leftParenthesis; + indentSpaces+=left*mTabWidth; + + mHighlighter->setLine(lineText,line); + mHighlighter->setState(range); + mHighlighter->nextToEol(); + range = mHighlighter->getRangeState(); + int right = range.rightBraces+range.rightBrackets+range.leftParenthesis; + indentSpaces-=right*mTabWidth; + } + return std::max(0,indentSpaces); +} + void SynEdit::doSelectAll() { BufferCoord LastPt; @@ -1894,7 +1927,8 @@ void SynEdit::insertLine(bool moveCaret) if (Len > 0) { if (Len >= mCaretX) { if (mCaretX <= 1) { - mLines->insert(mCaretY - 1, ""); + mLines->insert(mCaretY - 1, + GetLeftSpacing(calcIndentSpaces(mCaretY,""),true)); nLinesInserted++; mUndoList->AddChange(SynChangeReason::crLineBreak, caretXY(), caretXY(), Temp2, SynSelectionMode::smNormal); @@ -1944,7 +1978,7 @@ void SynEdit::insertLine(bool moveCaret) internalSetCaretXY(BufferCoord{indentSpacesForRightLineText.length()+1,mCaretY + 1}); } } else { - SpaceCount2 = 0; + SpaceCount2 = calcIndentSpaces(mCaretY,""); int BackCounter = mCaretY; if (mOptions.testFlag(eoAutoIndent)) { do { @@ -1953,7 +1987,7 @@ void SynEdit::insertLine(bool moveCaret) SpaceCount2 = leftSpaces(Temp); } while ((BackCounter != 0) && (Temp == "")); } - mLines->insert(mCaretY, ""); + mLines->insert(mCaretY, GetLeftSpacing(,true))); nLinesInserted++; BufferCoord Caret = caretXY(); if (moveCaret) { @@ -2839,8 +2873,7 @@ int SynEdit::scanFrom(int Index, int canStopIndex) return Result;// avoid the final Decrement } } - mLines->setRange(Result,iRange, mHighlighter->getLeftBraces(), - mHighlighter->getRightBraces()); + mLines->setRange(Result,iRange); Result ++ ; } while (Result < mLines->count()); Result--; @@ -2856,9 +2889,7 @@ void SynEdit::scanRanges() for (int i =0;icount();i++) { mHighlighter->setLine(mLines->getString(i), i); mHighlighter->nextToEol(); - mLines->setRange(i, mHighlighter->getRangeState(), - mHighlighter->getLeftBraces(), - mHighlighter->getRightBraces()); + mLines->setRange(i, mHighlighter->getRangeState()); } } if (mUseCodeFolding) diff --git a/RedPandaIDE/qsynedit/SynEdit.h b/RedPandaIDE/qsynedit/SynEdit.h index c8141dce..863cacde 100644 --- a/RedPandaIDE/qsynedit/SynEdit.h +++ b/RedPandaIDE/qsynedit/SynEdit.h @@ -506,6 +506,8 @@ private: void setSelWord(); void setWordBlock(BufferCoord Value); + int calcIndentSpaces(int line, const QString& lineText = ""); + void processGutterClick(QMouseEvent* event); diff --git a/RedPandaIDE/qsynedit/TextBuffer.cpp b/RedPandaIDE/qsynedit/TextBuffer.cpp index 863e6bfe..0131f26b 100644 --- a/RedPandaIDE/qsynedit/TextBuffer.cpp +++ b/RedPandaIDE/qsynedit/TextBuffer.cpp @@ -80,7 +80,7 @@ int SynEditStringList::leftBraces(int Index) { QMutexLocker locker(&mMutex); if (Index>=0 && Index < mList.size()) { - return mList[Index]->fLeftBraces; + return mList[Index]->fRange.leftBraces; } else return 0; } @@ -89,7 +89,7 @@ int SynEditStringList::rightBraces(int Index) { QMutexLocker locker(&mMutex); if (Index>=0 && Index < mList.size()) { - return mList[Index]->fRightBraces; + return mList[Index]->fRange.rightBraces; } else return 0; } @@ -171,7 +171,7 @@ void SynEditStringList::setAppendNewLineAtEOF(bool appendNewLineAtEOF) mAppendNewLineAtEOF = appendNewLineAtEOF; } -void SynEditStringList::setRange(int Index, const SynRangeState& ARange, int ALeftBraces, int ARightBraces) +void SynEditStringList::setRange(int Index, const SynRangeState& ARange) { QMutexLocker locker(&mMutex); if (Index<0 || Index>=mList.count()) { @@ -179,8 +179,6 @@ void SynEditStringList::setRange(int Index, const SynRangeState& ARange, int ALe } beginUpdate(); mList[Index]->fRange = ARange; - mList[Index]->fLeftBraces = ALeftBraces; - mList[Index]->fRightBraces = ARightBraces; endUpdate(); } diff --git a/RedPandaIDE/qsynedit/TextBuffer.h b/RedPandaIDE/qsynedit/TextBuffer.h index 626fa3c9..50b387dd 100644 --- a/RedPandaIDE/qsynedit/TextBuffer.h +++ b/RedPandaIDE/qsynedit/TextBuffer.h @@ -22,8 +22,6 @@ struct SynEditStringRec { QString fString; void * fObject; SynRangeState fRange; - int fLeftBraces; - int fRightBraces; int fColumns; // public: @@ -60,7 +58,7 @@ public: int lengthOfLongestLine(); QString lineBreak() const; SynRangeState ranges(int Index); - void setRange(int Index, const SynRangeState& ARange, int leftBraces, int rightBraces); + void setRange(int Index, const SynRangeState& ARange); QString getString(int Index); int count(); void* getObject(int Index); diff --git a/RedPandaIDE/qsynedit/highlighter/base.cpp b/RedPandaIDE/qsynedit/highlighter/base.cpp index ebe82037..f3c0c957 100644 --- a/RedPandaIDE/qsynedit/highlighter/base.cpp +++ b/RedPandaIDE/qsynedit/highlighter/base.cpp @@ -64,16 +64,6 @@ void SynHighlighter::nextToEol() next(); } -int SynHighlighter::getLeftBraces() -{ - return 0; -} - -int SynHighlighter::getRightBraces() -{ - return 0; -} - bool SynHighlighter::isSpaceChar(const QChar &ch) { return ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n'; diff --git a/RedPandaIDE/qsynedit/highlighter/base.h b/RedPandaIDE/qsynedit/highlighter/base.h index a5b69601..4bff7b11 100644 --- a/RedPandaIDE/qsynedit/highlighter/base.h +++ b/RedPandaIDE/qsynedit/highlighter/base.h @@ -16,6 +16,12 @@ struct SynRangeState { int braceLevel; int bracketLevel; int parenthesisLevel; + int leftBraces; + int rightBraces; + int leftBrackets; + int rightBrackets; + int leftParenthesis; + int rightParenthesis; bool operator==(const SynRangeState& s2); }; @@ -108,9 +114,6 @@ public: virtual void setLine(const QString& newLine, int lineNumber) = 0; virtual void resetState() = 0; - virtual int getLeftBraces(); - virtual int getRightBraces(); - virtual QString languageName() = 0; virtual SynHighlighterLanguage language() = 0; diff --git a/RedPandaIDE/qsynedit/highlighter/cpp.cpp b/RedPandaIDE/qsynedit/highlighter/cpp.cpp index 56c688fa..d3148ba4 100644 --- a/RedPandaIDE/qsynedit/highlighter/cpp.cpp +++ b/RedPandaIDE/qsynedit/highlighter/cpp.cpp @@ -153,14 +153,7 @@ SynEditCppHighlighter::SynEditCppHighlighter(): SynHighlighter() mVariableAttribute = std::make_shared(SYNS_AttrVariable); addAttribute(mVariableAttribute); - mRange.state = RangeState::rsUnknown; - mRange.spaceState = RangeState::rsUnknown; - mRange.braceLevel = 0; - mRange.bracketLevel = 0; - mRange.parenthesisLevel = 0; - mLeftBraces = 0; - mRightBraces = 0; - mAsmStart = false; + resetState(); } PSynHighlighterAttribute SynEditCppHighlighter::asmAttribute() const @@ -368,10 +361,10 @@ void SynEditCppHighlighter::braceCloseProc() } mRange.braceLevel -= 1; - if (mLeftBraces>0) { - mLeftBraces--; + if (mRange.leftBraces>0) { + mRange.leftBraces--; } else { - mRightBraces++ ; + mRange.rightBraces++ ; } } @@ -385,7 +378,7 @@ void SynEditCppHighlighter::braceOpenProc() mAsmStart = true; } mRange.braceLevel += 1; - mLeftBraces++; + mRange.leftBraces++; } void SynEditCppHighlighter::colonProc() @@ -887,6 +880,11 @@ void SynEditCppHighlighter::roundCloseProc() mTokenId = TokenKind::Symbol; mExtTokenId = ExtTokenKind::RoundClose; mRange.parenthesisLevel--; + if (mRange.leftParenthesis>0) { + mRange.leftParenthesis--; + } else { + mRange.rightParenthesis++ ; + } } void SynEditCppHighlighter::roundOpenProc() @@ -895,6 +893,7 @@ void SynEditCppHighlighter::roundOpenProc() mTokenId = TokenKind::Symbol; mExtTokenId = ExtTokenKind::RoundOpen; mRange.parenthesisLevel++; + mRange.leftParenthesis++; } void SynEditCppHighlighter::semiColonProc() @@ -957,6 +956,11 @@ void SynEditCppHighlighter::squareCloseProc() mTokenId = TokenKind::Symbol; mExtTokenId = ExtTokenKind::SquareClose; mRange.bracketLevel--; + if (mRange.leftBrackets>0) { + mRange.leftBrackets--; + } else { + mRange.rightBrackets++ ; + } } void SynEditCppHighlighter::squareOpenProc() @@ -965,6 +969,7 @@ void SynEditCppHighlighter::squareOpenProc() mTokenId = TokenKind::Symbol; mExtTokenId = ExtTokenKind::SquareOpen; mRange.bracketLevel++; + mRange.leftBrackets++; } void SynEditCppHighlighter::starProc() @@ -1480,8 +1485,6 @@ void SynEditCppHighlighter::setLine(const QString &newLine, int lineNumber) mLine = mLineString.data(); mLineNumber = lineNumber; mRun = 0; - mLeftBraces = 0; - mRightBraces = 0; next(); } @@ -1545,6 +1548,13 @@ SynHighlighterTokenType SynEditCppHighlighter::getTokenType() void SynEditCppHighlighter::setState(const SynRangeState& rangeState) { mRange = rangeState; + // current line's left / right parenthesis count should be reset before parsing each line + mRange.leftBraces = 0; + mRange.leftBrackets = 0; + mRange.leftParenthesis = 0; + mRange.rightBraces = 0; + mRange.rightBrackets = 0; + mRange.rightParenthesis = 0; } void SynEditCppHighlighter::resetState() @@ -1554,6 +1564,13 @@ void SynEditCppHighlighter::resetState() mRange.braceLevel = 0; mRange.bracketLevel = 0; mRange.parenthesisLevel = 0; + mRange.leftBraces = 0; + mRange.leftBrackets = 0; + mRange.leftParenthesis = 0; + mRange.rightBraces = 0; + mRange.rightBrackets = 0; + mRange.rightParenthesis = 0; + mAsmStart = false; } SynHighlighterClass SynEditCppHighlighter::getClass() const @@ -1576,16 +1593,6 @@ SynHighlighterLanguage SynEditCppHighlighter::language() return SynHighlighterLanguage::Cpp; } -int SynEditCppHighlighter::getLeftBraces() -{ - return mLeftBraces; -} - -int SynEditCppHighlighter::getRightBraces() -{ - return mRightBraces; -} - SynRangeState SynEditCppHighlighter::getRangeState() const { return mRange; diff --git a/RedPandaIDE/qsynedit/highlighter/cpp.h b/RedPandaIDE/qsynedit/highlighter/cpp.h index bb90e8b3..88ef6066 100644 --- a/RedPandaIDE/qsynedit/highlighter/cpp.h +++ b/RedPandaIDE/qsynedit/highlighter/cpp.h @@ -180,9 +180,6 @@ public: QString languageName() override; SynHighlighterLanguage language() override; - int getLeftBraces() override; - int getRightBraces() override; - // SynHighlighter interface public: SynRangeState getRangeState() const override;