better auto indent calculation for '{' after if/for statement
This commit is contained in:
parent
bb5d1b706c
commit
ba538c9b13
|
@ -1463,6 +1463,7 @@ int SynEdit::calcIndentSpaces(int line, const QString& lineText, bool addIndent)
|
||||||
}
|
}
|
||||||
int indentSpaces = 0;
|
int indentSpaces = 0;
|
||||||
if (startLine>=1) {
|
if (startLine>=1) {
|
||||||
|
//calculate the indents of last statement;
|
||||||
indentSpaces = leftSpaces(startLineText);
|
indentSpaces = leftSpaces(startLineText);
|
||||||
SynRangeState rangePreceeding = mLines->ranges(startLine-1);
|
SynRangeState rangePreceeding = mLines->ranges(startLine-1);
|
||||||
mHighlighter->setState(rangePreceeding);
|
mHighlighter->setState(rangePreceeding);
|
||||||
|
@ -1491,20 +1492,27 @@ int SynEdit::calcIndentSpaces(int line, const QString& lineText, bool addIndent)
|
||||||
firstToken = mHighlighter->getToken();
|
firstToken = mHighlighter->getToken();
|
||||||
attr = mHighlighter->getTokenAttribute();
|
attr = mHighlighter->getTokenAttribute();
|
||||||
}
|
}
|
||||||
bool dontAddIndent = false;
|
bool indentAdded = false;
|
||||||
int additionIndent = 0;
|
int additionIndent = 0;
|
||||||
QVector<int> matchingIndents;
|
QVector<int> matchingIndents;
|
||||||
int l;
|
int l;
|
||||||
if (attr == mHighlighter->symbolAttribute()
|
if (attr == mHighlighter->symbolAttribute()
|
||||||
&& (firstToken == '}' || firstToken == '{')) {
|
&& (firstToken == '}')) {
|
||||||
// current line starts with '}' or '{', we should consider them to calc indents
|
// current line starts with '}', we should consider it to calc indents
|
||||||
matchingIndents = rangeAfterFirstToken.matchingIndents;
|
matchingIndents = rangeAfterFirstToken.matchingIndents;
|
||||||
dontAddIndent = true;
|
indentAdded = true;
|
||||||
|
l = startLine;
|
||||||
|
} else if (attr == mHighlighter->symbolAttribute()
|
||||||
|
&& (firstToken == '{')
|
||||||
|
&& (rangePreceeding.getLastIndent()==sitStatement)) {
|
||||||
|
// current line starts with '{' and last statement not finished, we should consider it to calc indents
|
||||||
|
matchingIndents = rangeAfterFirstToken.matchingIndents;
|
||||||
|
indentAdded = true;
|
||||||
l = startLine;
|
l = startLine;
|
||||||
} else if (mHighlighter->getClass() == SynHighlighterClass::CppHighlighter
|
} else if (mHighlighter->getClass() == SynHighlighterClass::CppHighlighter
|
||||||
&& trimmedLineText.startsWith('#')
|
&& trimmedLineText.startsWith('#')
|
||||||
&& attr == ((SynEditCppHighlighter *)mHighlighter.get())->preprocessorAttribute()) {
|
&& attr == ((SynEditCppHighlighter *)mHighlighter.get())->preprocessorAttribute()) {
|
||||||
dontAddIndent = true;
|
indentAdded = true;
|
||||||
indentSpaces=0;
|
indentSpaces=0;
|
||||||
l=0;
|
l=0;
|
||||||
} else if (mHighlighter->getClass() == SynHighlighterClass::CppHighlighter
|
} else if (mHighlighter->getClass() == SynHighlighterClass::CppHighlighter
|
||||||
|
@ -1520,7 +1528,7 @@ int SynEdit::calcIndentSpaces(int line, const QString& lineText, bool addIndent)
|
||||||
indentSpaces = leftSpaces(mLines->getString(commentStartLine-1));
|
indentSpaces = leftSpaces(mLines->getString(commentStartLine-1));
|
||||||
range = mLines->ranges(commentStartLine-1);
|
range = mLines->ranges(commentStartLine-1);
|
||||||
matchingIndents = range.matchingIndents;
|
matchingIndents = range.matchingIndents;
|
||||||
dontAddIndent = true;
|
indentAdded = true;
|
||||||
l = commentStartLine;
|
l = commentStartLine;
|
||||||
} else if ( mHighlighter->isLastLineCommentNotFinished(statePrePre)
|
} else if ( mHighlighter->isLastLineCommentNotFinished(statePrePre)
|
||||||
&& rangePreceeding.matchingIndents.isEmpty()
|
&& rangePreceeding.matchingIndents.isEmpty()
|
||||||
|
@ -1533,7 +1541,7 @@ int SynEdit::calcIndentSpaces(int line, const QString& lineText, bool addIndent)
|
||||||
indentSpaces = leftSpaces(mLines->getString(commentStartLine-1));
|
indentSpaces = leftSpaces(mLines->getString(commentStartLine-1));
|
||||||
range = mLines->ranges(commentStartLine-1);
|
range = mLines->ranges(commentStartLine-1);
|
||||||
matchingIndents = range.matchingIndents;
|
matchingIndents = range.matchingIndents;
|
||||||
dontAddIndent = true;
|
indentAdded = true;
|
||||||
l = commentStartLine;
|
l = commentStartLine;
|
||||||
} else {
|
} else {
|
||||||
// we just use infos till preceeding line's end to calc indents
|
// we just use infos till preceeding line's end to calc indents
|
||||||
|
@ -1580,14 +1588,14 @@ int SynEdit::calcIndentSpaces(int line, const QString& lineText, bool addIndent)
|
||||||
l--;
|
l--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!dontAddIndent) {
|
if (!indentAdded) {
|
||||||
if (rangePreceeding.firstIndentThisLine < rangePreceeding.indents.length()) {
|
if (rangePreceeding.firstIndentThisLine < rangePreceeding.indents.length()) {
|
||||||
indentSpaces += mTabWidth;
|
indentSpaces += mTabWidth;
|
||||||
dontAddIndent = true;
|
indentAdded = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!dontAddIndent && !startLineText.isEmpty()) {
|
if (!indentAdded && !startLineText.isEmpty()) {
|
||||||
BufferCoord coord;
|
BufferCoord coord;
|
||||||
QString token;
|
QString token;
|
||||||
PSynHighlighterAttribute attr;
|
PSynHighlighterAttribute attr;
|
||||||
|
@ -1597,7 +1605,7 @@ int SynEdit::calcIndentSpaces(int line, const QString& lineText, bool addIndent)
|
||||||
&& attr == mHighlighter->symbolAttribute()
|
&& attr == mHighlighter->symbolAttribute()
|
||||||
&& token == ":") {
|
&& token == ":") {
|
||||||
indentSpaces += mTabWidth;
|
indentSpaces += mTabWidth;
|
||||||
dontAddIndent = true;
|
indentAdded = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
indentSpaces += additionIndent;
|
indentSpaces += additionIndent;
|
||||||
|
|
|
@ -516,8 +516,10 @@ private:
|
||||||
void setSelWord();
|
void setSelWord();
|
||||||
void setWordBlock(BufferCoord Value);
|
void setWordBlock(BufferCoord Value);
|
||||||
|
|
||||||
int findCommentStartLine(int searchStartLine);
|
|
||||||
int calcIndentSpaces(int line, const QString& lineText, bool addIndent);
|
int calcIndentSpaces(int line, const QString& lineText, bool addIndent);
|
||||||
|
int findCommentStartLine(int searchStartLine);
|
||||||
|
int findStatementStartLine(int searchStartLine);
|
||||||
|
|
||||||
void processGutterClick(QMouseEvent* event);
|
void processGutterClick(QMouseEvent* event);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue