work save

This commit is contained in:
royqh1979@gmail.com 2021-10-28 08:56:13 +08:00
parent 97b8fb1971
commit c8704fdfbb
8 changed files with 82 additions and 56 deletions

View File

@ -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;i<mLines->count();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)

View File

@ -506,6 +506,8 @@ private:
void setSelWord();
void setWordBlock(BufferCoord Value);
int calcIndentSpaces(int line, const QString& lineText = "");
void processGutterClick(QMouseEvent* event);

View File

@ -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();
}

View File

@ -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);

View File

@ -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';

View File

@ -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;

View File

@ -153,14 +153,7 @@ SynEditCppHighlighter::SynEditCppHighlighter(): SynHighlighter()
mVariableAttribute = std::make_shared<SynHighlighterAttribute>(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;

View File

@ -180,9 +180,6 @@ public:
QString languageName() override;
SynHighlighterLanguage language() override;
int getLeftBraces() override;
int getRightBraces() override;
// SynHighlighter interface
public:
SynRangeState getRangeState() const override;