Skip to content
Snippets Groups Projects
Commit 1a61ae01 authored by Christian Kamm's avatar Christian Kamm
Browse files

C++ indenter: Respect user indent with stream operators.

Reviewed-by: Erik Verbruggen
parent 9b24e0ae
No related branches found
No related tags found
No related merge requests found
...@@ -203,9 +203,17 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block) ...@@ -203,9 +203,17 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block)
} break; } break;
case stream_op: case stream_op:
if (tryExpression()) case stream_op_cont:
if (kind != T_LESS_LESS && kind != T_GREATER_GREATER && tryExpression())
break; break;
switch (kind) { switch (kind) {
case T_LESS_LESS:
case T_GREATER_GREATER:
if (m_currentState.top().type == stream_op)
enter(stream_op_cont);
else // stream_op_cont already
turnInto(stream_op_cont);
break;
case T_COMMA: case T_COMMA:
case T_SEMICOLON: leave(); continue; // always nested, propagate semicolon case T_SEMICOLON: leave(); continue; // always nested, propagate semicolon
} break; } break;
...@@ -604,12 +612,10 @@ bool CodeFormatter::tryExpression(bool alsoExpression) ...@@ -604,12 +612,10 @@ bool CodeFormatter::tryExpression(bool alsoExpression)
case T_LESS_LESS: case T_LESS_LESS:
case T_GREATER_GREATER: case T_GREATER_GREATER:
// don't go into stream operator state inside arglist_open
// or another stream_op
newState = stream_op; newState = stream_op;
for (int i = m_currentState.size() - 1; i >= 0; --i) { for (int i = m_currentState.size() - 1; i >= 0; --i) {
const int type = m_currentState.at(i).type; const int type = m_currentState.at(i).type;
if (type == arglist_open || type == stream_op) { if (type == arglist_open) { // likely a left-shift instead
newState = -1; newState = -1;
break; break;
} }
...@@ -1020,6 +1026,10 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd ...@@ -1020,6 +1026,10 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd
case stream_op: case stream_op:
*indentDepth = tokenPosition + tk.length() + 1; *indentDepth = tokenPosition + tk.length() + 1;
break; break;
case stream_op_cont:
if (firstToken)
*savedIndentDepth = *indentDepth = tokenPosition + tk.length() + 1;
break;
case member_init_open: case member_init_open:
// undo the continuation indent of the parent // undo the continuation indent of the parent
...@@ -1257,7 +1267,7 @@ void QtStyleCodeFormatter::adjustIndent(const QList<CPlusPlus::Token> &tokens, i ...@@ -1257,7 +1267,7 @@ void QtStyleCodeFormatter::adjustIndent(const QList<CPlusPlus::Token> &tokens, i
break; break;
case T_LESS_LESS: case T_LESS_LESS:
case T_GREATER_GREATER: case T_GREATER_GREATER:
if (topState.type == stream_op) if (topState.type == stream_op || topState.type == stream_op_cont)
*indentDepth -= 3; // to align << with << *indentDepth -= 3; // to align << with <<
break; break;
case T_COMMENT: case T_COMMENT:
......
...@@ -149,7 +149,8 @@ protected: ...@@ -149,7 +149,8 @@ protected:
substatement_open, // The brace that opens a substatement block. substatement_open, // The brace that opens a substatement block.
arglist_open, // after the lparen. TODO: check if this is enough. arglist_open, // after the lparen. TODO: check if this is enough.
stream_op, // Lines continuing a stream operator (C++ only). stream_op, // After a '<<' or '>>' in a context where it's likely a stream operator.
stream_op_cont, // When finding another stream operator in stream_op
ternary_op, // The ? : operator ternary_op, // The ? : operator
condition_open, // Start of a condition in 'if', 'while', entered after opening paren condition_open, // Start of a condition in 'if', 'while', entered after opening paren
......
...@@ -44,6 +44,7 @@ private Q_SLOTS: ...@@ -44,6 +44,7 @@ private Q_SLOTS:
void whitesmithsStyle(); void whitesmithsStyle();
void singleLineEnum(); void singleLineEnum();
void functionReturnType(); void functionReturnType();
void streamOp();
}; };
struct Line { struct Line {
...@@ -786,6 +787,27 @@ void tst_CodeFormatter::functionReturnType() ...@@ -786,6 +787,27 @@ void tst_CodeFormatter::functionReturnType()
checkIndent(data); checkIndent(data);
} }
void tst_CodeFormatter::streamOp()
{
QList<Line> data;
data
<< Line("void foo () {")
<< Line(" qDebug() << foo")
<< Line(" << bar << moose")
<< Line(" << bar +")
<< Line(" foo - blah(1)")
<< Line(" << '?'")
<< Line(" << \"\\n\";")
<< Line(" qDebug() << foo")
<< Line(" << bar << moose", 13)
<< Line(" << bar +")
<< Line(" foo - blah(1)")
<< Line(" << '?'")
<< Line(" << \"\\n\";")
;
checkIndent(data);
}
QTEST_APPLESS_MAIN(tst_CodeFormatter) QTEST_APPLESS_MAIN(tst_CodeFormatter)
#include "tst_codeformatter.moc" #include "tst_codeformatter.moc"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment