forked from qt-creator/qt-creator
Qml code model: handle modules dependencies.
Fix the broken hierarchies between qml objects. Change-Id: Id36c735cab0129af382fab7b9dd90c228304168c Reviewed-by: Erik Verbruggen <erik.verbruggen@theqtcompany.com> Reviewed-by: Alessandro Portale <alessandro.portale@theqtcompany.com>
This commit is contained in:
@@ -519,6 +519,7 @@ void Snapshot::insertLibraryInfo(const QString &path, const LibraryInfo &info)
|
||||
foreach (const Export &e, cImport.possibleExports)
|
||||
_dependencies.addExport(component.fileName, e.exportName, e.pathRequired, e.typeName);
|
||||
}
|
||||
|
||||
cImport.fingerprint = info.fingerprint();
|
||||
_dependencies.addCoreImport(cImport);
|
||||
}
|
||||
|
||||
@@ -154,6 +154,7 @@ private:
|
||||
typedef QList<LanguageUtils::FakeMetaObject::ConstPtr> FakeMetaObjectList;
|
||||
FakeMetaObjectList _metaObjects;
|
||||
QList<ModuleApiInfo> _moduleApis;
|
||||
QStringList _dependencies;
|
||||
QByteArray _fingerprint;
|
||||
|
||||
PluginTypeInfoStatus _dumpStatus;
|
||||
@@ -190,6 +191,12 @@ public:
|
||||
void setModuleApis(const QList<ModuleApiInfo> &apis)
|
||||
{ _moduleApis = apis; }
|
||||
|
||||
QStringList dependencies() const
|
||||
{ return _dependencies; }
|
||||
|
||||
void setDependencies(const QStringList &deps)
|
||||
{ _dependencies = deps; }
|
||||
|
||||
bool isValid() const
|
||||
{ return _status == Found; }
|
||||
|
||||
|
||||
@@ -1117,11 +1117,13 @@ const Value *ObjectValue::lookupMember(const QString &name, const Context *conte
|
||||
}
|
||||
}
|
||||
|
||||
const ObjectValue *prototypeObject = 0;
|
||||
|
||||
if (examinePrototypes && context) {
|
||||
PrototypeIterator iter(this, context);
|
||||
iter.next(); // skip this
|
||||
while (iter.hasNext()) {
|
||||
const ObjectValue *prototypeObject = iter.next();
|
||||
prototypeObject = iter.next();
|
||||
if (const Value *m = prototypeObject->lookupMember(name, context, foundInObject, false))
|
||||
return m;
|
||||
}
|
||||
@@ -1129,6 +1131,7 @@ const Value *ObjectValue::lookupMember(const QString &name, const Context *conte
|
||||
|
||||
if (foundInObject)
|
||||
*foundInObject = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1346,6 +1349,7 @@ CppQmlTypesLoader::BuiltinObjects CppQmlTypesLoader::defaultQtObjects;
|
||||
CppQmlTypesLoader::BuiltinObjects CppQmlTypesLoader::loadQmlTypes(const QFileInfoList &qmlTypeFiles, QStringList *errors, QStringList *warnings)
|
||||
{
|
||||
QHash<QString, FakeMetaObject::ConstPtr> newObjects;
|
||||
QStringList newDependencies;
|
||||
|
||||
foreach (const QFileInfo &qmlTypeFile, qmlTypeFiles) {
|
||||
QString error, warning;
|
||||
@@ -1355,7 +1359,8 @@ CppQmlTypesLoader::BuiltinObjects CppQmlTypesLoader::loadQmlTypes(const QFileInf
|
||||
file.close();
|
||||
|
||||
|
||||
parseQmlTypeDescriptions(contents, &newObjects, 0, &error, &warning, qmlTypeFile.absoluteFilePath());
|
||||
parseQmlTypeDescriptions(contents, &newObjects, 0, &newDependencies, &error, &warning,
|
||||
qmlTypeFile.absoluteFilePath());
|
||||
} else {
|
||||
error = file.errorString();
|
||||
}
|
||||
@@ -1377,6 +1382,7 @@ CppQmlTypesLoader::BuiltinObjects CppQmlTypesLoader::loadQmlTypes(const QFileInf
|
||||
void CppQmlTypesLoader::parseQmlTypeDescriptions(const QByteArray &contents,
|
||||
BuiltinObjects *newObjects,
|
||||
QList<ModuleApiInfo> *newModuleApis,
|
||||
QStringList *newDependencies,
|
||||
QString *errorMessage,
|
||||
QString *warningMessage, const QString &fileName)
|
||||
{
|
||||
@@ -1396,7 +1402,7 @@ void CppQmlTypesLoader::parseQmlTypeDescriptions(const QByteArray &contents,
|
||||
errorMessage->clear();
|
||||
warningMessage->clear();
|
||||
TypeDescriptionReader reader(fileName, QString::fromUtf8(contents));
|
||||
if (!reader(newObjects, newModuleApis)) {
|
||||
if (!reader(newObjects, newModuleApis, newDependencies)) {
|
||||
if (reader.errorMessage().isEmpty())
|
||||
*errorMessage = QLatin1String("unknown error");
|
||||
else
|
||||
|
||||
@@ -720,8 +720,12 @@ public:
|
||||
|
||||
// parses the contents of a qmltypes file and fills the newObjects map
|
||||
static void parseQmlTypeDescriptions(const QByteArray &contents,
|
||||
BuiltinObjects *newObjects,
|
||||
QList<ModuleApiInfo> *newModuleApis, QString *errorMessage, QString *warningMessage, const QString &fileName);
|
||||
BuiltinObjects *newObjects,
|
||||
QList<ModuleApiInfo> *newModuleApis,
|
||||
QStringList *newDependencies,
|
||||
QString *errorMessage,
|
||||
QString *warningMessage,
|
||||
const QString &fileName);
|
||||
};
|
||||
|
||||
class QMLJS_EXPORT FakeMetaObjectWithOrigin
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
#include "qmljsmodelmanagerinterface.h"
|
||||
|
||||
#include <qmljs/qmljsinterpreter.h>
|
||||
#include <qmljs/qmljsviewercontext.h>
|
||||
//#include <projectexplorer/session.h>
|
||||
//#include <coreplugin/messagemanager.h>
|
||||
#include <utils/filesystemwatcher.h>
|
||||
@@ -309,7 +310,9 @@ void PluginDumper::qmlPluginTypeDumpDone(int exitCode)
|
||||
QString warning;
|
||||
CppQmlTypesLoader::BuiltinObjects objectsList;
|
||||
QList<ModuleApiInfo> moduleApis;
|
||||
CppQmlTypesLoader::parseQmlTypeDescriptions(output, &objectsList, &moduleApis, &error, &warning,
|
||||
QStringList dependencies;
|
||||
CppQmlTypesLoader::parseQmlTypeDescriptions(output, &objectsList, &moduleApis, &dependencies,
|
||||
&error, &warning,
|
||||
QLatin1String("<dump of ") + libraryPath + QLatin1Char('>'));
|
||||
if (exitCode == 0) {
|
||||
if (!error.isEmpty()) {
|
||||
@@ -361,6 +364,115 @@ void PluginDumper::pluginChanged(const QString &pluginLibrary)
|
||||
dump(plugin);
|
||||
}
|
||||
|
||||
void PluginDumper::loadQmlTypeDescription(const QStringList &paths,
|
||||
QStringList &errors,
|
||||
QStringList &warnings,
|
||||
QList<FakeMetaObject::ConstPtr> &objects,
|
||||
QList<ModuleApiInfo> *moduleApi,
|
||||
QStringList *dependencies) const {
|
||||
for (const QString &p: paths) {
|
||||
Utils::FileReader reader;
|
||||
if (!reader.fetch(p, QFile::Text)) {
|
||||
errors += reader.errorString();
|
||||
continue;
|
||||
}
|
||||
QString error;
|
||||
QString warning;
|
||||
CppQmlTypesLoader::BuiltinObjects objs;
|
||||
QList<ModuleApiInfo> apis;
|
||||
QStringList deps;
|
||||
CppQmlTypesLoader::parseQmlTypeDescriptions(reader.data(), &objs, &apis, &deps,
|
||||
&error, &warning, p);
|
||||
if (!error.isEmpty()) {
|
||||
errors += tr("Failed to parse \"%1\".\nError: %2").arg(p, error);
|
||||
} else {
|
||||
objects += objs.values();
|
||||
if (moduleApi)
|
||||
*moduleApi += apis;
|
||||
if (!deps.isEmpty())
|
||||
*dependencies += deps;
|
||||
}
|
||||
if (!warning.isEmpty())
|
||||
warnings += warning;
|
||||
}
|
||||
}
|
||||
/*!
|
||||
* \brief Build the path of an existing qmltypes file from a module name.
|
||||
* \param name
|
||||
* \return the module's qmltypes file path
|
||||
*
|
||||
* Look for \a name qmltypes file in model manager's import paths.
|
||||
* For each import path the following files are searched, in this order:
|
||||
*
|
||||
* - <name>.<major>.<minor>/plugins.qmltypes
|
||||
* - <name>.<major>/plugins.qmltypes
|
||||
* - <name>/plugins.qmltypes
|
||||
*
|
||||
* That means that a more qualified directory name has precedence over a
|
||||
* less qualified one. Be aware that the import paths order has a stronger
|
||||
* precedence, so a less qualified name could shadow a more qualified one if
|
||||
* it resides in a different import path.
|
||||
*
|
||||
* \sa LinkPrivate::importNonFile
|
||||
*/
|
||||
QString PluginDumper::buildQmltypesPath(const QString &name) const
|
||||
{
|
||||
QStringList importName = name.split(QLatin1Char(' '));
|
||||
QString qualifiedName = importName[0];
|
||||
QString majorVersion;
|
||||
QString minorVersion;
|
||||
if (importName.length() == 2) {
|
||||
QString versionString = importName[1];
|
||||
QStringList version = versionString.split(QLatin1Char('.'));
|
||||
if (version.length() == 2) {
|
||||
majorVersion = version[0];
|
||||
minorVersion = version[1];
|
||||
}
|
||||
}
|
||||
|
||||
for (const PathAndLanguage &p: m_modelManager->importPaths()) {
|
||||
QString moduleName(qualifiedName.replace(QLatin1Char('.'), QLatin1Char('/')));
|
||||
QString moduleNameMajor(moduleName + QLatin1Char('.') + majorVersion);
|
||||
QString moduleNameMajorMinor(moduleNameMajor + QLatin1Char('.') + minorVersion);
|
||||
|
||||
for (const auto n: QStringList{moduleNameMajorMinor, moduleNameMajor, moduleName}) {
|
||||
QString filename(p.path().toString() + QLatin1Char('/') + n
|
||||
+ QLatin1String("/plugins.qmltypes"));
|
||||
if (QFile::exists(filename))
|
||||
return filename;
|
||||
}
|
||||
}
|
||||
return QString();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Recursively load dependencies.
|
||||
* \param dependencies
|
||||
* \param errors
|
||||
* \param warnings
|
||||
* \param objects
|
||||
*
|
||||
* Recursively load type descriptions of dependencies, collecting results
|
||||
* in \a objects.
|
||||
*/
|
||||
void PluginDumper::loadDependencies(const QStringList &dependencies,
|
||||
QStringList &errors,
|
||||
QStringList &warnings,
|
||||
QList<FakeMetaObject::ConstPtr> &objects) const
|
||||
{
|
||||
QStringList dependenciesPaths;
|
||||
QString path;
|
||||
for (const QString &name: dependencies) {
|
||||
path = buildQmltypesPath(name);
|
||||
if (!path.isNull())
|
||||
dependenciesPaths << path;
|
||||
}
|
||||
QStringList newDependencies;
|
||||
loadQmlTypeDescription(dependenciesPaths, errors, warnings, objects, 0, &newDependencies);
|
||||
if (!newDependencies.isEmpty())
|
||||
loadDependencies(newDependencies, errors, warnings, objects);
|
||||
}
|
||||
|
||||
void PluginDumper::loadQmltypesFile(const QStringList &qmltypesFilePaths,
|
||||
const QString &libraryPath,
|
||||
QmlJS::LibraryInfo libraryInfo)
|
||||
@@ -369,31 +481,14 @@ void PluginDumper::loadQmltypesFile(const QStringList &qmltypesFilePaths,
|
||||
QStringList warnings;
|
||||
QList<FakeMetaObject::ConstPtr> objects;
|
||||
QList<ModuleApiInfo> moduleApis;
|
||||
QStringList dependencies;
|
||||
|
||||
foreach (const QString &qmltypesFilePath, qmltypesFilePaths) {
|
||||
Utils::FileReader reader;
|
||||
if (!reader.fetch(qmltypesFilePath, QFile::Text)) {
|
||||
errors += reader.errorString();
|
||||
continue;
|
||||
}
|
||||
|
||||
QString error;
|
||||
QString warning;
|
||||
CppQmlTypesLoader::BuiltinObjects newObjects;
|
||||
QList<ModuleApiInfo> newModuleApis;
|
||||
CppQmlTypesLoader::parseQmlTypeDescriptions(reader.data(), &newObjects, &newModuleApis, &error, &warning, qmltypesFilePath);
|
||||
if (!error.isEmpty()) {
|
||||
errors += tr("Failed to parse \"%1\".\nError: %2").arg(qmltypesFilePath, error);
|
||||
} else {
|
||||
objects += newObjects.values();
|
||||
moduleApis += newModuleApis;
|
||||
}
|
||||
if (!warning.isEmpty())
|
||||
warnings += warning;
|
||||
}
|
||||
loadQmlTypeDescription(qmltypesFilePaths, errors, warnings, objects, &moduleApis, &dependencies);
|
||||
loadDependencies(dependencies, errors, warnings, objects);
|
||||
|
||||
libraryInfo.setMetaObjects(objects);
|
||||
libraryInfo.setModuleApis(moduleApis);
|
||||
libraryInfo.setDependencies(dependencies);
|
||||
if (errors.isEmpty()) {
|
||||
libraryInfo.setPluginTypeInfoStatus(LibraryInfo::TypeInfoFileDone);
|
||||
} else {
|
||||
|
||||
@@ -75,6 +75,15 @@ private:
|
||||
|
||||
void runQmlDump(const QmlJS::ModelManagerInterface::ProjectInfo &info, const QStringList &arguments, const QString &importPath);
|
||||
void dump(const Plugin &plugin);
|
||||
void loadQmlTypeDescription(const QStringList &path, QStringList &errors, QStringList &warnings,
|
||||
QList<LanguageUtils::FakeMetaObject::ConstPtr> &objects,
|
||||
QList<ModuleApiInfo> *moduleApi,
|
||||
QStringList *dependencies) const;
|
||||
QString buildQmltypesPath(const QString &name) const;
|
||||
void loadDependencies(const QStringList &dependencies,
|
||||
QStringList &errors,
|
||||
QStringList &warnings,
|
||||
QList<LanguageUtils::FakeMetaObject::ConstPtr> &objects) const;
|
||||
void loadQmltypesFile(const QStringList &qmltypesFilePaths,
|
||||
const QString &libraryPath,
|
||||
QmlJS::LibraryInfo libraryInfo);
|
||||
|
||||
@@ -52,7 +52,8 @@ TypeDescriptionReader::~TypeDescriptionReader()
|
||||
|
||||
bool TypeDescriptionReader::operator()(
|
||||
QHash<QString, FakeMetaObject::ConstPtr> *objects,
|
||||
QList<ModuleApiInfo> *moduleApis)
|
||||
QList<ModuleApiInfo> *moduleApis,
|
||||
QStringList *dependencies)
|
||||
{
|
||||
Engine engine;
|
||||
|
||||
@@ -71,6 +72,7 @@ bool TypeDescriptionReader::operator()(
|
||||
|
||||
_objects = objects;
|
||||
_moduleApis = moduleApis;
|
||||
_dependencies = dependencies;
|
||||
readDocument(parser.ast());
|
||||
|
||||
return _errorMessage.isEmpty();
|
||||
@@ -141,6 +143,12 @@ void TypeDescriptionReader::readModule(UiObjectDefinition *ast)
|
||||
UiObjectMember *member = it->member;
|
||||
UiObjectDefinition *component = dynamic_cast<UiObjectDefinition *>(member);
|
||||
|
||||
UiScriptBinding *script = dynamic_cast<UiScriptBinding *>(member);
|
||||
if (script && (toString(script->qualifiedId) == QStringLiteral("dependencies"))) {
|
||||
readDependencies(script);
|
||||
continue;
|
||||
}
|
||||
|
||||
QString typeName;
|
||||
if (component)
|
||||
typeName = toString(component->qualifiedTypeNameId);
|
||||
@@ -174,6 +182,27 @@ void TypeDescriptionReader::addWarning(const SourceLocation &loc, const QString
|
||||
message);
|
||||
}
|
||||
|
||||
void TypeDescriptionReader::readDependencies(UiScriptBinding *ast) {
|
||||
ExpressionStatement *stmt = dynamic_cast<ExpressionStatement*>(ast->statement);
|
||||
if (!stmt) {
|
||||
addError(ast->statement->firstSourceLocation(), tr("Expected dependency definitions"));
|
||||
return;
|
||||
}
|
||||
ArrayLiteral *exp = dynamic_cast<ArrayLiteral *>(stmt->expression);
|
||||
if (!exp) {
|
||||
addError(stmt->expression->firstSourceLocation(), tr("Expected dependency definitions"));
|
||||
return;
|
||||
}
|
||||
for (ElementList *l = exp->elements; l; l = l->next) {
|
||||
StringLiteral *str = dynamic_cast<StringLiteral *>(l->expression);
|
||||
if (!exp) {
|
||||
addWarning(l->expression->firstSourceLocation(),
|
||||
tr("Cannot read dependency: skipping."));
|
||||
}
|
||||
*_dependencies << str->value.toString();
|
||||
}
|
||||
}
|
||||
|
||||
void TypeDescriptionReader::readComponent(UiObjectDefinition *ast)
|
||||
{
|
||||
FakeMetaObject::Ptr fmo(new FakeMetaObject);
|
||||
|
||||
@@ -58,13 +58,15 @@ public:
|
||||
|
||||
bool operator()(
|
||||
QHash<QString, LanguageUtils::FakeMetaObject::ConstPtr> *objects,
|
||||
QList<ModuleApiInfo> *moduleApis);
|
||||
QList<ModuleApiInfo> *moduleApis,
|
||||
QStringList *dependencies);
|
||||
QString errorMessage() const;
|
||||
QString warningMessage() const;
|
||||
|
||||
private:
|
||||
void readDocument(AST::UiProgram *ast);
|
||||
void readModule(AST::UiObjectDefinition *ast);
|
||||
void readDependencies(AST::UiScriptBinding *ast);
|
||||
void readComponent(AST::UiObjectDefinition *ast);
|
||||
void readModuleApi(AST::UiObjectDefinition *ast);
|
||||
void readSignalOrMethod(AST::UiObjectDefinition *ast, bool isMethod, LanguageUtils::FakeMetaObject::Ptr fmo);
|
||||
@@ -90,6 +92,7 @@ private:
|
||||
QString _warningMessage;
|
||||
QHash<QString, LanguageUtils::FakeMetaObject::ConstPtr> *_objects;
|
||||
QList<ModuleApiInfo> *_moduleApis;
|
||||
QStringList *_dependencies;
|
||||
};
|
||||
|
||||
} // namespace QmlJS
|
||||
|
||||
Reference in New Issue
Block a user