qmljsmodelmanager.cpp 35.8 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

Roberto Raggi's avatar
Roberto Raggi committed
30
#include "qmljsmodelmanager.h"
31
#include "qmljstoolsconstants.h"
32
#include "qmljsplugindumper.h"
33
#include "qmljsfindexportedcpptypes.h"
34
#include "qmljssemanticinfo.h"
35
#include "qmljsbundleprovider.h"
36
37
38
39

#include <coreplugin/icore.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/progressmanager/progressmanager.h>
40
#include <coreplugin/mimedatabase.h>
41
#include <coreplugin/messagemanager.h>
42
#include <cpptools/ModelManagerInterface.h>
43
#include <cplusplus/CppDocument.h>
44
#include <qmljs/qmljscontext.h>
45
#include <qmljs/qmljsbind.h>
46
#include <qmljs/qmljsbundle.h>
47
#include <qmljs/parser/qmldirparser_p.h>
48
#include <texteditor/itexteditor.h>
49
#include <texteditor/basetexteditor.h>
50
51
52
#include <projectexplorer/buildconfiguration.h>
#include <projectexplorer/kit.h>
#include <projectexplorer/kitinformation.h>
53
#include <projectexplorer/project.h>
54
#include <projectexplorer/projectexplorer.h>
55
#include <projectexplorer/projectexplorerconstants.h>
56
#include <projectexplorer/session.h>
57
58
#include <projectexplorer/target.h>
#include <projectexplorer/toolchain.h>
59
#include <qtsupport/baseqtversion.h>
60
61
62
#include <qtsupport/qtkitinformation.h>
#include <qtsupport/qmldumptool.h>
#include <qtsupport/qtsupportconstants.h>
63
#include <utils/hostosinfo.h>
64
#include <extensionsystem/pluginmanager.h>
65

66
67
68
69
70
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QLibraryInfo>
#include <QtConcurrentRun>
71
#include <utils/runextensions.h>
72
73
74
#include <QTextStream>
#include <QCoreApplication>
#include <QTimer>
75
#include <QRegExp>
76

77
#include <QDebug>
78

79
using namespace QmlJS;
80
81
using namespace QmlJSTools;
using namespace QmlJSTools::Internal;
82

83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180

ModelManagerInterface::ProjectInfo QmlJSTools::defaultProjectInfoForProject(
        ProjectExplorer::Project *project)
{
    ModelManagerInterface::ProjectInfo projectInfo(project);
    ProjectExplorer::Target *activeTarget = 0;
    if (project) {
        Core::MimeDatabase *db = Core::ICore::mimeDatabase();
        QList<Core::MimeGlobPattern> globs;
        QList<Core::MimeType> mimeTypes = db->mimeTypes();
        foreach (const Core::MimeType &mimeType, mimeTypes)
            if (mimeType.type() == QLatin1String(Constants::QML_MIMETYPE)
                    || mimeType.subClassesOf().contains(QLatin1String(Constants::QML_MIMETYPE)))
                globs << mimeType.globPatterns();
        if (globs.isEmpty())
            globs << Core::MimeGlobPattern(QRegExp(QLatin1String(".*\\.(?:qbs|qml|qmltypes|qmlproject)$")));
        foreach (const QString &filePath
                 , project->files(ProjectExplorer::Project::ExcludeGeneratedFiles))
            foreach (const Core::MimeGlobPattern &glob, globs)
                if (glob.regExp().exactMatch(filePath))
                    projectInfo.sourceFiles << filePath;
        activeTarget = project->activeTarget();
    }
    ProjectExplorer::Kit *activeKit = activeTarget ? activeTarget->kit() :
                                           ProjectExplorer::KitManager::instance()->defaultKit();
    QtSupport::BaseQtVersion *qtVersion = QtSupport::QtKitInformation::qtVersion(activeKit);

    bool preferDebugDump = false;
    bool setPreferDump = false;
    projectInfo.tryQmlDump = false;

    if (activeTarget) {
        if (ProjectExplorer::BuildConfiguration *bc = activeTarget->activeBuildConfiguration()) {
            preferDebugDump = bc->buildType() == ProjectExplorer::BuildConfiguration::Debug;
            setPreferDump = true;
        }
    }
    if (!setPreferDump && qtVersion)
        preferDebugDump = (qtVersion->defaultBuildConfig() & QtSupport::BaseQtVersion::DebugBuild);
    if (qtVersion && qtVersion->isValid()) {
        projectInfo.tryQmlDump = project && (
                    qtVersion->type() == QLatin1String(QtSupport::Constants::DESKTOPQT)
                    || qtVersion->type() == QLatin1String(QtSupport::Constants::SIMULATORQT));
        projectInfo.qtQmlPath = qtVersion->qmakeProperty("QT_INSTALL_QML");
        projectInfo.qtImportsPath = qtVersion->qmakeProperty("QT_INSTALL_IMPORTS");
        projectInfo.qtVersionString = qtVersion->qtVersionString();
    }

    if (projectInfo.tryQmlDump) {
        ProjectExplorer::ToolChain *toolChain =
                ProjectExplorer::ToolChainKitInformation::toolChain(activeKit);
        QtSupport::QmlDumpTool::pathAndEnvironment(project, qtVersion,
                                                   toolChain,
                                                   preferDebugDump, &projectInfo.qmlDumpPath,
                                                   &projectInfo.qmlDumpEnvironment);
    } else {
        projectInfo.qmlDumpPath.clear();
        projectInfo.qmlDumpEnvironment.clear();
    }
    setupProjectInfoQmlBundles(projectInfo);
    return projectInfo;
}

