- refactor: brace states in parser

- changer: colors for braces in VS Code color scheme
This commit is contained in:
royqh1979@gmail.com 2021-09-23 14:46:42 +08:00
parent bc1c92e7b5
commit bfa5835cfa
9 changed files with 55 additions and 30 deletions

View File

@ -171,15 +171,15 @@
"foreground" : "#C0C0C0" "foreground" : "#C0C0C0"
}, },
"brace/parenthesis/bracket level 1" : { "brace/parenthesis/bracket level 1" : {
"foreground" : "#569CD6" "foreground" : "#569CD6"
}, },
"brace/parenthesis/bracket level 2" : { "brace/parenthesis/bracket level 2" : {
"foreground" : "#D4D4D4" "foreground" : "#D4D4D4"
}, },
"brace/parenthesis/bracket level 3" : { "brace/parenthesis/bracket level 3" : {
"foreground" : "#D69D85" "foreground" : "#D69D85"
}, },
"brace/parenthesis/bracket level 4" : { "brace/parenthesis/bracket level 4" : {
"foreground" : "#DCDCAA" "foreground" : "#4EC9B0"
} }
} }

View File

@ -2873,7 +2873,8 @@ int SynEdit::scanFrom(int Index, int canStopIndex)
) )
return Result;// avoid the final Decrement return Result;// avoid the final Decrement
} }
mLines->setRange(Result,iRange); mLines->setRange(Result,iRange, mHighlighter->getLeftBraces(),
mHighlighter->getRightBraces());
Result ++ ; Result ++ ;
} while (Result < mLines->count()); } while (Result < mLines->count());
Result--; Result--;
@ -2887,7 +2888,9 @@ void SynEdit::scanRanges()
for (int i =0;i<mLines->count();i++) { for (int i =0;i<mLines->count();i++) {
mHighlighter->setLine(mLines->getString(i), i); mHighlighter->setLine(mLines->getString(i), i);
mHighlighter->nextToEol(); mHighlighter->nextToEol();
mLines->setRange(i, mHighlighter->getRangeState()); mLines->setRange(i, mHighlighter->getRangeState(),
mHighlighter->getLeftBraces(),
mHighlighter->getRightBraces());
} }
} }
} }

View File

@ -73,7 +73,7 @@ int SynEditStringList::lineColumns(int Index)
int SynEditStringList::leftBraces(int Index) int SynEditStringList::leftBraces(int Index)
{ {
if (Index>=0 && Index < mList.size()) { if (Index>=0 && Index < mList.size()) {
return mList[Index]->fRange.leftBraces; return mList[Index]->fLeftBraces;
} else } else
return 0; return 0;
} }
@ -81,7 +81,7 @@ int SynEditStringList::leftBraces(int Index)
int SynEditStringList::rightBraces(int Index) int SynEditStringList::rightBraces(int Index)
{ {
if (Index>=0 && Index < mList.size()) { if (Index>=0 && Index < mList.size()) {
return mList[Index]->fRange.rightBraces; return mList[Index]->fRightBraces;
} else } else
return 0; return 0;
} }
@ -165,13 +165,15 @@ void SynEditStringList::setAppendNewLineAtEOF(bool appendNewLineAtEOF)
mAppendNewLineAtEOF = appendNewLineAtEOF; mAppendNewLineAtEOF = appendNewLineAtEOF;
} }
void SynEditStringList::setRange(int Index, const SynRangeState& ARange) void SynEditStringList::setRange(int Index, const SynRangeState& ARange, int ALeftBraces, int ARightBraces)
{ {
if (Index<0 || Index>=mList.count()) { if (Index<0 || Index>=mList.count()) {
ListIndexOutOfBounds(Index); ListIndexOutOfBounds(Index);
} }
beginUpdate(); beginUpdate();
mList[Index]->fRange = ARange; mList[Index]->fRange = ARange;
mList[Index]->fLeftBraces = ALeftBraces;
mList[Index]->fRightBraces = ARightBraces;
endUpdate(); endUpdate();
} }
@ -675,7 +677,7 @@ void SynEditStringList::invalidAllLineColumns()
SynEditStringRec::SynEditStringRec(): SynEditStringRec::SynEditStringRec():
fString(), fString(),
fObject(nullptr), fObject(nullptr),
fRange{0,0,0,0,0,0,0}, fRange{0,0,0,0,0},
fColumns(-1) fColumns(-1)
{ {
} }

