- fix: multi-line comments indents calculation

This commit is contained in:
royqh1979 2021-11-07 01:07:41 +08:00
parent 6e4afe83c0
commit 4d48cca3de
3 changed files with 67 additions and 4 deletions

View File

@ -6,12 +6,12 @@ Version 0.8.1 For Dev-C++ 7 Beta
- enhancement: when problem from competitive companion received, show the problem and problem set views. - enhancement: when problem from competitive companion received, show the problem and problem set views.
- enhancement: set problem's answer source file - enhancement: set problem's answer source file
- enhancement: open the problem's answer source file in editor - enhancement: open the problem's answer source file in editor
- fix: if the proceeding line is a comment, current line should not recalculate indent
- fix: if the proceeding line ends with ':' in comments, current line should not indent - fix: if the proceeding line ends with ':' in comments, current line should not indent
- enhancement: right click the problem set name label to rename it - enhancement: right click the problem set name label to rename it
- change: memory view and locals view use debug console's font settings - change: memory view and locals view use debug console's font settings
- fix: one line 'while' statement dosen't correctly indents - fix: one line 'while' statement dosen't correctly indents
- fix: line start with '{' that follow an un-ended 'if'/'for' statement is not correctly un-indented - fix: line start with '{' that follow an un-ended 'if'/'for' statement is not correctly un-indented
- fix: multi-line comments indents calculation
Version 0.8 For Dev-C++ 7 Beta Version 0.8 For Dev-C++ 7 Beta
- fix: find in the current file is not correcly saved in the search history - fix: find in the current file is not correcly saved in the search history

View File

@ -607,11 +607,15 @@ void Editor::keyPressEvent(QKeyEvent *event)
s=TrimLeft(lineText()); s=TrimLeft(lineText());
if (s.startsWith("* ")) { if (s.startsWith("* ")) {
handled = true; handled = true;
int right = lines()->getString(caretY()-1).length()-caretX();
s=lineBreak()+"* "; s=lineBreak()+"* ";
insertString(s,false); insertString(s,false);
BufferCoord p = caretXY(); BufferCoord p = caretXY();
p.Line++; p.Line++;
p.Char = lines()->getString(p.Line-1).length()+1; p.Char = lines()->getString(p.Line-1).length()+1;
if (right>0) {
p.Char -=right+1;
}
setCaretXY(p); setCaretXY(p);
} }
} }

View File

@ -1418,9 +1418,7 @@ int SynEdit::calcIndentSpaces(int line, const QString& lineText, bool addIndent)
indentSpaces = leftSpaces(s); indentSpaces = leftSpaces(s);
SynRangeState rangePreceeding = mLines->ranges(startLine-1); SynRangeState rangePreceeding = mLines->ranges(startLine-1);
mHighlighter->setState(rangePreceeding); mHighlighter->setState(rangePreceeding);
if (addIndent if (addIndent) {
&& !mHighlighter->isLastLineCommentNotFinished(rangePreceeding.state)
&& !mHighlighter->isLastLineStringNotFinished(rangePreceeding.state)) {
mHighlighter->setLine(lineText.trimmed(),line-1); mHighlighter->setLine(lineText.trimmed(),line-1);
SynRangeState rangeAfterFirstToken = mHighlighter->getRangeState(); SynRangeState rangeAfterFirstToken = mHighlighter->getRangeState();
QString firstToken = mHighlighter->getToken(); QString firstToken = mHighlighter->getToken();
@ -1430,6 +1428,7 @@ int SynEdit::calcIndentSpaces(int line, const QString& lineText, bool addIndent)
&& ( && (
firstToken == "public" || firstToken == "private" firstToken == "public" || firstToken == "private"
|| firstToken == "protected" || firstToken == "case")) { || firstToken == "protected" || firstToken == "case")) {
// public: private: protecte: case: should indents like it's parent statement
mHighlighter->setState(rangePreceeding); mHighlighter->setState(rangePreceeding);
mHighlighter->setLine("}",line-1); mHighlighter->setLine("}",line-1);
rangeAfterFirstToken = mHighlighter->getRangeState(); rangeAfterFirstToken = mHighlighter->getRangeState();
@ -1437,14 +1436,71 @@ int SynEdit::calcIndentSpaces(int line, const QString& lineText, bool addIndent)
attr = mHighlighter->getTokenAttribute(); attr = mHighlighter->getTokenAttribute();
} }
bool dontAddIndent = false; bool dontAddIndent = false;
bool addOwnIndent = false;
QVector<int> matchingIndents; QVector<int> matchingIndents;
int l; int l;
if (attr == mHighlighter->symbolAttribute() if (attr == mHighlighter->symbolAttribute()
&& (firstToken == '}' || firstToken == '{')) { && (firstToken == '}' || firstToken == '{')) {
// current line starts with '}' or '{', we should consider them to calc indents
matchingIndents = rangeAfterFirstToken.matchingIndents; matchingIndents = rangeAfterFirstToken.matchingIndents;
dontAddIndent = true; dontAddIndent = true;
l = startLine; l = startLine;
} else if (mHighlighter->isLastLineCommentNotFinished(rangePreceeding.state)
&& (lineText.startsWith(' ')
|| lineText.startsWith('\t'))
) {
// last line is a not finished comment, and this line start with indents
// we should use indents of the line comment beginning, plus this line's indents
addOwnIndent=true;
int commentStartLine = startLine-1;
SynRangeState range;
while (commentStartLine>=1) {
range = mLines->ranges(commentStartLine-1);
if (!mHighlighter->isLastLineCommentNotFinished(range.state)){
commentStartLine++;
break;
}
if (!range.matchingIndents.isEmpty()
|| range.firstIndentThisLine<range.indents.length())
break;
commentStartLine--;
}
if (commentStartLine<1)
commentStartLine = 1;
indentSpaces = leftSpaces(mLines->getString(commentStartLine-1));
range = mLines->ranges(commentStartLine-1);
matchingIndents = range.matchingIndents;
dontAddIndent = true;
l = commentStartLine;
} else if (mHighlighter->isLastLineStringNotFinished(rangePreceeding.state)
&& (lineText.startsWith(' ')
|| lineText.startsWith('\t'))
) {
// last line is a not finished string, and this line start with indents
// we should use indents of the line string beginning, plus this line's indents
addOwnIndent=true;
int commentStartLine = startLine-1;
SynRangeState range;
while (commentStartLine>=1) {
range = mLines->ranges(commentStartLine-1);
if (!mHighlighter->isLastLineStringNotFinished(range.state)){
commentStartLine++;
break;
}
if (!range.matchingIndents.isEmpty()
|| range.firstIndentThisLine<range.indents.length())
break;
commentStartLine--;
}
if (commentStartLine<1)
commentStartLine = 1;
indentSpaces = leftSpaces(mLines->getString(commentStartLine-1));
range = mLines->ranges(commentStartLine-1);
matchingIndents = range.matchingIndents;
dontAddIndent = true;
l = commentStartLine;
} else { } else {
// we just use infos till preceeding line's end to calc indents
matchingIndents = rangePreceeding.matchingIndents; matchingIndents = rangePreceeding.matchingIndents;
l = startLine-1; l = startLine-1;
} }
@ -1508,6 +1564,9 @@ int SynEdit::calcIndentSpaces(int line, const QString& lineText, bool addIndent)
dontAddIndent = true; dontAddIndent = true;
} }
} }
if (addOwnIndent) {
indentSpaces += leftSpaces(lineText);
}
} }
} }
return std::max(0,indentSpaces); return std::max(0,indentSpaces);