- enhancement: more accurate auto indent calculation

- change: remove "add indent" option in the editor general options widget ( It's merged with "auto indent" option)
This commit is contained in:
royqh1979@gmail.com 2021-10-29 15:15:43 +08:00
parent 1853332141
commit fde31ce7b5
10 changed files with 112 additions and 65 deletions

View File

@ -1,3 +1,7 @@
Version 0.7.5
- enhancement: more accurate auto indent calculation
- change: remove "add indent" option in the editor general options widget ( It's merged with "auto indent" option)
Version 0.7.4 Version 0.7.4
- fix: when debug a project, and have breakpoints that not in opened editors, dev-cpp will crash - fix: when debug a project, and have breakpoints that not in opened editors, dev-cpp will crash
- fix: when a file is parsing in background, exit dev-cpp will crash - fix: when a file is parsing in background, exit dev-cpp will crash

View File

@ -3638,7 +3638,6 @@ void Editor::applySettings()
eoRightMouseMovesCursor | eoScrollByOneLess | eoTabIndent | eoHideShowScrollbars; eoRightMouseMovesCursor | eoScrollByOneLess | eoTabIndent | eoHideShowScrollbars;
//options //options
options.setFlag(eoAddIndent,pSettings->editor().addIndent());
options.setFlag(eoAutoIndent,pSettings->editor().autoIndent()); options.setFlag(eoAutoIndent,pSettings->editor().autoIndent());
options.setFlag(eoTabsToSpaces,pSettings->editor().tabToSpaces()); options.setFlag(eoTabsToSpaces,pSettings->editor().tabToSpaces());

View File

@ -112,7 +112,7 @@ SynEdit::SynEdit(QWidget *parent) : QAbstractScrollArea(parent)
mBlockBegin.Char = 1; mBlockBegin.Char = 1;
mBlockBegin.Line = 1; mBlockBegin.Line = 1;
mBlockEnd = mBlockBegin; mBlockEnd = mBlockBegin;
mOptions = eoAutoIndent | eoAddIndent mOptions = eoAutoIndent
| eoDragDropEditing | eoEnhanceEndKey | eoTabIndent | | eoDragDropEditing | eoEnhanceEndKey | eoTabIndent |
eoGroupUndo | eoKeepCaretX | eoSelectWordByDblClick; eoGroupUndo | eoKeepCaretX | eoSelectWordByDblClick;
@ -1386,13 +1386,29 @@ int SynEdit::calcIndentSpaces(int line, const QString& lineText, bool addIndent)
indentSpaces = leftSpaces(s); indentSpaces = leftSpaces(s);
if (addIndent) { if (addIndent) {
SynRangeState rangePreceeding = mLines->ranges(startLine-1); SynRangeState rangePreceeding = mLines->ranges(startLine-1);
if (!rangePreceeding.lastMatchingIndent.isNull()) { if (!rangePreceeding.matchingIndents.isEmpty()) {
// find the indent's start line, and use it's indent as the default indent; // find the indent's start line, and use it's indent as the default indent;
QString matchingIndents = rangePreceeding.matchingIndents;
int l = startLine-1; int l = startLine-1;
int i = 0;
int len = matchingIndents.length();
while (l>=1) { while (l>=1) {
SynRangeState range = mLines->ranges(l-1); SynRangeState range = mLines->ranges(l-1);
if (range.indents.mid(range.firstIndentThisLine).contains(rangePreceeding.lastMatchingIndent)) { QString newIndents = range.indents.mid(range.firstIndentThisLine);
while (i<len && !newIndents.isEmpty()) {
QChar indent = matchingIndents[i];
int idx = newIndents.lastIndexOf(indent);
if (idx >=0) {
newIndents.truncate(idx);
} else {
break;
}
i++;
}
if (i>=len) {
indentSpaces = leftSpaces(mLines->getString(l-1)); indentSpaces = leftSpaces(mLines->getString(l-1));
if (newIndents.length()>0)
indentSpaces+=mTabWidth;
break; break;
} }
l--; l--;
@ -1962,7 +1978,7 @@ void SynEdit::insertLine(bool moveCaret)
mHighlighter->getRangeState().state); mHighlighter->getRangeState().state);
} }
int indentSpaces = calcIndentSpaces(mCaretY+1, int indentSpaces = calcIndentSpaces(mCaretY+1,
rightLineText,mOptions.testFlag(eoAddIndent) rightLineText,mOptions.testFlag(eoAutoIndent)
&& notInComment); && notInComment);
if (mOptions.testFlag(eoAutoIndent)) { if (mOptions.testFlag(eoAutoIndent)) {
rightLineText=TrimLeft(rightLineText); rightLineText=TrimLeft(rightLineText);
@ -1976,7 +1992,7 @@ void SynEdit::insertLine(bool moveCaret)
SynSelectionMode::smNormal); SynSelectionMode::smNormal);
//insert new line in middle of "{" and "}" //insert new line in middle of "{" and "}"
if (notInComment && leftLineText.endsWith('{') && rightLineText.startsWith('}')) { if (notInComment && leftLineText.endsWith('{') && rightLineText.startsWith('}')) {
indentSpaces = calcIndentSpaces(mCaretY+1, "" , mOptions.testFlag(eoAddIndent) indentSpaces = calcIndentSpaces(mCaretY+1, "" , mOptions.testFlag(eoAutoIndent)
&& notInComment); && notInComment);
indentSpacesForRightLineText = GetLeftSpacing(indentSpaces,true); indentSpacesForRightLineText = GetLeftSpacing(indentSpaces,true);
mLines->insert(mCaretY, indentSpacesForRightLineText); mLines->insert(mCaretY, indentSpacesForRightLineText);
@ -2456,21 +2472,47 @@ void SynEdit::doAddChar(QChar AChar)
} }
mUndoList->BeginBlock(); mUndoList->BeginBlock();
if (mOptions.testFlag(eoAddIndent)) { if (mOptions.testFlag(eoAutoIndent) && mHighlighter
&& (oldCaretY<=mLines->count())) {
//unindent if '{' is after an statement like 'if' 'for'
if (AChar == '{') {
QString temp = mLines->getString(oldCaretY-1).mid(0,oldCaretX-1);
// 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 != leftSpaces(line)) {
QString temp = GetLeftSpacing(indentSpaces,true);
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},
"",
SynSelectionMode::smNormal
);
}
}
}
// Remove TabWidth of indent of the current line when typing a } // Remove TabWidth of indent of the current line when typing a }
if (AChar == '}' && (oldCaretY<=mLines->count())) { if (AChar == '}') {
QString temp = mLines->getString(oldCaretY-1).mid(0,oldCaretX-1); QString temp = mLines->getString(oldCaretY-1).mid(0,oldCaretX-1);
// and the first nonblank char is this new } // and the first nonblank char is this new }
if (temp.trimmed().isEmpty()) { if (temp.trimmed().isEmpty()) {
BufferCoord MatchBracketPos = getPreviousLeftBracket(oldCaretX, oldCaretY); int indentSpaces = calcIndentSpaces(oldCaretY,"}", true);
if (MatchBracketPos.Line > 0) { QString line = mLines->getString(oldCaretY-1);
int i = 0; if (indentSpaces != leftSpaces(line)) {
QString matchline = mLines->getString(MatchBracketPos.Line-1); QString temp = GetLeftSpacing(indentSpaces,true);
QString line = mLines->getString(oldCaretY-1); int i = temp.length();
while (i<matchline.length() && (matchline[i]==' ' || matchline[i]=='\t')) {
i++;
}
QString temp = matchline.mid(0,i) + line.mid(mCaretX-1);
mLines->putString(oldCaretY-1,temp); mLines->putString(oldCaretY-1,temp);
internalSetCaretXY(BufferCoord{i+1,oldCaretY}); internalSetCaretXY(BufferCoord{i+1,oldCaretY});
mUndoList->AddChange( mUndoList->AddChange(
@ -2932,7 +2974,7 @@ int SynEdit::scanFrom(int Index, int canStopIndex)
&& mLines->ranges(Result).bracketLevel == iRange.bracketLevel && mLines->ranges(Result).bracketLevel == iRange.bracketLevel
) { ) {
if (mUseCodeFolding) if (mUseCodeFolding)
rescan(); rescanFolds();
return Result;// avoid the final Decrement return Result;// avoid the final Decrement
} }
} }
@ -2941,11 +2983,29 @@ int SynEdit::scanFrom(int Index, int canStopIndex)
} while (Result < mLines->count()); } while (Result < mLines->count());
Result--; Result--;
if (mUseCodeFolding) if (mUseCodeFolding)
rescan(); rescanFolds();
return Result; return Result;
} }
void SynEdit::scanRanges() void SynEdit::rescanRange(int line)
{
line--;
line = std::max(0,line);
if (line >= mLines->count())
return;
if (line == 0) {
mHighlighter->resetState();
} else {
mHighlighter->setState(mLines->ranges(line-1));
}
mHighlighter->setLine(mLines->getString(line), line);
mHighlighter->nextToEol();
SynRangeState iRange = mHighlighter->getRangeState();
mLines->setRange(line,iRange);
}
void SynEdit::rescanRanges()
{ {
if (mHighlighter && !mLines->empty()) { if (mHighlighter && !mLines->empty()) {
mHighlighter->resetState(); mHighlighter->resetState();
@ -2956,7 +3016,7 @@ void SynEdit::scanRanges()
} }
} }
if (mUseCodeFolding) if (mUseCodeFolding)
rescan(); rescanFolds();
} }
void SynEdit::uncollapse(PSynEditFoldRange FoldRange) void SynEdit::uncollapse(PSynEditFoldRange FoldRange)
@ -3025,7 +3085,7 @@ void SynEdit::foldOnListCleared()
mAllFoldRanges.clear(); mAllFoldRanges.clear();
} }
void SynEdit::rescan() void SynEdit::rescanFolds()
{ {
if (!mUseCodeFolding) if (!mUseCodeFolding)
return; return;
@ -4262,7 +4322,7 @@ void SynEdit::setHighlighter(const PSynHighlighter &highlighter)
auto action=finally([this]{ auto action=finally([this]{
mLines->endUpdate(); mLines->endUpdate();
}); });
scanRanges(); rescanRanges();
} }
onSizeOrFontChanged(true); onSizeOrFontChanged(true);
invalidate(); invalidate();
@ -4776,7 +4836,7 @@ int SynEdit::insertTextByNormalMode(const QString &Value)
int Start; int Start;
int P; int P;
bool bChangeScroll; bool bChangeScroll;
int SpaceCount; // int SpaceCount;
int Result = 0; int Result = 0;
sLeftSide = lineText().mid(0, mCaretX - 1); sLeftSide = lineText().mid(0, mCaretX - 1);
if (mCaretX - 1 > sLeftSide.length()) { if (mCaretX - 1 > sLeftSide.length()) {
@ -4786,22 +4846,23 @@ int SynEdit::insertTextByNormalMode(const QString &Value)
sLeftSide += QString(mCaretX - 1 - sLeftSide.length(),' '); sLeftSide += QString(mCaretX - 1 - sLeftSide.length(),' ');
} }
sRightSide = lineText().mid(mCaretX-1); sRightSide = lineText().mid(mCaretX-1);
if (mUndoing) { // if (mUndoing) {
SpaceCount = 0; // SpaceCount = 0;
} else { // } else {
SpaceCount = leftSpaces(sLeftSide); // SpaceCount = leftSpaces(sLeftSide);
} // }
// step1: insert the first line of Value into current line // step1: insert the first line of Value into current line
Start = 0; Start = 0;
P = GetEOL(Value,Start); P = GetEOL(Value,Start);
if (P<Value.length()) { if (P<Value.length()) {
Str = sLeftSide + Value.mid(0, P - Start); Str = sLeftSide + TrimLeft(Value.mid(0, P - Start));
properSetLine(mCaretY - 1, Str); properSetLine(mCaretY - 1, Str);
mLines->insertLines(mCaretY, CountLines(Value,P)); mLines->insertLines(mCaretY, CountLines(Value,P));
} else { } else {
Str = sLeftSide + Value + sRightSide; Str = sLeftSide + Value + sRightSide;
properSetLine(mCaretY - 1, Str); properSetLine(mCaretY - 1, Str);
} }
rescanRange(mCaretY);
// step2: insert remaining lines of Value // step2: insert remaining lines of Value
while (P < Value.length()) { while (P < Value.length()) {
if (Value[P] == '\r') if (Value[P] == '\r')
@ -4822,8 +4883,12 @@ int SynEdit::insertTextByNormalMode(const QString &Value)
if (P>=Value.length()) if (P>=Value.length())
Str += sRightSide; Str += sRightSide;
} }
Str = GetLeftSpacing(SpaceCount, true)+Str; if (mOptions.testFlag(eoAutoIndent)) {
int indentSpaces = calcIndentSpaces(mCaretY,Str,true);
Str = GetLeftSpacing(indentSpaces,true)+TrimLeft(Str);
}
properSetLine(mCaretY - 1, Str); properSetLine(mCaretY - 1, Str);
rescanRange(mCaretY);
Result++; Result++;
} }
bChangeScroll = !mOptions.testFlag(eoScrollPastEol); bChangeScroll = !mOptions.testFlag(eoScrollPastEol);

View File

@ -65,8 +65,8 @@ Q_DECLARE_OPERATORS_FOR_FLAGS(SynStateFlags)
enum SynEditorOption { enum SynEditorOption {
eoAltSetsColumnMode = 0x00000001, //Holding down the Alt Key will put the selection mode into columnar format eoAltSetsColumnMode = 0x00000001, //Holding down the Alt Key will put the selection mode into columnar format
eoAutoIndent = 0x00000002, //Will indent the caret on new lines with the same amount of leading white space as the preceding line eoAutoIndent = 0x00000002, //Will auto calculate the indent when input
eoAddIndent = 0x00000004, //Will add one tab width of indent when typing { and :, and remove the same amount when typing } // eoAddIndent = 0x00000004, //Will add one tab width of indent when typing { and :, and remove the same amount when typing }
eoDragDropEditing = 0x00000008, //Allows you to select a block of text and drag it within the document to another location eoDragDropEditing = 0x00000008, //Allows you to select a block of text and drag it within the document to another location
eoDropFiles = 0x00000010, //Allows the editor accept OLE file drops eoDropFiles = 0x00000010, //Allows the editor accept OLE file drops
eoEnhanceHomeKey = 0x00000020, //enhances home key positioning, similar to visual studio eoEnhanceHomeKey = 0x00000020, //enhances home key positioning, similar to visual studio
@ -455,14 +455,15 @@ private:
QString expandAtWideGlyphs(const QString& S); QString expandAtWideGlyphs(const QString& S);
void updateModifiedStatus(); void updateModifiedStatus();
int scanFrom(int Index, int canStopIndex); int scanFrom(int Index, int canStopIndex);
void scanRanges(); void rescanRange(int line);
void rescanRanges();
void uncollapse(PSynEditFoldRange FoldRange); void uncollapse(PSynEditFoldRange FoldRange);
void collapse(PSynEditFoldRange FoldRange); void collapse(PSynEditFoldRange FoldRange);
void foldOnListInserted(int Line, int Count); void foldOnListInserted(int Line, int Count);
void foldOnListDeleted(int Line, int Count); void foldOnListDeleted(int Line, int Count);
void foldOnListCleared(); void foldOnListCleared();
void rescan(); // rescan for folds void rescanFolds(); // rescan for folds
void rescanForFoldRanges(); void rescanForFoldRanges();
void scanForFoldRanges(PSynEditFoldRanges TopFoldRanges); void scanForFoldRanges(PSynEditFoldRanges TopFoldRanges);
int lineHasChar(int Line, int startChar, QChar character, const QString& highlighterAttrName); int lineHasChar(int Line, int startChar, QChar character, const QString& highlighterAttrName);

View File

@ -26,7 +26,7 @@ struct SynRangeState {
QString indents; // indents stack (each char represents an indent) (needed by auto indent) QString indents; // indents stack (each char represents an indent) (needed by auto indent)
int firstIndentThisLine; /* index of first indent that appended to the indents int firstIndentThisLine; /* index of first indent that appended to the indents
* stack at this line ( need by auto indent) */ * stack at this line ( need by auto indent) */
QChar lastMatchingIndent; /* the last indent matched ( and removed ) QString matchingIndents; /* the indent matched ( and removed )
but not started at this line but not started at this line
(need by auto indent) */ (need by auto indent) */
bool operator==(const SynRangeState& s2); bool operator==(const SynRangeState& s2);

View File

@ -389,7 +389,7 @@ void SynEditCppHighlighter::braceOpenProc()
} }
mRange.braceLevel += 1; mRange.braceLevel += 1;
mRange.leftBraces++; mRange.leftBraces++;
if (!mRange.indents.isEmpty() && mRange.indents.back() == StatementIndentType) { if (mRange.getLastIndent() == StatementIndentType) {
// if last indent is started by 'if' 'for' etc // if last indent is started by 'if' 'for' etc
// just replace it // just replace it
popIndents(StatementIndentType); popIndents(StatementIndentType);
@ -925,7 +925,7 @@ void SynEditCppHighlighter::semiColonProc()
mExtTokenId = ExtTokenKind::SemiColon; mExtTokenId = ExtTokenKind::SemiColon;
if (mRange.state == RangeState::rsAsm) if (mRange.state == RangeState::rsAsm)
mRange.state = RangeState::rsUnknown; mRange.state = RangeState::rsUnknown;
if (mRange.indents.back() == StatementIndentType) { if (mRange.getLastIndent() == StatementIndentType) {
popIndents(StatementIndentType); popIndents(StatementIndentType);
} }
} }
@ -1362,7 +1362,7 @@ void SynEditCppHighlighter::popIndents(QChar indentType)
if (!mRange.indents.isEmpty()) { if (!mRange.indents.isEmpty()) {
int idx = mRange.indents.length()-1; int idx = mRange.indents.length()-1;
if (idx < mRange.firstIndentThisLine) { if (idx < mRange.firstIndentThisLine) {
mRange.lastMatchingIndent = mRange.indents[idx]; mRange.matchingIndents.append(mRange.indents[idx]);
} }
mRange.indents.remove(idx,1); mRange.indents.remove(idx,1);
} }
@ -1531,7 +1531,7 @@ void SynEditCppHighlighter::setLine(const QString &newLine, int lineNumber)
mRange.leftBraces = 0; mRange.leftBraces = 0;
mRange.rightBraces = 0; mRange.rightBraces = 0;
mRange.firstIndentThisLine = mRange.indents.length(); mRange.firstIndentThisLine = mRange.indents.length();
mRange.lastMatchingIndent = QChar(); mRange.matchingIndents = "";
next(); next();
} }
@ -1599,7 +1599,7 @@ void SynEditCppHighlighter::setState(const SynRangeState& rangeState)
mRange.leftBraces = 0; mRange.leftBraces = 0;
mRange.rightBraces = 0; mRange.rightBraces = 0;
mRange.firstIndentThisLine = mRange.indents.length(); mRange.firstIndentThisLine = mRange.indents.length();
mRange.lastMatchingIndent = QChar(); mRange.matchingIndents = "";
} }
void SynEditCppHighlighter::resetState() void SynEditCppHighlighter::resetState()
@ -1613,7 +1613,7 @@ void SynEditCppHighlighter::resetState()
mRange.rightBraces = 0; mRange.rightBraces = 0;
mRange.indents = ""; mRange.indents = "";
mRange.firstIndentThisLine = 0; mRange.firstIndentThisLine = 0;
mRange.lastMatchingIndent = QChar(); mRange.matchingIndents = "";
mAsmStart = false; mAsmStart = false;
} }

