Commit 85458519 authored by Roberto Raggi's avatar Roberto Raggi
Browse files

Split create/apply change set.

parent 6594f7a2
......@@ -139,14 +139,13 @@ public:
return -1;
}
virtual void apply()
virtual void createChangeSet()
{
setTopLevelNode(pattern);
replace(pattern->binary_op_token, QLatin1String("||"));
replace(left->unary_op_token, QLatin1String("!("));
replace(right->unary_op_token, QLatin1String(""));
insert(endOf(pattern), QLatin1String(")"));
applyChanges(pattern);
}
private:
......@@ -230,8 +229,9 @@ public:
return -1;
}
virtual void apply()
virtual void createChangeSet()
{
setTopLevelNode(declaration);
SpecifierListAST *specifiers = declaration->decl_specifier_list;
const QString declSpecifiers = textOf(startOf(specifiers->firstToken()), endOf(specifiers->lastToken() - 1));
......@@ -250,8 +250,6 @@ public:
}
insert(endOf(declaration->semicolon_token), text);
applyChanges(declaration);
}
private:
......@@ -303,11 +301,11 @@ public:
return -1;
}
virtual void apply()
virtual void createChangeSet()
{
setTopLevelNode(_statement);
insert(endOf(_statement->firstToken() - 1), QLatin1String(" {"));
insert(endOf(_statement->lastToken() - 1), "\n}");
applyChanges(_statement);
}
private:
......@@ -358,8 +356,9 @@ public:
return -1;
}
virtual void apply()
virtual void createChangeSet()
{
setTopLevelNode(pattern);
const QString name = textOf(core);
QString declaration = textOf(condition);
declaration += QLatin1String(";\n");
......@@ -367,8 +366,6 @@ public:
insert(startOf(pattern), declaration);
insert(endOf(pattern->lparen_token), name);
replace(condition, name);
applyChanges(pattern);
}
private:
......@@ -431,8 +428,9 @@ public:
return -1;
}
virtual void apply()
virtual void createChangeSet()
{
setTopLevelNode(pattern);
const QString name = textOf(core);
const QString initializer = textOf(condition->declarator->initializer);
QString declaration = textOf(startOf(condition), endOf(condition->declarator->equals_token - 1));
......@@ -448,8 +446,6 @@ public:
insert(startOf(pattern), declaration);
insert(endOf(pattern->lparen_token), name);
replace(condition, newCondition);
applyChanges(pattern);
}
private:
......@@ -529,7 +525,7 @@ public:
return -1;
}
virtual void apply()
virtual void createChangeSet()
{
Token binaryToken = tokenAt(condition->binary_op_token);
......@@ -541,6 +537,7 @@ public:
void splitAndCondition()
{
setTopLevelNode(pattern);
StatementAST *ifTrueStatement = pattern->statement;
// take the right-expression from the condition.
......@@ -558,12 +555,11 @@ public:
insert(endOf(pattern->rparen_token), nestedIfStatement);
insert(endOf(ifTrueStatement), "\n}"); // finish the compound statement
applyChanges(pattern);
}
void splitOrCondition()
{
setTopLevelNode(pattern);
StatementAST *ifTrueStatement = pattern->statement;
CompoundStatementAST *compoundStatement = ifTrueStatement->asCompoundStatement();
......@@ -592,8 +588,6 @@ public:
elseIfStatement += body;
insert(endOf(pattern), elseIfStatement);
applyChanges(pattern);
}
private:
......@@ -607,7 +601,8 @@ private:
QuickFixOperation::QuickFixOperation(CPlusPlus::Document::Ptr doc,
const CPlusPlus::Snapshot &snapshot,
CPPEditor *editor)
: _doc(doc), _snapshot(snapshot), _editor(editor)
: _doc(doc), _snapshot(snapshot),
_editor(editor), _topLevelNode(0)
{ }
QuickFixOperation::~QuickFixOperation()
......@@ -616,6 +611,12 @@ QuickFixOperation::~QuickFixOperation()
CPPEditor *QuickFixOperation::editor() const
{ return _editor; }
CPlusPlus::AST *QuickFixOperation::topLevelNode() const
{ return _topLevelNode; }
void QuickFixOperation::setTopLevelNode(CPlusPlus::AST *topLevelNode)
{ _topLevelNode = topLevelNode; }
const Utils::ChangeSet &QuickFixOperation::changeSet() const
{ return _changeSet; }
......@@ -752,18 +753,18 @@ QString QuickFixOperation::textOf(AST *ast) const
return textOf(startOf(ast), endOf(ast));
}
void QuickFixOperation::applyChanges(AST *ast)
void QuickFixOperation::applyChangeSet()
{
Range range;
if (ast)
range = createRange(ast);
if (_topLevelNode)
range = createRange(_topLevelNode);
_textCursor.beginEditBlock();
_changeSet.write(&_textCursor);
if (ast)
if (_topLevelNode)
reindent(range);
_textCursor.endEditBlock();
......@@ -867,7 +868,8 @@ void CPPQuickFixCollector::complete(const TextEditor::CompletionItem &item)
void CPPQuickFixCollector::perform(QuickFixOperationPtr op)
{
op->setTextCursor(_editor->textCursor());
op->apply();
op->createChangeSet();
op->applyChangeSet();
}
void CPPQuickFixCollector::cleanup()
......
......@@ -77,7 +77,11 @@ public:
CPPEditor *editor() const;
const Utils::ChangeSet &changeSet() const;
virtual void apply() = 0;
CPlusPlus::AST *topLevelNode() const;
void setTopLevelNode(CPlusPlus::AST *topLevelNode);
virtual void createChangeSet() = 0;
void applyChangeSet();
protected:
const CPlusPlus::Token &tokenAt(unsigned index) const;
......@@ -112,14 +116,13 @@ protected:
Range createRange(CPlusPlus::AST *ast) const; // ### rename me
void reindent(const Range &range);
void applyChanges(CPlusPlus::AST *ast = 0);
private:
CPlusPlus::Document::Ptr _doc;
CPlusPlus::Snapshot _snapshot;
QTextCursor _textCursor;
Utils::ChangeSet _changeSet;
CPPEditor *_editor;
CPlusPlus::AST *_topLevelNode;
};
class CPPQuickFixCollector: public TextEditor::IQuickFixCollector
......
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