Commit f3faf34d authored by Erik Verbruggen's avatar Erik Verbruggen
Browse files

Removed implicit change set and related utility methods.

parent 00b53337
......@@ -56,6 +56,7 @@
using namespace CppEditor::Internal;
using namespace CPlusPlus;
using namespace Utils;
namespace {
......@@ -135,18 +136,20 @@ public:
return index;
}
virtual void createChangeSet()
virtual void createChanges()
{
ChangeSet changes;
if (negation) {
// can't remove parentheses since that might break precedence
remove(negation->unary_op_token);
remove(&changes, negation->unary_op_token);
} else if (nested) {
insert(startOf(nested), "!");
changes.insert(startOf(nested), "!");
} else {
insert(startOf(binary), "!(");
insert(endOf(binary), ")");
changes.insert(startOf(binary), "!(");
changes.insert(endOf(binary), ")");
}
replace(binary->binary_op_token, replacement);
replace(&changes, binary->binary_op_token, replacement);
cppRefactoringChanges()->changeFile(fileName(), changes);
}
private:
......@@ -221,11 +224,15 @@ public:
return index;
}
virtual void createChangeSet()
virtual void createChanges()
{
flip(binary->left_expression, binary->right_expression);
ChangeSet changes;
flip(&changes, binary->left_expression, binary->right_expression);
if (! replacement.isEmpty())
replace(binary->binary_op_token, replacement);
replace(&changes, binary->binary_op_token, replacement);
cppRefactoringChanges()->changeFile(fileName(), changes);
}
private:
......@@ -283,14 +290,19 @@ public:
return -1;
}
virtual void createChangeSet()
virtual void createChanges()
{
setTopLevelNode(pattern);
replace(pattern->binary_op_token, QLatin1String("||"));
remove(left->unary_op_token);
remove(right->unary_op_token);
insert(startOf(pattern), QLatin1String("!("));
insert(endOf(pattern), QLatin1String(")"));
ChangeSet changes;
replace(&changes, pattern->binary_op_token, QLatin1String("||"));
remove(&changes, left->unary_op_token);
remove(&changes, right->unary_op_token);
const int start = startOf(pattern);
const int end = endOf(pattern);
changes.insert(start, QLatin1String("!("));
changes.insert(end, QLatin1String(")"));
cppRefactoringChanges()->changeFile(fileName(), changes);
cppRefactoringChanges()->reindent(fileName(), range(start, end));
}
private:
......@@ -374,9 +386,10 @@ public:
return -1;
}
virtual void createChangeSet()
virtual void createChanges()
{
setTopLevelNode(declaration);
ChangeSet changes;
SpecifierListAST *specifiers = declaration->decl_specifier_list;
int declSpecifiersStart = startOf(specifiers->firstToken());
int declSpecifiersEnd = endOf(specifiers->lastToken() - 1);
......@@ -387,16 +400,21 @@ public:
for (DeclaratorListAST *it = declaration->declarator_list->next; it; it = it->next) {
DeclaratorAST *declarator = it->value;
insert(insertPos, QLatin1String("\n"));
copy(declSpecifiersStart, declSpecifiersEnd, insertPos);
insert(insertPos, QLatin1String(" "));
move(declarator, insertPos);
insert(insertPos, QLatin1String(";"));
changes.insert(insertPos, QLatin1String("\n"));
changes.copy(declSpecifiersStart, declSpecifiersEnd - declSpecifiersStart, insertPos);
changes.insert(insertPos, QLatin1String(" "));
move(&changes, declarator, insertPos);
changes.insert(insertPos, QLatin1String(";"));
remove(endOf(prevDeclarator), startOf(declarator));
const int prevDeclEnd = endOf(prevDeclarator);
changes.remove(prevDeclEnd, startOf(declarator) - prevDeclEnd);
prevDeclarator = declarator;
}
cppRefactoringChanges()->changeFile(fileName(), changes);
cppRefactoringChanges()->reindent(fileName(), range(startOf(declaration->firstToken()),
endOf(declaration->lastToken() - 1)));
}
private:
......@@ -448,11 +466,18 @@ public:
return -1;
}
virtual void createChangeSet()
virtual void createChanges()
{
setTopLevelNode(_statement);
insert(endOf(_statement->firstToken() - 1), QLatin1String(" {"));
insert(endOf(_statement->lastToken() - 1), "\n}");
ChangeSet changes;
const int start = endOf(_statement->firstToken() - 1);
changes.insert(start, QLatin1String(" {"));
const int end = endOf(_statement->lastToken() - 1);
changes.insert(end, "\n}");
cppRefactoringChanges()->changeFile(fileName(), changes);
cppRefactoringChanges()->reindent(fileName(), range(start, end));
}
private:
......@@ -502,15 +527,19 @@ public:
return -1;
}
virtual void createChangeSet()
virtual void createChanges()
{
setTopLevelNode(pattern);
ChangeSet changes;
copy(core, startOf(condition));
copy(&changes, core, startOf(condition));
int insertPos = startOf(pattern);
move(condition, insertPos);
insert(insertPos, QLatin1String(";\n"));
move(&changes, condition, insertPos);
changes.insert(insertPos, QLatin1String(";\n"));
cppRefactoringChanges()->changeFile(fileName(), changes);
cppRefactoringChanges()->reindent(fileName(), range(startOf(pattern),
endOf(pattern)));
}
private:
......@@ -572,17 +601,22 @@ public:
return -1;
}
virtual void createChangeSet()
virtual void createChanges()
{
setTopLevelNode(pattern);
ChangeSet changes;
insert(startOf(condition), QLatin1String("("));
insert(endOf(condition), QLatin1String(") != 0"));
changes.insert(startOf(condition), QLatin1String("("));
changes.insert(endOf(condition), QLatin1String(") != 0"));
int insertPos = startOf(pattern);
move(startOf(condition), startOf(core), insertPos);
copy(core, insertPos);
insert(insertPos, QLatin1String(";\n"));
const int conditionStart = startOf(condition);
changes.move(conditionStart, startOf(core) - conditionStart, insertPos);
copy(&changes, core, insertPos);
changes.insert(insertPos, QLatin1String(";\n"));
cppRefactoringChanges()->changeFile(fileName(), changes);
cppRefactoringChanges()->reindent(fileName(), range(startOf(pattern),
endOf(pattern)));
}
private:
......@@ -671,7 +705,7 @@ public:
return -1;
}
virtual void createChangeSet()
virtual void createChanges()
{
Token binaryToken = tokenAt(condition->binary_op_token);
......@@ -683,35 +717,51 @@ public:
void splitAndCondition()
{
setTopLevelNode(pattern);
ChangeSet changes;
int startPos = startOf(pattern);
insert(startPos, QLatin1String("if ("));
move(condition->left_expression, startPos);
insert(startPos, QLatin1String(") {\n"));
remove(endOf(condition->left_expression), startOf(condition->right_expression));
insert(endOf(pattern), QLatin1String("\n}"));
changes.insert(startPos, QLatin1String("if ("));
move(&changes, condition->left_expression, startPos);
changes.insert(startPos, QLatin1String(") {\n"));
const int lExprEnd = endOf(condition->left_expression);
changes.remove(lExprEnd,
startOf(condition->right_expression) - lExprEnd);
changes.insert(endOf(pattern), QLatin1String("\n}"));
cppRefactoringChanges()->changeFile(fileName(), changes);
cppRefactoringChanges()->reindent(fileName(), range(startOf(pattern),
endOf(pattern)));
}
void splitOrCondition()
{
ChangeSet changes;
StatementAST *ifTrueStatement = pattern->statement;
CompoundStatementAST *compoundStatement = ifTrueStatement->asCompoundStatement();
setTopLevelNode(pattern);
int insertPos = endOf(ifTrueStatement);
if (compoundStatement)
insert(insertPos, QLatin1String(" "));
changes.insert(insertPos, QLatin1String(" "));
else
insert(insertPos, QLatin1String("\n"));
insert(insertPos, QLatin1String("else if ("));
move(startOf(condition->right_expression), startOf(pattern->rparen_token), insertPos);
insert(insertPos, QLatin1String(")"));
copy(endOf(pattern->rparen_token), endOf(pattern->statement), insertPos);
changes.insert(insertPos, QLatin1String("\n"));
changes.insert(insertPos, QLatin1String("else if ("));
const int rExprStart = startOf(condition->right_expression);
changes.move(rExprStart, startOf(pattern->rparen_token) - rExprStart,
insertPos);
changes.insert(insertPos, QLatin1String(")"));
remove(endOf(condition->left_expression), startOf(condition->right_expression));
const int rParenEnd = endOf(pattern->rparen_token);
changes.copy(rParenEnd, endOf(pattern->statement) - rParenEnd, insertPos);
const int lExprEnd = endOf(condition->left_expression);
changes.remove(lExprEnd, startOf(condition->right_expression) - lExprEnd);
cppRefactoringChanges()->changeFile(fileName(), changes);
cppRefactoringChanges()->reindent(fileName(), range(startOf(pattern),
endOf(pattern)));
}
private:
......@@ -775,17 +825,21 @@ public:
return index;
}
virtual void createChangeSet()
virtual void createChanges()
{
ChangeSet changes;
const int startPos = startOf(stringLiteral);
const QLatin1String replacement("QLatin1String(");
if (isObjCStringLiteral)
replace(startPos, startPos + 1, replacement);
changes.replace(startPos, 1, replacement);
else
insert(startPos, replacement);
changes.insert(startPos, replacement);
insert(endOf(stringLiteral), ")");
changes.insert(endOf(stringLiteral), ")");
cppRefactoringChanges()->changeFile(fileName(), changes);
}
private:
......@@ -846,14 +900,21 @@ public:
return index;
}
virtual void createChangeSet()
virtual void createChanges()
{
ChangeSet changes;
if (qlatin1Call) {
replace(startOf(qlatin1Call), startOf(stringLiteral), QLatin1String("@"));
remove(endOf(stringLiteral), endOf(qlatin1Call));
changes.replace(startOf(qlatin1Call),
startOf(stringLiteral) - startOf(qlatin1Call),
QLatin1String("@"));
changes.remove(endOf(stringLiteral),
endOf(qlatin1Call) - endOf(stringLiteral));
} else {
insert(startOf(stringLiteral), "@");
changes.insert(startOf(stringLiteral), "@");
}
cppRefactoringChanges()->changeFile(fileName(), changes);
}
private:
......@@ -887,6 +948,9 @@ int CppQuickFixOperation::match(TextEditor::QuickFixState *state)
return match(s->path);
}
QString CppQuickFixOperation::fileName() const
{ return document()->fileName(); }
void CppQuickFixOperation::apply()
{
cppRefactoringChanges()->apply();
......@@ -898,12 +962,6 @@ CppTools::CppRefactoringChanges *CppQuickFixOperation::cppRefactoringChanges() c
TextEditor::RefactoringChanges *CppQuickFixOperation::refactoringChanges() const
{ return cppRefactoringChanges(); }
CPlusPlus::AST *CppQuickFixOperation::topLevelNode() const
{ return _topLevelNode; }
void CppQuickFixOperation::setTopLevelNode(CPlusPlus::AST *topLevelNode)
{ _topLevelNode = topLevelNode; }
Document::Ptr CppQuickFixOperation::document() const
{ return _document; }
......@@ -979,57 +1037,91 @@ bool CppQuickFixOperation::isCursorOn(const CPlusPlus::AST *ast) const
return false;
}
void CppQuickFixOperation::move(unsigned tokenIndex, int to)
void CppQuickFixOperation::move(ChangeSet *changeSet, unsigned tokenIndex,
int to)
{
Q_ASSERT(changeSet);
int start, end;
startAndEndOf(tokenIndex, &start, &end);
move(start, end, to);
changeSet->move(start, end - start, to);
}
void CppQuickFixOperation::move(const CPlusPlus::AST *ast, int to)
void CppQuickFixOperation::move(ChangeSet *changeSet, const CPlusPlus::AST *ast,
int to)
{
move(startOf(ast), endOf(ast), to);
Q_ASSERT(changeSet);
const int start = startOf(ast);
changeSet->move(start, endOf(ast) - start, to);
}
void CppQuickFixOperation::replace(unsigned tokenIndex, const QString &replacement)
void CppQuickFixOperation::replace(ChangeSet *changeSet, unsigned tokenIndex,
const QString &replacement)
{
Q_ASSERT(changeSet);
int start, end;
startAndEndOf(tokenIndex, &start, &end);
replace(start, end, replacement);
changeSet->replace(start, end - start, replacement);
}
void CppQuickFixOperation::replace(const CPlusPlus::AST *ast, const QString &replacement)
void CppQuickFixOperation::replace(ChangeSet *changeSet,
const CPlusPlus::AST *ast,
const QString &replacement)
{
replace(startOf(ast), endOf(ast), replacement);
Q_ASSERT(changeSet);
const int start = startOf(ast);
changeSet->replace(start, endOf(ast) - start, replacement);
}
void CppQuickFixOperation::remove(unsigned tokenIndex)
void CppQuickFixOperation::remove(ChangeSet *changeSet, unsigned tokenIndex)
{
Q_ASSERT(changeSet);
int start, end;
startAndEndOf(tokenIndex, &start, &end);
remove(start, end);
changeSet->remove(start, end - start);
}
void CppQuickFixOperation::remove(const CPlusPlus::AST *ast)
void CppQuickFixOperation::remove(ChangeSet *changeSet, const CPlusPlus::AST *ast)
{
remove(startOf(ast), endOf(ast));
Q_ASSERT(changeSet);
const int start = startOf(ast);
changeSet->remove(start, endOf(ast) - start);
}
void CppQuickFixOperation::flip(const CPlusPlus::AST *ast1, const CPlusPlus::AST *ast2)
void CppQuickFixOperation::flip(ChangeSet *changeSet,
const CPlusPlus::AST *ast1,
const CPlusPlus::AST *ast2)
{
flip(startOf(ast1), endOf(ast1), startOf(ast2), endOf(ast2));
Q_ASSERT(changeSet);
const int start1 = startOf(ast1);
const int start2 = startOf(ast2);
changeSet->flip(start1, endOf(ast1) - start1,
start2, endOf(ast2) - start2);
}
void CppQuickFixOperation::copy(unsigned tokenIndex, int to)
void CppQuickFixOperation::copy(ChangeSet *changeSet, unsigned tokenIndex,
int to)
{
Q_ASSERT(changeSet);
int start, end;
startAndEndOf(tokenIndex, &start, &end);
copy(start, end, to);
changeSet->copy(start, end - start, to);
}
void CppQuickFixOperation::copy(const CPlusPlus::AST *ast, int to)
void CppQuickFixOperation::copy(ChangeSet *changeSet, const CPlusPlus::AST *ast,
int to)
{
copy(startOf(ast), endOf(ast), to);
Q_ASSERT(changeSet);
const int start = startOf(ast);
changeSet->copy(start, endOf(ast) - start, to);
}
QString CppQuickFixOperation::textOf(const AST *ast) const
......
......@@ -65,13 +65,12 @@ public:
virtual int match(TextEditor::QuickFixState *state);
protected:
QString fileName() const;
virtual void apply();
virtual CppTools::CppRefactoringChanges *cppRefactoringChanges() const;
virtual TextEditor::RefactoringChanges *refactoringChanges() const;
CPlusPlus::AST *topLevelNode() const;
void setTopLevelNode(CPlusPlus::AST *topLevelNode);
const CPlusPlus::Token &tokenAt(unsigned index) const;
int startOf(unsigned index) const;
......@@ -83,25 +82,21 @@ protected:
bool isCursorOn(unsigned tokenIndex) const;
bool isCursorOn(const CPlusPlus::AST *ast) const;
using TextEditor::QuickFixOperation::move;
using TextEditor::QuickFixOperation::replace;
using TextEditor::QuickFixOperation::insert;
using TextEditor::QuickFixOperation::remove;
using TextEditor::QuickFixOperation::flip;
using TextEditor::QuickFixOperation::copy;
using TextEditor::QuickFixOperation::textOf;
using TextEditor::QuickFixOperation::charAt;
void move(unsigned tokenIndex, int to);
void move(const CPlusPlus::AST *ast, int to);
void replace(unsigned tokenIndex, const QString &replacement);
void replace(const CPlusPlus::AST *ast, const QString &replacement);
void remove(unsigned tokenIndex);
void remove(const CPlusPlus::AST *ast);
void flip(const CPlusPlus::AST *ast1, const CPlusPlus::AST *ast2);
void copy(unsigned tokenIndex, int to);
void copy(const CPlusPlus::AST *ast, int to);
void move(Utils::ChangeSet *changeSet, unsigned tokenIndex, int to);
void move(Utils::ChangeSet *changeSet, const CPlusPlus::AST *ast, int to);
void replace(Utils::ChangeSet *changeSet, unsigned tokenIndex,
const QString &replacement);
void replace(Utils::ChangeSet *changeSet, const CPlusPlus::AST *ast,
const QString &replacement);
void remove(Utils::ChangeSet *changeSet, unsigned tokenIndex);
void remove(Utils::ChangeSet *changeSet, const CPlusPlus::AST *ast);
void flip(Utils::ChangeSet *changeSet, const CPlusPlus::AST *ast1,
const CPlusPlus::AST *ast2);
void copy(Utils::ChangeSet *changeSet, unsigned tokenIndex, int to);
void copy(Utils::ChangeSet *changeSet, const CPlusPlus::AST *ast, int to);
QString textOf(const CPlusPlus::AST *ast) const;
......
......@@ -82,7 +82,7 @@ QString ComponentFromObjectDef::description() const
"Extract Component");
}
void ComponentFromObjectDef::createChangeSet()
void ComponentFromObjectDef::createChanges()
{
Q_ASSERT(_objDef != 0);
......@@ -90,7 +90,7 @@ void ComponentFromObjectDef::createChangeSet()
componentName[0] = componentName.at(0).toUpper();
const QString path = editor()->file()->fileName();
const QString fileName = QFileInfo(path).path() + QDir::separator() + componentName + QLatin1String(".qml");
const QString newFileName = QFileInfo(path).path() + QDir::separator() + componentName + QLatin1String(".qml");
QString imports;
UiProgram *prog = semanticInfo().document->qmlProgram();
......@@ -104,11 +104,13 @@ void ComponentFromObjectDef::createChangeSet()
const int end = position(_objDef->lastSourceLocation());
const QString txt = imports + textOf(start, end) + QLatin1String("}\n");
replace(start, end, componentName + QLatin1String(" {\n"));
reindent(range(start, end + 1));
Utils::ChangeSet changes;
changes.replace(start, end - start, componentName + QLatin1String(" {\n"));
qmljsRefactoringChanges()->changeFile(fileName(), changes);
qmljsRefactoringChanges()->reindent(fileName(), range(start, end + 1));
qmljsRefactoringChanges()->createFile(fileName, txt);
qmljsRefactoringChanges()->reindent(fileName, range(0, txt.length() - 1));
qmljsRefactoringChanges()->createFile(newFileName, txt);
qmljsRefactoringChanges()->reindent(newFileName, range(0, txt.length() - 1));
}
int ComponentFromObjectDef::check()
......
......@@ -41,7 +41,7 @@ public:
ComponentFromObjectDef(TextEditor::BaseTextEditor *editor);
virtual QString description() const;
virtual void createChangeSet();
virtual void createChanges();
virtual int check();
private:
......
......@@ -63,24 +63,27 @@ public:
return QApplication::translate("QmlJSEditor::QuickFix", "Split initializer");
}
virtual void createChangeSet()
virtual void createChanges()
{
Q_ASSERT(_objectInitializer != 0);
Utils::ChangeSet changes;
for (QmlJS::AST::UiObjectMemberList *it = _objectInitializer->members; it; it = it->next) {
if (QmlJS::AST::UiObjectMember *member = it->member) {
const QmlJS::AST::SourceLocation loc = member->firstSourceLocation();
// insert a newline at the beginning of this binding
insert(position(loc), QLatin1String("\n"));
changes.insert(position(loc), QLatin1String("\n"));
}
}
// insert a newline before the closing brace
insert(position(_objectInitializer->rbraceToken), QLatin1String("\n"));
changes.insert(position(_objectInitializer->rbraceToken), QLatin1String("\n"));
reindent(RefactoringChanges::Range(position(_objectInitializer->lbraceToken),
position(_objectInitializer->rbraceToken)));
refactoringChanges()->changeFile(fileName(), changes);
refactoringChanges()->reindent(fileName(), range(position(_objectInitializer->lbraceToken),
position(_objectInitializer->rbraceToken)));
}
......@@ -154,6 +157,11 @@ int QmlJSQuickFixOperation::match(TextEditor::QuickFixState *state)
return check();
}
QString QmlJSQuickFixOperation::fileName() const
{
return document()->fileName();