Commit 36913baf authored by Christian Kamm's avatar Christian Kamm
Browse files

Quickfix: Rewrite existing code to use the new edit operations.

parent 049f8d53
...@@ -142,8 +142,9 @@ public: ...@@ -142,8 +142,9 @@ public:
{ {
setTopLevelNode(pattern); setTopLevelNode(pattern);
replace(pattern->binary_op_token, QLatin1String("||")); replace(pattern->binary_op_token, QLatin1String("||"));
replace(left->unary_op_token, QLatin1String("!(")); remove(left->unary_op_token);
replace(right->unary_op_token, QLatin1String("")); remove(right->unary_op_token);
insert(startOf(pattern), QLatin1String("!("));
insert(endOf(pattern), QLatin1String(")")); insert(endOf(pattern), QLatin1String(")"));
} }
...@@ -232,23 +233,25 @@ public: ...@@ -232,23 +233,25 @@ public:
{ {
setTopLevelNode(declaration); setTopLevelNode(declaration);
SpecifierListAST *specifiers = declaration->decl_specifier_list; SpecifierListAST *specifiers = declaration->decl_specifier_list;
const QString declSpecifiers = textOf(startOf(specifiers->firstToken()), endOf(specifiers->lastToken() - 1)); int declSpecifiersStart = startOf(specifiers->firstToken());
int declSpecifiersEnd = endOf(specifiers->lastToken() - 1);
int insertPos = endOf(declaration->semicolon_token);
DeclaratorAST *declarator = declaration->declarator_list->value; DeclaratorAST *prevDeclarator = declaration->declarator_list->value;
replace(endOf(declarator), startOf(declaration->semicolon_token), QString());
QString text;
for (DeclaratorListAST *it = declaration->declarator_list->next; it; it = it->next) { for (DeclaratorListAST *it = declaration->declarator_list->next; it; it = it->next) {
DeclaratorAST *declarator = it->value; DeclaratorAST *declarator = it->value;
text += QLatin1Char('\n'); insert(insertPos, QLatin1String("\n"));
text += declSpecifiers; copy(declSpecifiersStart, declSpecifiersEnd, insertPos);
text += QLatin1Char(' '); insert(insertPos, QLatin1String(" "));
text += textOf(declarator); move(declarator, insertPos);
text += QLatin1String(";"); insert(insertPos, QLatin1String(";"));
}
remove(endOf(prevDeclarator), startOf(declarator));
insert(endOf(declaration->semicolon_token), text); prevDeclarator = declarator;
}
} }
private: private:
...@@ -356,13 +359,12 @@ public: ...@@ -356,13 +359,12 @@ public:
virtual void createChangeSet() virtual void createChangeSet()
{ {
setTopLevelNode(pattern); setTopLevelNode(pattern);
const QString name = textOf(core);
QString declaration = textOf(condition);
declaration += QLatin1String(";\n");
insert(startOf(pattern), declaration); copy(core, startOf(condition));
insert(endOf(pattern->lparen_token), name);
replace(condition, name); int insertPos = startOf(pattern);
move(condition, insertPos);
insert(insertPos, QLatin1String(";\n"));
} }
private: private:
...@@ -427,21 +429,14 @@ public: ...@@ -427,21 +429,14 @@ public:
virtual void createChangeSet() virtual void createChangeSet()
{ {
setTopLevelNode(pattern); setTopLevelNode(pattern);
const QString name = textOf(core);
const QString initializer = textOf(condition->declarator->initializer); insert(startOf(condition), QLatin1String("("));
QString declaration = textOf(startOf(condition), endOf(condition->declarator->equals_token - 1)); insert(endOf(condition), QLatin1String(") != 0"));
declaration += QLatin1String(";\n");
int insertPos = startOf(pattern);
QString newCondition; move(startOf(condition), startOf(core), insertPos);
newCondition += QLatin1String("("); copy(core, insertPos);
newCondition += name; insert(insertPos, QLatin1String(";\n"));
newCondition += QLatin1String(" = ");
newCondition += initializer;
newCondition += QLatin1String(") != 0");
insert(startOf(pattern), declaration);
insert(endOf(pattern->lparen_token), name);
replace(condition, newCondition);
} }
private: private:
...@@ -533,56 +528,34 @@ public: ...@@ -533,56 +528,34 @@ public:
void splitAndCondition() void splitAndCondition()
{ {
setTopLevelNode(pattern); setTopLevelNode(pattern);
StatementAST *ifTrueStatement = pattern->statement;
// take the right-expression from the condition. int startPos = startOf(pattern);
QTextCursor rightCursor = textCursor(); insert(startPos, QLatin1String("if ("));
rightCursor.setPosition(startOf(condition->right_expression)); move(condition->left_expression, startPos);
rightCursor.setPosition(endOf(pattern->rparen_token - 1), QTextCursor::KeepAnchor); insert(startPos, QLatin1String(") {\n"));
const QString rightCondition = rightCursor.selectedText();
replace(endOf(condition->left_expression), startOf(pattern->rparen_token), QString()); remove(endOf(condition->left_expression), startOf(condition->right_expression));
insert(endOf(pattern), QLatin1String("\n}"));
// create the nested if statement
QString nestedIfStatement;
nestedIfStatement += QLatin1String(" {\nif ("); // open new compound statement for outer
nestedIfStatement += rightCondition;
nestedIfStatement += QLatin1String(")");
insert(endOf(pattern->rparen_token), nestedIfStatement);
insert(endOf(ifTrueStatement), "\n}"); // finish the compound statement
} }
void splitOrCondition() void splitOrCondition()
{ {
setTopLevelNode(pattern);
StatementAST *ifTrueStatement = pattern->statement; StatementAST *ifTrueStatement = pattern->statement;
CompoundStatementAST *compoundStatement = ifTrueStatement->asCompoundStatement(); CompoundStatementAST *compoundStatement = ifTrueStatement->asCompoundStatement();
// take the right-expression from the condition. setTopLevelNode(pattern);
QTextCursor rightCursor = textCursor();
rightCursor.setPosition(startOf(condition->right_expression));
rightCursor.setPosition(endOf(pattern->rparen_token - 1), QTextCursor::KeepAnchor);
const QString rightCondition = rightCursor.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; int insertPos = endOf(pattern);
if (compoundStatement) if (compoundStatement)
elseIfStatement += QLatin1String(" "); insert(insertPos, QLatin1String(" "));
else else
elseIfStatement += QLatin1String("\n"); insert(insertPos, QLatin1String("\n"));
insert(insertPos, QLatin1String("else if ("));
elseIfStatement += QLatin1String("else if ("); move(startOf(condition->right_expression), startOf(pattern->rparen_token), insertPos);
elseIfStatement += rightCondition; insert(insertPos, QLatin1String(")"));
elseIfStatement += QLatin1String(")"); copy(endOf(pattern->rparen_token), endOf(pattern->statement), insertPos);
elseIfStatement += body;
remove(endOf(condition->left_expression), startOf(condition->right_expression));
insert(endOf(pattern), elseIfStatement);
} }
private: private:
...@@ -712,8 +685,7 @@ void QuickFixOperation::reindent(const Range &range) ...@@ -712,8 +685,7 @@ void QuickFixOperation::reindent(const Range &range)
void QuickFixOperation::move(int start, int end, int to) void QuickFixOperation::move(int start, int end, int to)
{ {
if (end > start) _changeSet.move(start, end-start, to);
_changeSet.move(start, end-start, to);
} }
void QuickFixOperation::move(unsigned tokenIndex, int to) void QuickFixOperation::move(unsigned tokenIndex, int to)
...@@ -728,8 +700,7 @@ void QuickFixOperation::move(const CPlusPlus::AST *ast, int to) ...@@ -728,8 +700,7 @@ void QuickFixOperation::move(const CPlusPlus::AST *ast, int to)
void QuickFixOperation::replace(int start, int end, const QString &replacement) void QuickFixOperation::replace(int start, int end, const QString &replacement)
{ {
if (end >= start) _changeSet.replace(start, end-start, replacement);
_changeSet.replace(start, end-start, replacement);
} }
void QuickFixOperation::replace(unsigned tokenIndex, const QString &replacement) void QuickFixOperation::replace(unsigned tokenIndex, const QString &replacement)
...@@ -744,7 +715,37 @@ void QuickFixOperation::replace(const CPlusPlus::AST *ast, const QString &replac ...@@ -744,7 +715,37 @@ void QuickFixOperation::replace(const CPlusPlus::AST *ast, const QString &replac
void QuickFixOperation::insert(int at, const QString &text) void QuickFixOperation::insert(int at, const QString &text)
{ {
replace(at, at, text); _changeSet.insert(at, text);
}
void QuickFixOperation::remove(int start, int end)
{
_changeSet.remove(start, end-start);
}
void QuickFixOperation::remove(unsigned tokenIndex)
{
remove(startOf(tokenIndex), endOf(tokenIndex));
}
void QuickFixOperation::remove(const CPlusPlus::AST *ast)
{
remove(startOf(ast), endOf(ast));
}
void QuickFixOperation::copy(int start, int end, int to)
{
_changeSet.copy(start, end-start, to);
}
void QuickFixOperation::copy(unsigned tokenIndex, int to)
{
copy(startOf(tokenIndex), endOf(tokenIndex), to);
}
void QuickFixOperation::copy(const CPlusPlus::AST *ast, int to)
{
copy(startOf(ast), endOf(ast), to);
} }
QString QuickFixOperation::textOf(int firstOffset, int lastOffset) const QString QuickFixOperation::textOf(int firstOffset, int lastOffset) const
......
...@@ -103,6 +103,12 @@ protected: ...@@ -103,6 +103,12 @@ protected:
void replace(unsigned tokenIndex, const QString &replacement); void replace(unsigned tokenIndex, const QString &replacement);
void replace(const CPlusPlus::AST *ast, const QString &replacement); void replace(const CPlusPlus::AST *ast, const QString &replacement);
void insert(int at, const QString &text); void insert(int at, const QString &text);
void remove(int start, int end);
void remove(unsigned tokenIndex);
void remove(const CPlusPlus::AST *ast);
void copy(int start, int end, int to);
void copy(unsigned tokenIndex, int to);
void copy(const CPlusPlus::AST *ast, int to);
QString textOf(int firstOffset, int lastOffset) const; QString textOf(int firstOffset, int lastOffset) const;
QString textOf(CPlusPlus::AST *ast) const; QString textOf(CPlusPlus::AST *ast) const;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment