Skip to content

Commit

Permalink
- fix: multi-line comments indents calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
royqh1979 committed Nov 6, 2021
1 parent 6e4afe8 commit 4d48cca
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: set problem's answer source file
- 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
- enhancement: right click the problem set name label to rename it
- change: memory view and locals view use debug console's font settings
- 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: multi-line comments indents calculation

Version 0.8 For Dev-C++ 7 Beta
- fix: find in the current file is not correcly saved in the search history
Expand Down
4 changes: 4 additions & 0 deletions RedPandaIDE/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -607,11 +607,15 @@ void Editor::keyPressEvent(QKeyEvent *event)
s=TrimLeft(lineText());
if (s.startsWith("* ")) {
handled = true;
int right = lines()->getString(caretY()-1).length()-caretX();
s=lineBreak()+"* ";
insertString(s,false);
BufferCoord p = caretXY();
p.Line++;
p.Char = lines()->getString(p.Line-1).length()+1;
if (right>0) {
p.Char -=right+1;
}
setCaretXY(p);
}
}
Expand Down
65 changes: 62 additions & 3 deletions RedPandaIDE/qsynedit/SynEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1418,9 +1418,7 @@ int SynEdit::calcIndentSpaces(int line, const QString& lineText, bool addIndent)
indentSpaces = leftSpaces(s);
SynRangeState rangePreceeding = mLines->ranges(startLine-1);
mHighlighter->setState(rangePreceeding);
if (addIndent
&& !mHighlighter->isLastLineCommentNotFinished(rangePreceeding.state)
&& !mHighlighter->isLastLineStringNotFinished(rangePreceeding.state)) {
if (addIndent) {
mHighlighter->setLine(lineText.trimmed(),line-1);
SynRangeState rangeAfterFirstToken = mHighlighter->getRangeState();
QString firstToken = mHighlighter->getToken();
Expand All @@ -1430,21 +1428,79 @@ int SynEdit::calcIndentSpaces(int line, const QString& lineText, bool addIndent)
&& (
firstToken == "public" || firstToken == "private"
|| firstToken == "protected" || firstToken == "case")) {
// public: private: protecte: case: should indents like it's parent statement
mHighlighter->setState(rangePreceeding);
mHighlighter->setLine("}",line-1);
rangeAfterFirstToken = mHighlighter->getRangeState();
firstToken = mHighlighter->getToken();
attr = mHighlighter->getTokenAttribute();
}
bool dontAddIndent = false;
bool addOwnIndent = false;
QVector<int> matchingIndents;
int l;
if (attr == mHighlighter->symbolAttribute()
&& (firstToken == '}' || firstToken == '{')) {
// current line starts with '}' or '{', we should consider them to calc indents
matchingIndents = rangeAfterFirstToken.matchingIndents;
dontAddIndent = true;
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 {
// we just use infos till preceeding line's end to calc indents
matchingIndents = rangePreceeding.matchingIndents;
l = startLine-1;
}
Expand Down Expand Up @@ -1508,6 +1564,9 @@ int SynEdit::calcIndentSpaces(int line, const QString& lineText, bool addIndent)
dontAddIndent = true;
}
}
if (addOwnIndent) {
indentSpaces += leftSpaces(lineText);
}
}
}
return std::max(0,indentSpaces);
Expand Down

0 comments on commit 4d48cca

Please sign in to comment.