void QmlJSTools::setupProjectInfoQmlBundles(ModelManagerInterface::ProjectInfo &projectInfo)
{
    ProjectExplorer::Target *activeTarget = 0;
    if (projectInfo.project) {
        activeTarget = projectInfo.project->activeTarget();
    }
    ProjectExplorer::Kit *activeKit = activeTarget
            ? activeTarget->kit() : ProjectExplorer::KitManager::instance()->defaultKit();
    QHash<QString, QString> replacements;
    replacements.insert(QLatin1String("$(QT_INSTALL_IMPORTS)"), projectInfo.qtImportsPath);
    replacements.insert(QLatin1String("$(QT_INSTALL_QML)"), projectInfo.qtQmlPath);

    QList<IBundleProvider *> bundleProviders =
            ExtensionSystem::PluginManager::getObjects<IBundleProvider>();

    foreach (IBundleProvider *bp, bundleProviders) {
        if (bp)
            bp->mergeBundlesForKit(activeKit, projectInfo.activeBundle, replacements);
    }
    projectInfo.extendedBundle = projectInfo.activeBundle;

    if (projectInfo.project) {
        QSet<ProjectExplorer::Kit *> currentKits;
        foreach (const ProjectExplorer::Target *t, projectInfo.project->targets())
            if (t->kit())
                currentKits.insert(t->kit());
        currentKits.remove(activeKit);
        foreach (ProjectExplorer::Kit *kit, currentKits) {
            foreach (IBundleProvider *bp, bundleProviders)
                if (bp)
                    bp->mergeBundlesForKit(kit, projectInfo.extendedBundle, replacements);
        }
    }
}

181
182
static QStringList environmentImportPaths();

183
184
185
186
187
188
static void mergeSuffixes(QStringList &l1, const QStringList &l2)
{
    if (!l2.isEmpty())
        l1 = l2;
}

189
190
QmlJS::Document::Language QmlJSTools::languageOfFile(const QString &fileName)
{
191
192
    QStringList jsSuffixes(QLatin1String("js"));
    QStringList qmlSuffixes(QLatin1String("qml"));
193
    QStringList qmlProjectSuffixes(QLatin1String("qmlproject"));
194
    QStringList jsonSuffixes(QLatin1String("json"));
195
    QStringList qbsSuffixes(QLatin1String("qbs"));
196
197

    if (Core::ICore::instance()) {
hjk's avatar
hjk committed
198
        Core::MimeDatabase *db = Core::ICore::mimeDatabase();
199
        Core::MimeType jsSourceTy = db->findByType(QLatin1String(Constants::JS_MIMETYPE));
200
        mergeSuffixes(jsSuffixes, jsSourceTy.suffixes());
201
        Core::MimeType qmlSourceTy = db->findByType(QLatin1String(Constants::QML_MIMETYPE));
202
203
204
205
206
        mergeSuffixes(qmlSuffixes, qmlSourceTy.suffixes());
        Core::MimeType qbsSourceTy = db->findByType(QLatin1String(Constants::QBS_MIMETYPE));
        mergeSuffixes(qbsSuffixes, qbsSourceTy.suffixes());
        Core::MimeType qmlProjectSourceTy = db->findByType(QLatin1String(Constants::QMLPROJECT_MIMETYPE));
        mergeSuffixes(qbsSuffixes, qmlProjectSourceTy.suffixes());
207
        Core::MimeType jsonSourceTy = db->findByType(QLatin1String(Constants::JSON_MIMETYPE));
208
        mergeSuffixes(jsonSuffixes, jsonSourceTy.suffixes());
209
210
211
212
213
214
    }

    const QFileInfo info(fileName);
    const QString fileSuffix = info.suffix();
    if (jsSuffixes.contains(fileSuffix))
        return QmlJS::Document::JavaScriptLanguage;
215
216
217
    if (qbsSuffixes.contains(fileSuffix))
        return QmlJS::Document::QmlQbsLanguage;
    if (qmlSuffixes.contains(fileSuffix) || qmlProjectSuffixes.contains(fileSuffix))
218
        return QmlJS::Document::QmlLanguage;
219
220
    if (jsonSuffixes.contains(fileSuffix))
        return QmlJS::Document::JsonLanguage;
221
222
223
224
225
226
227
    return QmlJS::Document::UnknownLanguage;
}

QStringList QmlJSTools::qmlAndJsGlobPatterns()
{
    QStringList pattern;
    if (Core::ICore::instance()) {
hjk's avatar
hjk committed
228
        Core::MimeDatabase *db = Core::ICore::mimeDatabase();
229
230
        Core::MimeType jsSourceTy = db->findByType(QLatin1String(Constants::JS_MIMETYPE));
        Core::MimeType qmlSourceTy = db->findByType(QLatin1String(Constants::QML_MIMETYPE));
231
232
233
234
235
236
237

        QStringList pattern;
        foreach (const Core::MimeGlobPattern &glob, jsSourceTy.globPatterns())
            pattern << glob.regExp().pattern();
        foreach (const Core::MimeGlobPattern &glob, qmlSourceTy.globPatterns())
            pattern << glob.regExp().pattern();
    } else {
238
        pattern << QLatin1String("*.qml") << QLatin1String("*.js");
239
240
241
242
    }
    return pattern;
}

