Commit 5a4cdc11 authored by Fawzi Mohamed's avatar Fawzi Mohamed
Browse files

qmljs: adding qrc support



Qmljs now keeps a cache of parsed qrc files, and can resolve "qrc:" links.

This breaks the assumption that the name that the qml files has on
the filesystem is the one that qml sees, and that contents of
directories can be found just looking at file whose path starts with the
directory path.

Currently the first file is used when multiple qrc files contain the same
path, but support for strict and weak path resolving is already there.

At the moment only qrc files for projects that call updateQmljsCodeModel
are updated.

ChangeLog: QmljsSupport: Imports using qrc links are resolved.

Task-number: QTCREATORBUG-8953
Change-Id: I695fac2692af2417d49c192c580a1c2e7b4873f4
Reviewed-by: default avatarFawzi Mohamed <fawzi.mohamed@digia.com>
parent 69a9dc3f
......@@ -39,7 +39,8 @@ HEADERS += \
$$PWD/consoleitem.h \
$$PWD/iscriptevaluator.h \
$$PWD/qmljssimplereader.h \
$$PWD/persistenttrie.h
$$PWD/persistenttrie.h \
$$PWD/qmljsqrcparser.h
SOURCES += \
$$PWD/qmljsbind.cpp \
......@@ -69,7 +70,8 @@ SOURCES += \
$$PWD/consolemanagerinterface.cpp \
$$PWD/consoleitem.cpp \
$$PWD/qmljssimplereader.cpp \
$$PWD/persistenttrie.cpp
$$PWD/persistenttrie.cpp \
$$PWD/qmljsqrcparser.cpp
RESOURCES += \
$$PWD/qmljs.qrc
......
DEFINES += QMLJS_BUILD_DIR
QT +=script
QT +=script xml
include(../../qtcreatorlibrary.pri)
include(qmljs-lib.pri)
......@@ -12,7 +12,7 @@ QtcLibrary {
Depends { name: "Utils" }
Depends { name: "LanguageUtils" }
Depends { name: "Qt"; submodules: ["widgets", "script"] }
Depends { name: "Qt"; submodules: ["widgets", "script", "xml"] }
files: [
"jsoncheck.cpp",
......@@ -52,6 +52,8 @@ QtcLibrary {
"qmljsmodelmanagerinterface.h",
"qmljspropertyreader.cpp",
"qmljspropertyreader.h",
"qmljsqrcparser.cpp",
"qmljsqrcparser.h",
"qmljsreformatter.cpp",
"qmljsreformatter.h",
"qmljsrewriter.cpp",
......
......@@ -115,6 +115,22 @@ bool Document::isFullySupportedLanguage(Document::Language language)
return false;
}
bool Document::isQmlLikeOrJsLanguage(Document::Language language)
{
switch (language) {
case QmlLanguage:
case QmlQtQuick1Language:
case QmlQtQuick2Language:
case QmlQbsLanguage:
case QmlProjectLanguage:
case QmlTypeInfoLanguage:
case JavaScriptLanguage:
return true;
default:
return false;
}
}
Document::Document(const QString &fileName, Language language)
: _engine(0)
, _ast(0)
......
......@@ -65,6 +65,7 @@ public:
static bool isQmlLikeLanguage(Language languge);
static bool isFullySupportedLanguage(Language language);
static bool isQmlLikeOrJsLanguage(Language language);
protected:
Document(const QString &fileName, Language language);
......
......@@ -35,6 +35,7 @@
#include "qmljstypedescriptionreader.h"
#include "qmljsvalueowner.h"
#include "qmljscontext.h"
#include "qmljsmodelmanagerinterface.h"
#include "parser/qmljsast_p.h"
#include <utils/qtcassert.h>
......@@ -2107,13 +2108,19 @@ ImportInfo ImportInfo::pathImport(const QString &docPath, const QString &path,
importFileInfo = QFileInfo(docPath + QDir::separator() + path);
info._path = importFileInfo.absoluteFilePath();
if (importFileInfo.isFile())
if (importFileInfo.isFile()) {
info._type = FileImport;
else if (importFileInfo.isDir())
} else if (importFileInfo.isDir()) {
info._type = DirectoryImport;
else
} else if (path.startsWith(QLatin1String("qrc:"))) {
info._path = path;
if (ModelManagerInterface::instance()->filesAtQrcPath(info.path()).isEmpty())
info._type = QrcDirectoryImport;
else
info._type = QrcFileImport;
} else {
info._type = UnknownFileImport;
}
info._version = version;
info._as = as;
info._ast = ast;
......@@ -2192,7 +2199,7 @@ const Value *TypeScope::lookupMember(const QString &name, const Context *context
const ImportInfo &info = i.info;
// JS import has no types
if (info.type() == ImportInfo::FileImport)
if (info.type() == ImportInfo::FileImport || info.type() == ImportInfo::QrcFileImport)
continue;
if (!info.as().isEmpty()) {
......@@ -2222,7 +2229,7 @@ void TypeScope::processMembers(MemberProcessor *processor) const
const ImportInfo &info = i.info;
// JS import has no types
if (info.type() == ImportInfo::FileImport)
if (info.type() == ImportInfo::FileImport || info.type() == ImportInfo::QrcFileImport)
continue;
if (!info.as().isEmpty())
......@@ -2249,7 +2256,7 @@ const Value *JSImportScope::lookupMember(const QString &name, const Context *,
const ImportInfo &info = i.info;
// JS imports are always: import "somefile.js" as Foo
if (info.type() != ImportInfo::FileImport)
if (info.type() != ImportInfo::FileImport && info.type() != ImportInfo::QrcFileImport)
continue;
if (info.as() == name) {
......@@ -2272,7 +2279,7 @@ void JSImportScope::processMembers(MemberProcessor *processor) const
const ObjectValue *import = i.object;
const ImportInfo &info = i.info;
if (info.type() == ImportInfo::FileImport)
if (info.type() == ImportInfo::FileImport || info.type() == ImportInfo::QrcFileImport)
processor->processProperty(info.as(), import);
}
}
......@@ -2329,7 +2336,7 @@ ImportInfo Imports::info(const QString &name, const Context *context) const
continue;
}
if (info.type() == ImportInfo::FileImport) {
if (info.type() == ImportInfo::FileImport || info.type() == ImportInfo::QrcFileImport) {
if (import->className() == firstId)
return info;
} else {
......@@ -2349,7 +2356,7 @@ QString Imports::nameForImportedObject(const ObjectValue *value, const Context *
const ObjectValue *import = i.object;
const ImportInfo &info = i.info;
if (info.type() == ImportInfo::FileImport) {
if (info.type() == ImportInfo::FileImport || info.type() == ImportInfo::QrcFileImport) {
if (import == value)
return import->className();
} else {
......
......@@ -872,6 +872,8 @@ public:
LibraryImport,
FileImport,
DirectoryImport,
QrcFileImport,
QrcDirectoryImport,
UnknownFileImport // refers a file/directory that wasn't found
};
......
......@@ -34,6 +34,7 @@
#include "qmljsbind.h"
#include "qmljsutils.h"
#include "qmljsmodelmanagerinterface.h"
#include <qmljs/qmljsqrcparser.h>
#include <QDir>
#include <QDebug>
......@@ -245,6 +246,8 @@ void LinkPrivate::populateImportedTypes(Imports *imports, Document::Ptr doc)
switch (info.type()) {
case ImportInfo::FileImport:
case ImportInfo::DirectoryImport:
case ImportInfo::QrcFileImport:
case ImportInfo::QrcDirectoryImport:
import = importFileOrDirectory(doc, info);
break;
case ImportInfo::LibraryImport:
......@@ -285,7 +288,7 @@ Import LinkPrivate::importFileOrDirectory(Document::Ptr doc, const ImportInfo &i
import.object = 0;
import.valid = true;
const QString &path = importInfo.path();
QString path = importInfo.path();
if (importInfo.type() == ImportInfo::DirectoryImport
|| importInfo.type() == ImportInfo::ImplicitDirectoryImport) {
......@@ -304,8 +307,36 @@ Import LinkPrivate::importFileOrDirectory(Document::Ptr doc, const ImportInfo &i
Document::Ptr importedDoc = snapshot.document(path);
if (importedDoc)
import.object = importedDoc->bind()->rootObjectValue();
}
} else if (importInfo.type() == ImportInfo::QrcFileImport) {
QLocale locale;
QStringList filePaths = ModelManagerInterface::instance()
->filesAtQrcPath(path, &locale, 0, ModelManagerInterface::ActiveQrcResources);
if (filePaths.isEmpty()) {
filePaths = ModelManagerInterface::instance()->filesAtQrcPath(path);
}
if (!filePaths.isEmpty()) {
Document::Ptr importedDoc = snapshot.document(filePaths.at(0));
if (importedDoc)
import.object = importedDoc->bind()->rootObjectValue();
}
} else if (importInfo.type() == ImportInfo::QrcDirectoryImport){
import.object = new ObjectValue(valueOwner);
importLibrary(doc, path, &import);
QMapIterator<QString,QStringList> iter(ModelManagerInterface::instance()
->filesInQrcPath(path));
while (iter.hasNext()) {
iter.next();
if (Document::isQmlLikeLanguage(Document::guessLanguageFromSuffix(iter.key()))) {
Document::Ptr importedDoc = snapshot.document(iter.value().at(0));
if (importedDoc && importedDoc->bind()->rootObjectValue()) {
const QString targetName = QFileInfo(iter.key()).baseName();
import.object->setMember(targetName, importedDoc->bind()->rootObjectValue());
}
}
}
}
return import;
}
......
......@@ -53,6 +53,11 @@ class QMLJS_EXPORT ModelManagerInterface: public QObject
Q_OBJECT
public:
enum QrcResourceSelector {
ActiveQrcResources,
AllQrcResources
};
class ProjectInfo
{
public:
......@@ -80,6 +85,8 @@ public:
QPointer<ProjectExplorer::Project> project;
QStringList sourceFiles;
QStringList importPaths;
QStringList activeResourceFiles;
QStringList allResourceFiles;
// whether trying to run qmldump makes sense
bool tryQmlDump;
......@@ -142,6 +149,14 @@ public:
bool emitDocumentOnDiskChanged) = 0;
virtual void fileChangedOnDisk(const QString &path) = 0;
virtual void removeFiles(const QStringList &files) = 0;
virtual QStringList filesAtQrcPath(const QString &path, const QLocale *locale = 0,
ProjectExplorer::Project *project = 0,
QrcResourceSelector resources = AllQrcResources) = 0;
virtual QMap<QString,QStringList> filesInQrcPath(const QString &path,
const QLocale *locale = 0,
ProjectExplorer::Project *project = 0,
bool addDirs = false,
QrcResourceSelector resources = AllQrcResources) = 0;
virtual QList<ProjectInfo> projectInfos() const = 0;
virtual ProjectInfo projectInfo(ProjectExplorer::Project *project) const = 0;
......
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** 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.
**
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "qmljsqrcparser.h"
#include <QFile>
#include <QDir>
#include <QFileInfo>
#include <QStringList>
#include <QDomDocument>
#include <QLocale>
#include <QMutex>
#include <QSet>
#include <QMutexLocker>
#include <QMultiHash>
#include <QCoreApplication>
#include <utils/qtcassert.h>
#include <QDebug>
namespace QmlJS {
namespace Internal {
/*!
* \class QrcParser
* \brief Parses one or more qrc files, and keeps their content cached
*
* A Qrc resource contains files read from the filesystem but organized in a possibly different way.
*
* To easily describe that with a simple structure we use a map from qrc paths to the paths in the
* filesystem.
* By using a map we can easily find all qrc paths that start with a given prefix, and thus loop
* on a qrc directory.
*
* Qrc files also support languages, those are mapped to a prefix of the qrc path.
* For example the french /image/bla.png (lang=fr) will have the path "fr/image/bla.png".
* The empty language represent the default resource.
* Languages are looked up using the locale uiLanguages() property
*
* For a single qrc a given path maps to a single file, but when one has multiple
* (platform specific exclusive) qrc files, then multiple files match, so QStringList are used.
*
* Especially the collect* methods are thought as low level interface.
*/
class QrcParserPrivate
{
Q_DECLARE_TR_FUNCTIONS(QmlJS::QrcParser)
public:
typedef QMap<QString,QStringList> SMap;
QrcParserPrivate(QrcParser *q);
bool parseFile(const QString &path);
QString firstFileAtPath(const QString &path, const QLocale &locale) const;
void collectFilesAtPath(const QString &path, QStringList *res, const QLocale *locale = 0) const;
bool hasDirAtPath(const QString &path, const QLocale *locale = 0) const;
void collectFilesInPath(const QString &path, QMap<QString,QStringList> *res, bool addDirs = false,
const QLocale *locale = 0) const;
QStringList errorMessages() const;
QStringList languages() const;
private:
static QString fixPrefix(const QString &prefix);
QStringList allUiLanguages(const QLocale *locale) const;
SMap m_resources;
QStringList m_languages;
QStringList m_errorMessages;
};
class QrcCachePrivate
{
Q_DECLARE_TR_FUNCTIONS(QmlJS::QrcCachePrivate)
public:
QrcCachePrivate(QrcCache *q);
QrcParser::Ptr addPath(const QString &path);
void removePath(const QString &path);
QrcParser::Ptr updatePath(const QString &path);
QrcParser::Ptr parsedPath(const QString &path);
void clear();
private:
QHash<QString, QPair<QrcParser::Ptr,int> > m_cache;
QMutex m_mutex;
};
} // namespace Internal
/*! \brief normalizes the path to a file in a qrc resource by dropping the "qrc:/" or ":" and
* any extra slashes at the beginning
*/
QString QrcParser::normalizedQrcFilePath(const QString &path) {
QString normPath = path;
int endPrefix = 0;
if (path.startsWith(QLatin1String("qrc:/"))) {
endPrefix = 4;
} else if (path.startsWith(QLatin1String(":/"))) {
endPrefix = 1;
}
if (endPrefix < path.size() && path.at(endPrefix) == QLatin1Char('/'))
while (endPrefix + 1 < path.size() && path.at(endPrefix+1) == QLatin1Char('/'))
++endPrefix;
normPath = path.right(path.size()-endPrefix);
if (!normPath.startsWith(QLatin1String("/")))
normPath.insert(0, QLatin1Char('/'));
return normPath;
}
/*! \brief normalizes the path to a directory in a qrc resource by dropping the "qrc:/" or ":" and
* any extra slashes at the beginning, and ensuring it ends with a slash
*/
QString QrcParser::normalizedQrcDirectoryPath(const QString &path) {
QString normPath = normalizedQrcFilePath(path);
if (!normPath.endsWith(QLatin1Char('/')))
normPath.append(QLatin1Char('/'));
return normPath;
}
QrcParser::QrcParser()
{
d = new Internal::QrcParserPrivate(this);
}
QrcParser::~QrcParser()
{
delete d;
}
bool QrcParser::parseFile(const QString &path)
{
return d->parseFile(path);
}
/*! \brief returns fs path of the first (active) file at the given qrc path
*/
QString QrcParser::firstFileAtPath(const QString &path, const QLocale &locale) const
{
return d->firstFileAtPath(path, locale);
}
/*! \brief adds al the fs paths for the given qrc path to *res
* If locale is null all possible files are added, otherwise just the first match
* using that locale.
*/
void QrcParser::collectFilesAtPath(const QString &path, QStringList *res, const QLocale *locale) const
{
d->collectFilesAtPath(path, res, locale);
}
/*! \brief returns true if the given path is a non empty directory
*/
bool QrcParser::hasDirAtPath(const QString &path, const QLocale *locale) const
{
return d->hasDirAtPath(path, locale);
}
/*! \brief adds the directory contents of the given qrc path to res
*
* adds the qrcFileName => fs paths associations contained in the given qrc path
* to res. If addDirs is true directories are also added.
* If locale is null all possible files are added, otherwise just the first match
* using that locale.
*/
void QrcParser::collectFilesInPath(const QString &path, QMap<QString,QStringList> *res, bool addDirs,
const QLocale *locale) const
{
d->collectFilesInPath(path, res, addDirs, locale);
}
/*! \brief returns the errors found while parsing
*/
QStringList QrcParser::errorMessages() const
{
return d->errorMessages();
}
/*! \brief returns all languages used in this qrc resource
*/
QStringList QrcParser::languages() const
{
return d->languages();
}
/*! \brief if the contents are valid
*/
bool QrcParser::isValid() const
{
return errorMessages().isEmpty();
}
QrcParser::Ptr QrcParser::parseQrcFile(const QString &path)
{
Ptr res(new QrcParser);
if (!path.isEmpty())
res->parseFile(path);
return res;
}
// ----------------
QrcCache::QrcCache()
{
d = new Internal::QrcCachePrivate(this);
}
QrcCache::~QrcCache()
{
delete d;
}
QrcParser::ConstPtr QrcCache::addPath(const QString &path)
{
return d->addPath(path);
}
void QrcCache::removePath(const QString &path)
{
d->removePath(path);
}
QrcParser::ConstPtr QrcCache::updatePath(const QString &path)
{
return d->updatePath(path);
}
QrcParser::ConstPtr QrcCache::parsedPath(const QString &path)
{
return d->parsedPath(path);
}
void QrcCache::clear()
{
d->clear();
}
// --------------------
namespace Internal {
QrcParserPrivate::QrcParserPrivate(QrcParser *)
{ }
bool QrcParserPrivate::parseFile(const QString &path)
{
QFile file(path);
if (!file.open(QIODevice::ReadOnly)) {
m_errorMessages.append(file.errorString());
return false;
}
QDomDocument doc;
QString error_msg;
int error_line, error_col;
if (!doc.setContent(&file, &error_msg, &error_line, &error_col)) {
m_errorMessages.append(tr("XML error on line %1, col %2: %3")
.arg(error_line).arg(error_col).arg(error_msg));
return false;
}
QDomElement root = doc.firstChildElement(QLatin1String("RCC"));
if (root.isNull()) {
m_errorMessages.append(tr("The <RCC> root element is missing."));
return false;
}
QDomElement relt = root.firstChildElement(QLatin1String("qresource"));
for (; !relt.isNull(); relt = relt.nextSiblingElement(QLatin1String("qresource"))) {
QString prefix = fixPrefix(relt.attribute(QLatin1String("prefix")));
const QString language = relt.attribute(QLatin1String("lang"));
if (!m_languages.contains(language))
m_languages.append(language);
QDomElement felt = relt.firstChildElement(QLatin1String("file"));
for (; !felt.isNull(); felt = felt.nextSiblingElement(QLatin1String("file"))) {
const QString fileName = felt.text();
QTC_CHECK(!QDir::isAbsolutePath(fileName));
const QString alias = felt.attribute(QLatin1String("alias"));
QString filePath = QFileInfo(path).path() + QLatin1Char('/') + fileName;
QString accessPath;
if (!alias.isEmpty())
accessPath = language + prefix + alias;
else
accessPath = language + prefix + fileName;
if (m_resources.contains(accessPath)) {
QStringList &val = m_resources[accessPath];
if (!val.contains(filePath))
val.append(filePath);
} else {
m_resources.insert(accessPath, QStringList(filePath));
}
}
}
return true;
}
// path is assumed to be a normalized absolute path
QString QrcParserPrivate::firstFileAtPath(const QString &path, const QLocale &locale) const
{
QTC_CHECK(path.startsWith(QLatin1Char('/')));
QStringList langs = allUiLanguages(&locale);
foreach (const QString &language, langs) {
if (m_languages.contains(language)) {
SMap::const_iterator res = m_resources.find(language + path);
if (res != m_resources.end())
return res.value().at(0);
}
}
return QString();
}
void QrcParserPrivate::collectFilesAtPath(const QString &path, QStringList *files,