qmljsmodelmanagerinterface.cpp 55.4 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11
** 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
12 13 14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
15
**
16 17 18 19 20 21 22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
con's avatar
con committed
23
**
hjk's avatar
hjk committed
24
****************************************************************************/
25

26 27 28 29
#include "qmljsbind.h"
#include "qmljsconstants.h"
#include "qmljsfindexportedcpptypes.h"
#include "qmljsinterpreter.h"
30
#include "qmljsmodelmanagerinterface.h"
31 32
#include "qmljsplugindumper.h"
#include "qmljstypedescriptionreader.h"
33
#include "qmljsdialect.h"
34
#include "qmljsviewercontext.h"
35

36
#include <cplusplus/cppmodelmanagerbase.h>
37
#include <utils/algorithm.h>
38
#include <utils/hostosinfo.h>
39
#include <utils/runextensions.h>
40 41 42

#include <QDir>
#include <QFile>
43
#include <QFileInfo>
44
#include <QMetaObject>
45 46 47 48 49
#include <QRegExp>
#include <QTextDocument>
#include <QTextStream>
#include <QTimer>
#include <QtAlgorithms>
50
#include <QLibraryInfo>
51 52

#include <stdio.h>
53

54
namespace QmlJS {
55

56 57
QMLJS_EXPORT Q_LOGGING_CATEGORY(qmljsLog, "qtc.qmljs.common")

58 59
/*!
    \class QmlJS::ModelManagerInterface
60 61
    \brief The ModelManagerInterface class acts as an interface to the
    global state of the QmlJS code model.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    \sa QmlJS::Document QmlJS::Snapshot QmlJSTools::Internal::ModelManager

    The ModelManagerInterface is an interface for global state and actions in
    the QmlJS code model. It is implemented by \l{QmlJSTools::Internal::ModelManager}
    and the instance can be accessed through ModelManagerInterface::instance().

    One of its primary concerns is to keep the Snapshots it
    maintains up to date by parsing documents and finding QML modules.

    It has a Snapshot that contains only valid Documents,
    accessible through ModelManagerInterface::snapshot() and a Snapshot with
    potentially more recent, but invalid documents that is exposed through
    ModelManagerInterface::newestSnapshot().
*/

77 78
static ModelManagerInterface *g_instance = 0;

79 80
const char qtQuickUISuffix[] = "ui.qml";

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
static QStringList environmentImportPaths()
{
    QStringList paths;

    QByteArray envImportPath = qgetenv("QML_IMPORT_PATH");

    foreach (const QString &path, QString::fromLatin1(envImportPath)
             .split(Utils::HostOsInfo::pathListSeparator(), QString::SkipEmptyParts)) {
        QString canonicalPath = QDir(path).canonicalPath();
        if (!canonicalPath.isEmpty() && !paths.contains(canonicalPath))
            paths.append(canonicalPath);
    }

    return paths;
}

97
ModelManagerInterface::ModelManagerInterface(QObject *parent)
98 99
    : QObject(parent),
      m_shouldScanImports(false),
100
      m_defaultProject(0),
101
      m_pluginDumper(new PluginDumper(this))
102
{
103
    m_indexerEnabled = qgetenv("QTC_NO_CODE_INDEXER") != "1";
104 105 106 107

    m_updateCppQmlTypesTimer = new QTimer(this);
    m_updateCppQmlTypesTimer->setInterval(1000);
    m_updateCppQmlTypesTimer->setSingleShot(true);
108 109
    connect(m_updateCppQmlTypesTimer, &QTimer::timeout,
            this, &ModelManagerInterface::startCppQmlTypeUpdate);
110 111 112 113

    m_asyncResetTimer = new QTimer(this);
    m_asyncResetTimer->setInterval(15000);
    m_asyncResetTimer->setSingleShot(true);
114
    connect(m_asyncResetTimer, &QTimer::timeout, this, &ModelManagerInterface::resetCodeModel);
115 116 117

    qRegisterMetaType<QmlJS::Document::Ptr>("QmlJS::Document::Ptr");
    qRegisterMetaType<QmlJS::LibraryInfo>("QmlJS::LibraryInfo");
118 119 120
    qRegisterMetaType<QmlJS::Dialect>("QmlJS::Dialect");
    qRegisterMetaType<QmlJS::PathAndLanguage>("QmlJS::PathAndLanguage");
    qRegisterMetaType<QmlJS::PathsAndLanguages>("QmlJS::PathsAndLanguages");
121

122 123 124 125
    m_defaultProjectInfo.qtImportsPath = QFileInfo(
                QLibraryInfo::location(QLibraryInfo::ImportsPath)).canonicalFilePath();
    m_defaultProjectInfo.qtQmlPath = QFileInfo(
                QLibraryInfo::location(QLibraryInfo::Qml2ImportsPath)).canonicalFilePath();
126

127 128 129
    m_defaultImportPaths << environmentImportPaths();
    updateImportPaths();

130 131
    Q_ASSERT(! g_instance);
    g_instance = this;
132 133
}

134
ModelManagerInterface::~ModelManagerInterface()
135
{
136 137
    m_cppQmlTypesUpdater.cancel();
    m_cppQmlTypesUpdater.waitForFinished();
138 139
    Q_ASSERT(g_instance == this);
    g_instance = 0;
140
}
141

142
static QHash<QString, Dialect> defaultLanguageMapping()
143
{
Tim Jenssen's avatar
Tim Jenssen committed
144 145 146 147 148 149 150 151 152
    static QHash<QString, Dialect> res{
        {QLatin1String("js"), Dialect::JavaScript},
        {QLatin1String("qml"), Dialect::Qml},
        {QLatin1String("qmltypes"), Dialect::QmlTypeInfo},
        {QLatin1String("qmlproject"), Dialect::QmlProject},
        {QLatin1String("json"), Dialect::Json},
        {QLatin1String("qbs"), Dialect::QmlQbs},
        {QLatin1String(qtQuickUISuffix), Dialect::QmlQtQuick2Ui}
    };
153 154 155
    return res;
}

156
Dialect ModelManagerInterface::guessLanguageOfFile(const QString &fileName)
157
{
158
    QHash<QString, Dialect> lMapping;
159 160 161 162 163
    if (instance())
        lMapping = instance()->languageForSuffix();
    else
        lMapping = defaultLanguageMapping();
    const QFileInfo info(fileName);
164 165 166 167 168 169 170 171 172 173
    QString fileSuffix = info.suffix();

    /*
     * I was reluctant to use complete suffix in all cases, because it is a huge
     * change in behaivour. But in case of .qml this should be safe.
     */

    if (fileSuffix == QLatin1String("qml"))
        fileSuffix = info.completeSuffix();

174
    return lMapping.value(fileSuffix, Dialect::NoLanguage);
175 176
}

177
QStringList ModelManagerInterface::globPatternsForLanguages(const QList<Dialect> languages)
178
{
179
    QHash<QString, Dialect> lMapping;
180 181 182 183 184
    if (instance())
        lMapping = instance()->languageForSuffix();
    else
        lMapping = defaultLanguageMapping();
    QStringList patterns;
185
    QHashIterator<QString,Dialect> i(lMapping);
186 187 188 189 190 191 192 193
    while (i.hasNext()) {
        i.next();
        if (languages.contains(i.value()))
            patterns << QLatin1String("*.") + i.key();
    }
    return patterns;
}

194 195 196 197
ModelManagerInterface *ModelManagerInterface::instance()
{
    return g_instance;
}
198

199 200 201 202 203
void ModelManagerInterface::writeWarning(const QString &msg)
{
    if (ModelManagerInterface *i = instance())
        i->writeMessageInternal(msg);
    else
204
        qCWarning(qmljsLog) << msg;
205 206 207 208 209 210 211 212 213
}

ModelManagerInterface::WorkingCopy ModelManagerInterface::workingCopy()
{
    if (ModelManagerInterface *i = instance())
        return i->workingCopyInternal();
    return WorkingCopy();
}

214 215 216 217 218 219 220 221
void ModelManagerInterface::activateScan()
{
    if (!m_shouldScanImports) {
        m_shouldScanImports = true;
        updateImportPaths();
    }
}

222
QHash<QString, Dialect> ModelManagerInterface::languageForSuffix() const
223 224 225 226
{
    return defaultLanguageMapping();
}

227 228
void ModelManagerInterface::writeMessageInternal(const QString &msg) const
{
229
    qCDebug(qmljsLog) << msg;
230 231 232 233 234 235 236 237 238 239 240 241
}

ModelManagerInterface::WorkingCopy ModelManagerInterface::workingCopyInternal() const
{
    ModelManagerInterface::WorkingCopy res;
    return res;
}

void ModelManagerInterface::addTaskInternal(QFuture<void> result, const QString &msg,
                                            const char *taskId) const
{
    Q_UNUSED(result);
242
    qCDebug(qmljsLog) << "started " << taskId << " " << msg;
243 244 245 246 247
}

void ModelManagerInterface::loadQmlTypeDescriptionsInternal(const QString &resourcePath)
{
    const QDir typeFileDir(resourcePath + QLatin1String("/qml-type-descriptions"));
248
    const QStringList qmlTypesExtensions = QStringList("*.qmltypes");
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
    QFileInfoList qmlTypesFiles = typeFileDir.entryInfoList(
                qmlTypesExtensions,
                QDir::Files,
                QDir::Name);

    QStringList errors;
    QStringList warnings;

    // 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));
            CppQmlTypesLoader::defaultQtObjects =
                    CppQmlTypesLoader::loadQmlTypes(list, &errors, &warnings);
            qmlTypesFiles.removeAt(i);
            break;
        }
    }

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

    foreach (const QString &error, errors)
        writeMessageInternal(error);
    foreach (const QString &warning, warnings)
        writeMessageInternal(warning);
}

