From bbb3071d44461dc3d7beb3edd9aa44002b9082ad Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Thu, 29 Feb 2024 08:45:18 +0800 Subject: [PATCH] - fix: Insert line after comments may auto add an extra '*'. --- NEWS.md | 1 + RedPandaIDE/editor.cpp | 139 ++++++++++++++++++++++------------------- 2 files changed, 75 insertions(+), 65 deletions(-) diff --git a/NEWS.md b/NEWS.md index 4af4a3e9..b26bfebe 100644 --- a/NEWS.md +++ b/NEWS.md @@ -17,6 +17,7 @@ Red Panda C++ Version 2.27 - enhancement: Support POSIX shell-like escaping in user inputs for compiler arguments. - fix: (Hopefully) properly escape filenames and arguments in makefile generation. - enhancement: Beautify display for spaces and linebreaks. + - fix: Insert line after comments may auto add an extra '*'. Red Panda C++ Version 2.26 - enhancement: Code suggestion for embedded std::vectors. diff --git a/RedPandaIDE/editor.cpp b/RedPandaIDE/editor.cpp index fac20c5c..c509dd55 100644 --- a/RedPandaIDE/editor.cpp +++ b/RedPandaIDE/editor.cpp @@ -736,74 +736,83 @@ void Editor::keyPressEvent(QKeyEvent *event) clearUserCodeInTabStops(); } else { QString s = lineText().mid(0,caretX()-1).trimmed(); - if (s=="/**") { //javadoc style docstring - s = lineText().mid(caretX()-1).trimmed(); - if (s=="*/") { - QSynedit::BufferCoord p = caretXY(); - setBlockBegin(p); - p.ch = lineText().length()+1; - setBlockEnd(p); - setSelText(""); - } - handled = true; - QStringList insertString; - insertString.append(""); - PStatement function; - if (mParser) - function = mParser->findFunctionAt(mFilename,caretY()+1); - if (function) { - QStringList params; - QString funcName = function->command; - bool isVoid = (function->type == "void"); - foreach (const PStatement& child, function->children) { - if (child->kind == StatementKind::skParameter) - params.append(child->command); - } - insertString.append(QString(" * @brief ")+USER_CODE_IN_INSERT_POS); - if (!params.isEmpty()) - insertString.append(" * "); - foreach (const QString& param, params) { - insertString.append(QString(" * @param %1 %2") - .arg(param, USER_CODE_IN_INSERT_POS)); - } - if (!isVoid) { - insertString.append(" * "); - insertString.append(QString(" * @return ")+USER_CODE_IN_INSERT_POS); - } - insertString.append(" */"); -// } else if (caretY()==1) { /* file header */ -// insertString.append(QString(" * @file %1%2") -// .arg(USER_CODE_IN_REPL_POS_BEGIN) -// .arg(USER_CODE_IN_REPL_POS_END)); -// insertString.append(QString(" * @brief: ")+ USER_CODE_IN_INSERT_POS); -// insertString.append(QString(" * @version: ")+ USER_CODE_IN_INSERT_POS); -// insertString.append(QString(" * @copyright: ")+ USER_CODE_IN_INSERT_POS); -// insertString.append(QString(" * @author: ")+ USER_CODE_IN_INSERT_POS); -// insertString.append(" * @date: "); -// insertString.append(" * "); -// insertString.append(" **/"); + if (syntaxer()) { + if (caretY()==1) { + syntaxer()->resetState(); } else { - insertString.append(QString(" * ")+USER_CODE_IN_INSERT_POS); - insertString.append(" */"); + syntaxer()->setState(document()->getSyntaxState(caretY()-2)); } - insertCodeSnippet(linesToText(insertString)); - } else if (syntaxer() - && caretY()>=2 - && syntaxer()->isLastLineCommentNotFinished( - document()->getSyntaxState(caretY()-2).state)) { - s=trimLeft(lineText()); - if (s.startsWith("* ")) { - handled = true; - int right = document()->getLine(caretY()-1).length()-caretX(); - s=lineBreak()+"* "; - insertString(s,false); - QSynedit::BufferCoord p = caretXY(); - p.line++; - p.ch = document()->getLine(p.line-1).length()+1; - if (right>0) { - p.ch -=right+1; + syntaxer()->setLine(s,caretY()); + syntaxer()->nextToEol(); + int state = syntaxer()->getState().state; + if (syntaxer()->isLastLineCommentNotFinished(state)) { + if (s=="/**") { //javadoc style docstring + s = lineText().mid(caretX()-1).trimmed(); + if (s=="*/") { + QSynedit::BufferCoord p = caretXY(); + setBlockBegin(p); + p.ch = lineText().length()+1; + setBlockEnd(p); + setSelText(""); + } + handled = true; + QStringList insertString; + insertString.append(""); + PStatement function; + if (mParser) + function = mParser->findFunctionAt(mFilename,caretY()+1); + if (function) { + QStringList params; + QString funcName = function->command; + bool isVoid = (function->type == "void"); + foreach (const PStatement& child, function->children) { + if (child->kind == StatementKind::skParameter) + params.append(child->command); + } + insertString.append(QString(" * @brief ")+USER_CODE_IN_INSERT_POS); + if (!params.isEmpty()) + insertString.append(" * "); + foreach (const QString& param, params) { + insertString.append(QString(" * @param %1 %2") + .arg(param, USER_CODE_IN_INSERT_POS)); + } + if (!isVoid) { + insertString.append(" * "); + insertString.append(QString(" * @return ")+USER_CODE_IN_INSERT_POS); + } + insertString.append(" */"); + // } else if (caretY()==1) { /* file header */ + // insertString.append(QString(" * @file %1%2") + // .arg(USER_CODE_IN_REPL_POS_BEGIN) + // .arg(USER_CODE_IN_REPL_POS_END)); + // insertString.append(QString(" * @brief: ")+ USER_CODE_IN_INSERT_POS); + // insertString.append(QString(" * @version: ")+ USER_CODE_IN_INSERT_POS); + // insertString.append(QString(" * @copyright: ")+ USER_CODE_IN_INSERT_POS); + // insertString.append(QString(" * @author: ")+ USER_CODE_IN_INSERT_POS); + // insertString.append(" * @date: "); + // insertString.append(" * "); + // insertString.append(" **/"); + } else { + insertString.append(QString(" * ")+USER_CODE_IN_INSERT_POS); + insertString.append(" */"); + } + insertCodeSnippet(linesToText(insertString)); + } else { + s=trimLeft(lineText()); + if (s.startsWith("* ")) { + handled = true; + int right = document()->getLine(caretY()-1).length()-caretX(); + s=lineBreak()+"* "; + insertString(s,false); + QSynedit::BufferCoord p = caretXY(); + p.line++; + p.ch = document()->getLine(p.line-1).length()+1; + if (right>0) { + p.ch -=right+1; + } + setCaretXY(p); + } } - setCaretXY(p); } } }