diff --git a/src/plugins/qmljseditor/qmljsquickfix.cpp b/src/plugins/qmljseditor/qmljsquickfix.cpp index db4fb16ab708e20f991c115e8d9d1659badb282c..531771632772d7707ab5faeb7cd492092e9dcaf8 100644 --- a/src/plugins/qmljseditor/qmljsquickfix.cpp +++ b/src/plugins/qmljseditor/qmljsquickfix.cpp @@ -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; }