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