Roberto Raggi's avatar
Roberto Raggi committed
243
244
ModelManager::ModelManager(QObject *parent):
        ModelManagerInterface(parent),
245
        m_pluginDumper(new PluginDumper(this))
246
247
248
{
    m_synchronizer.setCancelOnWait(true);

249
250
251
    m_updateCppQmlTypesTimer = new QTimer(this);
    m_updateCppQmlTypesTimer->setInterval(1000);
    m_updateCppQmlTypesTimer->setSingleShot(true);
252
    connect(m_updateCppQmlTypesTimer, SIGNAL(timeout()), SLOT(startCppQmlTypeUpdate()));
253

254
    qRegisterMetaType<QmlJS::Document::Ptr>("QmlJS::Document::Ptr");
255
    qRegisterMetaType<QmlJS::LibraryInfo>("QmlJS::LibraryInfo");
256
    qRegisterMetaType<QmlJSTools::SemanticInfo>("QmlJSTools::SemanticInfo");
257

258
    loadQmlTypeDescriptions();
259
260

    m_defaultImportPaths << environmentImportPaths();
261
    updateImportPaths();
262
263
}

264
265
266
267
268
269
ModelManager::~ModelManager()
{
    m_cppQmlTypesUpdater.cancel();
    m_cppQmlTypesUpdater.waitForFinished();
}

270
271
272
273
274
void ModelManager::delayedInitialization()
{
    CPlusPlus::CppModelManagerInterface *cppModelManager =
            CPlusPlus::CppModelManagerInterface::instance();
    if (cppModelManager) {
275
276
        // It's important to have a direct connection here so we can prevent
        // the source and AST of the cpp document being cleaned away.
277
        connect(cppModelManager, SIGNAL(documentUpdated(CPlusPlus::Document::Ptr)),
278
                this, SLOT(maybeQueueCppQmlTypeUpdate(CPlusPlus::Document::Ptr)), Qt::DirectConnection);
279
    }
280
281

    ProjectExplorer::SessionManager *sessionManager = ProjectExplorer::ProjectExplorerPlugin::instance()->session();
282
283
    connect(sessionManager, SIGNAL(projectRemoved(ProjectExplorer::Project*)),
            this, SLOT(removeProjectInfo(ProjectExplorer::Project*)));
284
285
}

286
287
void ModelManager::loadQmlTypeDescriptions()
{
288
    if (Core::ICore::instance()) {
hjk's avatar
hjk committed
289
290
        loadQmlTypeDescriptions(Core::ICore::resourcePath());
        loadQmlTypeDescriptions(Core::ICore::userResourcePath());
291
    }
292
293
294
295
}

void ModelManager::loadQmlTypeDescriptions(const QString &resourcePath)
{
296
    const QDir typeFileDir(resourcePath + QLatin1String("/qml-type-descriptions"));
297
    const QStringList qmlTypesExtensions = QStringList() << QLatin1String("*.qmltypes");
298
    QFileInfoList qmlTypesFiles = typeFileDir.entryInfoList(
299
300
301
                qmlTypesExtensions,
                QDir::Files,
                QDir::Name);
302

303
304
    QStringList errors;
    QStringList warnings;
305
306
307
308
309
310

    // filter out the actual Qt builtins
    for (int i = 0; i < qmlTypesFiles.size(); ++i) {
        if (qmlTypesFiles.at(i).baseName() == QLatin1String("builtins")) {
            QFileInfoList list;
            list.append(qmlTypesFiles.at(i));
311
312
            CppQmlTypesLoader::defaultQtObjects =
                    CppQmlTypesLoader::loadQmlTypes(list, &errors, &warnings);
313
314
315
316
317
318
            qmlTypesFiles.removeAt(i);
            break;
        }
    }

    // load the fallbacks for libraries
319
320
    CppQmlTypesLoader::defaultLibraryObjects.unite(
                CppQmlTypesLoader::loadQmlTypes(qmlTypesFiles, &errors, &warnings));
321
322
323

    Core::MessageManager *messageManager = Core::MessageManager::instance();
    foreach (const QString &error, errors)
324
        messageManager->printToOutputPane(error, Core::MessageManager::Flash);
325
    foreach (const QString &warning, warnings)
326
        messageManager->printToOutputPane(warning, Core::MessageManager::Flash);
327
328
}

329
330
331
ModelManagerInterface::WorkingCopy ModelManager::workingCopy() const
{
    WorkingCopy workingCopy;
hjk's avatar
hjk committed
332
    if (!Core::ICore::instance())
333
334
        return workingCopy;

hjk's avatar
hjk committed
335
    foreach (Core::IEditor *editor, Core::ICore::editorManager()->openedEditors()) {
336
        const QString key = editor->document()->fileName();
337
338

        if (TextEditor::ITextEditor *textEditor = qobject_cast<TextEditor::ITextEditor*>(editor)) {
339
            if (textEditor->context().contains(ProjectExplorer::Constants::LANG_QMLJS)) {
340
                if (TextEditor::BaseTextEditorWidget *ed = qobject_cast<TextEditor::BaseTextEditorWidget *>(textEditor->widget()))
341
                    workingCopy.insert(key, ed->toPlainText(), ed->document()->revision());
342
343
344
345
346
347
348
            }
        }
    }

    return workingCopy;
}

