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,16 +1384,20 @@ 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);
if (addIndent) {
int left = range.leftBraces+range.leftBrackets+range.leftParenthesis; int left = range.leftBraces+range.leftBrackets+range.leftParenthesis;
indentSpaces+=left*mTabWidth; 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();
//todo: if line ends with unclosed comment or string
if (s.trimmed().endsWith(':'))
indentSpaces += mTabWidth;
int right = range.rightBraces+range.rightBrackets+range.leftParenthesis; int right = range.rightBraces+range.rightBrackets+range.leftParenthesis;
indentSpaces-=right*mTabWidth; indentSpaces-=right*mTabWidth;
} }
}
return std::max(0,indentSpaces); return std::max(0,indentSpaces);
} }
@ -1924,38 +1928,30 @@ 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) {
if (Len >= mCaretX) {
if (mCaretX <= 1) {
mLines->insert(mCaretY - 1,
GetLeftSpacing(calcIndentSpaces(mCaretY,""),true));
nLinesInserted++;
mUndoList->AddChange(SynChangeReason::crLineBreak, caretXY(), caretXY(), Temp2,
SynSelectionMode::smNormal);
if (moveCaret)
internalSetCaretY(mCaretY + 1);
} else {
QString leftLineText = lineText().mid(0, mCaretX - 1); QString leftLineText = lineText().mid(0, mCaretX - 1);
QString rightLineText = lineText().mid(mCaretX-1); QString rightLineText = lineText().mid(mCaretX-1);
int indentSpacesOfLeftLineText = leftSpaces(leftLineText);
int indentSpaces = indentSpacesOfLeftLineText;
bool notInComment=true; bool notInComment=true;
properSetLine(mCaretY-1,leftLineText);
if (mOptions.testFlag(eoAutoIndent)) {
rightLineText=TrimLeft(rightLineText);
}
if (getHighlighterAttriAtRowCol(BufferCoord{leftLineText.length(), mCaretY}, if (getHighlighterAttriAtRowCol(BufferCoord{leftLineText.length(), mCaretY},
leftLineText, Attr)) { leftLineText, Attr)) {
notInComment = (Attr != mHighlighter->commentAttribute()); notInComment = (Attr != mHighlighter->commentAttribute());
} }
leftLineText = leftLineText.trimmed(); properSetLine(mCaretY-1,leftLineText);
if (mOptions.testFlag(eoAddIndent)) { // only add indent to source files //update range stated for line mCaretY
if (notInComment) { // and outside of comments if (mHighlighter) {
if (leftLineText.endsWith(':') || leftLineText.endsWith('{')) if (mCaretY==1) {
indentSpaces+=mTabWidth; mHighlighter->resetState();
if (rightLineText.startsWith('}')) } else {
indentSpaces-=mTabWidth; mHighlighter->setState(mLines->ranges(mCaretY-2));
} }
mHighlighter->setLine(leftLineText, mCaretY-1);
mHighlighter->nextToEol();
mLines->setRange(mCaretY-1,mHighlighter->getRangeState());
}
int indentSpaces = calcIndentSpaces(mCaretY,
rightLineText,mOptions.testFlag(eoAddIndent)
&& notInComment);
if (mOptions.testFlag(eoAutoIndent)) {
rightLineText=TrimLeft(rightLineText);
} }
QString indentSpacesForRightLineText = GetLeftSpacing(indentSpaces,true); QString indentSpacesForRightLineText = GetLeftSpacing(indentSpaces,true);
mLines->insert(mCaretY, indentSpacesForRightLineText+rightLineText); mLines->insert(mCaretY, indentSpacesForRightLineText+rightLineText);
@ -1966,8 +1962,8 @@ void SynEdit::insertLine(bool moveCaret)
SynSelectionMode::smNormal); SynSelectionMode::smNormal);
//insert new line in middle of "{" and "}" //insert new line in middle of "{" and "}"
if (notInComment && leftLineText.endsWith('{') && rightLineText.startsWith('}')) { if (notInComment && leftLineText.endsWith('{') && rightLineText.startsWith('}')) {
indentSpaces = indentSpacesOfLeftLineText; indentSpaces = calcIndentSpaces(mCaretY, "" , mOptions.testFlag(eoAddIndent)
indentSpaces += mTabWidth; && notInComment);
indentSpacesForRightLineText = GetLeftSpacing(indentSpaces,true); indentSpacesForRightLineText = GetLeftSpacing(indentSpaces,true);
mLines->insert(mCaretY, indentSpacesForRightLineText); mLines->insert(mCaretY, indentSpacesForRightLineText);
nLinesInserted++; nLinesInserted++;
@ -1976,59 +1972,112 @@ void SynEdit::insertLine(bool moveCaret)
} }
if (moveCaret) if (moveCaret)
internalSetCaretXY(BufferCoord{indentSpacesForRightLineText.length()+1,mCaretY + 1}); internalSetCaretXY(BufferCoord{indentSpacesForRightLineText.length()+1,mCaretY + 1});
}
} else {
SpaceCount2 = calcIndentSpaces(mCaretY,""); // if (Len > 0) {
int BackCounter = mCaretY; // if (Len >= mCaretX) {
if (mOptions.testFlag(eoAutoIndent)) { // if (mCaretX <= 1) {
do { // mLines->insert(mCaretY - 1, "");
BackCounter--; // nLinesInserted++;
Temp = mLines->getString(BackCounter); // mUndoList->AddChange(SynChangeReason::crLineBreak, caretXY(), caretXY(), Temp2,
SpaceCount2 = leftSpaces(Temp); // SynSelectionMode::smNormal);
} while ((BackCounter != 0) && (Temp == "")); // if (moveCaret)
} // internalSetCaretY(mCaretY + 1);
mLines->insert(mCaretY, GetLeftSpacing(,true))); // } else {
nLinesInserted++; // QString leftLineText = lineText().mid(0, mCaretX - 1);
BufferCoord Caret = caretXY(); // QString rightLineText = lineText().mid(mCaretX-1);
if (moveCaret) { // int indentSpacesOfLeftLineText = leftSpaces(leftLineText);
QString Temp4=GetLeftSpacing(SpaceCount2,true); // int indentSpaces = indentSpacesOfLeftLineText;
if (SpaceCount2 > 0) { // bool notInComment=true;
} // properSetLine(mCaretY-1,leftLineText);
if (mOptions.testFlag(eoAddIndent) && getHighlighterAttriAtRowCol(BufferCoord{Temp.length(), mCaretY}, // if (mOptions.testFlag(eoAutoIndent)) {
Temp, Attr)) { // only add indent to source files // rightLineText=TrimLeft(rightLineText);
if (Attr != mHighlighter->commentAttribute()) { // and outside of comments // }
Temp = Temp.trimmed(); // if (getHighlighterAttriAtRowCol(BufferCoord{leftLineText.length(), mCaretY},
if (Temp.endsWith('{') || Temp.endsWith(':')) { // add more indent for these too // leftLineText, Attr)) {
Temp4=GetLeftSpacing(mTabWidth,true)+Temp4; // notInComment = (Attr != mHighlighter->commentAttribute());
} // }
} // leftLineText = leftLineText.trimmed();
} // if (mOptions.testFlag(eoAddIndent)) { // only add indent to source files
mLines->putString(mCaretY,Temp4); // copy previous indent // if (notInComment) { // and outside of comments
internalSetCaretXY(BufferCoord{Temp4.length()+1, mCaretY + 1}); // if (leftLineText.endsWith(':') || leftLineText.endsWith('{'))
} // indentSpaces+=mTabWidth;
mUndoList->AddChange(SynChangeReason::crLineBreak, Caret, Caret, "", SynSelectionMode::smNormal); // if (rightLineText.startsWith('}'))
} // indentSpaces-=mTabWidth;
} else { // }
if (mLines->count() == 0) // }
mLines->add(""); // QString indentSpacesForRightLineText = GetLeftSpacing(indentSpaces,true);
SpaceCount2 = 0; // mLines->insert(mCaretY, indentSpacesForRightLineText+rightLineText);
if (mOptions.testFlag(eoAutoIndent)) { // nLinesInserted++;
int BackCounter = mCaretY - 1;
while (BackCounter >= 0) { // //SpaceCount1 = mLines->getString(mCaretY).length(); //???
SpaceCount2 = leftSpaces(mLines->getString(BackCounter)); // mUndoList->AddChange(SynChangeReason::crLineBreak, caretXY(), caretXY(), rightLineText,
if (mLines->getString(BackCounter).length() > 0) // SynSelectionMode::smNormal);
break; // //insert new line in middle of "{" and "}"
BackCounter--; // if (notInComment && leftLineText.endsWith('{') && rightLineText.startsWith('}')) {
} // indentSpaces = indentSpacesOfLeftLineText;
} // indentSpaces += mTabWidth;
mLines->insert(mCaretY - 1, GetLeftSpacing(SpaceCount2,true)); // indentSpacesForRightLineText = GetLeftSpacing(indentSpaces,true);
nLinesInserted++; // mLines->insert(mCaretY, indentSpacesForRightLineText);
mUndoList->AddChange(SynChangeReason::crLineBreak, caretXY(), caretXY(), "", // nLinesInserted++;
SynSelectionMode::smNormal); // mUndoList->AddChange(SynChangeReason::crLineBreak, caretXY(), caretXY(), "",
if (moveCaret) { // SynSelectionMode::smNormal);
internalSetCaretXY(BufferCoord{1, mCaretY + 1}); // }
} // 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();
} }