rewriterview.cpp 21.7 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29

30 31
#include "rewriterview.h"

32 33 34 35 36
#include <filemanager/astobjecttextextractor.h>
#include <filemanager/objectlengthcalculator.h>
#include <filemanager/firstdefinitionfinder.h>
#include <customnotifications.h>

37

38
#include "rewritingexception.h"
39 40 41 42
#include "texttomodelmerger.h"
#include "modelnodepositionstorage.h"
#include "modeltotextmerger.h"
#include "nodelistproperty.h"
43
#include "signalhandlerproperty.h"
44

45 46


47 48 49 50 51 52 53 54 55 56 57
using namespace QmlDesigner::Internal;

namespace QmlDesigner {

RewriterView::Error::Error():
        m_type(NoError),
        m_line(-1),
        m_column(-1)
{
}

58
RewriterView::Error::Error(Exception *exception):
59
        m_type(InternalError),
60
        m_line(exception->line()),
61
        m_column(-1),
62 63
        m_description(exception->description()),
        m_url(exception->file())
64 65 66
{
}

67
RewriterView::Error::Error(const QmlJS::DiagnosticMessage &qmlError, const QUrl &document):
68
        m_type(ParseError),
69 70 71 72
        m_line(qmlError.loc.startLine),
        m_column(qmlError.loc.startColumn),
        m_description(qmlError.message),
        m_url(document)
73 74 75
{
}

76 77 78 79 80 81 82 83 84 85
RewriterView::Error::Error(const QString &shortDescription) :
    m_type(ParseError),
    m_line(1),
    m_column(0),
    m_description(shortDescription),
    m_url()
{
}


86 87 88 89
QString RewriterView::Error::toString() const
{
    QString str;

90 91 92
    if (m_type == ParseError)
        str += tr("Error parsing");
    else if (m_type == InternalError)
93
        str += tr("Internal error");
94

95 96 97 98 99 100 101 102 103 104 105 106 107
    if (url().isValid()) {
        if (!str.isEmpty())
            str += QLatin1Char(' ');

        str += tr("\"%1\"").arg(url().toString());
    }

    if (line() != -1) {
        if (!str.isEmpty())
            str += QLatin1Char(' ');
        str += tr("line %1").arg(line());
    }

108
    if (column() != -1) {
109 110 111 112 113 114 115 116
        if (!str.isEmpty())
            str += QLatin1Char(' ');

        str += tr("column %1").arg(column());
    }

    if (!str.isEmpty())
        QLatin1String(": ");
117 118 119 120 121 122 123 124 125 126 127 128 129
    str += description();

    return str;
}

RewriterView::RewriterView(DifferenceHandling differenceHandling, QObject *parent):
        AbstractView(parent),
        m_differenceHandling(differenceHandling),
        m_modificationGroupActive(false),
        m_positionStorage(new ModelNodePositionStorage),
        m_modelToTextMerger(new Internal::ModelToTextMerger(this)),
        m_textToModelMerger(new Internal::TextToModelMerger(this)),
        m_textModifier(0),
130 131
        transactionLevel(0),
        m_checkErrors(true)
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
{
}

RewriterView::~RewriterView()
{
    delete m_positionStorage;
}

Internal::ModelToTextMerger *RewriterView::modelToTextMerger() const
{
    return m_modelToTextMerger.data();
}

Internal::TextToModelMerger *RewriterView::textToModelMerger() const
{
    return m_textToModelMerger.data();
}

void RewriterView::modelAttached(Model *model)
{
    AbstractView::modelAttached(model);

    ModelAmender differenceHandler(m_textToModelMerger.data());
155
    const QString qmlSource = m_textModifier->text();
156
    if (m_textToModelMerger->load(qmlSource, differenceHandler))
157
        lastCorrectQmlSource = qmlSource;
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
}

void RewriterView::modelAboutToBeDetached(Model * /*model*/)
{
    m_positionStorage->clear();
}

void RewriterView::nodeCreated(const ModelNode &createdNode)
{
    Q_ASSERT(textModifier());
    m_positionStorage->setNodeOffset(createdNode, ModelNodePositionStorage::INVALID_LOCATION);
    if (textToModelMerger()->isActive())
        return;

    modelToTextMerger()->nodeCreated(createdNode);

    if (!isModificationGroupActive())
175
        applyChanges();
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
}

void RewriterView::nodeAboutToBeRemoved(const ModelNode &/*removedNode*/)
{
}

void RewriterView::nodeRemoved(const ModelNode &removedNode, const NodeAbstractProperty &parentProperty, PropertyChangeFlags propertyChange)
{
    Q_ASSERT(textModifier());
    if (textToModelMerger()->isActive())
        return;

    modelToTextMerger()->nodeRemoved(removedNode, parentProperty, propertyChange);

    if (!isModificationGroupActive())
191
        applyChanges();
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
}

void RewriterView::propertiesAboutToBeRemoved(const QList<AbstractProperty> &propertyList)
{
    Q_ASSERT(textModifier());
    if (textToModelMerger()->isActive())
        return;


    foreach (const AbstractProperty &property, propertyList) {
        if (property.isDefaultProperty() && property.isNodeListProperty()) {
            m_removeDefaultPropertyTransaction = beginRewriterTransaction();

            foreach (const ModelNode &node, property.toNodeListProperty().toModelNodeList()) {
                modelToTextMerger()->nodeRemoved(node, property.toNodeAbstractProperty(), AbstractView::NoAdditionalChanges);
            }
        }
    }
}

void RewriterView::propertiesRemoved(const QList<AbstractProperty>& propertyList)
{
    Q_ASSERT(textModifier());
    if (textToModelMerger()->isActive())
        return;

    modelToTextMerger()->propertiesRemoved(propertyList);

    if (m_removeDefaultPropertyTransaction.isValid())
        m_removeDefaultPropertyTransaction.commit();

    if (!isModificationGroupActive())
224
        applyChanges();
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
}

void RewriterView::variantPropertiesChanged(const QList<VariantProperty>& propertyList, PropertyChangeFlags propertyChange)
{
    Q_ASSERT(textModifier());
    if (textToModelMerger()->isActive())
        return;

    QList<AbstractProperty> usefulPropertyList;
    foreach (const VariantProperty &property, propertyList)
        usefulPropertyList.append(property);

    modelToTextMerger()->propertiesChanged(usefulPropertyList, propertyChange);

    if (!isModificationGroupActive())
240
        applyChanges();
241 242 243 244 245 246 247 248 249 250
}

void RewriterView::bindingPropertiesChanged(const QList<BindingProperty>& propertyList, PropertyChangeFlags propertyChange)
{
    Q_ASSERT(textModifier());
    if (textToModelMerger()->isActive())
        return;

    QList<AbstractProperty> usefulPropertyList;
    foreach (const BindingProperty &property, propertyList)
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
        usefulPropertyList.append(property);

    modelToTextMerger()->propertiesChanged(usefulPropertyList, propertyChange);

    if (!isModificationGroupActive())
        applyChanges();
}

void RewriterView::signalHandlerPropertiesChanged(const QVector<SignalHandlerProperty> &propertyList, AbstractView::PropertyChangeFlags propertyChange)
{
    Q_ASSERT(textModifier());
    if (textToModelMerger()->isActive())
        return;

    QList<AbstractProperty> usefulPropertyList;
    foreach (const SignalHandlerProperty &property, propertyList)
267 268 269 270 271
        usefulPropertyList.append(property);

    modelToTextMerger()->propertiesChanged(usefulPropertyList, propertyChange);

    if (!isModificationGroupActive())
272
        applyChanges();
273 274 275 276 277 278 279 280 281 282 283
}

void RewriterView::nodeReparented(const ModelNode &node, const NodeAbstractProperty &newPropertyParent, const NodeAbstractProperty &oldPropertyParent, AbstractView::PropertyChangeFlags propertyChange)
{
    Q_ASSERT(textModifier());
    if (textToModelMerger()->isActive())
        return;

    modelToTextMerger()->nodeReparented(node, newPropertyParent, oldPropertyParent, propertyChange);

    if (!isModificationGroupActive())
284
        applyChanges();
285 286
}

287 288 289 290
void RewriterView::nodeAboutToBeReparented(const ModelNode &/*node*/, const NodeAbstractProperty &/*newPropertyParent*/, const NodeAbstractProperty &/*oldPropertyParent*/, AbstractView::PropertyChangeFlags /*propertyChange*/)
{
}

291 292 293 294 295 296 297 298 299
void RewriterView::importsChanged(const QList<Import> &addedImports, const QList<Import> &removedImports)
{
    foreach (const Import &import, addedImports)
        importAdded(import);

    foreach (const Import &import, removedImports)
        importRemoved(import);

}
300

301 302 303 304 305 306
void RewriterView::importAdded(const Import &import)
{
    Q_ASSERT(textModifier());
    if (textToModelMerger()->isActive())
        return;

307 308 309 310 311 312
    if (import.url() == "Qt")
        foreach (const Import &import, model()->imports()) {
            if (import.url() == "QtQuick")
                return; //QtQuick magic we do not have to add an import for Qt
        }

313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
    modelToTextMerger()->addImport(import);

    if (!isModificationGroupActive())
        applyChanges();
}

void RewriterView::importRemoved(const Import &import)
{
    Q_ASSERT(textModifier());
    if (textToModelMerger()->isActive())
        return;

    modelToTextMerger()->removeImport(import);

    if (!isModificationGroupActive())
        applyChanges();
}

Friedemann Kleint's avatar
Friedemann Kleint committed
331
void RewriterView::fileUrlChanged(const QUrl & /*oldUrl*/, const QUrl & /*newUrl*/)
332 333 334 335 336 337 338 339 340 341 342 343
{
}

void RewriterView::nodeIdChanged(const ModelNode& node, const QString& newId, const QString& oldId)
{
    Q_ASSERT(textModifier());
    if (textToModelMerger()->isActive())
        return;

    modelToTextMerger()->nodeIdChanged(node, newId, oldId);

    if (!isModificationGroupActive())
344
        applyChanges();
345 346
}

347 348
void RewriterView::nodeOrderChanged(const NodeListProperty &listProperty, const ModelNode &movedNode, int /*oldIndex*/)
{
349 350 351 352 353
    Q_ASSERT(textModifier());
    if (textToModelMerger()->isActive())
        return;

    const QList<ModelNode> nodes = listProperty.toModelNodeList();
354

355
    ModelNode trailingNode;
356
    int newIndex = nodes.indexOf(movedNode);
357 358
    if (newIndex + 1 < nodes.size())
        trailingNode = nodes.at(newIndex + 1);
359
    modelToTextMerger()->nodeSlidAround(movedNode, trailingNode);
360 361

    if (!isModificationGroupActive())
362
        applyChanges();
363 364
}

365
void RewriterView::rootNodeTypeChanged(const QString &type, int majorVersion, int minorVersion)
366 367 368 369 370
{
     Q_ASSERT(textModifier());
    if (textToModelMerger()->isActive())
        return;

371
    modelToTextMerger()->nodeTypeChanged(rootModelNode(), type, majorVersion, minorVersion);
372 373

    if (!isModificationGroupActive())
374
        applyChanges();
375 376 377 378
}

void RewriterView::customNotification(const AbstractView * /*view*/, const QString &identifier, const QList<ModelNode> & /* nodeList */, const QList<QVariant> & /*data */)
{
379 380
    if (identifier == StartRewriterAmend || identifier == EndRewriterAmend)
        return; // we emitted this ourselves, so just ignore these notifications.
381 382
}

Friedemann Kleint's avatar
Friedemann Kleint committed
383
void RewriterView::scriptFunctionsChanged(const ModelNode & /*node*/, const QStringList & /*scriptFunctionList*/)
Marco Bubke's avatar
Marco Bubke committed
384 385 386
{
}

387
void RewriterView::instancePropertyChange(const QList<QPair<ModelNode, PropertyName> > & /*propertyList*/)
388 389 390
{
}

Friedemann Kleint's avatar
Friedemann Kleint committed
391
void RewriterView::instancesCompleted(const QVector<ModelNode> & /*completedNodeList*/)
392 393 394
{
}

Friedemann Kleint's avatar
Friedemann Kleint committed
395
void RewriterView::instanceInformationsChange(const QMultiHash<ModelNode, InformationName> & /* informationChangeHash */)
396 397 398 399
{

}

Friedemann Kleint's avatar
Friedemann Kleint committed
400
void RewriterView::instancesRenderImageChanged(const QVector<ModelNode> & /*nodeList*/)
401 402 403 404
{

}

Friedemann Kleint's avatar
Friedemann Kleint committed
405
void RewriterView::instancesPreviewImageChanged(const QVector<ModelNode> & /*nodeList*/)
406 407 408 409
{

}

Friedemann Kleint's avatar
Friedemann Kleint committed
410
void RewriterView::instancesChildrenChanged(const QVector<ModelNode> & /*nodeList*/)
411 412 413 414
{

}

415 416 417 418 419
void RewriterView::instancesToken(const QString &/*tokenName*/, int /*tokenNumber*/, const QVector<ModelNode> &/*nodeVector*/)
{

}

420
void RewriterView::nodeSourceChanged(const ModelNode &, const QString & /*newNodeSource*/)
421 422 423 424
{

}

425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
void RewriterView::rewriterBeginTransaction()
{
    transactionLevel++;
    setModificationGroupActive(true);
}

void RewriterView::rewriterEndTransaction()
{
    transactionLevel--;
    Q_ASSERT(transactionLevel >= 0);
    if (transactionLevel == 0)
    {
        setModificationGroupActive(false);
        applyModificationGroupChanges();
    }
}
441

Friedemann Kleint's avatar
Friedemann Kleint committed
442
void RewriterView::actualStateChanged(const ModelNode & /*node*/)
443 444 445
{
}

446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
void RewriterView::selectedNodesChanged(const QList<ModelNode> & /* selectedNodeList, */, const QList<ModelNode> & /*lastSelectedNodeList */)
{
}

bool RewriterView::isModificationGroupActive() const
{
    return m_modificationGroupActive;
}

void RewriterView::setModificationGroupActive(bool active)
{
    m_modificationGroupActive = active;
}

TextModifier *RewriterView::textModifier() const
{
    return m_textModifier;
}

void RewriterView::setTextModifier(TextModifier *textModifier)
{
    if (m_textModifier)
        disconnect(m_textModifier, SIGNAL(textChanged()), this, SLOT(qmlTextChanged()));

    m_textModifier = textModifier;

    if (m_textModifier)
        connect(m_textModifier, SIGNAL(textChanged()), this, SLOT(qmlTextChanged()));
}

476 477 478 479 480 481 482 483
QString RewriterView::textModifierContent() const
{
    if (textModifier())
        return textModifier()->text();

    return QString();
}

Marco Bubke's avatar
Marco Bubke committed
484 485 486 487 488 489 490 491 492 493
void RewriterView::reactivateTextMofifierChangeSignals()
{
    textModifier()->reactivateChangeSignals();
}

void RewriterView::deactivateTextMofifierChangeSignals()
{
    textModifier()->deactivateChangeSignals();
}

494 495 496
void RewriterView::applyModificationGroupChanges()
{
    Q_ASSERT(transactionLevel == 0);
497
    applyChanges();
498 499
}

500 501
void RewriterView::applyChanges()
{
502 503 504
    if (modelToTextMerger()->hasNoPendingChanges())
        return; // quick exit: nothing to be done.

505 506
    clearErrors();

507
    if (inErrorState()) {
508
        const QString content = textModifierContent();
509
        qDebug() << "RewriterView::applyChanges() got called while in error state. Will do a quick-exit now.";
510 511
        qDebug() << "Content:" << content;
        throw RewritingException(__LINE__, __FUNCTION__, __FILE__, "RewriterView::applyChanges() already in error state", content);
512 513
    }

514 515
    m_differenceHandling = Validate;

516
    try {
517
        modelToTextMerger()->applyChanges();
518
        if (!errors().isEmpty())
519
            enterErrorState(errors().first().description());
520
    } catch (Exception &e) {
521 522 523
        const QString content = textModifierContent();
        qDebug() << "RewriterException:" << m_rewritingErrorMessage;
        qDebug() << "Content:" << content;
524 525 526
        enterErrorState(e.description());
    }

527 528
    m_differenceHandling = Amend;

529
    if (inErrorState()) {
530 531 532
        const QString content = textModifierContent();
        qDebug() << "RewriterException:" << m_rewritingErrorMessage;
        qDebug() << "Content:" << content;
533 534
        if (!errors().isEmpty())
            qDebug() << "Error:" << errors().first().description();
535
        throw RewritingException(__LINE__, __FUNCTION__, __FILE__, m_rewritingErrorMessage, content);
536 537 538
    }
}

539 540 541 542 543 544 545 546 547 548 549
QList<RewriterView::Error> RewriterView::errors() const
{
    return m_errors;
}

void RewriterView::clearErrors()
{
    m_errors.clear();
    emit errorsChanged(m_errors);
}

550
void RewriterView::setErrors(const QList<RewriterView::Error> &errors)
551
{
552
    m_errors = errors;
553 554 555 556 557 558 559 560 561
    emit errorsChanged(m_errors);
}

void RewriterView::addError(const RewriterView::Error &error)
{
    m_errors.append(error);
    emit errorsChanged(m_errors);
}

562 563 564 565 566
void RewriterView::enterErrorState(const QString &errorMessage)
{
    m_rewritingErrorMessage = errorMessage;
}

567 568
void RewriterView::resetToLastCorrectQml()
{
569
    m_textModifier->textDocument()->undo();
570
    m_textModifier->textDocument()->clearUndoRedoStacks(QTextDocument::RedoStack);
571
    ModelAmender differenceHandler(m_textToModelMerger.data());
572
    m_textToModelMerger->load(m_textModifier->text(), differenceHandler);
573 574

    leaveErrorState();
575 576
}

577 578
QMap<ModelNode, QString> RewriterView::extractText(const QList<ModelNode> &nodes) const
{
579
    QmlDesigner::ASTObjectTextExtractor extract(m_textModifier->text());
580 581 582 583 584 585
    QMap<ModelNode, QString> result;

    foreach (const ModelNode &node, nodes) {
        const int nodeLocation = m_positionStorage->nodeOffset(node);

        if (nodeLocation == ModelNodePositionStorage::INVALID_LOCATION)
586
            result.insert(node, QString());
587 588 589 590 591 592 593 594 595 596 597 598
        else
            result.insert(node, extract(nodeLocation));
    }

    return result;
}

int RewriterView::nodeOffset(const ModelNode &node) const
{
    return m_positionStorage->nodeOffset(node);
}

599 600 601 602
/**
 * \return the length of the node's text, or -1 if it wasn't found or if an error
 *         occurred.
 */
603 604
int RewriterView::nodeLength(const ModelNode &node) const
{
605 606 607 608 609 610
    ObjectLengthCalculator objectLengthCalculator;
    unsigned length;
    if (objectLengthCalculator(m_textModifier->text(), nodeOffset(node), length))
        return (int) length;
    else
        return -1;
611 612 613 614 615 616 617 618 619 620 621
}

int RewriterView::firstDefinitionInsideOffset(const ModelNode &node) const
{
    FirstDefinitionFinder firstDefinitionFinder(m_textModifier->text());
    return firstDefinitionFinder(nodeOffset(node));
}

int RewriterView::firstDefinitionInsideLength(const ModelNode &node) const
{
    FirstDefinitionFinder firstDefinitionFinder(m_textModifier->text());
622 623 624 625 626 627 628 629
    const int offset =  firstDefinitionFinder(nodeOffset(node));

    ObjectLengthCalculator objectLengthCalculator;
    unsigned length;
    if (objectLengthCalculator(m_textModifier->text(), offset, length))
        return length;
    else
        return -1;
630 631
}

Thomas Hartmann's avatar
Thomas Hartmann committed
632 633 634 635 636
bool RewriterView::modificationGroupActive()
{
    return m_modificationGroupActive;
}

Marco Bubke's avatar
Marco Bubke committed
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
static bool isInNodeDefinition(int nodeTextOffset, int nodeTextLength, int cursorPosition)
{
    return (nodeTextOffset <= cursorPosition) && (nodeTextOffset + nodeTextLength > cursorPosition);
}

ModelNode RewriterView::nodeAtTextCursorPosition(int cursorPosition) const
{
    const QList<ModelNode> allNodes = allModelNodes();

    ModelNode nearestNode;
    int nearestNodeTextOffset = -1;

    foreach (const ModelNode &currentNode, allNodes) {
        const int nodeTextOffset = nodeOffset(currentNode);
        const int nodeTextLength = nodeLength(currentNode);
        if (isInNodeDefinition(nodeTextOffset, nodeTextLength, cursorPosition)
            && (nodeTextOffset > nearestNodeTextOffset)) {
            nearestNode = currentNode;
            nearestNodeTextOffset = nodeTextOffset;
        }
    }

    return nearestNode;
}

662 663 664 665 666 667 668 669
bool RewriterView::renameId(const QString& oldId, const QString& newId)
{
    if (textModifier())
        return textModifier()->renameId(oldId, newId);

    return false;
}

Thomas Hartmann's avatar
Thomas Hartmann committed
670
const QmlJS::ScopeChain *RewriterView::scopeChain() const
671
{
Christian Kamm's avatar
Christian Kamm committed
672
    return textToModelMerger()->scopeChain();
673 674
}

675
const QmlJS::Document *RewriterView::document() const
676 677 678 679
{
    return textToModelMerger()->document();
}

680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
static inline QString getUrlFromType(const QString& typeName)
{
    QStringList nameComponents = typeName.split('.');
    QString result;

    for (int i = 0; i < (nameComponents.count() - 1); i++) {
        result += nameComponents.at(i);
    }

    return result;
}

QString RewriterView::convertTypeToImportAlias(const QString &type) const
{
    QString url;
    QString simplifiedType = type;
    if (type.contains('.')) {
        QStringList nameComponents = type.split('.');
        url = getUrlFromType(type);
        simplifiedType = nameComponents.last();
    }

    QString alias;
    if (!url.isEmpty()) {
        foreach (const Import &import, model()->imports()) {
            if (import.url() == url) {
                alias = import.alias();
                break;
            }
            if (import.file() == url) {
                alias = import.alias();
                break;
            }
        }
    }

    QString result;

    if (!alias.isEmpty())
        result = alias + '.';

    result += simplifiedType;

    return result;
}

726 727 728 729 730 731 732 733
QString RewriterView::pathForImport(const Import &import)
{
    if (scopeChain() && scopeChain()->context() && document()) {
        const QString importStr = import.isFileImport() ? import.file() : import.url();
        const QmlJS::Imports *imports = scopeChain()->context()->imports(document());

        QmlJS::ImportInfo importInfo;

734
        foreach (const QmlJS::Import &qmljsImport, imports->all()) {
735
            if (qmljsImport.info.name() == importStr)
736 737 738 739 740 741 742 743 744
                importInfo = qmljsImport.info;
        }
        const QString importPath = importInfo.path();
        return importPath;
    }

    return QString();
}

745 746
void RewriterView::qmlTextChanged()
{
747 748 749
    if (inErrorState())
        return;

750 751 752
    if (m_textToModelMerger && m_textModifier) {
        const QString newQmlText = m_textModifier->text();

753 754 755 756 757
#if 0
        qDebug() << Q_FUNC_INFO;
        qDebug() << "old:" << lastCorrectQmlSource;
        qDebug() << "new:" << newQmlText;
#endif
758 759 760 761

        switch (m_differenceHandling) {
            case Validate: {
                ModelValidator differenceHandler(m_textToModelMerger.data());
762
                if (m_textToModelMerger->load(newQmlText, differenceHandler))
763
                    lastCorrectQmlSource = newQmlText;
764 765 766 767 768 769 770
                break;
            }

            case Amend:
            default: {
                emitCustomNotification(StartRewriterAmend);
                ModelAmender differenceHandler(m_textToModelMerger.data());
771
                if (m_textToModelMerger->load(newQmlText, differenceHandler))
772
                    lastCorrectQmlSource = newQmlText;
773 774 775 776 777 778 779
                emitCustomNotification(EndRewriterAmend);
                break;
            }
        }
    }
}

780 781 782 783 784 785
void RewriterView::delayedSetup()
{
    if (m_textToModelMerger)
        m_textToModelMerger->delayedSetup();
}

786
} //QmlDesigner