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

Fixed ObjectLengthCalculator to handle incorrect QML.

Task-number: QTCREATORBUG-961
parent b2bb919e
......@@ -35,23 +35,28 @@ using namespace QmlJS;
using namespace QmlDesigner;
using namespace QmlJS::AST;
ObjectLengthCalculator::ObjectLengthCalculator(const QString &text):
ObjectLengthCalculator::ObjectLengthCalculator():
m_doc(Document::create("<internal>"))
{
m_doc->setSource(text);
bool ok = m_doc->parseQml();
Q_ASSERT(ok);
}
quint32 ObjectLengthCalculator::operator()(quint32 offset)
bool ObjectLengthCalculator::operator()(const QString &text, quint32 offset,
quint32 &length)
{
m_offset = offset;
m_length = 0;
m_doc->setSource(text);
Node::accept(m_doc->qmlProgram(), this);
if (!m_doc->parseQml())
return false;
return m_length;
Node::accept(m_doc->qmlProgram(), this);
if (m_length) {
length = m_length;
return true;
} else {
return false;
}
}
bool ObjectLengthCalculator::visit(QmlJS::AST::UiObjectBinding *ast)
......
......@@ -38,9 +38,9 @@ namespace QmlDesigner {
class ObjectLengthCalculator: protected QmlJS::AST::Visitor
{
public:
ObjectLengthCalculator(const QString &text);
ObjectLengthCalculator();
quint32 operator()(quint32 offset);
bool operator()(const QString &text, quint32 offset, quint32 &length);
protected:
using QmlJS::AST::Visitor::visit;
......
......@@ -477,10 +477,18 @@ int RewriterView::nodeOffset(const ModelNode &node) const
return m_positionStorage->nodeOffset(node);
}
/**
* \return the length of the node's text, or -1 if it wasn't found or if an error
* occurred.
*/
int RewriterView::nodeLength(const ModelNode &node) const
{
ObjectLengthCalculator objectLengthCalculator(m_textModifier->text());
return objectLengthCalculator(nodeOffset(node));
ObjectLengthCalculator objectLengthCalculator;
unsigned length;
if (objectLengthCalculator(m_textModifier->text(), nodeOffset(node), length))
return (int) length;
else
return -1;
}
int RewriterView::firstDefinitionInsideOffset(const ModelNode &node) const
......@@ -492,9 +500,14 @@ int RewriterView::firstDefinitionInsideOffset(const ModelNode &node) const
int RewriterView::firstDefinitionInsideLength(const ModelNode &node) const
{
FirstDefinitionFinder firstDefinitionFinder(m_textModifier->text());
int offset = firstDefinitionFinder(nodeOffset(node));
ObjectLengthCalculator objectLengthCalculator(m_textModifier->text());
return objectLengthCalculator(offset);
const int offset = firstDefinitionFinder(nodeOffset(node));
ObjectLengthCalculator objectLengthCalculator;
unsigned length;
if (objectLengthCalculator(m_textModifier->text(), offset, length))
return length;
else
return -1;
}
bool RewriterView::modificationGroupActive()
......
......@@ -952,10 +952,13 @@ void TextToModelMerger::setupComponent(const ModelNode &node)
if (componentText.contains("Component")) { //explicit component
FirstDefinitionFinder firstDefinitionFinder(componentText);
int offset = firstDefinitionFinder(0);
ObjectLengthCalculator objectLengthCalculator(componentText);
int length = objectLengthCalculator(offset);
for (int i = offset;i<offset + length;i++)
result.append(componentText.at(i));
ObjectLengthCalculator objectLengthCalculator;
unsigned length;
if (objectLengthCalculator(componentText, offset, length)) {
result = componentText.mid(offset, length);
} else {
result = componentText;
}
} else {
result = componentText; //implicit component
}
......
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