Skip to content
Snippets Groups Projects
Commit 040ce975 authored by Roberto Raggi's avatar Roberto Raggi
Browse files

Split or conditions.

parent a3580db7
No related branches found
No related tags found
No related merge requests found
...@@ -152,6 +152,16 @@ private: ...@@ -152,6 +152,16 @@ private:
if (something_else) { if (something_else) {
} }
} }
and
if (something || something_else)
x;
with
if (something)
x;
else if (something_else)
x;
*/ */
class SplitIfStatementOp: public QuickFixOperation class SplitIfStatementOp: public QuickFixOperation
{ {
...@@ -174,7 +184,8 @@ public: ...@@ -174,7 +184,8 @@ public:
if (statement->match(pattern, &matcher) if (statement->match(pattern, &matcher)
&& pattern->statement && pattern->statement
&& pattern->rparen_token && pattern->rparen_token
&& tokenAt(condition->binary_op_token).is(T_AMPER_AMPER)) && (tokenAt(condition->binary_op_token).is(T_AMPER_AMPER) ||
tokenAt(condition->binary_op_token).is(T_PIPE_PIPE)))
return true; return true;
return false; return false;
...@@ -182,9 +193,16 @@ public: ...@@ -182,9 +193,16 @@ public:
virtual void apply() virtual void apply()
{ {
StatementAST *ifTrueStatement = pattern->statement; const Token binaryOp = tokenAt(condition->binary_op_token);
CompoundStatementAST *compoundStatement = ifTrueStatement->asCompoundStatement();
if (binaryOp.is(T_AMPER_AMPER))
splitAndCondition();
else
splitOrCondition();
}
void splitAndCondition()
{
QTextCursor completeIfStatement = selectNode(pattern); QTextCursor completeIfStatement = selectNode(pattern);
{ {
// ### HACK // ### HACK
...@@ -194,6 +212,9 @@ public: ...@@ -194,6 +212,9 @@ public:
completeIfStatement.setPosition(anchor, QTextCursor::KeepAnchor); completeIfStatement.setPosition(anchor, QTextCursor::KeepAnchor);
} }
StatementAST *ifTrueStatement = pattern->statement;
CompoundStatementAST *compoundStatement = ifTrueStatement->asCompoundStatement();
// take the right-expression from the condition. // take the right-expression from the condition.
const QString rightCondition = selectNode(condition->right_expression).selectedText(); const QString rightCondition = selectNode(condition->right_expression).selectedText();
replace(endOf(condition->left_expression), startOf(pattern->rparen_token), QString()); replace(endOf(condition->left_expression), startOf(pattern->rparen_token), QString());
...@@ -227,6 +248,50 @@ public: ...@@ -227,6 +248,50 @@ public:
tc.endEditBlock(); tc.endEditBlock();
} }
void splitOrCondition()
{
QTextCursor completeIfStatement = selectNode(pattern);
{
// ### HACK
const int anchor = completeIfStatement.anchor();
const int position = completeIfStatement.position();
completeIfStatement.setPosition(position);
completeIfStatement.setPosition(anchor, QTextCursor::KeepAnchor);
}
StatementAST *ifTrueStatement = pattern->statement;
CompoundStatementAST *compoundStatement = ifTrueStatement->asCompoundStatement();
// take the right-expression from the condition.
const QString rightCondition = selectNode(condition->right_expression).selectedText();
replace(endOf(condition->left_expression), startOf(pattern->rparen_token), QString());
// copy the if-body
QTextCursor bodyCursor = textCursor();
bodyCursor.setPosition(endOf(pattern->rparen_token));
bodyCursor.setPosition(endOf(pattern->statement), QTextCursor::KeepAnchor);
const QString body = bodyCursor.selectedText();
QString elseIfStatement;
if (compoundStatement)
elseIfStatement += QLatin1String(" ");
else
elseIfStatement += QLatin1String("\n");
elseIfStatement += QLatin1String("else if (");
elseIfStatement += rightCondition;
elseIfStatement += QLatin1String(")");
elseIfStatement += body;
insert(endOf(pattern), elseIfStatement);
QTextCursor tc = textCursor();
tc.beginEditBlock();
execute();
editor->indentInsertedText(completeIfStatement);
tc.endEditBlock();
}
private: private:
ASTMatcher matcher; ASTMatcher matcher;
ASTPatternBuilder mk; ASTPatternBuilder mk;
......
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