From ee488384a14cc3deaab0a0f5e03fab4c2236139c Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Thu, 29 Feb 2024 13:28:19 +0800 Subject: [PATCH] - fix: Wrong indent for the line after the pasted context. - Enhancement: When '{' is inputted and there are contents selected, auto add line breaks and indents. --- NEWS.md | 2 + RedPandaIDE/editor.cpp | 62 ++++++++++++++++++----- RedPandaIDE/editor.h | 1 + libs/qsynedit/qsynedit/qsynedit.cpp | 14 ++--- libs/redpanda_qt_utils/qt_utils/utils.cpp | 4 +- 5 files changed, 59 insertions(+), 24 deletions(-) diff --git a/NEWS.md b/NEWS.md index 77062c76..8d9e2857 100644 --- a/NEWS.md +++ b/NEWS.md @@ -19,6 +19,8 @@ Red Panda C++ Version 2.27 - enhancement: Beautify display for spaces and linebreaks. - fix: Insert line after comments may auto add an extra '*'. - fix: Can't show function tips for std::ios::sync_with_stdio. + - fix: Wrong indent for the line after the pasted context. + - Enhancement: When '{' is inputted and there are contents selected, auto add line breaks and indents. Red Panda C++ Version 2.26 - enhancement: Code suggestion for embedded std::vectors. diff --git a/RedPandaIDE/editor.cpp b/RedPandaIDE/editor.cpp index c509dd55..2819fb16 100644 --- a/RedPandaIDE/editor.cpp +++ b/RedPandaIDE/editor.cpp @@ -2207,6 +2207,11 @@ void Editor::insertLine() processCommand(QSynedit::EditCommand::InsertLine,QChar(),nullptr); } +void Editor::breakLine() +{ + processCommand(QSynedit::EditCommand::LineBreak,QChar(),nullptr); +} + void Editor::deleteWord() { processCommand(QSynedit::EditCommand::DeleteWord,QChar(),nullptr); @@ -2777,23 +2782,13 @@ bool Editor::handleMultilineCommentCompletion() bool Editor::handleBraceCompletion() { + bool addSemicolon=false; QString s = lineText().trimmed(); int i= caretY()-2; while ((s.isEmpty()) && (i>=0)) { s=document()->getLine(i).trimmed(); i--; } - QString text=selText(); - beginEditing(); - processCommand(QSynedit::EditCommand::Char,'{'); - QSynedit::BufferCoord oldCaret; - if (text.isEmpty()) { - oldCaret = caretXY(); - } else { - setSelText(text); - } - - processCommand(QSynedit::EditCommand::Char,'}'); if ( ( ( (s.startsWith("struct") && !s.endsWith(")")) || s.startsWith("class") @@ -2804,10 +2799,51 @@ bool Editor::handleBraceCompletion() || (s.startsWith("enum") && !s.endsWith(")")) ) && !s.contains(';') ) || s.endsWith('=')) { - processCommand(QSynedit::EditCommand::Char,';'); + addSemicolon = true; +// processCommand(QSynedit::EditCommand::Char,';'); } - if (text.isEmpty()) + + beginEditing(); + if (!selAvail()) { + processCommand(QSynedit::EditCommand::Char,'{'); + QSynedit::BufferCoord oldCaret = caretXY(); + processCommand(QSynedit::EditCommand::Char,'}'); + if (addSemicolon) + processCommand(QSynedit::EditCommand::Char,';'); setCaretXY(oldCaret); + } else { + QString text = selText(); + QSynedit::BufferCoord oldSelBegin = blockBegin(); + QSynedit::BufferCoord oldSelEnd = blockEnd(); + bool shouldBreakLine = false; + bool shouldAddEndLine = false; + QString s1=document()->getLine(oldSelBegin.line-1).left(oldSelBegin.ch-1).trimmed(); + + if (s1.isEmpty() ) { + QString s2 = document()->getLine(oldSelEnd.line-1); + if (s2.left(oldSelEnd.ch-1).trimmed().isEmpty()) { + shouldBreakLine = true; + } else if (oldSelEnd.ch > trimRight(s2).length()) { + shouldBreakLine = true; + } + shouldAddEndLine = !s2.mid(oldSelEnd.ch).trimmed().isEmpty(); + } + if (shouldBreakLine) { + text = "{" + lineBreak() + text; + if (!trimRight(text).endsWith(lineBreak())) { + text.append(lineBreak()); + } + } else { + text = "{"+text; + } + if (addSemicolon) + text.append("};"); + else + text.append("}"); + if (shouldAddEndLine) + text.append(lineBreak()); + setSelText(text); + } endEditing(); return true; } diff --git a/RedPandaIDE/editor.h b/RedPandaIDE/editor.h index ad5d3f02..152b32ea 100644 --- a/RedPandaIDE/editor.h +++ b/RedPandaIDE/editor.h @@ -199,6 +199,7 @@ public: void resetBreakpoints(); bool notParsed(); void insertLine(); + void breakLine(); void deleteWord(); void deleteToWordStart(); void deleteToWordEnd(); diff --git a/libs/qsynedit/qsynedit/qsynedit.cpp b/libs/qsynedit/qsynedit/qsynedit.cpp index 311e460d..e96b4fad 100644 --- a/libs/qsynedit/qsynedit/qsynedit.cpp +++ b/libs/qsynedit/qsynedit/qsynedit.cpp @@ -5474,20 +5474,16 @@ int QSynEdit::doInsertTextByNormalMode(const BufferCoord& pos, const QStringList if (i==text.length()-1) { str = sRightSide; } else { - if (!mUndoing && mSyntaxer && mSyntaxer->language()==ProgrammingLanguage::CPP && mOptions.testFlag(eoAutoIndent) && notInComment) { - str = GetLeftSpacing(calcIndentSpaces(caretY,"",true),true); - } else { - str = ""; - } + str = ""; } } else { str = text[i]; if (i==text.length()-1) str += sRightSide; - if (!mUndoing && mSyntaxer && mSyntaxer->language()==ProgrammingLanguage::CPP && mOptions.testFlag(eoAutoIndent) && notInComment) { - int indentSpaces = calcIndentSpaces(caretY,str,true); - str = GetLeftSpacing(indentSpaces,true)+trimLeft(str); - } + } + if (!mUndoing && mSyntaxer && mSyntaxer->language()==ProgrammingLanguage::CPP && mOptions.testFlag(eoAutoIndent) && notInComment) { + int indentSpaces = calcIndentSpaces(caretY,str,true); + str = GetLeftSpacing(indentSpaces,true)+trimLeft(str); } properSetLine(caretY - 1, str,false); reparseLine(caretY); diff --git a/libs/redpanda_qt_utils/qt_utils/utils.cpp b/libs/redpanda_qt_utils/qt_utils/utils.cpp index 144f62b1..5735e2b3 100644 --- a/libs/redpanda_qt_utils/qt_utils/utils.cpp +++ b/libs/redpanda_qt_utils/qt_utils/utils.cpp @@ -197,7 +197,7 @@ QString trimRight(const QString &s) return s; int i = s.length()-1; // while ((i>=0) && ((s[i] == '\r') || (s[i]=='\n') || (s[i] == '\t') || (s[i]==' '))) { - while ((i>=0) && (s[i]<=32)) { + while ((i>=0) && ((s[i] == '\t') || (s[i]==' '))) { i--; }; if (i>=0) { @@ -215,7 +215,7 @@ QString trimLeft(const QString &s) // while ((i