349
Snapshot ModelManager::snapshot() const
350
{
351
    QMutexLocker locker(&m_mutex);
352
353
    return _validSnapshot;
}
354

355
356
357
358
Snapshot ModelManager::newestSnapshot() const
{
    QMutexLocker locker(&m_mutex);
    return _newestSnapshot;
359
360
}

361
362
void ModelManager::updateSourceFiles(const QStringList &files,
                                     bool emitDocumentOnDiskChanged)
363
{
364
    refreshSourceFiles(files, emitDocumentOnDiskChanged);
365
366
}

367
368
QFuture<void> ModelManager::refreshSourceFiles(const QStringList &sourceFiles,
                                               bool emitDocumentOnDiskChanged)
369
{
370
    if (sourceFiles.isEmpty())
371
372
        return QFuture<void>();

Roberto Raggi's avatar
Roberto Raggi committed
373
    QFuture<void> result = QtConcurrent::run(&ModelManager::parse,
374
                                              workingCopy(), sourceFiles,
375
376
                                              this,
                                              emitDocumentOnDiskChanged);
377

378
379
    if (m_synchronizer.futures().size() > 10) {
        QList<QFuture<void> > futures = m_synchronizer.futures();
380

381
        m_synchronizer.clearFutures();
382

Friedemann Kleint's avatar
Friedemann Kleint committed
383
        foreach (const QFuture<void> &future, futures) {
384
385
            if (! (future.isFinished() || future.isCanceled()))
                m_synchronizer.addFuture(future);
386
        }
387
    }
388

389
    m_synchronizer.addFuture(result);
390

391
    if (sourceFiles.count() > 1) {
hjk's avatar
hjk committed
392
        Core::ICore::progressManager()->addTask(result, tr("Indexing"),
393
                        QLatin1String(Constants::TASK_INDEX));
394
    }
395
396

    return result;
397
398
}

399
400
401
void ModelManager::fileChangedOnDisk(const QString &path)
{
    QtConcurrent::run(&ModelManager::parse,
402
                      workingCopy(), QStringList() << path,
403
404
405
                      this, true);
}

Erik Verbruggen's avatar
Erik Verbruggen committed
406
407
408
409
410
411
void ModelManager::removeFiles(const QStringList &files)
{
    emit aboutToRemoveFiles(files);

    QMutexLocker locker(&m_mutex);

412
413
414
415
    foreach (const QString &file, files) {
        _validSnapshot.remove(file);
        _newestSnapshot.remove(file);
    }
Erik Verbruggen's avatar
Erik Verbruggen committed
416
417
}

418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
QList<ModelManager::ProjectInfo> ModelManager::projectInfos() const
{
    QMutexLocker locker(&m_mutex);

    return m_projects.values();
}

ModelManager::ProjectInfo ModelManager::projectInfo(ProjectExplorer::Project *project) const
{
    QMutexLocker locker(&m_mutex);

    return m_projects.value(project, ProjectInfo(project));
}

void ModelManager::updateProjectInfo(const ProjectInfo &pinfo)
{
    if (! pinfo.isValid())
        return;

437
438
    Snapshot snapshot;
    ProjectInfo oldInfo;
439
440
    {
        QMutexLocker locker(&m_mutex);
441
        oldInfo = m_projects.value(pinfo.project);
442
        m_projects.insert(pinfo.project, pinfo);
443
        snapshot = _validSnapshot;
444
    }
445

446
447
448
449
    if (oldInfo.qmlDumpPath != pinfo.qmlDumpPath
            || oldInfo.qmlDumpEnvironment != pinfo.qmlDumpEnvironment) {
        m_pluginDumper->scheduleRedumpPlugins();
        m_pluginDumper->scheduleMaybeRedumpBuiltins(pinfo);
450
451
    }

452

453
    updateImportPaths();
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472

    // remove files that are no longer in the project and have been deleted
    QStringList deletedFiles;
    foreach (const QString &oldFile, oldInfo.sourceFiles) {
        if (snapshot.document(oldFile)
                && !pinfo.sourceFiles.contains(oldFile)
                && !QFile::exists(oldFile)) {
            deletedFiles += oldFile;
        }
    }
    removeFiles(deletedFiles);

    // parse any files not yet in the snapshot
    QStringList newFiles;
    foreach (const QString &file, pinfo.sourceFiles) {
        if (!snapshot.document(file))
            newFiles += file;
    }
    updateSourceFiles(newFiles, false);
473

474
475
476
    // dump builtin types if the shipped definitions are probably outdated and the
    // Qt version ships qmlplugindump
    if (QtSupport::QtVersionNumber(pinfo.qtVersionString) >= QtSupport::QtVersionNumber(4, 8, 0))
477
478
        m_pluginDumper->loadBuiltinTypes(pinfo);

479
    emit projectInfoUpdated(pinfo);
480
481
}

