work save

This commit is contained in:
royqh1979@gmail.com 2021-11-13 09:05:02 +08:00
parent 7e68309057
commit 664c8e752a
2 changed files with 56 additions and 9 deletions

View File

@ -3528,7 +3528,12 @@ void Editor::reformat()
content); content);
selectAll(); selectAll();
SynEditorOptions oldOptions = getOptions();
SynEditorOptions newOptions = oldOptions;
newOptions.setFlag(SynEditorOption::eoAutoIndent,false);
setOptions(newOptions);
setSelText(QString::fromUtf8(newContent)); setSelText(QString::fromUtf8(newContent));
setOptions(oldOptions);
reparse(); reparse();
checkSyntaxInBack(); checkSyntaxInBack();
reparseTodo(); reparseTodo();

View File

@ -1,4 +1,5 @@
#include "SynEdit.h" #include "SynEdit.h"
#include "highlighter/cpp.h"
#include <QApplication> #include <QApplication>
#include <QFontMetrics> #include <QFontMetrics>
#include <algorithm> #include <algorithm>
@ -1473,7 +1474,14 @@ int SynEdit::calcIndentSpaces(int line, const QString& lineText, bool addIndent)
matchingIndents = rangeAfterFirstToken.matchingIndents; matchingIndents = rangeAfterFirstToken.matchingIndents;
dontAddIndent = true; dontAddIndent = true;
l = startLine; l = startLine;
} else if (mHighlighter->isLastLineCommentNotFinished(rangePreceeding.state) } else if (mHighlighter->getClass() == SynHighlighterClass::CppHighlighter
&& trimmedLineText.startsWith('#')
&& attr == ((SynEditCppHighlighter *)mHighlighter.get())->preprocessorAttribute()) {
dontAddIndent = true;
indentSpaces=0;
l=0;
} else if (mHighlighter->getClass() == SynHighlighterClass::CppHighlighter
&& mHighlighter->isLastLineCommentNotFinished(rangePreceeding.state)
&& (trimmedLineText.startsWith("*")) && (trimmedLineText.startsWith("*"))
) { ) {
// last line is a not finished comment, and this line start with "*" // last line is a not finished comment, and this line start with "*"
@ -2624,8 +2632,40 @@ void SynEdit::doAddChar(QChar AChar)
} }
mUndoList->BeginBlock(); mUndoList->BeginBlock();
if (mOptions.testFlag(eoAutoIndent) && mHighlighter if (mOptions.testFlag(eoAutoIndent)
&& (oldCaretY<=mLines->count())) { && mHighlighter
&& mHighlighter->getClass()==SynHighlighterClass::CppHighlighter
&& (oldCaretY<=mLines->count()) ) {
if (AChar == '#') {
QString temp = mLines->getString(oldCaretY-1).mid(0,oldCaretX-1);
QString right = mLines->getString(oldCaretY-1).mid(oldCaretX);
// and the first nonblank char is this new {
if (temp.trimmed().isEmpty()) {
int indentSpaces = calcIndentSpaces(oldCaretY,"#", true);
QString line = mLines->getString(oldCaretY-1);
if (indentSpaces==0 && leftSpaces(temp)!=0) {
QString temp = right;
int i = temp.length();
mLines->putString(oldCaretY-1,"");
internalSetCaretXY(BufferCoord{i+1,oldCaretY});
mUndoList->AddChange(
SynChangeReason::crDelete,
BufferCoord{1, oldCaretY},
BufferCoord{line.length()+1, oldCaretY},
line,
SynSelectionMode::smNormal
);
mUndoList->AddChange(
SynChangeReason::crInsert,
BufferCoord{1, oldCaretY},
BufferCoord{temp.length()+1, oldCaretY},
"",
SynSelectionMode::smNormal
);
}
}
} else
//unindent if ':' at end of the line //unindent if ':' at end of the line
if (AChar == ':') { if (AChar == ':') {
QString line = mLines->getString(oldCaretY-1); QString line = mLines->getString(oldCaretY-1);
@ -2652,15 +2692,16 @@ void SynEdit::doAddChar(QChar AChar)
); );
} }
} }
} } else
//unindent if '{' is after an statement like 'if' 'for' //unindent if '{' is after an statement like 'if' 'for'
if (AChar == '{') { if (AChar == '{') {
QString temp = mLines->getString(oldCaretY-1).mid(0,oldCaretX-1); QString temp = mLines->getString(oldCaretY-1).mid(0,oldCaretX-1);
QString right = mLines->getString(oldCaretY-1).mid(oldCaretX);
// and the first nonblank char is this new { // and the first nonblank char is this new {
if (temp.trimmed().isEmpty()) { if (temp.trimmed().isEmpty()) {
int indentSpaces = calcIndentSpaces(oldCaretY,"{", true); int indentSpaces = calcIndentSpaces(oldCaretY,"{", true);
QString line = mLines->getString(oldCaretY-1); if (indentSpaces != leftSpaces(temp)) {
if (indentSpaces != leftSpaces(line)) { QString line = mLines->getString(oldCaretY-1) + right;
QString temp = GetLeftSpacing(indentSpaces,true); QString temp = GetLeftSpacing(indentSpaces,true);
int i = temp.length(); int i = temp.length();
mLines->putString(oldCaretY-1,temp); mLines->putString(oldCaretY-1,temp);
@ -2681,16 +2722,17 @@ void SynEdit::doAddChar(QChar AChar)
); );
} }
} }
} } else
// Remove TabWidth of indent of the current line when typing a } // Remove TabWidth of indent of the current line when typing a }
if (AChar == '}') { if (AChar == '}') {
QString temp = mLines->getString(oldCaretY-1).mid(0,oldCaretX-1); QString temp = mLines->getString(oldCaretY-1).mid(0,oldCaretX-1);
QString right = mLines->getString(oldCaretY-1).mid(oldCaretX);
// and the first nonblank char is this new } // and the first nonblank char is this new }
if (temp.trimmed().isEmpty()) { if (temp.trimmed().isEmpty()) {
int indentSpaces = calcIndentSpaces(oldCaretY,"}", true); int indentSpaces = calcIndentSpaces(oldCaretY,"}", true);
QString line = mLines->getString(oldCaretY-1); QString line = mLines->getString(oldCaretY-1);
if (indentSpaces != leftSpaces(line)) { if (indentSpaces != leftSpaces(temp)) {
QString temp = GetLeftSpacing(indentSpaces,true); QString temp = GetLeftSpacing(indentSpaces,true) + right;
int i = temp.length(); int i = temp.length();
mLines->putString(oldCaretY-1,temp); mLines->putString(oldCaretY-1,temp);
internalSetCaretXY(BufferCoord{i+1,oldCaretY}); internalSetCaretXY(BufferCoord{i+1,oldCaretY});