View File

@ -21,6 +21,8 @@ struct SynEditStringRec {
QString fString; QString fString;
void * fObject; void * fObject;
SynRangeState fRange; SynRangeState fRange;
int fLeftBraces;
int fRightBraces;
int fColumns; // int fColumns; //
public: public:
@ -57,7 +59,7 @@ public:
int lengthOfLongestLine(); int lengthOfLongestLine();
QString lineBreak(); QString lineBreak();
const SynRangeState& ranges(int Index); const SynRangeState& ranges(int Index);
void setRange(int Index, const SynRangeState& ARange); void setRange(int Index, const SynRangeState& ARange, int leftBraces, int rightBraces);
QString getString(int Index); QString getString(int Index);
int count(); int count();
void* getObject(int Index); void* getObject(int Index);

View File

@ -373,7 +373,7 @@ bool SynEditASMHighlighter::isLastLineStringNotFinished(int state) const
SynRangeState SynEditASMHighlighter::getRangeState() const SynRangeState SynEditASMHighlighter::getRangeState() const
{ {
return {0,0,0,0,0,0,0}; return {0,0,0,0,0};
} }
void SynEditASMHighlighter::setState(const SynRangeState&) void SynEditASMHighlighter::setState(const SynRangeState&)

View File

@ -64,6 +64,16 @@ void SynHighlighter::nextToEol()
next(); next();
} }
int SynHighlighter::getLeftBraces()
{
return 0;
}
int SynHighlighter::getRightBraces()
{
return 0;
}
bool SynHighlighter::isSpaceChar(const QChar &ch) bool SynHighlighter::isSpaceChar(const QChar &ch)
{ {
return ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n'; return ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n';
@ -215,7 +225,5 @@ bool SynRangeState::operator==(const SynRangeState &s2)
&& (spaceState == s2.spaceState) && (spaceState == s2.spaceState)
&& (braceLevel == s2.braceLevel) && (braceLevel == s2.braceLevel)
&& (bracketLevel == s2.bracketLevel) && (bracketLevel == s2.bracketLevel)
&& (parenthesisLevel == s2.parenthesisLevel) && (parenthesisLevel == s2.parenthesisLevel);
&& (leftBraces == s2.leftBraces)
&& (rightBraces == s2.rightBraces);
} }

View File

@ -16,8 +16,6 @@ struct SynRangeState {
int braceLevel; int braceLevel;
int bracketLevel; int bracketLevel;
int parenthesisLevel; int parenthesisLevel;
int leftBraces;
int rightBraces;
bool operator==(const SynRangeState& s2); bool operator==(const SynRangeState& s2);
}; };
@ -110,6 +108,9 @@ public:
virtual void setLine(const QString& newLine, int lineNumber) = 0; virtual void setLine(const QString& newLine, int lineNumber) = 0;
virtual void resetState() = 0; virtual void resetState() = 0;
virtual int getLeftBraces();
virtual int getRightBraces();
virtual QString languageName() = 0; virtual QString languageName() = 0;
virtual SynHighlighterLanguage language() = 0; virtual SynHighlighterLanguage language() = 0;

View File