279 280 281 282 283 284 285
void ModelManagerInterface::setDefaultProject(const ModelManagerInterface::ProjectInfo &pInfo,
                                              ProjectExplorer::Project *p)
{
    QMutexLocker l(mutex());
    m_defaultProject = p;
    m_defaultProjectInfo = pInfo;
}
286 287 288 289

Snapshot ModelManagerInterface::snapshot() const
{
    QMutexLocker locker(&m_mutex);
290
    return m_validSnapshot;
291 292 293 294 295
}

Snapshot ModelManagerInterface::newestSnapshot() const
{
    QMutexLocker locker(&m_mutex);
296
    return m_newestSnapshot;
297 298 299 300 301
}

void ModelManagerInterface::updateSourceFiles(const QStringList &files,
                                     bool emitDocumentOnDiskChanged)
{
302 303
    if (!m_indexerEnabled)
        return;
304 305 306
    refreshSourceFiles(files, emitDocumentOnDiskChanged);
}

307 308 309 310 311 312 313 314 315 316 317 318
void ModelManagerInterface::cleanupFutures()
{
    if (m_futures.size() > 10) {
        QList<QFuture<void> > futures = m_futures;
        m_futures.clear();
        foreach (const QFuture<void> &future, futures) {
            if (!(future.isFinished() || future.isCanceled()))
                m_futures.append(future);
        }
    }
}

