- 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.
This commit is contained in:
Roy Qu 2024-02-29 13:28:19 +08:00
parent a1387a7c2c
commit ee488384a1
5 changed files with 59 additions and 24 deletions

View File

@ -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.

View File

@ -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;
}

View File

@ -199,6 +199,7 @@ public:
void resetBreakpoints();
bool notParsed();
void insertLine();
void breakLine();
void deleteWord();
void deleteToWordStart();
void deleteToWordEnd();

View File

@ -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);

View File

@ -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<s.length()) && ((s[i] == '\r') || (s[i]=='\n') || (s[i] == '\t') || (s[i]==' '))) {
// i++;
// };
while ((i<s.length()) && (s[i]<=32)) {
while ((i<s.length()) && ((s[i] == '\t') || (s[i]==' '))) {
i++;
};
if (i<s.length()) {