rewriterview.cpp 21.9 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
{
}

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)
{
152
153
154
    if (model && model->textModifier())
        setTextModifier(model->textModifier());

155
156
157
    AbstractView::modelAttached(model);

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

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())
178
        applyChanges();
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
}

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())
194
        applyChanges();
195
196
197
198
199
200
201
202
203
204
205
}

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


    foreach (const AbstractProperty &property, propertyList) {
        if (property.isDefaultProperty() && property.isNodeListProperty()) {
206
            m_removeDefaultPropertyTransaction = beginRewriterTransaction(QByteArrayLiteral("RewriterView::propertiesAboutToBeRemoved"));
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226

            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())
227
        applyChanges();
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
}

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())
243
        applyChanges();
244
245
246
247
248
249
250
251
252
253
}

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

    QList<AbstractProperty> usefulPropertyList;
    foreach (const BindingProperty &property, propertyList)
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
        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)
270
271
272
273
274
        usefulPropertyList.append(property);

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

    if (!isModificationGroupActive())
275
        applyChanges();
276
277
278
279
280
281
282
283
284
285
286
}

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())
287
        applyChanges();
288
289
}

290
291
292
293
void RewriterView::nodeAboutToBeReparented(const ModelNode &/*node*/, const NodeAbstractProperty &/*newPropertyParent*/, const NodeAbstractProperty &/*oldPropertyParent*/, AbstractView::PropertyChangeFlags /*propertyChange*/)
{
}

294
295
296
297
298
299
300
301
302
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);

}
303

304
305
306
307
308
309
void RewriterView::importAdded(const Import &import)
{
    Q_ASSERT(textModifier());
    if (textToModelMerger()->isActive())
        return;

310
311
312
313
314
315
    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
        }

316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
    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
334
void RewriterView::fileUrlChanged(const QUrl & /*oldUrl*/, const QUrl & /*newUrl*/)
335
336
337
338
339
340
341
342
343
344
345
346
{
}

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())
347
        applyChanges();
348
349
}

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

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

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

    if (!isModificationGroupActive())
365
        applyChanges();
366
367
}

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

374
    modelToTextMerger()->nodeTypeChanged(rootModelNode(), type, majorVersion, minorVersion);
375
376

    if (!isModificationGroupActive())
377
        applyChanges();
378
379
380
381
}

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

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

390
void RewriterView::instancePropertyChange(const QList<QPair<ModelNode, PropertyName> > & /*propertyList*/)
391
392
393
{
}

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

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

}

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

}

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

}

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

}

418
419
420
421
422
void RewriterView::instancesToken(const QString &/*tokenName*/, int /*tokenNumber*/, const QVector<ModelNode> &/*nodeVector*/)
{

}

423
void RewriterView::nodeSourceChanged(const ModelNode &, const QString & /*newNodeSource*/)
424
425
426
427
{

}

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

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

445
void RewriterView::currentStateChanged(const ModelNode & /*node*/)
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
476
477
478
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()));
}

479
480
481
482
483
484
485
486
QString RewriterView::textModifierContent() const
{
    if (textModifier())
        return textModifier()->text();

    return QString();
}

Marco Bubke's avatar
Marco Bubke committed
487
488
void RewriterView::reactivateTextMofifierChangeSignals()
{
Thomas Hartmann's avatar
Thomas Hartmann committed
489
490
    if (textModifier())
        textModifier()->reactivateChangeSignals();
Marco Bubke's avatar
Marco Bubke committed
491
492
493
494
}

void RewriterView::deactivateTextMofifierChangeSignals()
{
Thomas Hartmann's avatar
Thomas Hartmann committed
495
496
    if (textModifier())
        textModifier()->deactivateChangeSignals();
Marco Bubke's avatar
Marco Bubke committed
497
498
}

499
500
501
void RewriterView::applyModificationGroupChanges()
{
    Q_ASSERT(transactionLevel == 0);
502
    applyChanges();
503
504
}

505
506
void RewriterView::applyChanges()
{
507
508
509
    if (modelToTextMerger()->hasNoPendingChanges())
        return; // quick exit: nothing to be done.

510
511
    clearErrors();

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

519
520
    m_differenceHandling = Validate;

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

532
533
    m_differenceHandling = Amend;

534
    if (inErrorState()) {
535
536
537
        const QString content = textModifierContent();
        qDebug() << "RewriterException:" << m_rewritingErrorMessage;
        qDebug() << "Content:" << content;
538
539
        if (!errors().isEmpty())
            qDebug() << "Error:" << errors().first().description();
540
        throw RewritingException(__LINE__, __FUNCTION__, __FILE__, m_rewritingErrorMessage, content);
541
542
543
    }
}

