- enhancement: auto indent line to column 1 when enter '#' at beginning of line

- fix: when enter '{' or '}' at beginning of line, auto indent will remove all contents of the line
 - fix: auto indent should be turned off when reformat code
 - fix: auto indent should be turned off when replace in code
This commit is contained in:
royqh1979@gmail.com 2021-11-13 10:38:48 +08:00
parent 4c359dd7cd
commit 33ba4afc20
2 changed files with 29 additions and 81 deletions

View File

@ -1,3 +1,9 @@
Version 0.8.7 For Dev-C++ 7 Beta
- enhancement: auto indent line to column 1 when enter '#' at beginning of line
- fix: when enter '{' or '}' at beginning of line, auto indent will remove all contents of the line
- fix: auto indent should be turned off when reformat code
- fix: auto indent should be turned off when replace in code
Version 0.8.6 For Dev-C++ 7 Beta Version 0.8.6 For Dev-C++ 7 Beta
- enhancement: greatly reduces memory usage for symbol parsing ( memory needed for bits/stdc++.h reduced from 150m+ to 80m+) - enhancement: greatly reduces memory usage for symbol parsing ( memory needed for bits/stdc++.h reduced from 150m+ to 80m+)
- fix: currect compiler set not correctly updated when switch between normal file and project file - fix: currect compiler set not correctly updated when switch between normal file and project file

View File

@ -2632,49 +2632,21 @@ void SynEdit::doAddChar(QChar AChar)
} }
mUndoList->BeginBlock(); mUndoList->BeginBlock();
// auto
if (mOptions.testFlag(eoAutoIndent) if (mOptions.testFlag(eoAutoIndent)
&& mHighlighter && mHighlighter
&& mHighlighter->getClass()==SynHighlighterClass::CppHighlighter && mHighlighter->getClass()==SynHighlighterClass::CppHighlighter
&& (oldCaretY<=mLines->count()) ) { && (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},
temp,
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);
if (line.length() < oldCaretX) { if (line.length() < oldCaretX) {
int indentSpaces = calcIndentSpaces(oldCaretY,line+":", true); int indentSpaces = calcIndentSpaces(oldCaretY,line+":", true);
if (indentSpaces != leftSpaces(line)) { if (indentSpaces != leftSpaces(line)) {
QString temp = GetLeftSpacing(indentSpaces,true) + TrimLeft(line); QString newLine = GetLeftSpacing(indentSpaces,true) + TrimLeft(line);
int i = temp.length(); int i = newLine.length();
mLines->putString(oldCaretY-1,temp); mLines->putString(oldCaretY-1,newLine);
internalSetCaretXY(BufferCoord{i+1,oldCaretY}); internalSetCaretXY(BufferCoord{i+1,oldCaretY});
mUndoList->AddChange( mUndoList->AddChange(
SynChangeReason::crDelete, SynChangeReason::crDelete,
@ -2686,68 +2658,35 @@ void SynEdit::doAddChar(QChar AChar)
mUndoList->AddChange( mUndoList->AddChange(
SynChangeReason::crInsert, SynChangeReason::crInsert,
BufferCoord{1, oldCaretY}, BufferCoord{1, oldCaretY},
BufferCoord{temp.length()+1, oldCaretY}, BufferCoord{newLine.length()+1, oldCaretY},
temp, newLine,
SynSelectionMode::smNormal SynSelectionMode::smNormal
); );
} }
} }
} else } else if (AChar == '{' || AChar == '}' || AChar == '#') {
//unindent if '{' is after an statement like 'if' 'for' //Reindent line when add '{' '}' and '#' at the beginning
if (AChar == '{') { QString left = 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 (left.trimmed().isEmpty()) {
int indentSpaces = calcIndentSpaces(oldCaretY,"{", true); int indentSpaces = calcIndentSpaces(oldCaretY,AChar, true);
if (indentSpaces != leftSpaces(temp)) { if (indentSpaces != leftSpaces(left)) {
QString line = mLines->getString(oldCaretY-1); QString right = mLines->getString(oldCaretY-1).mid(oldCaretX);
QString temp = GetLeftSpacing(indentSpaces,true) + right; QString newLeft = GetLeftSpacing(indentSpaces,true);
int i = temp.length(); mLines->putString(oldCaretY-1,newLeft+right);
mLines->putString(oldCaretY-1,temp); internalSetCaretXY(BufferCoord{newLeft.length()+1,oldCaretY});
internalSetCaretXY(BufferCoord{i+1,oldCaretY});
mUndoList->AddChange( mUndoList->AddChange(
SynChangeReason::crDelete, SynChangeReason::crDelete,
BufferCoord{1, oldCaretY}, BufferCoord{1, oldCaretY},
BufferCoord{line.length()+1, oldCaretY}, BufferCoord{left.length()+1, oldCaretY},
line, left,
SynSelectionMode::smNormal SynSelectionMode::smNormal
); );
mUndoList->AddChange( mUndoList->AddChange(
SynChangeReason::crInsert, SynChangeReason::crInsert,
BufferCoord{1, oldCaretY}, BufferCoord{1, oldCaretY},
BufferCoord{temp.length()+1, oldCaretY}, BufferCoord{newLeft.length()+1, oldCaretY},
temp, newLeft,
SynSelectionMode::smNormal
);
}
}
} else
// Remove TabWidth of indent of the current line when typing a }
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);
if (indentSpaces != leftSpaces(temp)) {
QString line = mLines->getString(oldCaretY-1);
QString temp = GetLeftSpacing(indentSpaces,true) + right;
int i = temp.length();
mLines->putString(oldCaretY-1,temp);
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},
temp,
SynSelectionMode::smNormal SynSelectionMode::smNormal
); );
} }
@ -4923,6 +4862,8 @@ int SynEdit::searchReplace(const QString &sSearch, const QString &sReplace, SynS
mUndoList->BeginBlock(); mUndoList->BeginBlock();
dobatchReplace = true; dobatchReplace = true;
} }
bool oldAutoIndent = mOptions.testFlag(SynEditorOption::eoAutoIndent);
mOptions.setFlag(SynEditorOption::eoAutoIndent,false);
doSetSelText(replaceText); doSetSelText(replaceText);
nReplaceLen = caretX() - nFound; nReplaceLen = caretX() - nFound;
// fix the caret position and the remaining results // fix the caret position and the remaining results
@ -4936,6 +4877,7 @@ int SynEdit::searchReplace(const QString &sSearch, const QString &sReplace, SynS
} }
} }
} }
mOptions.setFlag(SynEditorOption::eoAutoIndent,oldAutoIndent);
} }
} }
// search next / previous line // search next / previous line