@ -158,8 +158,8 @@ SynEditCppHighlighter::SynEditCppHighlighter(): SynHighlighter()
mRange.braceLevel = 0; mRange.braceLevel = 0;
mRange.bracketLevel = 0; mRange.bracketLevel = 0;
mRange.parenthesisLevel = 0; mRange.parenthesisLevel = 0;
mRange.leftBraces = 0; mLeftBraces = 0;
mRange.rightBraces = 0; mRightBraces = 0;
mAsmStart = false; mAsmStart = false;
} }
@ -368,10 +368,10 @@ void SynEditCppHighlighter::braceCloseProc()
} }
mRange.braceLevel -= 1; mRange.braceLevel -= 1;
if (mRange.leftBraces>0) { if (mLeftBraces>0) {
mRange.leftBraces--; mLeftBraces--;
} else { } else {
mRange.rightBraces++ ; mRightBraces++ ;
} }
} }
@ -385,7 +385,7 @@ void SynEditCppHighlighter::braceOpenProc()
mAsmStart = true; mAsmStart = true;
} }
mRange.braceLevel += 1; mRange.braceLevel += 1;
mRange.leftBraces++; mLeftBraces++;
} }
void SynEditCppHighlighter::colonProc() void SynEditCppHighlighter::colonProc()
@ -1479,8 +1479,8 @@ void SynEditCppHighlighter::setLine(const QString &newLine, int lineNumber)
mLine = mLineString.data(); mLine = mLineString.data();
mLineNumber = lineNumber; mLineNumber = lineNumber;
mRun = 0; mRun = 0;
mRange.leftBraces = 0; mLeftBraces = 0;
mRange.rightBraces = 0; mRightBraces = 0;
next(); next();
} }
@ -1544,8 +1544,6 @@ SynHighlighterTokenType SynEditCppHighlighter::getTokenType()
void SynEditCppHighlighter::setState(const SynRangeState& rangeState) void SynEditCppHighlighter::setState(const SynRangeState& rangeState)
{ {
mRange = rangeState; mRange = rangeState;
mRange.leftBraces = 0;
mRange.rightBraces = 0;
} }
void SynEditCppHighlighter::resetState() void SynEditCppHighlighter::resetState()
@ -1555,8 +1553,6 @@ void SynEditCppHighlighter::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;
} }
SynHighlighterClass SynEditCppHighlighter::getClass() const SynHighlighterClass SynEditCppHighlighter::getClass() const
@ -1579,6 +1575,16 @@ SynHighlighterLanguage SynEditCppHighlighter::language()
return SynHighlighterLanguage::Cpp; return SynHighlighterLanguage::Cpp;
} }
int SynEditCppHighlighter::getLeftBraces()
{
return mLeftBraces;
}
int SynEditCppHighlighter::getRightBraces()
{
return mRightBraces;
}
SynRangeState SynEditCppHighlighter::getRangeState() const SynRangeState SynEditCppHighlighter::getRangeState() const
{ {
return mRange; return mRange;

View File

@ -140,6 +140,8 @@ private:
int mTokenId; int mTokenId;
ExtTokenKind mExtTokenId; ExtTokenKind mExtTokenId;
int mLineNumber; int mLineNumber;
int mLeftBraces;
int mRightBraces;
PSynHighlighterAttribute mAsmAttribute; PSynHighlighterAttribute mAsmAttribute;
PSynHighlighterAttribute mDirecAttribute; PSynHighlighterAttribute mDirecAttribute;
@ -156,8 +158,6 @@ private:
PSynHighlighterAttribute mGlobalVarAttribute; PSynHighlighterAttribute mGlobalVarAttribute;
PSynHighlighterAttribute mLocalVarAttribute; PSynHighlighterAttribute mLocalVarAttribute;
// SynHighligterBase interface // SynHighligterBase interface
public: public:
bool getTokenFinished() const override; bool getTokenFinished() const override;
@ -180,6 +180,9 @@ public:
QString languageName() override; QString languageName() override;
SynHighlighterLanguage language() override; SynHighlighterLanguage language() override;
int getLeftBraces() override;
int getRightBraces() override;
// SynHighlighter interface // SynHighlighter interface
public: public:
SynRangeState getRangeState() const override; SynRangeState getRangeState() const override;