work save

This commit is contained in:
royqh1979@gmail.com 2021-10-28 09:20:58 +08:00
parent c8704fdfbb
commit 317a798cce
3 changed files with 166 additions and 111 deletions

View File

@ -1364,7 +1364,7 @@ void SynEdit::setWordBlock(BufferCoord Value)
setCaretAndSelection(v_WordEnd, v_WordStart, v_WordEnd); setCaretAndSelection(v_WordEnd, v_WordStart, v_WordEnd);
} }
int SynEdit::calcIndentSpaces(int line, const QString& lineText) int SynEdit::calcIndentSpaces(int line, const QString& lineText, bool addIndent)
{ {
if (!mHighlighter) if (!mHighlighter)
return 0; return 0;
@ -1384,15 +1384,19 @@ int SynEdit::calcIndentSpaces(int line, const QString& lineText)
if (startLine>=1) { if (startLine>=1) {
SynRangeState range = mLines->ranges(startLine-1); SynRangeState range = mLines->ranges(startLine-1);
indentSpaces = leftSpaces(s); indentSpaces = leftSpaces(s);
int left = range.leftBraces+range.leftBrackets+range.leftParenthesis; if (addIndent) {
indentSpaces+=left*mTabWidth; int left = range.leftBraces+range.leftBrackets+range.leftParenthesis;
indentSpaces+=left*mTabWidth;
mHighlighter->setLine(lineText,line); mHighlighter->setState(range);
mHighlighter->setState(range); mHighlighter->setLine(lineText,line);
mHighlighter->nextToEol(); mHighlighter->nextToEol();
range = mHighlighter->getRangeState(); range = mHighlighter->getRangeState();
int right = range.rightBraces+range.rightBrackets+range.leftParenthesis; //todo: if line ends with unclosed comment or string
indentSpaces-=right*mTabWidth; if (s.trimmed().endsWith(':'))
indentSpaces += mTabWidth;
int right = range.rightBraces+range.rightBrackets+range.leftParenthesis;
indentSpaces-=right*mTabWidth;
}
} }
return std::max(0,indentSpaces); return std::max(0,indentSpaces);
} }
@ -1924,111 +1928,156 @@ void SynEdit::insertLine(bool moveCaret)
// too, so they could be moved depending on whether they are after the caret... // too, so they could be moved depending on whether they are after the caret...
int InsDelta = (mCaretX == 1)?1:0; int InsDelta = (mCaretX == 1)?1:0;
int Len = Temp.length(); int Len = Temp.length();
if (Len > 0) { QString leftLineText = lineText().mid(0, mCaretX - 1);
if (Len >= mCaretX) { QString rightLineText = lineText().mid(mCaretX-1);
if (mCaretX <= 1) { bool notInComment=true;
mLines->insert(mCaretY - 1, if (getHighlighterAttriAtRowCol(BufferCoord{leftLineText.length(), mCaretY},
GetLeftSpacing(calcIndentSpaces(mCaretY,""),true)); leftLineText, Attr)) {
nLinesInserted++; notInComment = (Attr != mHighlighter->commentAttribute());
mUndoList->AddChange(SynChangeReason::crLineBreak, caretXY(), caretXY(), Temp2, }
SynSelectionMode::smNormal); properSetLine(mCaretY-1,leftLineText);
if (moveCaret) //update range stated for line mCaretY
internalSetCaretY(mCaretY + 1); if (mHighlighter) {
} else { if (mCaretY==1) {
QString leftLineText = lineText().mid(0, mCaretX - 1); mHighlighter->resetState();
QString rightLineText = lineText().mid(mCaretX-1);
int indentSpacesOfLeftLineText = leftSpaces(leftLineText);
int indentSpaces = indentSpacesOfLeftLineText;
bool notInComment=true;
properSetLine(mCaretY-1,leftLineText);
if (mOptions.testFlag(eoAutoIndent)) {
rightLineText=TrimLeft(rightLineText);
}
if (getHighlighterAttriAtRowCol(BufferCoord{leftLineText.length(), mCaretY},
leftLineText, Attr)) {
notInComment = (Attr != mHighlighter->commentAttribute());
}
leftLineText = leftLineText.trimmed();
if (mOptions.testFlag(eoAddIndent)) { // only add indent to source files
if (notInComment) { // and outside of comments
if (leftLineText.endsWith(':') || leftLineText.endsWith('{'))
indentSpaces+=mTabWidth;
if (rightLineText.startsWith('}'))
indentSpaces-=mTabWidth;
}
}
QString indentSpacesForRightLineText = GetLeftSpacing(indentSpaces,true);
mLines->insert(mCaretY, indentSpacesForRightLineText+rightLineText);
nLinesInserted++;
//SpaceCount1 = mLines->getString(mCaretY).length(); //???
mUndoList->AddChange(SynChangeReason::crLineBreak, caretXY(), caretXY(), rightLineText,
SynSelectionMode::smNormal);
//insert new line in middle of "{" and "}"
if (notInComment && leftLineText.endsWith('{') && rightLineText.startsWith('}')) {
indentSpaces = indentSpacesOfLeftLineText;
indentSpaces += mTabWidth;
indentSpacesForRightLineText = GetLeftSpacing(indentSpaces,true);
mLines->insert(mCaretY, indentSpacesForRightLineText);
nLinesInserted++;
mUndoList->AddChange(SynChangeReason::crLineBreak, caretXY(), caretXY(), "",
SynSelectionMode::smNormal);
}
if (moveCaret)
internalSetCaretXY(BufferCoord{indentSpacesForRightLineText.length()+1,mCaretY + 1});
}
} else { } else {
SpaceCount2 = calcIndentSpaces(mCaretY,""); mHighlighter->setState(mLines->ranges(mCaretY-2));
int BackCounter = mCaretY;
if (mOptions.testFlag(eoAutoIndent)) {
do {
BackCounter--;
Temp = mLines->getString(BackCounter);
SpaceCount2 = leftSpaces(Temp);
} while ((BackCounter != 0) && (Temp == ""));
}
mLines->insert(mCaretY, GetLeftSpacing(,true)));
nLinesInserted++;
BufferCoord Caret = caretXY();
if (moveCaret) {
QString Temp4=GetLeftSpacing(SpaceCount2,true);
if (SpaceCount2 > 0) {
}
if (mOptions.testFlag(eoAddIndent) && getHighlighterAttriAtRowCol(BufferCoord{Temp.length(), mCaretY},
Temp, Attr)) { // only add indent to source files
if (Attr != mHighlighter->commentAttribute()) { // and outside of comments
Temp = Temp.trimmed();
if (Temp.endsWith('{') || Temp.endsWith(':')) { // add more indent for these too
Temp4=GetLeftSpacing(mTabWidth,true)+Temp4;
}
}
}
mLines->putString(mCaretY,Temp4); // copy previous indent
internalSetCaretXY(BufferCoord{Temp4.length()+1, mCaretY + 1});
}
mUndoList->AddChange(SynChangeReason::crLineBreak, Caret, Caret, "", SynSelectionMode::smNormal);
} }
} else { mHighlighter->setLine(leftLineText, mCaretY-1);
if (mLines->count() == 0) mHighlighter->nextToEol();
mLines->add(""); mLines->setRange(mCaretY-1,mHighlighter->getRangeState());
SpaceCount2 = 0; }
if (mOptions.testFlag(eoAutoIndent)) { int indentSpaces = calcIndentSpaces(mCaretY,
int BackCounter = mCaretY - 1; rightLineText,mOptions.testFlag(eoAddIndent)
while (BackCounter >= 0) { && notInComment);
SpaceCount2 = leftSpaces(mLines->getString(BackCounter)); if (mOptions.testFlag(eoAutoIndent)) {
if (mLines->getString(BackCounter).length() > 0) rightLineText=TrimLeft(rightLineText);
break; }
BackCounter--; QString indentSpacesForRightLineText = GetLeftSpacing(indentSpaces,true);
} mLines->insert(mCaretY, indentSpacesForRightLineText+rightLineText);
} nLinesInserted++;
mLines->insert(mCaretY - 1, GetLeftSpacing(SpaceCount2,true));
//SpaceCount1 = mLines->getString(mCaretY).length(); //???
mUndoList->AddChange(SynChangeReason::crLineBreak, caretXY(), caretXY(), rightLineText,
SynSelectionMode::smNormal);
//insert new line in middle of "{" and "}"
if (notInComment && leftLineText.endsWith('{') && rightLineText.startsWith('}')) {
indentSpaces = calcIndentSpaces(mCaretY, "" , mOptions.testFlag(eoAddIndent)
&& notInComment);
indentSpacesForRightLineText = GetLeftSpacing(indentSpaces,true);
mLines->insert(mCaretY, indentSpacesForRightLineText);
nLinesInserted++; nLinesInserted++;
mUndoList->AddChange(SynChangeReason::crLineBreak, caretXY(), caretXY(), "", mUndoList->AddChange(SynChangeReason::crLineBreak, caretXY(), caretXY(), "",
SynSelectionMode::smNormal); SynSelectionMode::smNormal);
if (moveCaret) {
internalSetCaretXY(BufferCoord{1, mCaretY + 1});
}
} }
if (moveCaret)
internalSetCaretXY(BufferCoord{indentSpacesForRightLineText.length()+1,mCaretY + 1});
// if (Len > 0) {
// if (Len >= mCaretX) {
// if (mCaretX <= 1) {
// mLines->insert(mCaretY - 1, "");
// nLinesInserted++;
// mUndoList->AddChange(SynChangeReason::crLineBreak, caretXY(), caretXY(), Temp2,
// SynSelectionMode::smNormal);
// if (moveCaret)
// internalSetCaretY(mCaretY + 1);
// } else {
// QString leftLineText = lineText().mid(0, mCaretX - 1);
// QString rightLineText = lineText().mid(mCaretX-1);
// int indentSpacesOfLeftLineText = leftSpaces(leftLineText);
// int indentSpaces = indentSpacesOfLeftLineText;
// bool notInComment=true;
// properSetLine(mCaretY-1,leftLineText);
// if (mOptions.testFlag(eoAutoIndent)) {
// rightLineText=TrimLeft(rightLineText);
// }
// if (getHighlighterAttriAtRowCol(BufferCoord{leftLineText.length(), mCaretY},
// leftLineText, Attr)) {
// notInComment = (Attr != mHighlighter->commentAttribute());
// }
// leftLineText = leftLineText.trimmed();
// if (mOptions.testFlag(eoAddIndent)) { // only add indent to source files
// if (notInComment) { // and outside of comments
// if (leftLineText.endsWith(':') || leftLineText.endsWith('{'))
// indentSpaces+=mTabWidth;
// if (rightLineText.startsWith('}'))
// indentSpaces-=mTabWidth;
// }
// }
// QString indentSpacesForRightLineText = GetLeftSpacing(indentSpaces,true);
// mLines->insert(mCaretY, indentSpacesForRightLineText+rightLineText);
// nLinesInserted++;
// //SpaceCount1 = mLines->getString(mCaretY).length(); //???
// mUndoList->AddChange(SynChangeReason::crLineBreak, caretXY(), caretXY(), rightLineText,
// SynSelectionMode::smNormal);
// //insert new line in middle of "{" and "}"
// if (notInComment && leftLineText.endsWith('{') && rightLineText.startsWith('}')) {
// indentSpaces = indentSpacesOfLeftLineText;
// indentSpaces += mTabWidth;
// indentSpacesForRightLineText = GetLeftSpacing(indentSpaces,true);
// mLines->insert(mCaretY, indentSpacesForRightLineText);
// nLinesInserted++;
// mUndoList->AddChange(SynChangeReason::crLineBreak, caretXY(), caretXY(), "",
// SynSelectionMode::smNormal);
// }
// if (moveCaret)
// internalSetCaretXY(BufferCoord{indentSpacesForRightLineText.length()+1,mCaretY + 1});
// }
// } else {
// SpaceCount2 = 0;
// int BackCounter = mCaretY;
// if (mOptions.testFlag(eoAutoIndent)) {
// do {
// BackCounter--;
// Temp = mLines->getString(BackCounter);
// SpaceCount2 = leftSpaces(Temp);
// } while ((BackCounter != 0) && (Temp == ""));
// }
// mLines->insert(mCaretY, "");
// nLinesInserted++;
// BufferCoord Caret = caretXY();
// if (moveCaret) {
// QString Temp4=GetLeftSpacing(SpaceCount2,true);
// if (SpaceCount2 > 0) {
// }
// if (mOptions.testFlag(eoAddIndent) && getHighlighterAttriAtRowCol(BufferCoord{Temp.length(), mCaretY},
// Temp, Attr)) { // only add indent to source files
// if (Attr != mHighlighter->commentAttribute()) { // and outside of comments
// Temp = Temp.trimmed();
// if (Temp.endsWith('{') || Temp.endsWith(':')) { // add more indent for these too
// Temp4=GetLeftSpacing(mTabWidth,true)+Temp4;
// }
// }
// }
// mLines->putString(mCaretY,Temp4); // copy previous indent
// internalSetCaretXY(BufferCoord{Temp4.length()+1, mCaretY + 1});
// }
// mUndoList->AddChange(SynChangeReason::crLineBreak, Caret, Caret, "", SynSelectionMode::smNormal);
// }
// } else {
// if (mLines->count() == 0)
// mLines->add("");
// SpaceCount2 = 0;
// if (mOptions.testFlag(eoAutoIndent)) {
// int BackCounter = mCaretY - 1;
// while (BackCounter >= 0) {
// SpaceCount2 = leftSpaces(mLines->getString(BackCounter));
// if (mLines->getString(BackCounter).length() > 0)
// break;
// BackCounter--;
// }
// }
// mLines->insert(mCaretY - 1, GetLeftSpacing(SpaceCount2,true));
// nLinesInserted++;
// mUndoList->AddChange(SynChangeReason::crLineBreak, caretXY(), caretXY(), "",
// SynSelectionMode::smNormal);
// if (moveCaret) {
// internalSetCaretXY(BufferCoord{1, mCaretY + 1});
// }
// }
doLinesInserted(mCaretY - InsDelta, nLinesInserted); doLinesInserted(mCaretY - InsDelta, nLinesInserted);
setBlockBegin(caretXY()); setBlockBegin(caretXY());
setBlockEnd(caretXY()); setBlockEnd(caretXY());

View File

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

View File

@ -1485,6 +1485,12 @@ 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;
mRange.leftBrackets = 0;
mRange.leftParenthesis = 0;
mRange.rightBraces = 0;
mRange.rightBrackets = 0;
mRange.rightParenthesis = 0;
next(); next();
} }