2009-09-04 16:51:11 +02:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2010-03-05 11:25:49 +01:00
|
|
|
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
2009-09-04 16:51:11 +02:00
|
|
|
**
|
|
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
|
|
|
**
|
|
|
|
|
** Commercial Usage
|
|
|
|
|
**
|
|
|
|
|
** Licensees holding valid Qt Commercial licenses may use this file in
|
|
|
|
|
** accordance with the Qt Commercial License Agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Nokia.
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
|
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
** If you are unsure which license is appropriate for your use, please
|
|
|
|
|
** contact the sales department at http://qt.nokia.com/contact.
|
|
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
2010-01-18 12:38:36 +01:00
|
|
|
#include "qmljseditorconstants.h"
|
2010-02-15 13:49:00 +01:00
|
|
|
#include "qmljsmodelmanager.h"
|
2010-01-25 14:18:53 +01:00
|
|
|
#include "qmljseditor.h"
|
2009-09-04 16:51:11 +02:00
|
|
|
|
|
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
|
|
|
|
#include <coreplugin/progressmanager/progressmanager.h>
|
2010-09-15 14:13:33 +02:00
|
|
|
#include <coreplugin/messagemanager.h>
|
2010-01-18 12:38:36 +01:00
|
|
|
#include <coreplugin/mimedatabase.h>
|
2010-03-01 13:01:05 +01:00
|
|
|
#include <qmljs/qmljsinterpreter.h>
|
2010-04-06 09:52:29 +02:00
|
|
|
#include <qmljs/qmljsbind.h>
|
2010-03-18 15:43:33 +01:00
|
|
|
#include <qmljs/parser/qmldirparser_p.h>
|
2009-09-04 16:51:11 +02:00
|
|
|
#include <texteditor/itexteditor.h>
|
2010-06-22 12:54:19 +02:00
|
|
|
#include <projectexplorer/project.h>
|
2010-09-27 15:51:49 +02:00
|
|
|
#include <projectexplorer/projectexplorer.h>
|
2009-09-04 16:51:11 +02:00
|
|
|
|
2010-03-01 13:01:05 +01:00
|
|
|
#include <QDir>
|
2010-01-18 12:38:36 +01:00
|
|
|
#include <QFile>
|
|
|
|
|
#include <QFileInfo>
|
2010-03-16 16:34:33 +01:00
|
|
|
#include <QLibraryInfo>
|
2010-01-18 12:38:36 +01:00
|
|
|
#include <QtConcurrentRun>
|
|
|
|
|
#include <qtconcurrent/runextensions.h>
|
|
|
|
|
#include <QTextStream>
|
2010-06-09 14:27:30 +02:00
|
|
|
#include <QCoreApplication>
|
2009-10-01 16:38:08 +02:00
|
|
|
|
2010-03-01 13:01:05 +01:00
|
|
|
#include <QDebug>
|
|
|
|
|
|
2010-01-25 14:18:53 +01:00
|
|
|
using namespace QmlJS;
|
2010-01-15 17:20:03 +01:00
|
|
|
using namespace QmlJSEditor;
|
|
|
|
|
using namespace QmlJSEditor::Internal;
|
2009-09-04 16:51:11 +02:00
|
|
|
|
2010-03-16 16:34:33 +01:00
|
|
|
static QStringList environmentImportPaths();
|
|
|
|
|
|
2010-02-15 13:49:00 +01:00
|
|
|
ModelManager::ModelManager(QObject *parent):
|
|
|
|
|
ModelManagerInterface(parent),
|
2009-09-04 16:51:11 +02:00
|
|
|
m_core(Core::ICore::instance())
|
|
|
|
|
{
|
|
|
|
|
m_synchronizer.setCancelOnWait(true);
|
|
|
|
|
|
2010-01-18 16:15:23 +01:00
|
|
|
qRegisterMetaType<QmlJS::Document::Ptr>("QmlJS::Document::Ptr");
|
2010-03-18 15:43:33 +01:00
|
|
|
qRegisterMetaType<QmlJS::LibraryInfo>("QmlJS::LibraryInfo");
|
2009-09-04 16:51:11 +02:00
|
|
|
|
2010-03-01 13:01:05 +01:00
|
|
|
loadQmlTypeDescriptions();
|
2010-03-16 16:34:33 +01:00
|
|
|
|
|
|
|
|
m_defaultImportPaths << environmentImportPaths();
|
|
|
|
|
m_defaultImportPaths << QLibraryInfo::location(QLibraryInfo::ImportsPath);
|
2010-03-01 13:01:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ModelManager::loadQmlTypeDescriptions()
|
|
|
|
|
{
|
2010-09-08 10:11:44 +02:00
|
|
|
loadQmlTypeDescriptions(Core::ICore::instance()->resourcePath());
|
|
|
|
|
loadQmlTypeDescriptions(Core::ICore::instance()->userResourcePath());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ModelManager::loadQmlTypeDescriptions(const QString &resourcePath)
|
|
|
|
|
{
|
2010-03-01 13:01:05 +01:00
|
|
|
const QDir typeFileDir(resourcePath + QLatin1String("/qml-type-descriptions"));
|
|
|
|
|
const QStringList xmlExtensions = QStringList() << QLatin1String("*.xml");
|
|
|
|
|
const QFileInfoList xmlFiles = typeFileDir.entryInfoList(xmlExtensions,
|
|
|
|
|
QDir::Files,
|
|
|
|
|
QDir::Name);
|
|
|
|
|
|
2010-06-09 14:27:30 +02:00
|
|
|
const QStringList errors = Interpreter::CppQmlTypesLoader::load(xmlFiles);
|
2010-03-01 13:01:05 +01:00
|
|
|
foreach (const QString &error, errors)
|
|
|
|
|
qWarning() << qPrintable(error);
|
2010-06-10 10:48:23 +02:00
|
|
|
|
2010-06-21 15:04:39 +02:00
|
|
|
// disabled for now: Prefer the xml file until the type dumping functionality
|
|
|
|
|
// has been moved into Qt.
|
2010-06-10 10:48:23 +02:00
|
|
|
// loads the builtin types
|
2010-06-21 15:04:39 +02:00
|
|
|
//loadQmlPluginTypes(QString());
|
2009-09-04 16:51:11 +02:00
|
|
|
}
|
|
|
|
|
|
2010-09-24 14:05:34 +02:00
|
|
|
ModelManagerInterface::WorkingCopy ModelManager::workingCopy() const
|
|
|
|
|
{
|
|
|
|
|
WorkingCopy workingCopy;
|
|
|
|
|
Core::EditorManager *editorManager = m_core->editorManager();
|
|
|
|
|
|
|
|
|
|
foreach (Core::IEditor *editor, editorManager->openedEditors()) {
|
|
|
|
|
const QString key = editor->file()->fileName();
|
|
|
|
|
|
|
|
|
|
if (TextEditor::ITextEditor *textEditor = qobject_cast<TextEditor::ITextEditor*>(editor)) {
|
|
|
|
|
if (QmlJSTextEditor *ed = qobject_cast<QmlJSTextEditor *>(textEditor->widget())) {
|
|
|
|
|
workingCopy.insert(key, ed->toPlainText(), ed->document()->revision());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return workingCopy;
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-15 13:49:00 +01:00
|
|
|
Snapshot ModelManager::snapshot() const
|
2009-09-04 16:51:11 +02:00
|
|
|
{
|
2009-09-04 17:54:38 +02:00
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
|
|
2009-09-04 16:51:11 +02:00
|
|
|
return _snapshot;
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-16 13:08:59 +02:00
|
|
|
void ModelManager::updateSourceFiles(const QStringList &files,
|
|
|
|
|
bool emitDocumentOnDiskChanged)
|
2009-09-04 16:51:11 +02:00
|
|
|
{
|
2010-04-16 13:08:59 +02:00
|
|
|
refreshSourceFiles(files, emitDocumentOnDiskChanged);
|
2009-09-04 16:51:11 +02:00
|
|
|
}
|
|
|
|
|
|
2010-04-16 13:08:59 +02:00
|
|
|
QFuture<void> ModelManager::refreshSourceFiles(const QStringList &sourceFiles,
|
|
|
|
|
bool emitDocumentOnDiskChanged)
|
2009-09-04 16:51:11 +02:00
|
|
|
{
|
2009-09-21 18:27:15 +02:00
|
|
|
if (sourceFiles.isEmpty()) {
|
|
|
|
|
return QFuture<void>();
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-15 13:49:00 +01:00
|
|
|
QFuture<void> result = QtConcurrent::run(&ModelManager::parse,
|
2010-09-24 14:05:34 +02:00
|
|
|
workingCopy(), sourceFiles,
|
2010-04-16 13:08:59 +02:00
|
|
|
this,
|
|
|
|
|
emitDocumentOnDiskChanged);
|
2009-09-04 16:51:11 +02:00
|
|
|
|
2009-09-21 18:27:15 +02:00
|
|
|
if (m_synchronizer.futures().size() > 10) {
|
|
|
|
|
QList<QFuture<void> > futures = m_synchronizer.futures();
|
2009-09-04 16:51:11 +02:00
|
|
|
|
2009-09-21 18:27:15 +02:00
|
|
|
m_synchronizer.clearFutures();
|
2009-09-04 16:51:11 +02:00
|
|
|
|
2009-09-21 18:27:15 +02:00
|
|
|
foreach (QFuture<void> future, futures) {
|
|
|
|
|
if (! (future.isFinished() || future.isCanceled()))
|
|
|
|
|
m_synchronizer.addFuture(future);
|
2009-09-04 16:51:11 +02:00
|
|
|
}
|
2009-09-21 18:27:15 +02:00
|
|
|
}
|
2009-09-04 16:51:11 +02:00
|
|
|
|
2009-09-21 18:27:15 +02:00
|
|
|
m_synchronizer.addFuture(result);
|
2009-09-04 16:51:11 +02:00
|
|
|
|
2009-09-21 18:27:15 +02:00
|
|
|
if (sourceFiles.count() > 1) {
|
|
|
|
|
m_core->progressManager()->addTask(result, tr("Indexing"),
|
2010-01-15 17:20:03 +01:00
|
|
|
QmlJSEditor::Constants::TASK_INDEX);
|
2009-09-04 16:51:11 +02:00
|
|
|
}
|
2009-09-21 18:27:15 +02:00
|
|
|
|
|
|
|
|
return result;
|
2009-09-04 16:51:11 +02:00
|
|
|
}
|
|
|
|
|
|
2010-04-16 12:42:12 +02:00
|
|
|
void ModelManager::fileChangedOnDisk(const QString &path)
|
|
|
|
|
{
|
|
|
|
|
QtConcurrent::run(&ModelManager::parse,
|
2010-09-24 14:05:34 +02:00
|
|
|
workingCopy(), QStringList() << path,
|
2010-04-16 12:42:12 +02:00
|
|
|
this, true);
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-18 13:40:35 +02:00
|
|
|
void ModelManager::removeFiles(const QStringList &files)
|
|
|
|
|
{
|
|
|
|
|
emit aboutToRemoveFiles(files);
|
|
|
|
|
|
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
|
|
|
|
|
|
foreach (const QString &file, files)
|
|
|
|
|
_snapshot.remove(file);
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-22 12:54:19 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
|
m_projects.insert(pinfo.project, pinfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateImportPaths();
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-16 12:42:12 +02:00
|
|
|
void ModelManager::emitDocumentChangedOnDisk(Document::Ptr doc)
|
|
|
|
|
{ emit documentChangedOnDisk(doc); }
|
|
|
|
|
|
2010-10-15 10:06:59 +02:00
|
|
|
void ModelManager::updateDocument(Document::Ptr doc)
|
2009-09-04 16:51:11 +02:00
|
|
|
{
|
2010-10-15 10:06:59 +02:00
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
|
_snapshot.insert(doc);
|
|
|
|
|
}
|
|
|
|
|
emit documentUpdated(doc);
|
2009-09-04 16:51:11 +02:00
|
|
|
}
|
|
|
|
|
|
2010-10-15 10:06:59 +02:00
|
|
|
void ModelManager::updateLibraryInfo(const QString &path, const LibraryInfo &info)
|
2010-03-18 15:43:33 +01:00
|
|
|
{
|
2010-10-15 10:06:59 +02:00
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
|
_snapshot.insertLibraryInfo(path, info);
|
|
|
|
|
}
|
|
|
|
|
emit libraryInfoUpdated(path, info);
|
2010-03-18 15:43:33 +01:00
|
|
|
}
|
|
|
|
|
|
2010-04-06 09:52:29 +02:00
|
|
|
static QStringList qmlFilesInDirectory(const QString &path)
|
|
|
|
|
{
|
|
|
|
|
// ### It would suffice to build pattern once. This function needs to be thread-safe.
|
|
|
|
|
Core::MimeDatabase *db = Core::ICore::instance()->mimeDatabase();
|
|
|
|
|
Core::MimeType jsSourceTy = db->findByType(QmlJSEditor::Constants::JS_MIMETYPE);
|
|
|
|
|
Core::MimeType qmlSourceTy = db->findByType(QmlJSEditor::Constants::QML_MIMETYPE);
|
|
|
|
|
|
|
|
|
|
QStringList pattern;
|
2010-09-13 16:26:16 +02:00
|
|
|
foreach (const Core::MimeGlobPattern &glob, jsSourceTy.globPatterns())
|
|
|
|
|
pattern << glob.regExp().pattern();
|
|
|
|
|
foreach (const Core::MimeGlobPattern &glob, qmlSourceTy.globPatterns())
|
|
|
|
|
pattern << glob.regExp().pattern();
|
2010-04-06 09:52:29 +02:00
|
|
|
|
|
|
|
|
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
|
2010-09-16 15:29:37 +02:00
|
|
|
foreach (const Interpreter::ImportInfo &import, doc->bind()->imports()) {
|
|
|
|
|
const QString &importName = import.name();
|
|
|
|
|
if (import.type() == Interpreter::ImportInfo::FileImport) {
|
|
|
|
|
if (! snapshot.document(importName))
|
|
|
|
|
*importedFiles += importName;
|
|
|
|
|
} else if (import.type() == Interpreter::ImportInfo::DirectoryImport) {
|
|
|
|
|
if (snapshot.documentsInDirectory(importName).isEmpty()) {
|
|
|
|
|
if (! scannedPaths->contains(importName)) {
|
|
|
|
|
*importedFiles += qmlFilesInDirectory(importName);
|
|
|
|
|
scannedPaths->insert(importName);
|
2010-09-16 09:40:19 +02:00
|
|
|
}
|
2010-04-06 09:52:29 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void findNewLibraryImports(const Document::Ptr &doc, const Snapshot &snapshot,
|
|
|
|
|
ModelManager *modelManager,
|
|
|
|
|
QStringList *importedFiles, QSet<QString> *scannedPaths)
|
|
|
|
|
{
|
|
|
|
|
// scan library imports
|
2010-04-26 10:40:18 +02:00
|
|
|
const QStringList importPaths = modelManager->importPaths();
|
2010-09-16 15:29:37 +02:00
|
|
|
foreach (const Interpreter::ImportInfo &import, doc->bind()->imports()) {
|
|
|
|
|
if (import.type() != Interpreter::ImportInfo::LibraryImport)
|
2010-09-16 09:40:19 +02:00
|
|
|
continue;
|
2010-04-07 10:22:36 +02:00
|
|
|
foreach (const QString &importPath, importPaths) {
|
2010-04-06 09:52:29 +02:00
|
|
|
QDir dir(importPath);
|
2010-09-16 15:29:37 +02:00
|
|
|
dir.cd(import.name());
|
2010-04-06 09:52:29 +02:00
|
|
|
const QString targetPath = dir.absolutePath();
|
|
|
|
|
|
|
|
|
|
// if we know there is a library, done
|
|
|
|
|
if (snapshot.libraryInfo(targetPath).isValid())
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// if there is a qmldir file, we found a new library!
|
|
|
|
|
if (dir.exists("qmldir")) {
|
|
|
|
|
QFile qmldirFile(dir.filePath("qmldir"));
|
|
|
|
|
qmldirFile.open(QFile::ReadOnly);
|
|
|
|
|
QString qmldirData = QString::fromUtf8(qmldirFile.readAll());
|
|
|
|
|
|
|
|
|
|
QmlDirParser qmldirParser;
|
|
|
|
|
qmldirParser.setSource(qmldirData);
|
|
|
|
|
qmldirParser.parse();
|
|
|
|
|
|
2010-10-15 10:06:59 +02:00
|
|
|
modelManager->updateLibraryInfo(QFileInfo(qmldirFile).absolutePath(),
|
2010-04-06 09:52:29 +02:00
|
|
|
LibraryInfo(qmldirParser));
|
|
|
|
|
|
|
|
|
|
// scan the qml files in the library
|
|
|
|
|
foreach (const QmlDirParser::Component &component, qmldirParser.components()) {
|
|
|
|
|
if (! component.fileName.isEmpty()) {
|
|
|
|
|
QFileInfo componentFileInfo(dir.filePath(component.fileName));
|
|
|
|
|
const QString path = componentFileInfo.absolutePath();
|
|
|
|
|
if (! scannedPaths->contains(path)) {
|
|
|
|
|
*importedFiles += qmlFilesInDirectory(path);
|
|
|
|
|
scannedPaths->insert(path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-15 13:49:00 +01:00
|
|
|
void ModelManager::parse(QFutureInterface<void> &future,
|
2010-09-24 14:05:34 +02:00
|
|
|
WorkingCopy workingCopy,
|
2009-09-04 16:51:11 +02:00
|
|
|
QStringList files,
|
2010-04-16 12:42:12 +02:00
|
|
|
ModelManager *modelManager,
|
|
|
|
|
bool emitDocChangedOnDisk)
|
2009-09-04 16:51:11 +02:00
|
|
|
{
|
2010-01-25 14:18:53 +01:00
|
|
|
Core::MimeDatabase *db = Core::ICore::instance()->mimeDatabase();
|
|
|
|
|
Core::MimeType jsSourceTy = db->findByType(QLatin1String("application/javascript"));
|
|
|
|
|
Core::MimeType qmlSourceTy = db->findByType(QLatin1String("application/x-qml"));
|
|
|
|
|
|
2010-04-06 09:52:29 +02:00
|
|
|
int progressRange = files.size();
|
|
|
|
|
future.setProgressRange(0, progressRange);
|
|
|
|
|
|
|
|
|
|
Snapshot snapshot = modelManager->_snapshot;
|
|
|
|
|
|
|
|
|
|
// paths we have scanned for files and added to the files list
|
|
|
|
|
QSet<QString> scannedPaths;
|
2009-09-04 16:51:11 +02:00
|
|
|
|
|
|
|
|
for (int i = 0; i < files.size(); ++i) {
|
2010-04-06 09:52:29 +02:00
|
|
|
future.setProgressValue(qreal(i) / files.size() * progressRange);
|
2009-09-04 16:51:11 +02:00
|
|
|
|
|
|
|
|
const QString fileName = files.at(i);
|
2010-02-15 14:07:35 +01:00
|
|
|
|
|
|
|
|
const QFileInfo fileInfo(fileName);
|
|
|
|
|
Core::MimeType fileMimeTy = db->findByFile(fileInfo);
|
|
|
|
|
|
|
|
|
|
bool isQmlFile = true;
|
|
|
|
|
|
|
|
|
|
if (matchesMimeType(fileMimeTy, jsSourceTy))
|
|
|
|
|
isQmlFile = false;
|
|
|
|
|
|
|
|
|
|
else if (! matchesMimeType(fileMimeTy, qmlSourceTy))
|
|
|
|
|
continue; // skip it. it's not a QML or a JS file.
|
|
|
|
|
|
2009-09-04 16:51:11 +02:00
|
|
|
QString contents;
|
2010-01-25 14:18:53 +01:00
|
|
|
int documentRevision = 0;
|
2009-09-04 16:51:11 +02:00
|
|
|
|
|
|
|
|
if (workingCopy.contains(fileName)) {
|
2010-09-24 14:05:34 +02:00
|
|
|
QPair<QString, int> entry = workingCopy.get(fileName);
|
|
|
|
|
contents = entry.first;
|
|
|
|
|
documentRevision = entry.second;
|
2009-09-04 16:51:11 +02:00
|
|
|
} else {
|
|
|
|
|
QFile inFile(fileName);
|
|
|
|
|
|
|
|
|
|
if (inFile.open(QIODevice::ReadOnly)) {
|
|
|
|
|
QTextStream ins(&inFile);
|
|
|
|
|
contents = ins.readAll();
|
|
|
|
|
inFile.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-25 14:18:53 +01:00
|
|
|
Document::Ptr doc = Document::create(fileName);
|
2010-07-07 17:52:02 +02:00
|
|
|
doc->setEditorRevision(documentRevision);
|
2009-09-04 16:51:11 +02:00
|
|
|
doc->setSource(contents);
|
2010-04-06 09:52:29 +02:00
|
|
|
doc->parse();
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
|
|
// add new files to parse list
|
|
|
|
|
foreach (const QString &file, importedFiles) {
|
|
|
|
|
if (! files.contains(file))
|
|
|
|
|
files.append(file);
|
2010-03-18 15:43:33 +01:00
|
|
|
}
|
|
|
|
|
|
2010-10-15 10:06:59 +02:00
|
|
|
modelManager->updateDocument(doc);
|
2010-04-16 12:42:12 +02:00
|
|
|
if (emitDocChangedOnDisk)
|
|
|
|
|
modelManager->emitDocumentChangedOnDisk(doc);
|
2010-03-16 16:34:33 +01:00
|
|
|
}
|
|
|
|
|
|
2010-04-06 09:52:29 +02:00
|
|
|
future.setProgressValue(progressRange);
|
2010-03-16 16:34:33 +01:00
|
|
|
}
|
|
|
|
|
|
2010-01-20 16:43:50 +01:00
|
|
|
// Check whether fileMimeType is the same or extends knownMimeType
|
2010-02-15 13:49:00 +01:00
|
|
|
bool ModelManager::matchesMimeType(const Core::MimeType &fileMimeType, const Core::MimeType &knownMimeType)
|
2010-01-20 16:43:50 +01:00
|
|
|
{
|
|
|
|
|
Core::MimeDatabase *db = Core::ICore::instance()->mimeDatabase();
|
|
|
|
|
|
|
|
|
|
const QStringList knownTypeNames = QStringList(knownMimeType.type()) + knownMimeType.aliases();
|
|
|
|
|
|
|
|
|
|
foreach (const QString knownTypeName, knownTypeNames)
|
|
|
|
|
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;
|
|
|
|
|
}
|
2010-03-16 16:34:33 +01:00
|
|
|
|
|
|
|
|
QStringList ModelManager::importPaths() const
|
|
|
|
|
{
|
2010-06-22 12:54:19 +02:00
|
|
|
return m_allImportPaths;
|
2010-03-16 16:34:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static QStringList environmentImportPaths()
|
|
|
|
|
{
|
|
|
|
|
QStringList paths;
|
|
|
|
|
|
|
|
|
|
QByteArray envImportPath = qgetenv("QML_IMPORT_PATH");
|
|
|
|
|
|
|
|
|
|
#if defined(Q_OS_WIN)
|
|
|
|
|
QLatin1Char pathSep(';');
|
|
|
|
|
#else
|
|
|
|
|
QLatin1Char pathSep(':');
|
|
|
|
|
#endif
|
|
|
|
|
foreach (const QString &path, QString::fromLatin1(envImportPath).split(pathSep, QString::SkipEmptyParts)) {
|
|
|
|
|
QString canonicalPath = QDir(path).canonicalPath();
|
|
|
|
|
if (!canonicalPath.isEmpty() && !paths.contains(canonicalPath))
|
|
|
|
|
paths.append(canonicalPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return paths;
|
|
|
|
|
}
|
2010-06-09 14:27:30 +02:00
|
|
|
|
2010-08-25 14:15:57 +02:00
|
|
|
void ModelManager::loadPluginTypes(const QString &libraryPath, const QString &importPath, const QString &importUri)
|
|
|
|
|
{
|
|
|
|
|
// make sure loading is always triggered in ModelManager's thread
|
|
|
|
|
metaObject()->invokeMethod(this, "onLoadPluginTypes",
|
|
|
|
|
Q_ARG(QString, libraryPath),
|
|
|
|
|
Q_ARG(QString, importPath),
|
|
|
|
|
Q_ARG(QString, importUri));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ModelManager::onLoadPluginTypes(const QString &libraryPath, const QString &importPath, const QString &importUri)
|
2010-06-09 14:27:30 +02:00
|
|
|
{
|
2010-09-08 15:10:35 +02:00
|
|
|
const QString canonicalLibraryPath = QDir::cleanPath(libraryPath);
|
|
|
|
|
if (m_runningQmldumps.values().contains(canonicalLibraryPath))
|
2010-08-25 14:15:57 +02:00
|
|
|
return;
|
2010-10-14 14:44:50 +02:00
|
|
|
if (_snapshot.libraryInfo(canonicalLibraryPath).isDumped())
|
|
|
|
|
return;
|
2010-08-25 14:15:57 +02:00
|
|
|
|
2010-09-27 17:22:57 +02:00
|
|
|
ProjectExplorer::Project *activeProject = ProjectExplorer::ProjectExplorerPlugin::instance()->startupProject();
|
|
|
|
|
if (!activeProject)
|
|
|
|
|
return;
|
2010-06-09 14:27:30 +02:00
|
|
|
|
2010-09-27 17:22:57 +02:00
|
|
|
ProjectInfo info = projectInfo(activeProject);
|
|
|
|
|
|
|
|
|
|
if (info.qmlDumpPath.isEmpty())
|
2010-06-09 14:27:30 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
QProcess *process = new QProcess(this);
|
2010-10-26 15:39:37 +02:00
|
|
|
process->setEnvironment(info.qmlDumpEnvironment.toStringList());
|
2010-06-09 14:27:30 +02:00
|
|
|
connect(process, SIGNAL(finished(int)), SLOT(qmlPluginTypeDumpDone(int)));
|
2010-09-15 14:13:33 +02:00
|
|
|
connect(process, SIGNAL(error(QProcess::ProcessError)), SLOT(qmlPluginTypeDumpError(QProcess::ProcessError)));
|
2010-08-25 14:15:57 +02:00
|
|
|
QStringList args;
|
|
|
|
|
args << importPath;
|
|
|
|
|
args << importUri;
|
2010-09-27 17:22:57 +02:00
|
|
|
process->start(info.qmlDumpPath, args);
|
2010-09-08 15:10:35 +02:00
|
|
|
m_runningQmldumps.insert(process, canonicalLibraryPath);
|
2010-06-09 14:27:30 +02:00
|
|
|
}
|
|
|
|
|
|
2010-06-22 12:54:19 +02:00
|
|
|
void ModelManager::updateImportPaths()
|
|
|
|
|
{
|
2010-09-08 16:52:24 +02:00
|
|
|
m_allImportPaths.clear();
|
2010-06-22 12:54:19 +02:00
|
|
|
QMapIterator<ProjectExplorer::Project *, ProjectInfo> it(m_projects);
|
|
|
|
|
while (it.hasNext()) {
|
|
|
|
|
it.next();
|
|
|
|
|
m_allImportPaths += it.value().importPaths;
|
|
|
|
|
}
|
|
|
|
|
m_allImportPaths += m_defaultImportPaths;
|
2010-09-08 16:52:24 +02:00
|
|
|
m_allImportPaths.removeDuplicates();
|
2010-06-22 12:54:19 +02:00
|
|
|
|
|
|
|
|
// check if any file in the snapshot imports something new in the new paths
|
|
|
|
|
Snapshot snapshot = _snapshot;
|
|
|
|
|
QStringList importedFiles;
|
|
|
|
|
QSet<QString> scannedPaths;
|
|
|
|
|
foreach (const Document::Ptr &doc, snapshot)
|
|
|
|
|
findNewLibraryImports(doc, snapshot, this, &importedFiles, &scannedPaths);
|
|
|
|
|
|
|
|
|
|
updateSourceFiles(importedFiles, true);
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-15 14:13:33 +02:00
|
|
|
static QString qmldumpErrorMessage(const QString &libraryPath, const QString &error)
|
|
|
|
|
{
|
|
|
|
|
return ModelManager::tr("Type dump of QML plugin in %0 failed.\nErrors:\n%1\n").arg(libraryPath, error);
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-09 14:27:30 +02:00
|
|
|
void ModelManager::qmlPluginTypeDumpDone(int exitCode)
|
|
|
|
|
{
|
|
|
|
|
QProcess *process = qobject_cast<QProcess *>(sender());
|
|
|
|
|
if (!process)
|
|
|
|
|
return;
|
|
|
|
|
process->deleteLater();
|
2010-09-15 14:13:33 +02:00
|
|
|
|
|
|
|
|
const QString libraryPath = m_runningQmldumps.take(process);
|
2010-10-14 14:44:50 +02:00
|
|
|
LibraryInfo libraryInfo = _snapshot.libraryInfo(libraryPath);
|
|
|
|
|
libraryInfo.setDumped(true);
|
2010-09-15 14:13:33 +02:00
|
|
|
|
|
|
|
|
if (exitCode != 0) {
|
|
|
|
|
Core::MessageManager *messageManager = Core::MessageManager::instance();
|
|
|
|
|
messageManager->printToOutputPane(qmldumpErrorMessage(libraryPath, process->readAllStandardError()));
|
|
|
|
|
}
|
2010-06-09 14:27:30 +02:00
|
|
|
|
|
|
|
|
const QByteArray output = process->readAllStandardOutput();
|
|
|
|
|
QMap<QString, Interpreter::FakeMetaObject *> newObjects;
|
|
|
|
|
const QString error = Interpreter::CppQmlTypesLoader::parseQmlTypeXml(output, &newObjects);
|
|
|
|
|
|
2010-10-14 14:44:50 +02:00
|
|
|
if (exitCode == 0 && error.isEmpty()) {
|
|
|
|
|
// convert from QList<T *> to QList<const T *>
|
|
|
|
|
QList<const Interpreter::FakeMetaObject *> objectsList;
|
|
|
|
|
QMapIterator<QString, Interpreter::FakeMetaObject *> it(newObjects);
|
|
|
|
|
while (it.hasNext()) {
|
|
|
|
|
it.next();
|
|
|
|
|
objectsList.append(it.value());
|
|
|
|
|
}
|
2010-06-10 10:48:23 +02:00
|
|
|
libraryInfo.setMetaObjects(objectsList);
|
2010-10-14 14:44:50 +02:00
|
|
|
if (libraryPath.isEmpty())
|
|
|
|
|
Interpreter::CppQmlTypesLoader::builtinObjects.append(objectsList);
|
2010-06-10 10:48:23 +02:00
|
|
|
}
|
2010-10-14 14:44:50 +02:00
|
|
|
|
|
|
|
|
if (!libraryPath.isEmpty())
|
2010-10-15 10:06:59 +02:00
|
|
|
updateLibraryInfo(libraryPath, libraryInfo);
|
2010-06-09 14:27:30 +02:00
|
|
|
}
|
2010-09-15 14:13:33 +02:00
|
|
|
|
|
|
|
|
void ModelManager::qmlPluginTypeDumpError(QProcess::ProcessError)
|
|
|
|
|
{
|
|
|
|
|
QProcess *process = qobject_cast<QProcess *>(sender());
|
|
|
|
|
if (!process)
|
|
|
|
|
return;
|
|
|
|
|
process->deleteLater();
|
|
|
|
|
|
|
|
|
|
const QString libraryPath = m_runningQmldumps.take(process);
|
|
|
|
|
|
|
|
|
|
Core::MessageManager *messageManager = Core::MessageManager::instance();
|
|
|
|
|
messageManager->printToOutputPane(qmldumpErrorMessage(libraryPath, process->readAllStandardError()));
|
2010-10-14 14:44:50 +02:00
|
|
|
|
|
|
|
|
if (!libraryPath.isEmpty()) {
|
|
|
|
|
LibraryInfo libraryInfo = _snapshot.libraryInfo(libraryPath);
|
|
|
|
|
libraryInfo.setDumped(true);
|
2010-10-15 10:06:59 +02:00
|
|
|
updateLibraryInfo(libraryPath, libraryInfo);
|
2010-10-14 14:44:50 +02:00
|
|
|
}
|
2010-09-15 14:13:33 +02:00
|
|
|
}
|