482
483
484
485

void ModelManager::removeProjectInfo(ProjectExplorer::Project *project)
{
    ProjectInfo info(project);
486
    info.sourceFiles.clear();
487
488
489
490
491
492
493
494
495
    // update with an empty project info to clear data
    updateProjectInfo(info);

    {
        QMutexLocker locker(&m_mutex);
        m_projects.remove(project);
    }
}

496
497
498
499
500
501
502
503
504
505
ModelManagerInterface::ProjectInfo ModelManager::projectInfoForPath(QString path)
{
    QMutexLocker locker(&m_mutex);

    foreach (const ProjectInfo &p, m_projects)
        if (p.sourceFiles.contains(path))
            return p;
    return ProjectInfo();
}

506
507
508
void ModelManager::emitDocumentChangedOnDisk(Document::Ptr doc)
{ emit documentChangedOnDisk(doc); }

509
void ModelManager::updateDocument(Document::Ptr doc)
510
{
511
512
    {
        QMutexLocker locker(&m_mutex);
513
514
        _validSnapshot.insert(doc);
        _newestSnapshot.insert(doc, true);
515
516
    }
    emit documentUpdated(doc);
517
518
}

519
void ModelManager::updateLibraryInfo(const QString &path, const LibraryInfo &info)
520
{
521
522
    {
        QMutexLocker locker(&m_mutex);
523
524
        _validSnapshot.insertLibraryInfo(path, info);
        _newestSnapshot.insertLibraryInfo(path, info);
525
    }
526
527
528
    // only emit if we got new useful information
    if (info.isValid())
        emit libraryInfoUpdated(path, info);
529
530
}

531
532
static QStringList qmlFilesInDirectory(const QString &path)
{
533
    const QStringList pattern = qmlAndJsGlobPatterns();
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
    QStringList files;

    const QDir dir(path);
    foreach (const QFileInfo &fi, dir.entryInfoList(pattern, QDir::Files))
        files += fi.absoluteFilePath();

    return files;
}

static void findNewImplicitImports(const Document::Ptr &doc, const Snapshot &snapshot,
                            QStringList *importedFiles, QSet<QString> *scannedPaths)
{
    // scan files that could be implicitly imported
    // it's important we also do this for JS files, otherwise the isEmpty check will fail
    if (snapshot.documentsInDirectory(doc->path()).isEmpty()) {
        if (! scannedPaths->contains(doc->path())) {
            *importedFiles += qmlFilesInDirectory(doc->path());
            scannedPaths->insert(doc->path());
        }
    }
}

static void findNewFileImports(const Document::Ptr &doc, const Snapshot &snapshot,
                        QStringList *importedFiles, QSet<QString> *scannedPaths)
{
    // scan files and directories that are explicitly imported
560
    foreach (const ImportInfo &import, doc->bind()->imports()) {
561
        const QString &importName = import.path();
562
        if (import.type() == ImportInfo::FileImport) {
563
564
            if (! snapshot.document(importName))
                *importedFiles += importName;
565
        } else if (import.type() == ImportInfo::DirectoryImport) {
566
567
568
569
            if (snapshot.documentsInDirectory(importName).isEmpty()) {
                if (! scannedPaths->contains(importName)) {
                    *importedFiles += qmlFilesInDirectory(importName);
                    scannedPaths->insert(importName);
570
                }
571
572
573
574
575
            }
        }
    }
}

576
577
578
579
580
581
582
583
static bool findNewQmlLibraryInPath(const QString &path,
                                    const Snapshot &snapshot,
                                    ModelManager *modelManager,
                                    QStringList *importedFiles,
                                    QSet<QString> *scannedPaths,
                                    QSet<QString> *newLibraries)
{
    // if we know there is a library, done
584
585
    const LibraryInfo &existingInfo = snapshot.libraryInfo(path);
    if (existingInfo.isValid())
586
587
588
        return true;
    if (newLibraries->contains(path))
        return true;
589
590
591
    // if we looked at the path before, done
    if (existingInfo.wasScanned())
        return false;
592
593
594

    const QDir dir(path);
    QFile qmldirFile(dir.filePath(QLatin1String("qmldir")));
595
596
597
    if (!qmldirFile.exists()) {
        LibraryInfo libraryInfo(LibraryInfo::NotFound);
        modelManager->updateLibraryInfo(path, libraryInfo);
598
        return false;
599
    }
600

601
602
603
    if (Utils::HostOsInfo::isWindowsHost()) {
        // QTCREATORBUG-3402 - be case sensitive even here?
    }
604
605
606
607
608
609

    // found a new library!
    qmldirFile.open(QFile::ReadOnly);
    QString qmldirData = QString::fromUtf8(qmldirFile.readAll());

    QmlDirParser qmldirParser;
610
    qmldirParser.parse(qmldirData);
611
612
613

    const QString libraryPath = QFileInfo(qmldirFile).absolutePath();
    newLibraries->insert(libraryPath);
614
    modelManager->updateLibraryInfo(libraryPath, LibraryInfo(qmldirParser));
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630

    // scan the qml files in the library
    foreach (const QmlDirParser::Component &component, qmldirParser.components()) {
        if (! component.fileName.isEmpty()) {
            const QFileInfo componentFileInfo(dir.filePath(component.fileName));
            const QString path = QDir::cleanPath(componentFileInfo.absolutePath());
            if (! scannedPaths->contains(path)) {
                *importedFiles += qmlFilesInDirectory(path);
                scannedPaths->insert(path);
            }
        }
    }

    return true;
}