544
545
546
547
548
549
550
551
552
553
554
QList<RewriterView::Error> RewriterView::errors() const
{
    return m_errors;
}

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

555
void RewriterView::setErrors(const QList<RewriterView::Error> &errors)
556
{
557
    m_errors = errors;
558
559
560
561
562
563
564
565
566
    emit errorsChanged(m_errors);
}

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

567
568
569
570
571
void RewriterView::enterErrorState(const QString &errorMessage)
{
    m_rewritingErrorMessage = errorMessage;
}

572
573
void RewriterView::resetToLastCorrectQml()
{
574
    m_textModifier->textDocument()->undo();
575
    m_textModifier->textDocument()->clearUndoRedoStacks(QTextDocument::RedoStack);
576
    ModelAmender differenceHandler(m_textToModelMerger.data());
577
    m_textToModelMerger->load(m_textModifier->text(), differenceHandler);
578
579

    leaveErrorState();
580
581
}

582
583
QMap<ModelNode, QString> RewriterView::extractText(const QList<ModelNode> &nodes) const
{
584
    QmlDesigner::ASTObjectTextExtractor extract(m_textModifier->text());
585
586
587
588
589
590
    QMap<ModelNode, QString> result;

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

        if (nodeLocation == ModelNodePositionStorage::INVALID_LOCATION)
591
            result.insert(node, QString());
592
593
594
595
596
597
598
599
600
601
602
603
        else
            result.insert(node, extract(nodeLocation));
    }

    return result;
}

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

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

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());
627
628
629
630
631
632
633
634
    const int offset =  firstDefinitionFinder(nodeOffset(node));

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

Thomas Hartmann's avatar
Thomas Hartmann committed
637
638
639
640
641
bool RewriterView::modificationGroupActive()
{
    return m_modificationGroupActive;
}

Marco Bubke's avatar
Marco Bubke committed
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
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;
}

667
668
669
670
671
672
673
674
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
675
const QmlJS::ScopeChain *RewriterView::scopeChain() const
676
{
Christian Kamm's avatar
Christian Kamm committed
677
    return textToModelMerger()->scopeChain();
678
679
}

680
const QmlJS::Document *RewriterView::document() const
681
682
683
684
{
    return textToModelMerger()->document();
}

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
726
727
728
729
730
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;
}

731
732
733
734
735
736
737
738
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;

739
        foreach (const QmlJS::Import &qmljsImport, imports->all()) {
740
            if (qmljsImport.info.name() == importStr)
741
742
743
744
745
746
747
748
749
                importInfo = qmljsImport.info;
        }
        const QString importPath = importInfo.path();
        return importPath;
    }

    return QString();
}

750
751
void RewriterView::qmlTextChanged()
{
752
753
754
    if (inErrorState())
        return;

755
756
757
    if (m_textToModelMerger && m_textModifier) {
        const QString newQmlText = m_textModifier->text();

758
759
760
761
762
#if 0
        qDebug() << Q_FUNC_INFO;
        qDebug() << "old:" << lastCorrectQmlSource;
        qDebug() << "new:" << newQmlText;
#endif
763
764
765
766

        switch (m_differenceHandling) {
            case Validate: {
                ModelValidator differenceHandler(m_textToModelMerger.data());
767
                if (m_textToModelMerger->load(newQmlText, differenceHandler))
768
                    lastCorrectQmlSource = newQmlText;
769
770
771
772
773
774
775
                break;
            }

            case Amend:
            default: {
                emitCustomNotification(StartRewriterAmend);
                ModelAmender differenceHandler(m_textToModelMerger.data());
776
                if (m_textToModelMerger->load(newQmlText, differenceHandler))
777
                    lastCorrectQmlSource = newQmlText;
778
779
780
781
782
783
784
                emitCustomNotification(EndRewriterAmend);
                break;
            }
        }
    }
}

785
786
787
788
789
790
void RewriterView::delayedSetup()
{
    if (m_textToModelMerger)
        m_textToModelMerger->delayedSetup();
}

791
} //QmlDesigner