rewriterview.cpp 21.3 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
32
33
34

#include <filemanager/astobjecttextextractor.h>
#include <filemanager/objectlengthcalculator.h>
#include <filemanager/firstdefinitionfinder.h>
#include <customnotifications.h>

35
36
#include <qmljs/parser/qmljsengine_p.h>

37
#include "rewriterview.h"
38
#include "rewritingexception.h"
39
40
41
42
43
44
45
46
#include "textmodifier.h"
#include "texttomodelmerger.h"
#include "modelnodepositionstorage.h"
#include "modeltotextmerger.h"
#include "nodelistproperty.h"
#include "nodeproperty.h"
#include "invalidmodelnodeexception.h"

47
48


49
50
51
52
53
54
55
56
57
58
59
using namespace QmlDesigner::Internal;

namespace QmlDesigner {

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

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

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

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


88
89
90
91
QString RewriterView::Error::toString() const
{
    QString str;

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

97
98
99
100
101
102
103
104
105
106
107
108
109
    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());
    }

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

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

    if (!str.isEmpty())
        QLatin1String(": ");
119
120
121
122
123
124
125
126
127
128
129
130
131
    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),
132
133
        transactionLevel(0),
        m_checkErrors(true)
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
{
}

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());
157
    const QString qmlSource = m_textModifier->text();
158
    if (m_textToModelMerger->load(qmlSource, differenceHandler))
159
        lastCorrectQmlSource = qmlSource;
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
}

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

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())
193
        applyChanges();
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
224
225
}

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

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

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

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

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

    if (!isModificationGroupActive())
258
        applyChanges();
259
260
261
262
263
264
265
266
267
268
269
}

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())
270
        applyChanges();
271
272
}

273
274
275
276
void RewriterView::nodeAboutToBeReparented(const ModelNode &/*node*/, const NodeAbstractProperty &/*newPropertyParent*/, const NodeAbstractProperty &/*oldPropertyParent*/, AbstractView::PropertyChangeFlags /*propertyChange*/)
{
}

277
278
279
280
281
282
283
284
285
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);

}
286

287
288
289
290
291
292
void RewriterView::importAdded(const Import &import)
{
    Q_ASSERT(textModifier());
    if (textToModelMerger()->isActive())
        return;

293
294
295
296
297
298
    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
        }

299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
    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
317
void RewriterView::fileUrlChanged(const QUrl & /*oldUrl*/, const QUrl & /*newUrl*/)
318
319
320
321
322
323
324
325
326
327
328
329
{
}

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())
330
        applyChanges();
331
332
}

333
334
void RewriterView::nodeOrderChanged(const NodeListProperty &listProperty, const ModelNode &movedNode, int /*oldIndex*/)
{
335
336
337
338
339
    Q_ASSERT(textModifier());
    if (textToModelMerger()->isActive())
        return;

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

341
    ModelNode trailingNode;
342
    int newIndex = nodes.indexOf(movedNode);
343
344
    if (newIndex + 1 < nodes.size())
        trailingNode = nodes.at(newIndex + 1);
345
    modelToTextMerger()->nodeSlidAround(movedNode, trailingNode);
346
347

    if (!isModificationGroupActive())
348
        applyChanges();
349
350
}

351
void RewriterView::rootNodeTypeChanged(const QString &type, int majorVersion, int minorVersion)
352
353
354
355
356
{
     Q_ASSERT(textModifier());
    if (textToModelMerger()->isActive())
        return;

357
    modelToTextMerger()->nodeTypeChanged(rootModelNode(), type, majorVersion, minorVersion);
358
359

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

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

Friedemann Kleint's avatar
Friedemann Kleint committed
369
void RewriterView::scriptFunctionsChanged(const ModelNode & /*node*/, const QStringList & /*scriptFunctionList*/)
Marco Bubke's avatar
Marco Bubke committed
370
371
372
{
}

373
void RewriterView::instancePropertyChange(const QList<QPair<ModelNode, PropertyName> > & /*propertyList*/)
374
375
376
{
}

Friedemann Kleint's avatar
Friedemann Kleint committed
377
void RewriterView::instancesCompleted(const QVector<ModelNode> & /*completedNodeList*/)
378
379
380
{
}

Friedemann Kleint's avatar
Friedemann Kleint committed
381
void RewriterView::instanceInformationsChange(const QMultiHash<ModelNode, InformationName> & /* informationChangeHash */)
382
383
384
385
{

}

Friedemann Kleint's avatar
Friedemann Kleint committed
386
void RewriterView::instancesRenderImageChanged(const QVector<ModelNode> & /*nodeList*/)
387
388
389
390
{

}

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

}

