- fix: wrong auto indent calculation for comments

This commit is contained in:
Roy Qu 2022-06-28 09:37:18 +08:00
parent a0ee8b436d
commit 0985b6444b
3 changed files with 63 additions and 21 deletions

View File

@ -1,3 +1,7 @@
Red Panda C++ Version 1.1.3
- fix: wrong auto indent calculation for comments
Red Panda C++ Version 1.1.2 Red Panda C++ Version 1.1.2
- enhancement: use different color to differenciate folder and headers in completion popup window - enhancement: use different color to differenciate folder and headers in completion popup window
- enhancement: auto add "/" to folder when completing #include headers - enhancement: auto add "/" to folder when completing #include headers

View File

@ -2709,6 +2709,8 @@ void Editor::insertCodeSnippet(const QString &code)
auto action = finally([this]{ auto action = finally([this]{
endUpdate(); endUpdate();
}); });
if (selAvail())
setSelText("");
QStringList sl = textToLines(parseMacros(code)); QStringList sl = textToLines(parseMacros(code));
int lastI=0; int lastI=0;
// int spaceCount = GetLeftSpacing( // int spaceCount = GetLeftSpacing(

View File

@ -1623,19 +1623,30 @@ int SynEdit::calcIndentSpaces(int line, const QString& lineText, bool addIndent)
l=0; l=0;
} else if (mHighlighter->getClass() == SynHighlighterClass::CppHighlighter } else if (mHighlighter->getClass() == SynHighlighterClass::CppHighlighter
&& mHighlighter->isLastLineCommentNotFinished(rangePreceeding.state) && mHighlighter->isLastLineCommentNotFinished(rangePreceeding.state)
&& (trimmedLineText.startsWith("*"))
) { ) {
// last line is a not finished comment, and this line start with "*" // last line is a not finished comment,
// it means this line is a docstring, should indents according to if (trimmedLineText.startsWith("*")) {
// the line the comment beginning , and add 1 additional space // this line start with "* "
additionIndent = 1; // it means this line is a docstring, should indents according to
int commentStartLine = findCommentStartLine(startLine-1); // the line the comment beginning , and add 1 additional space
SynRangeState range; additionIndent = 1;
indentSpaces = leftSpaces(mDocument->getString(commentStartLine-1)); int commentStartLine = findCommentStartLine(startLine-1);
range = mDocument->ranges(commentStartLine-1); SynRangeState range;
matchingIndents = range.matchingIndents; indentSpaces = leftSpaces(mDocument->getString(commentStartLine-1));
indentAdded = true; range = mDocument->ranges(commentStartLine-1);
l = commentStartLine; matchingIndents = range.matchingIndents;
indentAdded = true;
l = commentStartLine;
} else {
//add indent according to the beginning of the comment
additionIndent = 0;
SynRangeState range;
indentSpaces = leftSpaces(startLineText);
range = mDocument->ranges(startLine-1);
matchingIndents = range.matchingIndents;
indentAdded = true;
l = startLine;
}
} else if ( mHighlighter->isLastLineCommentNotFinished(statePrePre) } else if ( mHighlighter->isLastLineCommentNotFinished(statePrePre)
&& rangePreceeding.matchingIndents.isEmpty() && rangePreceeding.matchingIndents.isEmpty()
&& rangePreceeding.firstIndentThisLine>=rangePreceeding.indents.length() && rangePreceeding.firstIndentThisLine>=rangePreceeding.indents.length()
@ -2388,12 +2399,11 @@ void SynEdit::insertLine(bool moveCaret)
rightLineText=trimLeft(rightLineText); rightLineText=trimLeft(rightLineText);
indentSpaces = calcIndentSpaces(mCaretY+1, indentSpaces = calcIndentSpaces(mCaretY+1,
rightLineText,mOptions.testFlag(eoAutoIndent) rightLineText,mOptions.testFlag(eoAutoIndent)
&& notInComment); );
} }
QString indentSpacesForRightLineText = GetLeftSpacing(indentSpaces,true); QString indentSpacesForRightLineText = GetLeftSpacing(indentSpaces,true);
mDocument->insert(mCaretY, indentSpacesForRightLineText+rightLineText); mDocument->insert(mCaretY, indentSpacesForRightLineText+rightLineText);
nLinesInserted++; nLinesInserted++;
mUndoList->AddChange(SynChangeReason::crLineBreak, caretXY(), caretXY(), rightLineText, mUndoList->AddChange(SynChangeReason::crLineBreak, caretXY(), caretXY(), rightLineText,
SynSelectionMode::smNormal); SynSelectionMode::smNormal);
@ -2402,7 +2412,7 @@ void SynEdit::insertLine(bool moveCaret)
if (!notInComment && if (!notInComment &&
( leftLineText.endsWith("/*") && rightLineText.startsWith("*/") ( leftLineText.endsWith("/*") && rightLineText.startsWith("*/")
)) { )) {
indentSpaces = calcIndentSpaces(mCaretY+1, "" , mOptions.testFlag(eoAutoIndent)) + tabWidth(); indentSpaces = calcIndentSpaces(mCaretY+1, "" , mOptions.testFlag(eoAutoIndent));
indentSpacesForRightLineText = GetLeftSpacing(indentSpaces,true); indentSpacesForRightLineText = GetLeftSpacing(indentSpaces,true);
mDocument->insert(mCaretY, indentSpacesForRightLineText); mDocument->insert(mCaretY, indentSpacesForRightLineText);
nLinesInserted++; nLinesInserted++;
@ -2913,6 +2923,32 @@ void SynEdit::doAddChar(QChar AChar)
); );
} }
} }
} else if (AChar == '*') {
QString line = mDocument->getString(oldCaretY-1);
if (line.length() <= oldCaretX) {
int indentSpaces = calcIndentSpaces(oldCaretY,line+"*", true);
if (indentSpaces != leftSpaces(line)) {
QString newLine = GetLeftSpacing(indentSpaces,true) + trimLeft(line);
mDocument->putString(oldCaretY-1,newLine);
internalSetCaretXY(BufferCoord{newLine.length()+2,oldCaretY});
setBlockBegin(caretXY());
setBlockEnd(caretXY());
mUndoList->AddChange(
SynChangeReason::crDelete,
BufferCoord{1, oldCaretY},
BufferCoord{line.length()+1, oldCaretY},
line,
SynSelectionMode::smNormal
);
mUndoList->AddChange(
SynChangeReason::crInsert,
BufferCoord{1, oldCaretY},
BufferCoord{newLine.length()+1, oldCaretY},
"",
SynSelectionMode::smNormal
);
}
}
} else if (AChar == '{' || AChar == '}' || AChar == '#') { } else if (AChar == '{' || AChar == '}' || AChar == '#') {
//Reindent line when add '{' '}' and '#' at the beginning //Reindent line when add '{' '}' and '#' at the beginning
QString left = mDocument->getString(oldCaretY-1).mid(0,oldCaretX-1); QString left = mDocument->getString(oldCaretY-1).mid(0,oldCaretX-1);
@ -5533,12 +5569,12 @@ int SynEdit::insertTextByNormalMode(const QString &Value)
// step2: insert remaining lines of Value // step2: insert remaining lines of Value
while (P < Value.length()) { while (P < Value.length()) {
bool notInComment = true; bool notInComment = true;
if (mHighlighter) { // if (mHighlighter) {
notInComment = !mHighlighter->isLastLineCommentNotFinished( // notInComment = !mHighlighter->isLastLineCommentNotFinished(
mHighlighter->getRangeState().state) // mHighlighter->getRangeState().state)
&& !mHighlighter->isLastLineStringNotFinished( // && !mHighlighter->isLastLineStringNotFinished(
mHighlighter->getRangeState().state); // mHighlighter->getRangeState().state);
} // }
if (Value[P] == '\r') if (Value[P] == '\r')
P++; P++;
if (Value[P] == '\n') if (Value[P] == '\n')