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

Introduced a quickfix refactoring operation to split QML initializers.

parent f5dae8bc
......@@ -41,6 +41,76 @@ public:
SemanticInfo semanticInfo;
};
namespace {
class SplitInitializerOp: public QmlJSQuickFixOperation {
public:
SplitInitializerOp(TextEditor::BaseTextEditor *editor)
: QmlJSQuickFixOperation(editor), _objectInitializer(0)
{}
virtual QString description() const
{
return QApplication::translate("QmlJSEditor::QuickFix", "Split initializer");
}
virtual Range topLevelRange() const
{
Q_ASSERT(_objectInitializer);
return range(position(_objectInitializer->lbraceToken),
position(_objectInitializer->rbraceToken));
}
virtual void createChangeSet()
{
Q_ASSERT(_objectInitializer != 0);
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"));
}
}
// insert a newline before the closing brace
insert(position(_objectInitializer->rbraceToken), QLatin1String("\n"));
}
virtual int check()
{
_objectInitializer = 0;
const int pos = textCursor().position();
if (QmlJS::AST::Node *member = semanticInfo().declaringMember(pos)) {
if (QmlJS::AST::UiObjectBinding *b = QmlJS::AST::cast<QmlJS::AST::UiObjectBinding *>(member)) {
if (b->initializer->lbraceToken.startLine == b->initializer->rbraceToken.startLine) {
_objectInitializer = b->initializer;
return 0; // very high priority
}
} else if (QmlJS::AST::UiObjectDefinition *b = QmlJS::AST::cast<QmlJS::AST::UiObjectDefinition *>(member)) {
if (b->initializer->lbraceToken.startLine == b->initializer->rbraceToken.startLine) {
_objectInitializer = b->initializer;
return 0; // very high priority
}
}
return false;
}
return -1;
}
private:
QmlJS::AST::UiObjectInitializer *_objectInitializer;
};
} // end of anonymous namespace
QmlJSQuickFixOperation::QmlJSQuickFixOperation(TextEditor::BaseTextEditor *editor)
......@@ -137,5 +207,6 @@ TextEditor::QuickFixState *QmlJSQuickFixCollector::initializeCompletion(TextEdit
QList<TextEditor::QuickFixOperation::Ptr> QmlJSQuickFixCollector::quickFixOperations(TextEditor::BaseTextEditor *editor) const
{
QList<TextEditor::QuickFixOperation::Ptr> quickFixOperations;
quickFixOperations.append(TextEditor::QuickFixOperation::Ptr(new SplitInitializerOp(editor)));
return quickFixOperations;
}
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