Friedemann Kleint's avatar
Friedemann Kleint committed
396
void RewriterView::instancesChildrenChanged(const QVector<ModelNode> & /*nodeList*/)
397
398
399
400
{

}

401
402
403
404
405
void RewriterView::instancesToken(const QString &/*tokenName*/, int /*tokenNumber*/, const QVector<ModelNode> &/*nodeVector*/)
{

}

406
void RewriterView::nodeSourceChanged(const ModelNode &, const QString & /*newNodeSource*/)
407
408
409
410
{

}

411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
void RewriterView::rewriterBeginTransaction()
{
    transactionLevel++;
    setModificationGroupActive(true);
}

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

Friedemann Kleint's avatar
Friedemann Kleint committed
428
void RewriterView::actualStateChanged(const ModelNode & /*node*/)
429
430
431
{
}

432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
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()));
}

462
463
464
465
466
467
468
469
QString RewriterView::textModifierContent() const
{
    if (textModifier())
        return textModifier()->text();

    return QString();
}

Marco Bubke's avatar
Marco Bubke committed
470
471
472
473
474
475
476
477
478
479
void RewriterView::reactivateTextMofifierChangeSignals()
{
    textModifier()->reactivateChangeSignals();
}

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

480
481
482
void RewriterView::applyModificationGroupChanges()
{
    Q_ASSERT(transactionLevel == 0);
483
    applyChanges();
484
485
}

486
487
void RewriterView::applyChanges()
{
488
489
490
    if (modelToTextMerger()->hasNoPendingChanges())
        return; // quick exit: nothing to be done.

491
492
    clearErrors();

493
    if (inErrorState()) {
494
        const QString content = textModifierContent();
495
        qDebug() << "RewriterView::applyChanges() got called while in error state. Will do a quick-exit now.";
496
497
        qDebug() << "Content:" << content;
        throw RewritingException(__LINE__, __FUNCTION__, __FILE__, "RewriterView::applyChanges() already in error state", content);
498
499
    }

500
501
    m_differenceHandling = Validate;

502
    try {
503
        modelToTextMerger()->applyChanges();
504
        if (!errors().isEmpty())
505
            enterErrorState(errors().first().description());
506
    } catch (Exception &e) {
507
508
509
        const QString content = textModifierContent();
        qDebug() << "RewriterException:" << m_rewritingErrorMessage;
        qDebug() << "Content:" << content;
510
511
512
        enterErrorState(e.description());
    }

513
514
    m_differenceHandling = Amend;

515
    if (inErrorState()) {
516
517
518
        const QString content = textModifierContent();
        qDebug() << "RewriterException:" << m_rewritingErrorMessage;
        qDebug() << "Content:" << content;
519
520
        if (!errors().isEmpty())
            qDebug() << "Error:" << errors().first().description();
521
        throw RewritingException(__LINE__, __FUNCTION__, __FILE__, m_rewritingErrorMessage, content);
522
523
524
    }
}

525
526
527
528
529
530
531
532
533
534
535
QList<RewriterView::Error> RewriterView::errors() const
{
    return m_errors;
}

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

536
void RewriterView::setErrors(const QList<RewriterView::Error> &errors)
537
{
538
    m_errors = errors;
539
540
541
542
543
544
545
546
547
    emit errorsChanged(m_errors);
}

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

548
549
550
551
552
void RewriterView::enterErrorState(const QString &errorMessage)
{
    m_rewritingErrorMessage = errorMessage;
}