View File

@ -982,7 +982,6 @@ void Settings::Editor::doSave()
// indents // indents
saveValue("auto_indent", mAutoIndent); saveValue("auto_indent", mAutoIndent);
saveValue("add_indent", mAddIndent);
saveValue("tab_to_spaces", mTabToSpaces); saveValue("tab_to_spaces", mTabToSpaces);
saveValue("tab_width", mTabWidth); saveValue("tab_width", mTabWidth);
saveValue("show_indent_lines", mShowIndentLines); saveValue("show_indent_lines", mShowIndentLines);
@ -1092,7 +1091,6 @@ void Settings::Editor::doLoad()
// indents // indents
mAutoIndent = boolValue("auto_indent", true); mAutoIndent = boolValue("auto_indent", true);
mAddIndent = boolValue("add_indent", true);
mTabToSpaces = boolValue("tab_to_spaces",false); mTabToSpaces = boolValue("tab_to_spaces",false);
mTabWidth = intValue("tab_width",4); mTabWidth = intValue("tab_width",4);
mShowIndentLines = boolValue("show_indent_lines",true); mShowIndentLines = boolValue("show_indent_lines",true);
@ -1281,16 +1279,6 @@ void Settings::Editor::setTabToSpaces(bool tabToSpaces)
mTabToSpaces = tabToSpaces; mTabToSpaces = tabToSpaces;
} }
bool Settings::Editor::addIndent() const
{
return mAddIndent;
}
void Settings::Editor::setAddIndent(bool addIndent)
{
mAddIndent = addIndent;
}
Settings::CompilerSet::CompilerSet(const QString& compilerFolder): Settings::CompilerSet::CompilerSet(const QString& compilerFolder):
mAutoAddCharsetParams(true), mAutoAddCharsetParams(true),
mStaticLink(true) mStaticLink(true)