319 320 321 322 323 324
QFuture<void> ModelManagerInterface::refreshSourceFiles(const QStringList &sourceFiles,
                                               bool emitDocumentOnDiskChanged)
{
    if (sourceFiles.isEmpty())
        return QFuture<void>();

325 326 327 328
    QFuture<void> result = Utils::runAsync(&ModelManagerInterface::parse,
                                           workingCopyInternal(), sourceFiles,
                                           this, Dialect(Dialect::Qml),
                                           emitDocumentOnDiskChanged);
329 330
    cleanupFutures();
    m_futures.append(result);
331 332

    if (sourceFiles.count() > 1)
333
         addTaskInternal(result, tr("Parsing QML Files"), Constants::TASK_INDEX);
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353

    if (sourceFiles.count() > 1 && !m_shouldScanImports) {
        bool scan = false;
        {
            QMutexLocker l(&m_mutex);
            if (!m_shouldScanImports) {
                m_shouldScanImports = true;
                scan = true;
            }
        }
        if (scan)
        updateImportPaths();
    }

    return result;
}


void ModelManagerInterface::fileChangedOnDisk(const QString &path)
{
354
    Utils::runAsync(&ModelManagerInterface::parse,
355
                    workingCopyInternal(), QStringList(path),
356
                    this, Dialect(Dialect::AnyLanguage), true);
357 358 359 360 361 362 363 364 365
}

void ModelManagerInterface::removeFiles(const QStringList &files)
{
    emit aboutToRemoveFiles(files);

    QMutexLocker locker(&m_mutex);

    foreach (const QString &file, files) {
366 367
        m_validSnapshot.remove(file);
        m_newestSnapshot.remove(file);
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
    }
}

namespace {
bool pInfoLessThanActive(const ModelManagerInterface::ProjectInfo &p1, const ModelManagerInterface::ProjectInfo &p2)
{
    QStringList s1 = p1.activeResourceFiles;
    QStringList s2 = p2.activeResourceFiles;
    if (s1.size() < s2.size())
        return true;
    if (s1.size() > s2.size())
        return false;
    for (int i = 0; i < s1.size(); ++i) {
        if (s1.at(i) < s2.at(i))
            return true;
        else if (s1.at(i) > s2.at(i))
            return false;
    }
    return false;
}

bool pInfoLessThanAll(const ModelManagerInterface::ProjectInfo &p1, const ModelManagerInterface::ProjectInfo &p2)
{
    QStringList s1 = p1.allResourceFiles;
    QStringList s2 = p2.allResourceFiles;
    if (s1.size() < s2.size())
        return true;
    if (s1.size() > s2.size())
        return false;
    for (int i = 0; i < s1.size(); ++i) {
        if (s1.at(i) < s2.at(i))
            return true;
        else if (s1.at(i) > s2.at(i))
            return false;
    }
    return false;
}
405 406 407 408 409 410 411 412 413 414 415

bool pInfoLessThanImports(const ModelManagerInterface::ProjectInfo &p1, const ModelManagerInterface::ProjectInfo &p2)
{
    if (p1.qtQmlPath < p2.qtQmlPath)
        return true;
    if (p1.qtQmlPath > p2.qtQmlPath)
        return false;
    if (p1.qtImportsPath < p2.qtImportsPath)
        return true;
    if (p1.qtImportsPath > p2.qtImportsPath)
        return false;
416 417
    const PathsAndLanguages &s1 = p1.importPaths;
    const PathsAndLanguages &s2 = p2.importPaths;
418 419 420 421 422 423 424
    if (s1.size() < s2.size())
        return true;
    if (s1.size() > s2.size())
        return false;
    for (int i = 0; i < s1.size(); ++i) {
        if (s1.at(i) < s2.at(i))
            return true;
425
        else if (s2.at(i) < s1.at(i))
426 427 428 429 430
            return false;
    }
    return false;
}

431 432
}

433 434 435
void ModelManagerInterface::iterateQrcFiles(ProjectExplorer::Project *project,
                                            QrcResourceSelector resources,
                                            std::function<void(QrcParser::ConstPtr)> callback)