553
554
void RewriterView::resetToLastCorrectQml()
{
555
    m_textModifier->textDocument()->undo();
556
    m_textModifier->textDocument()->clearUndoRedoStacks(QTextDocument::RedoStack);
557
    ModelAmender differenceHandler(m_textToModelMerger.data());
558
    m_textToModelMerger->load(m_textModifier->text().toUtf8(), differenceHandler);
559
560

    leaveErrorState();
561
562
}

563
564
QMap<ModelNode, QString> RewriterView::extractText(const QList<ModelNode> &nodes) const
{
565
    QmlDesigner::ASTObjectTextExtractor extract(m_textModifier->text());
566
567
568
569
570
571
    QMap<ModelNode, QString> result;

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

        if (nodeLocation == ModelNodePositionStorage::INVALID_LOCATION)
572
            result.insert(node, QString());
573
574
575
576
577
578
579
580
581
582
583
584
        else
            result.insert(node, extract(nodeLocation));
    }

    return result;
}

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

585
586
587
588
/**
 * \return the length of the node's text, or -1 if it wasn't found or if an error
 *         occurred.
 */
589
590
int RewriterView::nodeLength(const ModelNode &node) const
{
591
592
593
594
595
596
    ObjectLengthCalculator objectLengthCalculator;
    unsigned length;
    if (objectLengthCalculator(m_textModifier->text(), nodeOffset(node), length))
        return (int) length;
    else
        return -1;
597
598
599
600
601
602
603
604
605
606
607
}

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());
608
609
610
611
612
613
614
615
    const int offset =  firstDefinitionFinder(nodeOffset(node));

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

Thomas Hartmann's avatar
Thomas Hartmann committed
618
619
620
621
622
bool RewriterView::modificationGroupActive()
{
    return m_modificationGroupActive;
}

Marco Bubke's avatar
Marco Bubke committed
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
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;
}

648
649
650
651
652
653
654
655
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
656
const QmlJS::ScopeChain *RewriterView::scopeChain() const
657
{
Christian Kamm's avatar
Christian Kamm committed
658
    return textToModelMerger()->scopeChain();
659
660
}

661
const QmlJS::Document *RewriterView::document() const
662
663
664
665
{
    return textToModelMerger()->document();
}

666
667
668
669
670
671
672
673
674
675
676
677
678
679
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
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;
}

712
713
714
715
716
717
718
719
720
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;

        foreach (QmlJS::Import qmljsImport, imports->all()) {
721
            if (qmljsImport.info.name() == importStr)
722
723
724
725
726
727
728
729
730
                importInfo = qmljsImport.info;
        }
        const QString importPath = importInfo.path();
        return importPath;
    }

    return QString();
}

Marco Bubke's avatar
Marco Bubke committed
731
732
733
734
QWidget *RewriterView::widget()
{
    return 0;
}
735

736
737
void RewriterView::qmlTextChanged()
{
738
739
740
    if (inErrorState())
        return;

741
742
743
    if (m_textToModelMerger && m_textModifier) {
        const QString newQmlText = m_textModifier->text();

744
745
746
747
748
#if 0
        qDebug() << Q_FUNC_INFO;
        qDebug() << "old:" << lastCorrectQmlSource;
        qDebug() << "new:" << newQmlText;
#endif
749
750
751
752

        switch (m_differenceHandling) {
            case Validate: {
                ModelValidator differenceHandler(m_textToModelMerger.data());
753
                if (m_textToModelMerger->load(newQmlText.toUtf8(), differenceHandler))
754
                    lastCorrectQmlSource = newQmlText;
755
756
757
758
759
760
761
                break;
            }

            case Amend:
            default: {
                emitCustomNotification(StartRewriterAmend);
                ModelAmender differenceHandler(m_textToModelMerger.data());
762
                if (m_textToModelMerger->load(newQmlText, differenceHandler))
763
                    lastCorrectQmlSource = newQmlText;
764
765
766
767
768
769
770
                emitCustomNotification(EndRewriterAmend);
                break;
            }
        }
    }
}

771
772
773
774
775
776
void RewriterView::delayedSetup()
{
    if (m_textToModelMerger)
        m_textToModelMerger->delayedSetup();
}

777
} //QmlDesigner