631
632
633
634
635
636
637
638
639
static void findNewQmlLibrary(
    const QString &path,
    const LanguageUtils::ComponentVersion &version,
    const Snapshot &snapshot,
    ModelManager *modelManager,
    QStringList *importedFiles,
    QSet<QString> *scannedPaths,
    QSet<QString> *newLibraries)
{
640
    QString libraryPath = QString::fromLatin1("%1.%2.%3").arg(
641
642
643
644
645
646
647
                path,
                QString::number(version.majorVersion()),
                QString::number(version.minorVersion()));
    findNewQmlLibraryInPath(
                libraryPath, snapshot, modelManager,
                importedFiles, scannedPaths, newLibraries);

648
    libraryPath = QString::fromLatin1("%1.%2").arg(
649
650
651
652
653
654
655
656
657
658
659
                path,
                QString::number(version.majorVersion()));
    findNewQmlLibraryInPath(
                libraryPath, snapshot, modelManager,
                importedFiles, scannedPaths, newLibraries);

    findNewQmlLibraryInPath(
                path, snapshot, modelManager,
                importedFiles, scannedPaths, newLibraries);
}

660
661
static void findNewLibraryImports(const Document::Ptr &doc, const Snapshot &snapshot,
                           ModelManager *modelManager,
662
                           QStringList *importedFiles, QSet<QString> *scannedPaths, QSet<QString> *newLibraries)
663
{
664
665
666
667
668
    // scan current dir
    findNewQmlLibraryInPath(doc->path(), snapshot, modelManager,
                            importedFiles, scannedPaths, newLibraries);

    // scan dir and lib imports
669
    const QStringList importPaths = modelManager->importPaths();
670
671
    foreach (const ImportInfo &import, doc->bind()->imports()) {
        if (import.type() == ImportInfo::DirectoryImport) {
672
            const QString targetPath = import.path();
673
674
675
676
            findNewQmlLibraryInPath(targetPath, snapshot, modelManager,
                                    importedFiles, scannedPaths, newLibraries);
        }

677
        if (import.type() == ImportInfo::LibraryImport) {
678
679
            if (!import.version().isValid())
                continue;
680
            foreach (const QString &importPath, importPaths) {
681
                const QString targetPath = QDir(importPath).filePath(import.path());
682
683
                findNewQmlLibrary(targetPath, import.version(), snapshot, modelManager,
                                  importedFiles, scannedPaths, newLibraries);
684
685
686
687
688
            }
        }
    }
}

Roberto Raggi's avatar
Roberto Raggi committed
689
void ModelManager::parse(QFutureInterface<void> &future,
690
                            WorkingCopy workingCopy,
691
                            QStringList files,
692
693
                            ModelManager *modelManager,
                            bool emitDocChangedOnDisk)
694
{
695
696
697
698
699
    int progressRange = files.size();
    future.setProgressRange(0, progressRange);

    // paths we have scanned for files and added to the files list
    QSet<QString> scannedPaths;
700
701
    // libraries we've found while scanning imports
    QSet<QString> newLibraries;
702
703

    for (int i = 0; i < files.size(); ++i) {
704
        future.setProgressValue(qreal(i) / files.size() * progressRange);
705
706

        const QString fileName = files.at(i);
707

708
709
710
711
        Document::Language language = languageOfFile(fileName);
        if (language == Document::UnknownLanguage)
            continue;

712
        QString contents;
713
        int documentRevision = 0;
714
715

        if (workingCopy.contains(fileName)) {
716
717
718
            QPair<QString, int> entry = workingCopy.get(fileName);
            contents = entry.first;
            documentRevision = entry.second;
719
720
721
722
723
724
725
726
727
728
        } else {
            QFile inFile(fileName);

            if (inFile.open(QIODevice::ReadOnly)) {
                QTextStream ins(&inFile);
                contents = ins.readAll();
                inFile.close();
            }
        }

729
        Document::MutablePtr doc = Document::create(fileName, language);
730
        doc->setEditorRevision(documentRevision);
731
        doc->setSource(contents);
732
733
        doc->parse();

734
735
736
737
        // update snapshot. requires synchronization, but significantly reduces amount of file
        // system queries for library imports because queries are cached in libraryInfo
        const Snapshot snapshot = modelManager->snapshot();

738
739
740
741
        // get list of referenced files not yet in snapshot or in directories already scanned
        QStringList importedFiles;
        findNewImplicitImports(doc, snapshot, &importedFiles, &scannedPaths);
        findNewFileImports(doc, snapshot, &importedFiles, &scannedPaths);
742
        findNewLibraryImports(doc, snapshot, modelManager, &importedFiles, &scannedPaths, &newLibraries);
743
744
745
746
747

        // add new files to parse list
        foreach (const QString &file, importedFiles) {
            if (! files.contains(file))
                files.append(file);
748
749
        }

750
        modelManager->updateDocument(doc);
751
752
        if (emitDocChangedOnDisk)
            modelManager->emitDocumentChangedOnDisk(doc);
753
754
    }

755
    future.setProgressValue(progressRange);
756
757
}