436 437
{
    QList<ProjectInfo> pInfos;
438
    if (project) {
439
        pInfos.append(projectInfo(project));
440
    } else {
441
        pInfos = projectInfos();
442
        if (resources == ActiveQrcResources) // make the result predictable
443
            Utils::sort(pInfos, &pInfoLessThanActive);
444
        else
445
            Utils::sort(pInfos, &pInfoLessThanAll);
446
    }
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461

    QSet<QString> pathsChecked;
    foreach (const ModelManagerInterface::ProjectInfo &pInfo, pInfos) {
        QStringList qrcFilePaths;
        if (resources == ActiveQrcResources)
            qrcFilePaths = pInfo.activeResourceFiles;
        else
            qrcFilePaths = pInfo.allResourceFiles;
        foreach (const QString &qrcFilePath, qrcFilePaths) {
            if (pathsChecked.contains(qrcFilePath))
                continue;
            pathsChecked.insert(qrcFilePath);
            QrcParser::ConstPtr qrcFile = m_qrcCache.parsedPath(qrcFilePath);
            if (qrcFile.isNull())
                continue;
462
            callback(qrcFile);
463 464
        }
    }
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
}

QStringList ModelManagerInterface::qrcPathsForFile(const QString &file, const QLocale *locale,
                                                   ProjectExplorer::Project *project,
                                                   QrcResourceSelector resources)
{
    QStringList res;
    iterateQrcFiles(project, resources, [&](QrcParser::ConstPtr qrcFile) {
        qrcFile->collectResourceFilesForSourceFile(file, &res, locale);
    });
    return res;
}

QStringList ModelManagerInterface::filesAtQrcPath(const QString &path, const QLocale *locale,
                                         ProjectExplorer::Project *project,
                                         QrcResourceSelector resources)
{
    QString normPath = QrcParser::normalizedQrcFilePath(path);
    QStringList res;
    iterateQrcFiles(project, resources, [&](QrcParser::ConstPtr qrcFile) {
        qrcFile->collectFilesAtPath(normPath, &res, locale);
    });
487 488 489 490 491 492 493 494 495 496 497
    return res;
}

QMap<QString, QStringList> ModelManagerInterface::filesInQrcPath(const QString &path,
                                                        const QLocale *locale,
                                                        ProjectExplorer::Project *project,
                                                        bool addDirs,
                                                        QrcResourceSelector resources)
{
    QString normPath = QrcParser::normalizedQrcDirectoryPath(path);
    QMap<QString, QStringList> res;
498 499 500
    iterateQrcFiles(project, resources, [&](QrcParser::ConstPtr qrcFile) {
        qrcFile->collectFilesInPath(normPath, &res, addDirs, locale);
    });
501 502 503 504 505 506 507 508 509 510
    return res;
}

QList<ModelManagerInterface::ProjectInfo> ModelManagerInterface::projectInfos() const
{
    QMutexLocker locker(&m_mutex);

    return m_projects.values();
}

511 512 513
ModelManagerInterface::ProjectInfo ModelManagerInterface::projectInfo(
        ProjectExplorer::Project *project,
        const ModelManagerInterface::ProjectInfo &defaultValue) const
514 515 516
{
    QMutexLocker locker(&m_mutex);

517
    return m_projects.value(project, defaultValue);
518 519 520 521
}

void ModelManagerInterface::updateProjectInfo(const ProjectInfo &pinfo, ProjectExplorer::Project *p)
{
522
    if (! pinfo.isValid() || !p || !m_indexerEnabled)
523 524 525 526 527 528 529 530
        return;

    Snapshot snapshot;
    ProjectInfo oldInfo;
    {
        QMutexLocker locker(&m_mutex);
        oldInfo = m_projects.value(p);
        m_projects.insert(p, pinfo);
531 532
        if (p == m_defaultProject)
            m_defaultProjectInfo = pinfo;
533
        snapshot = m_validSnapshot;
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 560 561 562 563
    }

    if (oldInfo.qmlDumpPath != pinfo.qmlDumpPath
            || oldInfo.qmlDumpEnvironment != pinfo.qmlDumpEnvironment) {
        m_pluginDumper->scheduleRedumpPlugins();
        m_pluginDumper->scheduleMaybeRedumpBuiltins(pinfo);
    }


    updateImportPaths();

    // 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);
    foreach (const QString &oldFile, deletedFiles)
        m_fileToProject.remove(oldFile, p);

    // parse any files not yet in the snapshot
    QStringList newFiles;
    foreach (const QString &file, pinfo.sourceFiles) {
        if (!snapshot.document(file))
            newFiles += file;
    }
564
    foreach (const QString &newFile, newFiles)
565
        m_fileToProject.insert(newFile, p);
566
    updateSourceFiles(newFiles, false);
567 568

    // update qrc cache
569
    m_qrcContents = pinfo.resourceFileContents;
570
    foreach (const QString &newQrc, pinfo.allResourceFiles)
571
        m_qrcCache.addPath(newQrc, m_qrcContents.value(newQrc));
572 573 574 575 576 577 578 579 580 581
    foreach (const QString &oldQrc, oldInfo.allResourceFiles)
        m_qrcCache.removePath(oldQrc);

    int majorVersion, minorVersion, patchVersion;
    // dump builtin types if the shipped definitions are probably outdated and the
    // Qt version ships qmlplugindump
    if (::sscanf(pinfo.qtVersionString.toLatin1().constData(), "%d.%d.%d",
           &majorVersion, &minorVersion, &patchVersion) != 3)
        majorVersion = minorVersion = patchVersion = -1;

582
    if (majorVersion > 4 || (majorVersion == 4 && (minorVersion > 8 || (minorVersion == 8
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
                                                                       && patchVersion >= 5)))) {
        m_pluginDumper->loadBuiltinTypes(pinfo);
    }

    emit projectInfoUpdated(pinfo);
}


