Commit 21b211fe authored by Erik Verbruggen's avatar Erik Verbruggen
Browse files

Created a utility rewriting class from existing visitors.

parent 3267ba12
......@@ -5,6 +5,7 @@ contains(CONFIG, dll) {
}
include(parser/parser.pri)
include(../utils/utils.pri)
DEPENDPATH += $$PWD
INCLUDEPATH += $$PWD/..
......@@ -20,7 +21,8 @@ HEADERS += \
$$PWD/qmljscheck.h \
$$PWD/qmljsscopebuilder.h \
$$PWD/qmljslineinfo.h \
$$PWD/qmljscompletioncontextfinder.h
$$PWD/qmljscompletioncontextfinder.h \
$$PWD/qmljsrewriter.h
SOURCES += \
$$PWD/qmljsbind.cpp \
......@@ -32,7 +34,8 @@ SOURCES += \
$$PWD/qmljscheck.cpp \
$$PWD/qmljsscopebuilder.cpp \
$$PWD/qmljslineinfo.cpp \
$$PWD/qmljscompletioncontextfinder.cpp
$$PWD/qmljscompletioncontextfinder.cpp \
$$PWD/qmljsrewriter.cpp
OTHER_FILES += \
$$PWD/parser/qmljs.g
......
This diff is collapsed.
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#ifndef QMLJSREWRITER_H
#define QMLJSREWRITER_H
#include <qmljs/qmljs_global.h>
#include <qmljs/parser/qmljsastfwd_p.h>
#include <QtCore/QStringList>
namespace Utils {
class ChangeSet;
}
namespace QmlJS {
class QMLJS_EXPORT Rewriter
{
public:
enum BindingType {
ScriptBinding,
ObjectBinding,
ArrayBinding
};
public:
Rewriter(const QString &originalText,
Utils::ChangeSet *changeSet,
const QStringList &propertyOrder);
void addBinding(AST::UiObjectInitializer *ast,
const QString &propertyName,
const QString &propertyValue,
BindingType bindingType);
void changeProperty(AST::UiObjectInitializer *ast,
const QString &propertyName,
const QString &newValue,
BindingType binding);
void removeProperty(AST::UiObjectInitializer *ast, const QString &propertyName);
void appendToArrayBinding(AST::UiArrayBinding *arrayBinding,
const QString &content);
void addObject(AST::UiObjectInitializer *ast, const QString &content);
void removeObjectMember(AST::UiObjectMember *member, AST::UiObjectMember *parent);
static AST::UiObjectMemberList *searchMemberToInsertAfter(AST::UiObjectMemberList *members, const QStringList &propertyOrder);
static AST::UiObjectMemberList *searchMemberToInsertAfter(AST::UiObjectMemberList *members, const QString &propertyName, const QStringList &propertyOrder);
static QString flatten(AST::UiQualifiedId *first);
bool includeSurroundingWhitespace(int &start, int &end) const;
void includeLeadingEmptyLine(int &start) const;
private:
void replaceMemberValue(AST::UiObjectMember *propertyMember,
const QString &newValue,
bool needsSemicolon);
static bool isMatchingPropertyMember(const QString &propertyName,
AST::UiObjectMember *member);
static bool nextMemberOnSameLine(AST::UiObjectMemberList *members);
void insertIntoArray(AST::UiArrayBinding* ast, const QString &newValue);
void removeMember(AST::UiObjectMember *member);
void removeGroupedProperty(AST::UiObjectDefinition *ast,
const QString &propertyName);
void extendToLeadingOrTrailingComma(AST::UiArrayBinding *parentArray,
AST::UiObjectMember *member,
int &start,
int &end) const;
private:
QString m_originalText;
Utils::ChangeSet *m_changeSet;
const QStringList m_propertyOrder;
};
} // namespace QmlJS
#endif // QMLJSREWRITER_H
......@@ -84,6 +84,7 @@ bool AddArrayMemberVisitor::visit(QmlJS::AST::UiObjectDefinition *ast)
return !didRewriting();
}
// FIXME: duplicate code in the QmlJS::Rewriter class, remove this
void AddArrayMemberVisitor::insertInto(QmlJS::AST::UiArrayBinding *arrayBinding)
{
UiObjectMember *lastMember = 0;
......
......@@ -70,6 +70,7 @@ bool AddObjectVisitor::visit(QmlJS::AST::UiObjectDefinition *ast)
return !didRewriting();
}
// FIXME: duplicate code in the QmlJS::Rewriter class, remove this
void AddObjectVisitor::insertInto(QmlJS::AST::UiObjectInitializer *ast)
{
UiObjectMemberList *insertAfter = searchMemberToInsertAfter(ast->members, m_propertyOrder);
......
......@@ -58,6 +58,7 @@ bool AddPropertyVisitor::visit(QmlJS::AST::UiObjectDefinition *ast)
return false;
if (ast->firstSourceLocation().offset == m_parentLocation) {
// FIXME: change this to use the QmlJS::Rewriter class
addInMembers(ast->initializer);
return false;
}
......@@ -71,6 +72,7 @@ bool AddPropertyVisitor::visit(QmlJS::AST::UiObjectBinding *ast)
return false;
if (ast->qualifiedTypeNameId->identifierToken.offset == m_parentLocation) {
// FIXME: change this to use the QmlJS::Rewriter class
addInMembers(ast->initializer);
return false;
}
......@@ -78,6 +80,7 @@ bool AddPropertyVisitor::visit(QmlJS::AST::UiObjectBinding *ast)
return !didRewriting();
}
// FIXME: duplicate code in the QmlJS::Rewriter class, remove this
void AddPropertyVisitor::addInMembers(QmlJS::AST::UiObjectInitializer *initializer)
{
UiObjectMemberList *insertAfter = searchMemberToInsertAfter(initializer->members, m_name, m_propertyOrder);
......
......@@ -58,6 +58,7 @@ bool ChangePropertyVisitor::visit(QmlJS::AST::UiObjectDefinition *ast)
const quint32 objectStart = ast->firstSourceLocation().offset;
if (objectStart == m_parentLocation) {
// FIXME: change this to use the QmlJS::Rewriter class
replaceInMembers(ast->initializer, m_name);
return false;
}
......@@ -73,6 +74,7 @@ bool ChangePropertyVisitor::visit(QmlJS::AST::UiObjectBinding *ast)
const quint32 objectStart = ast->qualifiedTypeNameId->identifierToken.offset;
if (objectStart == m_parentLocation) {
// FIXME: change this to use the QmlJS::Rewriter class
replaceInMembers(ast->initializer, m_name);
return false;
}
......@@ -80,6 +82,7 @@ bool ChangePropertyVisitor::visit(QmlJS::AST::UiObjectBinding *ast)
return !didRewriting();
}
// FIXME: duplicate code in the QmlJS::Rewriter class, remove this
void ChangePropertyVisitor::replaceInMembers(UiObjectInitializer *initializer,
const QString &propertyName)
{
......@@ -125,6 +128,7 @@ void ChangePropertyVisitor::replaceInMembers(UiObjectInitializer *initializer,
}
}
// FIXME: duplicate code in the QmlJS::Rewriter class, remove this
void ChangePropertyVisitor::replaceMemberValue(UiObjectMember *propertyMember, bool needsSemicolon)
{
QString replacement = m_value;
......@@ -164,6 +168,7 @@ void ChangePropertyVisitor::replaceMemberValue(UiObjectMember *propertyMember, b
setDidRewriting(true);
}
// FIXME: duplicate code in the QmlJS::Rewriter class, remove this
bool ChangePropertyVisitor::isMatchingPropertyMember(const QString &propName,
UiObjectMember *member)
{
......@@ -180,6 +185,7 @@ bool ChangePropertyVisitor::isMatchingPropertyMember(const QString &propName,
}
}
// FIXME: duplicate code in the QmlJS::Rewriter class, remove this
bool ChangePropertyVisitor::nextMemberOnSameLine(UiObjectMemberList *members)
{
if (members && members->next && members->next->member) {
......@@ -189,6 +195,7 @@ bool ChangePropertyVisitor::nextMemberOnSameLine(UiObjectMemberList *members)
}
}
// FIXME: duplicate code in the QmlJS::Rewriter class, remove this
void ChangePropertyVisitor::insertIntoArray(QmlJS::AST::UiArrayBinding *ast)
{
if (!ast)
......
......@@ -195,6 +195,7 @@ bool QMLRewriter::isMissingSemicolon(QmlJS::AST::Statement *stmt)
}
}
// FIXME: duplicate code in the QmlJS::Rewriter class, remove this
QString QMLRewriter::flatten(UiQualifiedId *first)
{
QString flatId;
......@@ -209,6 +210,7 @@ QString QMLRewriter::flatten(UiQualifiedId *first)
return flatId;
}
// FIXME: duplicate code in the QmlJS::Rewriter class, remove this
bool QMLRewriter::includeSurroundingWhitespace(int &start, int &end) const
{
QTextDocument *doc = m_textModifier->textDocument();
......@@ -249,6 +251,7 @@ bool QMLRewriter::includeSurroundingWhitespace(int &start, int &end) const
return paragraphFound;
}
// FIXME: duplicate code in the QmlJS::Rewriter class, remove this
void QMLRewriter::includeLeadingEmptyLine(int &start) const
{
QTextDocument *doc = textModifier()->textDocument();
......@@ -273,6 +276,7 @@ void QMLRewriter::includeLeadingEmptyLine(int &start) const
start = prevBlock.position();
}
// FIXME: duplicate code in the QmlJS::Rewriter class, remove this
UiObjectMemberList *QMLRewriter::searchMemberToInsertAfter(UiObjectMemberList *members, const QStringList &propertyOrder)
{
const int objectDefinitionInsertionPoint = propertyOrder.indexOf(QString::null);
......@@ -305,6 +309,7 @@ UiObjectMemberList *QMLRewriter::searchMemberToInsertAfter(UiObjectMemberList *m
return lastNonObjectDef;
}
// FIXME: duplicate code in the QmlJS::Rewriter class, remove this
UiObjectMemberList *QMLRewriter::searchMemberToInsertAfter(UiObjectMemberList *members, const QString &propertyName, const QStringList &propertyOrder)
{
if (!members)
......
......@@ -48,6 +48,7 @@ RemovePropertyVisitor::RemovePropertyVisitor(QmlDesigner::TextModifier &modifier
bool RemovePropertyVisitor::visit(QmlJS::AST::UiObjectBinding *ast)
{
if (ast->firstSourceLocation().offset == parentLocation) {
// FIXME: change this to use the QmlJS::Rewriter class
removeFrom(ast->initializer);
}
......@@ -57,12 +58,14 @@ bool RemovePropertyVisitor::visit(QmlJS::AST::UiObjectBinding *ast)
bool RemovePropertyVisitor::visit(QmlJS::AST::UiObjectDefinition *ast)
{
if (ast->firstSourceLocation().offset == parentLocation) {
// FIXME: change this to use the QmlJS::Rewriter class
removeFrom(ast->initializer);
}
return !didRewriting();
}
// FIXME: duplicate code in the QmlJS::Rewriter class, remove this
void RemovePropertyVisitor::removeFrom(QmlJS::AST::UiObjectInitializer *ast)
{
QString prefix;
......@@ -88,6 +91,7 @@ void RemovePropertyVisitor::removeFrom(QmlJS::AST::UiObjectInitializer *ast)
}
}
// FIXME: duplicate code in the QmlJS::Rewriter class, remove this
void RemovePropertyVisitor::removeGroupedProperty(UiObjectDefinition *ast)
{
int dotIdx = propertyName.indexOf(QLatin1Char('.'));
......@@ -115,6 +119,7 @@ void RemovePropertyVisitor::removeGroupedProperty(UiObjectDefinition *ast)
removeMember(wanted);
}
// FIXME: duplicate code in the QmlJS::Rewriter class, remove this
void RemovePropertyVisitor::removeMember(UiObjectMember *member)
{
int start = member->firstSourceLocation().offset;
......@@ -126,6 +131,7 @@ void RemovePropertyVisitor::removeMember(UiObjectMember *member)
setDidRewriting(true);
}
// FIXME: duplicate code in the QmlJS::Rewriter class, remove this
bool RemovePropertyVisitor::memberNameMatchesPropertyName(const QString &propertyName, UiObjectMember *ast)
{
if (UiPublicMember *publicMember = cast<UiPublicMember*>(ast))
......
......@@ -65,6 +65,7 @@ bool RemoveUIObjectMemberVisitor::visit(QmlJS::AST::UiObjectBinding *ast) { retu
bool RemoveUIObjectMemberVisitor::visit(QmlJS::AST::UiScriptBinding *ast) { return visitObjectMember(ast); }
bool RemoveUIObjectMemberVisitor::visit(QmlJS::AST::UiArrayBinding *ast) { return visitObjectMember(ast); }
// FIXME: duplicate code in the QmlJS::Rewriter class, remove this
bool RemoveUIObjectMemberVisitor::visitObjectMember(QmlJS::AST::UiObjectMember *ast)
{
const quint32 memberStart = ast->firstSourceLocation().offset;
......@@ -106,6 +107,7 @@ UiArrayBinding *RemoveUIObjectMemberVisitor::containingArray() const
return 0;
}
// FIXME: duplicate code in the QmlJS::Rewriter class, remove this
void RemoveUIObjectMemberVisitor::extendToLeadingOrTrailingComma(QmlJS::AST::UiArrayBinding *parentArray,
QmlJS::AST::UiObjectMember *ast,
int &start,
......
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