From 0985b6444b701aa3e38e6c0291146b4ee84f3b3c Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Tue, 28 Jun 2022 09:37:18 +0800 Subject: [PATCH] - fix: wrong auto indent calculation for comments --- NEWS.md | 4 ++ RedPandaIDE/editor.cpp | 2 + RedPandaIDE/qsynedit/SynEdit.cpp | 78 +++++++++++++++++++++++--------- 3 files changed, 63 insertions(+), 21 deletions(-) diff --git a/NEWS.md b/NEWS.md index ff17fbe9..bd2c3f24 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 - enhancement: use different color to differenciate folder and headers in completion popup window - enhancement: auto add "/" to folder when completing #include headers diff --git a/RedPandaIDE/editor.cpp b/RedPandaIDE/editor.cpp index dc3701b1..0b2a9b63 100644 --- a/RedPandaIDE/editor.cpp +++ b/RedPandaIDE/editor.cpp @@ -2709,6 +2709,8 @@ void Editor::insertCodeSnippet(const QString &code) auto action = finally([this]{ endUpdate(); }); + if (selAvail()) + setSelText(""); QStringList sl = textToLines(parseMacros(code)); int lastI=0; // int spaceCount = GetLeftSpacing( diff --git a/RedPandaIDE/qsynedit/SynEdit.cpp b/RedPandaIDE/qsynedit/SynEdit.cpp index 7b8ab6aa..4bc2c5fa 100644 --- a/RedPandaIDE/qsynedit/SynEdit.cpp +++ b/RedPandaIDE/qsynedit/SynEdit.cpp @@ -1623,19 +1623,30 @@ int SynEdit::calcIndentSpaces(int line, const QString& lineText, bool addIndent) l=0; } else if (mHighlighter->getClass() == SynHighlighterClass::CppHighlighter && mHighlighter->isLastLineCommentNotFinished(rangePreceeding.state) - && (trimmedLineText.startsWith("*")) ) { - // last line is a not finished comment, and this line start with "*" - // it means this line is a docstring, should indents according to - // the line the comment beginning , and add 1 additional space - additionIndent = 1; - int commentStartLine = findCommentStartLine(startLine-1); - SynRangeState range; - indentSpaces = leftSpaces(mDocument->getString(commentStartLine-1)); - range = mDocument->ranges(commentStartLine-1); - matchingIndents = range.matchingIndents; - indentAdded = true; - l = commentStartLine; + // last line is a not finished comment, + if (trimmedLineText.startsWith("*")) { + // this line start with "* " + // it means this line is a docstring, should indents according to + // the line the comment beginning , and add 1 additional space + additionIndent = 1; + int commentStartLine = findCommentStartLine(startLine-1); + SynRangeState range; + indentSpaces = leftSpaces(mDocument->getString(commentStartLine-1)); + range = mDocument->ranges(commentStartLine-1); + 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) && rangePreceeding.matchingIndents.isEmpty() && rangePreceeding.firstIndentThisLine>=rangePreceeding.indents.length() @@ -2388,12 +2399,11 @@ void SynEdit::insertLine(bool moveCaret) rightLineText=trimLeft(rightLineText); indentSpaces = calcIndentSpaces(mCaretY+1, rightLineText,mOptions.testFlag(eoAutoIndent) - && notInComment); + ); } QString indentSpacesForRightLineText = GetLeftSpacing(indentSpaces,true); mDocument->insert(mCaretY, indentSpacesForRightLineText+rightLineText); nLinesInserted++; - mUndoList->AddChange(SynChangeReason::crLineBreak, caretXY(), caretXY(), rightLineText, SynSelectionMode::smNormal); @@ -2402,7 +2412,7 @@ void SynEdit::insertLine(bool moveCaret) if (!notInComment && ( leftLineText.endsWith("/*") && rightLineText.startsWith("*/") )) { - indentSpaces = calcIndentSpaces(mCaretY+1, "" , mOptions.testFlag(eoAutoIndent)) + tabWidth(); + indentSpaces = calcIndentSpaces(mCaretY+1, "" , mOptions.testFlag(eoAutoIndent)); indentSpacesForRightLineText = GetLeftSpacing(indentSpaces,true); mDocument->insert(mCaretY, indentSpacesForRightLineText); 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 == '#') { //Reindent line when add '{' '}' and '#' at the beginning 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 while (P < Value.length()) { bool notInComment = true; - if (mHighlighter) { - notInComment = !mHighlighter->isLastLineCommentNotFinished( - mHighlighter->getRangeState().state) - && !mHighlighter->isLastLineStringNotFinished( - mHighlighter->getRangeState().state); - } +// if (mHighlighter) { +// notInComment = !mHighlighter->isLastLineCommentNotFinished( +// mHighlighter->getRangeState().state) +// && !mHighlighter->isLastLineStringNotFinished( +// mHighlighter->getRangeState().state); +// } if (Value[P] == '\r') P++; if (Value[P] == '\n')