void ModelManagerInterface::removeProjectInfo(ProjectExplorer::Project *project)
{
    ProjectInfo info;
    info.sourceFiles.clear();
    // update with an empty project info to clear data
    updateProjectInfo(info, project);

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

604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
/*!
    Returns project info with summarized info for \a path

    \note Project pointer will be empty
 */
ModelManagerInterface::ProjectInfo ModelManagerInterface::projectInfoForPath(const QString &path) const
{
    QList<ProjectInfo> infos = allProjectInfosForPath(path);

    ProjectInfo res;
    foreach (const ProjectInfo &pInfo, infos) {
        if (res.qtImportsPath.isEmpty())
            res.qtImportsPath = pInfo.qtImportsPath;
        if (res.qtQmlPath.isEmpty())
            res.qtQmlPath = pInfo.qtQmlPath;
        for (int i = 0; i < pInfo.importPaths.size(); ++i)
            res.importPaths.maybeInsert(pInfo.importPaths.at(i));
    }
    return res;
}

/*!
    Returns list of project infos for \a path
 */
QList<ModelManagerInterface::ProjectInfo> ModelManagerInterface::allProjectInfosForPath(const QString &path) const
629
{
630 631 632 633
    QList<ProjectExplorer::Project *> projects;
    {
        QMutexLocker locker(&m_mutex);
        projects = m_fileToProject.values(path);
634 635 636 637
        if (projects.isEmpty()) {
            QFileInfo fInfo(path);
            projects = m_fileToProject.values(fInfo.canonicalFilePath());
        }
638 639 640 641 642 643 644 645
    }
    QList<ProjectInfo> infos;
    foreach (ProjectExplorer::Project *project, projects) {
        ProjectInfo info = projectInfo(project);
        if (info.isValid())
            infos.append(info);
    }
    std::sort(infos.begin(), infos.end(), &pInfoLessThanImports);
646
    infos.append(m_defaultProjectInfo);
647
    return infos;
648 649
}

650 651
bool ModelManagerInterface::isIdle() const
{
652
    return m_futures.isEmpty();
653 654
}

655 656 657 658 659
void ModelManagerInterface::emitDocumentChangedOnDisk(Document::Ptr doc)
{ emit documentChangedOnDisk(doc); }

void ModelManagerInterface::updateQrcFile(const QString &path)
{
660
    m_qrcCache.updatePath(path, m_qrcContents.value(path));
661 662 663 664 665 666
}

void ModelManagerInterface::updateDocument(Document::Ptr doc)
{
    {
        QMutexLocker locker(&m_mutex);
667 668
        m_validSnapshot.insert(doc);
        m_newestSnapshot.insert(doc, true);
669 670 671 672 673 674 675
    }
    emit documentUpdated(doc);
}

void ModelManagerInterface::updateLibraryInfo(const QString &path, const LibraryInfo &info)
{
    if (!info.pluginTypeInfoError().isEmpty())
676
        qCDebug(qmljsLog) << "Dumping errors for " << path << ":" << info.pluginTypeInfoError();
677 678 679

    {
        QMutexLocker locker(&m_mutex);
680 681
        m_validSnapshot.insertLibraryInfo(path, info);
        m_newestSnapshot.insertLibraryInfo(path, info);
682 683 684 685 686 687
    }
    // only emit if we got new useful information
    if (info.isValid())
        emit libraryInfoUpdated(path, info);
}

688
static QStringList filesInDirectoryForLanguages(const QString &path, QList<Dialect> languages)
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
{
    const QStringList pattern = ModelManagerInterface::globPatternsForLanguages(languages);
    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 += filesInDirectoryForLanguages(doc->path(),
708
                                                  doc->language().companionLanguages());
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
            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
    foreach (const ImportInfo &import, doc->bind()->imports()) {
        const QString &importName = import.path();
        if (import.type() == ImportType::File) {
            if (! snapshot.document(importName))
                *importedFiles += importName;
        } else if (import.type() == ImportType::Directory) {
            if (snapshot.documentsInDirectory(importName).isEmpty()) {
                if (! scannedPaths->contains(importName)) {
                    *importedFiles += filesInDirectoryForLanguages(importName,
727
                                                          doc->language().companionLanguages());
728 729 730 731 732 733 734 735 736 737 738 739 740
                    scannedPaths->insert(importName);
                }
            }
        } else if (import.type() == ImportType::QrcFile) {
            QStringList importPaths = ModelManagerInterface::instance()->filesAtQrcPath(importName);
            foreach (const QString &importPath, importPaths) {
                if (! snapshot.document(importPath))
                    *importedFiles += importPath;
            }
        } else if (import.type() == ImportType::QrcDirectory) {
            QMapIterator<QString,QStringList> dirContents(ModelManagerInterface::instance()->filesInQrcPath(importName));
            while (dirContents.hasNext()) {
                dirContents.next();
741
                if (ModelManagerInterface::guessLanguageOfFile(dirContents.key()).isQmlLikeOrJsLanguage()) {
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
                    foreach (const QString &filePath, dirContents.value()) {
                        if (! snapshot.document(filePath))
                            *importedFiles += filePath;
                    }
                }
            }
        }
    }
}

static bool findNewQmlLibraryInPath(const QString &path,
                                    const Snapshot &snapshot,
                                    ModelManagerInterface *modelManager,
                                    QStringList *importedFiles,
                                    QSet<QString> *scannedPaths,
                                    QSet<QString> *newLibraries,
                                    bool ignoreMissing)
{
    // if we know there is a library, done
    const LibraryInfo &existingInfo = snapshot.libraryInfo(path);
    if (existingInfo.isValid())
        return true;
    if (newLibraries->contains(path))
        return true;
    // if we looked at the path before, done
    if (existingInfo.wasScanned())
        return false;

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

    if (Utils::HostOsInfo::isWindowsHost()) {
        // QTCREATORBUG-3402 - be case sensitive even here?
    }

    // found a new library!
785 786
    if (!qmldirFile.open(QFile::ReadOnly))
        return false;
787 788 789 790 791 792 793 794
    QString qmldirData = QString::fromUtf8(qmldirFile.readAll());

    QmlDirParser qmldirParser;
    qmldirParser.parse(qmldirData);

    const QString libraryPath = QFileInfo(qmldirFile).absolutePath();
    newLibraries->insert(libraryPath);
    modelManager->updateLibraryInfo(libraryPath, LibraryInfo(qmldirParser));
795 796
    modelManager->loadPluginTypes(QFileInfo(libraryPath).canonicalFilePath(), libraryPath,
                QString(), QString());
797 798 799 800 801 802 803 804

    // 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 += filesInDirectoryForLanguages(path,
805
                     Dialect(Dialect::AnyLanguage).companionLanguages());
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 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
                scannedPaths->insert(path);
            }
        }
    }

    return true;
}