758
// Check whether fileMimeType is the same or extends knownMimeType
Roberto Raggi's avatar
Roberto Raggi committed
759
bool ModelManager::matchesMimeType(const Core::MimeType &fileMimeType, const Core::MimeType &knownMimeType)
760
{
hjk's avatar
hjk committed
761
    Core::MimeDatabase *db = Core::ICore::mimeDatabase();
762
763
764

    const QStringList knownTypeNames = QStringList(knownMimeType.type()) + knownMimeType.aliases();

Friedemann Kleint's avatar
Friedemann Kleint committed
765
    foreach (const QString &knownTypeName, knownTypeNames)
766
767
768
769
770
771
772
773
774
775
776
        if (fileMimeType.matchesType(knownTypeName))
            return true;

    // recursion to parent types of fileMimeType
    foreach (const QString &parentMimeType, fileMimeType.subClassesOf()) {
        if (matchesMimeType(db->findByType(parentMimeType), knownMimeType))
            return true;
    }

    return false;
}
777
778
779

QStringList ModelManager::importPaths() const
{
780
    QMutexLocker l(&m_mutex);
781
    return m_allImportPaths;
782
783
}

784
785
786
787
788
789
790
791
792
793
794
795
QmlLanguageBundles ModelManager::activeBundles() const
{
    QMutexLocker l(&m_mutex);
    return m_activeBundles;
}

QmlLanguageBundles ModelManager::extendedBundles() const
{
    QMutexLocker l(&m_mutex);
    return m_extendedBundles;
}

796
797
798
799
800
801
static QStringList environmentImportPaths()
{
    QStringList paths;

    QByteArray envImportPath = qgetenv("QML_IMPORT_PATH");

802
803
    foreach (const QString &path, QString::fromLatin1(envImportPath)
             .split(Utils::HostOsInfo::pathListSeparator(), QString::SkipEmptyParts)) {
804
805
806
807
808
809
810
        QString canonicalPath = QDir(path).canonicalPath();
        if (!canonicalPath.isEmpty() && !paths.contains(canonicalPath))
            paths.append(canonicalPath);
    }

    return paths;
}
811

812
813
void ModelManager::updateImportPaths()
{
814
    QStringList allImportPaths;
815
816
    QmlLanguageBundles activeBundles;
    QmlLanguageBundles extendedBundles;
817
818
819
    QMapIterator<ProjectExplorer::Project *, ProjectInfo> it(m_projects);
    while (it.hasNext()) {
        it.next();
820
821
822
        foreach (const QString &path, it.value().importPaths) {
            const QString canonicalPath = QFileInfo(path).canonicalFilePath();
            if (!canonicalPath.isEmpty())
823
                allImportPaths += canonicalPath;
824
        }
825
    }
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
    it.toFront();
    while (it.hasNext()) {
        it.next();
        activeBundles.mergeLanguageBundles(it.value().activeBundle);
        foreach (Document::Language l, it.value().activeBundle.languages()) {
            foreach (const QString &path, it.value().activeBundle.bundleForLanguage(l)
                 .searchPaths().stringList()) {
                const QString canonicalPath = QFileInfo(path).canonicalFilePath();
                if (!canonicalPath.isEmpty())
                    allImportPaths += canonicalPath;
            }
        }
    }
    it.toFront();
    while (it.hasNext()) {
        it.next();
        extendedBundles.mergeLanguageBundles(it.value().extendedBundle);
        foreach (Document::Language l, it.value().extendedBundle.languages()) {
            foreach (const QString &path, it.value().extendedBundle.bundleForLanguage(l)
                     .searchPaths().stringList()) {
                const QString canonicalPath = QFileInfo(path).canonicalFilePath();
                if (!canonicalPath.isEmpty())
                    allImportPaths += canonicalPath;
            }
        }
    }
852
853
854
855
856
857
    allImportPaths += m_defaultImportPaths;
    allImportPaths.removeDuplicates();

    {
        QMutexLocker l(&m_mutex);
        m_allImportPaths = allImportPaths;
858
859
        m_activeBundles = activeBundles;
        m_extendedBundles = extendedBundles;
860
861
    }

862
863

    // check if any file in the snapshot imports something new in the new paths
864
    Snapshot snapshot = _validSnapshot;
865
866
    QStringList importedFiles;
    QSet<QString> scannedPaths;
867
    QSet<QString> newLibraries;
868
    foreach (const Document::Ptr &doc, snapshot)
869
        findNewLibraryImports(doc, snapshot, this, &importedFiles, &scannedPaths, &newLibraries);
870
871
872
873

    updateSourceFiles(importedFiles, true);
}

874
875
void ModelManager::loadPluginTypes(const QString &libraryPath, const QString &importPath,
                                   const QString &importUri, const QString &importVersion)
876
{
877
    m_pluginDumper->loadPluginTypes(libraryPath, importPath, importUri, importVersion);
878
}
879