View File

@ -340,7 +340,6 @@ public:
//General //General
// indents // indents
bool mAutoIndent; bool mAutoIndent;
bool mAddIndent;
bool mTabToSpaces; bool mTabToSpaces;
int mTabWidth; int mTabWidth;
bool mShowIndentLines; bool mShowIndentLines;

View File

@ -38,7 +38,6 @@ void EditorGeneralWidget::doLoad()
{ {
pSettings->editor().load(); pSettings->editor().load();
//indents //indents
ui->chkAddIndent->setChecked(pSettings->editor().addIndent());
ui->chkAutoIndent->setChecked(pSettings->editor().autoIndent()); ui->chkAutoIndent->setChecked(pSettings->editor().autoIndent());
ui->chkTabToSpaces->setChecked(pSettings->editor().tabToSpaces()); ui->chkTabToSpaces->setChecked(pSettings->editor().tabToSpaces());
ui->spTabWidth->setValue(pSettings->editor().tabWidth()); ui->spTabWidth->setValue(pSettings->editor().tabWidth());
@ -70,7 +69,6 @@ void EditorGeneralWidget::doLoad()
void EditorGeneralWidget::doSave() void EditorGeneralWidget::doSave()
{ {
//indents //indents
pSettings->editor().setAddIndent(ui->chkAddIndent->isChecked());
pSettings->editor().setAutoIndent(ui->chkAutoIndent->isChecked()); pSettings->editor().setAutoIndent(ui->chkAutoIndent->isChecked());
pSettings->editor().setTabToSpaces(ui->chkTabToSpaces->isChecked()); pSettings->editor().setTabToSpaces(ui->chkTabToSpaces->isChecked());
pSettings->editor().setTabWidth(ui->spTabWidth->value()); pSettings->editor().setTabWidth(ui->spTabWidth->value());

View File

@ -27,13 +27,6 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QCheckBox" name="chkAddIndent">
<property name="text">
<string>Add Indent after { and :</string>
</property>
</widget>
</item>
<item> <item>
<widget class="QCheckBox" name="chkTabToSpaces"> <widget class="QCheckBox" name="chkTabToSpaces">
<property name="text"> <property name="text">