static void findNewQmlLibrary(
    const QString &path,
    const LanguageUtils::ComponentVersion &version,
    const Snapshot &snapshot,
    ModelManagerInterface *modelManager,
    QStringList *importedFiles,
    QSet<QString> *scannedPaths,
    QSet<QString> *newLibraries)
{
    QString libraryPath = QString::fromLatin1("%1.%2.%3").arg(
                path,
                QString::number(version.majorVersion()),
                QString::number(version.minorVersion()));
    findNewQmlLibraryInPath(
                libraryPath, snapshot, modelManager,
                importedFiles, scannedPaths, newLibraries, false);

    libraryPath = QString::fromLatin1("%1.%2").arg(
                path,
                QString::number(version.majorVersion()));
    findNewQmlLibraryInPath(
                libraryPath, snapshot, modelManager,
                importedFiles, scannedPaths, newLibraries, false);

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

static void findNewLibraryImports(const Document::Ptr &doc, const Snapshot &snapshot,
                           ModelManagerInterface *modelManager,
                           QStringList *importedFiles, QSet<QString> *scannedPaths, QSet<QString> *newLibraries)
{
    // scan current dir
    findNewQmlLibraryInPath(doc->path(), snapshot, modelManager,
                            importedFiles, scannedPaths, newLibraries, false);

    // scan dir and lib imports
852
    const PathsAndLanguages importPaths = modelManager->importPaths();
853 854 855 856 857 858 859 860 861 862
    foreach (const ImportInfo &import, doc->bind()->imports()) {
        if (import.type() == ImportType::Directory) {
            const QString targetPath = import.path();
            findNewQmlLibraryInPath(targetPath, snapshot, modelManager,
                                    importedFiles, scannedPaths, newLibraries, false);
        }

        if (import.type() == ImportType::Library) {
            if (!import.version().isValid())
                continue;
863
            foreach (const PathAndLanguage &importPath, importPaths) {
864
                const QString targetPath = importPath.path().appendPath(import.path()).toString();
865 866 867 868 869 870 871 872 873 874 875 876
                findNewQmlLibrary(targetPath, import.version(), snapshot, modelManager,
                                  importedFiles, scannedPaths, newLibraries);
            }
        }
    }
}

void ModelManagerInterface::parseLoop(QSet<QString> &scannedPaths,
                             QSet<QString> &newLibraries,
                             WorkingCopy workingCopy,
                             QStringList files,
                             ModelManagerInterface *modelManager,
877
                             Dialect mainLanguage,
878
                             bool emitDocChangedOnDisk,
hjk's avatar
hjk committed
879
                             std::function<bool(qreal)> reportProgress)
880 881 882 883 884 885 886
{
    for (int i = 0; i < files.size(); ++i) {
        if (!reportProgress(qreal(i) / files.size()))
            return;

        const QString fileName = files.at(i);

887 888
        Dialect language = guessLanguageOfFile(fileName);
        if (language == Dialect::NoLanguage) {
889 890 891 892
            if (fileName.endsWith(QLatin1String(".qrc")))
                modelManager->updateQrcFile(fileName);
            continue;
        }
893 894
        if (language == Dialect::Qml
                && (mainLanguage == Dialect::QmlQtQuick1 || mainLanguage == Dialect::QmlQtQuick2))
895
            language = mainLanguage;
896 897
        if (language == Dialect::Qml && mainLanguage == Dialect::QmlQtQuick2Ui)
            language = Dialect::QmlQtQuick2;
898 899
        if (language == Dialect::QmlTypeInfo || language == Dialect::QmlProject)
            continue;
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
        QString contents;
        int documentRevision = 0;

        if (workingCopy.contains(fileName)) {
            QPair<QString, int> entry = workingCopy.get(fileName);
            contents = entry.first;
            documentRevision = entry.second;
        } else {
            QFile inFile(fileName);

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

        Document::MutablePtr doc = Document::create(fileName, language);
        doc->setEditorRevision(documentRevision);
        doc->setSource(contents);
        doc->parse();

        // 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();

        // 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);
        findNewLibraryImports(doc, snapshot, modelManager, &importedFiles, &scannedPaths, &newLibraries);

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

        modelManager->updateDocument(doc);
        if (emitDocChangedOnDisk)
            modelManager->emitDocumentChangedOnDisk(doc);
    }
}

class FutureReporter
{
public:
    FutureReporter(QFutureInterface<void> &future, int multiplier = 100, int base = 0)
        :future(future), multiplier(multiplier), base(base)
    { }
    bool operator()(qreal val)
    {
        if (future.isCanceled())
            return false;
        future.setProgressValue(int(base + multiplier * val));
        return true;
    }
private:
    QFutureInterface<void> &future;
    int multiplier;
    int base;
};

void ModelManagerInterface::parse(QFutureInterface<void> &future,
                         WorkingCopy workingCopy,
                         QStringList files,
                         ModelManagerInterface *modelManager,
967
                         Dialect mainLanguage,
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
                         bool emitDocChangedOnDisk)
{
    FutureReporter reporter(future);
    future.setProgressRange(0, 100);

    // paths we have scanned for files and added to the files list
    QSet<QString> scannedPaths;
    // libraries we've found while scanning imports
    QSet<QString> newLibraries;
    parseLoop(scannedPaths, newLibraries, workingCopy, files, modelManager, mainLanguage,
              emitDocChangedOnDisk, reporter);
    future.setProgressValue(100);
}

struct ScanItem {
    QString path;
    int depth;
985 986 987
    Dialect language;
    ScanItem(QString path = QString(), int depth = 0, Dialect language = Dialect::AnyLanguage)
        : path(path), depth(depth), language(language)
988 989 990 991 992
    { }
};

void ModelManagerInterface::importScan(QFutureInterface<void> &future,
                              ModelManagerInterface::WorkingCopy workingCopy,
993
                              PathsAndLanguages paths, ModelManagerInterface *modelManager,
994
                              bool emitDocChangedOnDisk, bool libOnly, bool forceRescan)
995 996
{
    // paths we have scanned for files and added to the files list
997 998 999 1000 1001
    QSet<QString> scannedPaths;
    {
        QMutexLocker l(&modelManager->m_mutex);
        scannedPaths = modelManager->m_scannedPaths;
    }
1002 1003 1004 1005 1006 1007 1008
    // libraries we've found while scanning imports
    QSet<QString> newLibraries;

    QVector<ScanItem> pathsToScan;
    pathsToScan.reserve(paths.size());
    {
        QMutexLocker l(&modelManager->m_mutex);
1009 1010 1011
        for (int i = 0; i < paths.size(); ++i) {
            PathAndLanguage pAndL = paths.at(i);
            QString cPath = QDir::cleanPath(pAndL.path().toString());
1012
            if (!forceRescan && modelManager->m_scannedPaths.contains(cPath))
1013
                continue;
1014
            pathsToScan.append(ScanItem(cPath, 0, pAndL.language()));
1015 1016 1017 1018 1019 1020 1021 1022
            modelManager->m_scannedPaths.insert(cPath);
        }
    }
    const int maxScanDepth = 5;
    int progressRange = pathsToScan.size() * (1 << (2 + maxScanDepth));
    int totalWork(progressRange), workDone(0);
    future.setProgressRange(0, progressRange); // update max length while iterating?
    const Snapshot snapshot = modelManager->snapshot();
1023 1024
    bool isCanceled = future.isCanceled();
    while (!pathsToScan.isEmpty() && !isCanceled) {
1025 1026
        ScanItem toScan = pathsToScan.last();
        pathsToScan.pop_back();
1027
        int pathBudget = (1 << (maxScanDepth + 2 - toScan.depth));
1028
        if (forceRescan || !scannedPaths.contains(toScan.path)) {
1029
            QStringList importedFiles;
1030 1031
            if (forceRescan ||
                    (!findNewQmlLibraryInPath(toScan.path, snapshot, modelManager, &importedFiles,
1032
                                         &scannedPaths, &newLibraries, true)
1033
                    && !libOnly && snapshot.documentsInDirectory(toScan.path).isEmpty())) {
1034
                importedFiles += filesInDirectoryForLanguages(toScan.path,
1035
                                                     toScan.language.companionLanguages());
1036
            }
1037 1038 1039 1040 1041 1042
            workDone += 1;
            future.setProgressValue(progressRange * workDone / totalWork);
            if (!importedFiles.isEmpty()) {
                FutureReporter reporter(future, progressRange * pathBudget / (4 * totalWork),
                                        progressRange * workDone / totalWork);
                parseLoop(scannedPaths, newLibraries, workingCopy, importedFiles, modelManager,
1043
                          toScan.language, emitDocChangedOnDisk, reporter); // run in parallel??
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
                importedFiles.clear();
            }
            workDone += pathBudget / 4 - 1;
            future.setProgressValue(progressRange * workDone / totalWork);
        } else {
            workDone += pathBudget / 4;
        }
        // always descend tree, as we might have just scanned with a smaller depth
        if (toScan.depth < maxScanDepth) {
            QDir dir(toScan.path);
1054
            QStringList subDirs(dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot));
1055 1056
            workDone += 1;
            totalWork += pathBudget / 2 * subDirs.size() - pathBudget * 3 / 4 + 1;
1057 1058
            foreach (const QString path, subDirs)
                pathsToScan.append(ScanItem(dir.absoluteFilePath(path), toScan.depth + 1, toScan.language));
1059
        } else {
1060
            workDone += pathBudget * 3 / 4;
1061 1062
        }
        future.setProgressValue(progressRange * workDone / totalWork);
1063
        isCanceled = future.isCanceled();
1064 1065
    }
    future.setProgressValue(progressRange);
1066
    if (isCanceled) {
1067 1068
        // assume no work has been done
        QMutexLocker l(&modelManager->m_mutex);
1069 1070
        for (int i = 0; i < paths.size(); ++i)
            modelManager->m_scannedPaths.remove(paths.at(i).path().toString());
1071 1072 1073
    }
}

1074
PathsAndLanguages ModelManagerInterface::importPaths() const
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
{
    QMutexLocker l(&m_mutex);
    return m_allImportPaths;
}

QmlLanguageBundles ModelManagerInterface::activeBundles() const
{
    QMutexLocker l(&m_mutex);
    return m_activeBundles;
}

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

1092
void ModelManagerInterface::maybeScan(const PathsAndLanguages &importPaths)
Fawzi Mohamed's avatar
Fawzi Mohamed committed
1093
{
1094 1095
    if (!m_indexerEnabled)
        return;
1096
    PathsAndLanguages pathToScan;
Fawzi Mohamed's avatar
Fawzi Mohamed committed
1097 1098
    {
        QMutexLocker l(&m_mutex);
1099 1100 1101
        foreach (const PathAndLanguage &importPath, importPaths)
            if (!m_scannedPaths.contains(importPath.path().toString()))
                pathToScan.maybeInsert(importPath);
Fawzi Mohamed's avatar
Fawzi Mohamed committed
1102 1103
    }

1104
    if (pathToScan.length() > 1) {
1105 1106
        QFuture<void> result = Utils::runAsync(&ModelManagerInterface::importScan,
                                               workingCopyInternal(), pathToScan,
1107
                                               this, true, true, false);
1108 1109
        cleanupFutures();
        m_futures.append(result);
Fawzi Mohamed's avatar
Fawzi Mohamed committed
1110

1111
        addTaskInternal(result, tr("Scanning QML Imports"), Constants::TASK_IMPORT_SCAN);
Fawzi Mohamed's avatar
Fawzi Mohamed committed
1112 1113 1114
    }
}

1115 1116
void ModelManagerInterface::updateImportPaths()
{
1117 1118
    if (!m_indexerEnabled)
        return;
1119
    PathsAndLanguages allImportPaths;
1120 1121
    QmlLanguageBundles activeBundles;
    QmlLanguageBundles extendedBundles;
1122
    QMapIterator<ProjectExplorer::Project *, ProjectInfo> pInfoIter(m_projects);
1123
    QHashIterator<Dialect, QmlJS::ViewerContext> vCtxsIter = m_defaultVContexts;
1124 1125
    while (pInfoIter.hasNext()) {
        pInfoIter.next();
1126 1127 1128 1129
        const PathsAndLanguages &iPaths = pInfoIter.value().importPaths;
        for (int i = 0; i < iPaths.size(); ++i) {
            PathAndLanguage pAndL = iPaths.at(i);
            const QString canonicalPath = pAndL.path().toFileInfo().canonicalFilePath();
1130
            if (!canonicalPath.isEmpty())
1131 1132
                allImportPaths.maybeInsert(Utils::FileName::fromString(canonicalPath),
                                           pAndL.language());
1133 1134
        }
    }
1135 1136
    while (vCtxsIter.hasNext()) {
        vCtxsIter.next();