880
881
// is called *inside a c++ parsing thread*, to allow hanging on to source and ast
void ModelManager::maybeQueueCppQmlTypeUpdate(const CPlusPlus::Document::Ptr &doc)
882
{
883
884
    // avoid scanning documents without source code available
    doc->keepSourceAndAST();
885
    if (doc->utf8Source().isEmpty()) {
886
887
888
889
        doc->releaseSourceAndAST();
        return;
    }

890
891
    // keep source and AST alive if we want to scan for register calls
    const bool scan = FindExportedCppTypes::maybeExportsTypes(doc);
892
893
    if (!scan)
        doc->releaseSourceAndAST();
894
895
896
897
898
899
900
901
902
903
904
905

    // delegate actual queuing to the gui thread
    QMetaObject::invokeMethod(this, "queueCppQmlTypeUpdate",
                              Q_ARG(CPlusPlus::Document::Ptr, doc), Q_ARG(bool, scan));
}

void ModelManager::queueCppQmlTypeUpdate(const CPlusPlus::Document::Ptr &doc, bool scan)
{
    QPair<CPlusPlus::Document::Ptr, bool> prev = m_queuedCppDocuments.value(doc->fileName());
    if (prev.first && prev.second)
        prev.first->releaseSourceAndAST();
    m_queuedCppDocuments.insert(doc->fileName(), qMakePair(doc, scan));
906
907
908
909
    m_updateCppQmlTypesTimer->start();
}

void ModelManager::startCppQmlTypeUpdate()
910
{
911
912
913
914
915
916
    // if a future is still running, delay
    if (m_cppQmlTypesUpdater.isRunning()) {
        m_updateCppQmlTypesTimer->start();
        return;
    }

917
918
919
920
921
    CPlusPlus::CppModelManagerInterface *cppModelManager =
            CPlusPlus::CppModelManagerInterface::instance();
    if (!cppModelManager)
        return;

922
923
924
    m_cppQmlTypesUpdater = QtConcurrent::run(
                &ModelManager::updateCppQmlTypes,
                this, cppModelManager->snapshot(), m_queuedCppDocuments);
925
926
927
    m_queuedCppDocuments.clear();
}

928
929
930
void ModelManager::updateCppQmlTypes(QFutureInterface<void> &interface,
                                     ModelManager *qmlModelManager,
                                     CPlusPlus::Snapshot snapshot,
931
                                     QHash<QString, QPair<CPlusPlus::Document::Ptr, bool> > documents)
932
{
933
934
    CppDataHash newData = qmlModelManager->cppData();

935
936
937
938
    FindExportedCppTypes finder(snapshot);

    typedef QPair<CPlusPlus::Document::Ptr, bool> DocScanPair;
    foreach (const DocScanPair &pair, documents) {
939
940
941
        if (interface.isCanceled())
            return;

942
943
944
945
        CPlusPlus::Document::Ptr doc = pair.first;
        const bool scan = pair.second;
        const QString fileName = doc->fileName();
        if (!scan) {
946
            newData.remove(fileName);
947
948
949
            continue;
        }

950
        finder(doc);
951

952
        QList<LanguageUtils::FakeMetaObject::ConstPtr> exported = finder.exportedTypes();
953
        QHash<QString, QString> contextProperties = finder.contextProperties();
954
955
956
957
958
959
960
        if (exported.isEmpty() && contextProperties.isEmpty()) {
            newData.remove(fileName);
        } else {
            CppData &data = newData[fileName];
            data.exportedTypes = exported;
            data.contextProperties = contextProperties;
        }
961
962

        doc->releaseSourceAndAST();
963
964
    }

965
966
    QMutexLocker locker(&qmlModelManager->m_cppDataMutex);
    qmlModelManager->m_cppDataHash = newData;
967
968
}

969
ModelManager::CppDataHash ModelManager::cppData() const
970
{
971
972
    QMutexLocker locker(&m_cppDataMutex);
    return m_cppDataHash;
973
}
974

975
LibraryInfo ModelManager::builtins(const Document::Ptr &doc) const
976
{
977
978
979
980
981
982
983
984
985
986
    ProjectExplorer::SessionManager *sessionManager = ProjectExplorer::ProjectExplorerPlugin::instance()->session();
    ProjectExplorer::Project *project = sessionManager->projectForFile(doc->fileName());
    if (!project)
        return LibraryInfo();

    QMutexLocker locker(&m_mutex);
    ProjectInfo info = m_projects.value(project);
    if (!info.isValid())
        return LibraryInfo();

987
    return _validSnapshot.libraryInfo(info.qtImportsPath);
988
}
989

990
991
992
993
994
995
void ModelManager::joinAllThreads()
{
    foreach (QFuture<void> future, m_synchronizer.futures())
        future.waitForFinished();
}

996
997
998
999
1000
1001
1002
1003
void ModelManager::resetCodeModel()
{
    QStringList documents;

    {
        QMutexLocker locker(&m_mutex);

        // find all documents currently in the code model
1004
        foreach (Document::Ptr doc, _validSnapshot)
1005
1006
1007
            documents.append(doc->fileName());

        // reset the snapshot
1008
1009
        _validSnapshot = Snapshot();
        _newestSnapshot = Snapshot();
1010
1011
1012
1013
1014
    }

    // start a reparse thread
    updateSourceFiles(documents, false);
}