From 4d48cca3de969178b1c5f5f04a2c59644a800de1 Mon Sep 17 00:00:00 2001 From: royqh1979 Date: Sun, 7 Nov 2021 01:07:41 +0800 Subject: [PATCH] - fix: multi-line comments indents calculation --- NEWS.md | 2 +- RedPandaIDE/editor.cpp | 4 ++ RedPandaIDE/qsynedit/SynEdit.cpp | 65 ++++++++++++++++++++++++++++++-- 3 files changed, 67 insertions(+), 4 deletions(-) diff --git a/NEWS.md b/NEWS.md index 0eac45ce..bb75dd84 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,12 +6,12 @@ Version 0.8.1 For Dev-C++ 7 Beta - enhancement: when problem from competitive companion received, show the problem and problem set views. - enhancement: set problem's answer source file - enhancement: open the problem's answer source file in editor - - fix: if the proceeding line is a comment, current line should not recalculate indent - fix: if the proceeding line ends with ':' in comments, current line should not indent - enhancement: right click the problem set name label to rename it - change: memory view and locals view use debug console's font settings - fix: one line 'while' statement dosen't correctly indents - fix: line start with '{' that follow an un-ended 'if'/'for' statement is not correctly un-indented + - fix: multi-line comments indents calculation Version 0.8 For Dev-C++ 7 Beta - fix: find in the current file is not correcly saved in the search history diff --git a/RedPandaIDE/editor.cpp b/RedPandaIDE/editor.cpp index e68fd535..78738bae 100644 --- a/RedPandaIDE/editor.cpp +++ b/RedPandaIDE/editor.cpp @@ -607,11 +607,15 @@ void Editor::keyPressEvent(QKeyEvent *event) s=TrimLeft(lineText()); if (s.startsWith("* ")) { handled = true; + int right = lines()->getString(caretY()-1).length()-caretX(); s=lineBreak()+"* "; insertString(s,false); BufferCoord p = caretXY(); p.Line++; p.Char = lines()->getString(p.Line-1).length()+1; + if (right>0) { + p.Char -=right+1; + } setCaretXY(p); } } diff --git a/RedPandaIDE/qsynedit/SynEdit.cpp b/RedPandaIDE/qsynedit/SynEdit.cpp index 7c04b962..50aff84a 100644 --- a/RedPandaIDE/qsynedit/SynEdit.cpp +++ b/RedPandaIDE/qsynedit/SynEdit.cpp @@ -1418,9 +1418,7 @@ int SynEdit::calcIndentSpaces(int line, const QString& lineText, bool addIndent) indentSpaces = leftSpaces(s); SynRangeState rangePreceeding = mLines->ranges(startLine-1); mHighlighter->setState(rangePreceeding); - if (addIndent - && !mHighlighter->isLastLineCommentNotFinished(rangePreceeding.state) - && !mHighlighter->isLastLineStringNotFinished(rangePreceeding.state)) { + if (addIndent) { mHighlighter->setLine(lineText.trimmed(),line-1); SynRangeState rangeAfterFirstToken = mHighlighter->getRangeState(); QString firstToken = mHighlighter->getToken(); @@ -1430,6 +1428,7 @@ int SynEdit::calcIndentSpaces(int line, const QString& lineText, bool addIndent) && ( firstToken == "public" || firstToken == "private" || firstToken == "protected" || firstToken == "case")) { + // public: private: protecte: case: should indents like it's parent statement mHighlighter->setState(rangePreceeding); mHighlighter->setLine("}",line-1); rangeAfterFirstToken = mHighlighter->getRangeState(); @@ -1437,14 +1436,71 @@ int SynEdit::calcIndentSpaces(int line, const QString& lineText, bool addIndent) attr = mHighlighter->getTokenAttribute(); } bool dontAddIndent = false; + bool addOwnIndent = false; QVector matchingIndents; int l; if (attr == mHighlighter->symbolAttribute() && (firstToken == '}' || firstToken == '{')) { + // current line starts with '}' or '{', we should consider them to calc indents matchingIndents = rangeAfterFirstToken.matchingIndents; dontAddIndent = true; l = startLine; + } else if (mHighlighter->isLastLineCommentNotFinished(rangePreceeding.state) + && (lineText.startsWith(' ') + || lineText.startsWith('\t')) + ) { + // last line is a not finished comment, and this line start with indents + // we should use indents of the line comment beginning, plus this line's indents + addOwnIndent=true; + int commentStartLine = startLine-1; + SynRangeState range; + while (commentStartLine>=1) { + range = mLines->ranges(commentStartLine-1); + if (!mHighlighter->isLastLineCommentNotFinished(range.state)){ + commentStartLine++; + break; + } + if (!range.matchingIndents.isEmpty() + || range.firstIndentThisLinegetString(commentStartLine-1)); + range = mLines->ranges(commentStartLine-1); + matchingIndents = range.matchingIndents; + dontAddIndent = true; + l = commentStartLine; + } else if (mHighlighter->isLastLineStringNotFinished(rangePreceeding.state) + && (lineText.startsWith(' ') + || lineText.startsWith('\t')) + ) { + // last line is a not finished string, and this line start with indents + // we should use indents of the line string beginning, plus this line's indents + addOwnIndent=true; + int commentStartLine = startLine-1; + SynRangeState range; + while (commentStartLine>=1) { + range = mLines->ranges(commentStartLine-1); + if (!mHighlighter->isLastLineStringNotFinished(range.state)){ + commentStartLine++; + break; + } + if (!range.matchingIndents.isEmpty() + || range.firstIndentThisLinegetString(commentStartLine-1)); + range = mLines->ranges(commentStartLine-1); + matchingIndents = range.matchingIndents; + dontAddIndent = true; + l = commentStartLine; } else { + // we just use infos till preceeding line's end to calc indents matchingIndents = rangePreceeding.matchingIndents; l = startLine-1; } @@ -1508,6 +1564,9 @@ int SynEdit::calcIndentSpaces(int line, const QString& lineText, bool addIndent) dontAddIndent = true; } } + if (addOwnIndent) { + indentSpaces += leftSpaces(lineText); + } } } return std::max(0,indentSpaces);