forked from qt-creator/qt-creator
QmlJS: Fix resolution of "alias" directives in qrc files
Previously qrc paths of QML/JS documents were not considered for implicit imports. Only the path of the document in the file system was considered. The QML engine, however, doesn't know the original path at all and only uses the qrc paths for import resolution. This created a mismatch between what the QML engine could recognize and what the code model suggested. Without alias directives, any files imported from a qrc file would have to reside in the same directory as the one implicitly importing them, so this arrangement happened to work, most of the time. In order to support aliases we have to search the files in the same qrc path to figure out the imports. To do that, we keep a reverse map of qrc paths to files in the QrcParser and iterate that when resolving imports. Change-Id: I84baae6cbfbe96ddd5322c81494c1e4a3f473f3f Reviewed-by: Eike Ziller <eike.ziller@theqtcompany.com> Reviewed-by: Tobias Hunger <tobias.hunger@theqtcompany.com>
This commit is contained in:
@@ -74,6 +74,9 @@ public:
|
||||
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;
|
||||
void collectResourceFilesForSourceFile(const QString &sourceFile, QStringList *res,
|
||||
const QLocale *locale = 0) const;
|
||||
|
||||
QStringList errorMessages() const;
|
||||
QStringList languages() const;
|
||||
private:
|
||||
@@ -81,6 +84,7 @@ private:
|
||||
QStringList allUiLanguages(const QLocale *locale) const;
|
||||
|
||||
SMap m_resources;
|
||||
SMap m_files;
|
||||
QStringList m_languages;
|
||||
QStringList m_errorMessages;
|
||||
};
|
||||
@@ -130,6 +134,11 @@ QString QrcParser::normalizedQrcDirectoryPath(const QString &path) {
|
||||
return normPath;
|
||||
}
|
||||
|
||||
QString QrcParser::qrcDirectoryPathForQrcFilePath(const QString &file)
|
||||
{
|
||||
return file.left(file.lastIndexOf(QLatin1Char('/')));
|
||||
}
|
||||
|
||||
QrcParser::QrcParser()
|
||||
{
|
||||
d = new Internal::QrcParserPrivate(this);
|
||||
@@ -181,6 +190,12 @@ void QrcParser::collectFilesInPath(const QString &path, QMap<QString,QStringList
|
||||
d->collectFilesInPath(path, res, addDirs, locale);
|
||||
}
|
||||
|
||||
void QrcParser::collectResourceFilesForSourceFile(const QString &sourceFile, QStringList *res,
|
||||
const QLocale *locale) const
|
||||
{
|
||||
d->collectResourceFilesForSourceFile(sourceFile, res, locale);
|
||||
}
|
||||
|
||||
/*! \brief returns the errors found while parsing
|
||||
*/
|
||||
QStringList QrcParser::errorMessages() const
|
||||
@@ -297,13 +312,12 @@ bool QrcParserPrivate::parseFile(const QString &path)
|
||||
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));
|
||||
}
|
||||
QStringList &resources = m_resources[accessPath];
|
||||
if (!resources.contains(filePath))
|
||||
resources.append(filePath);
|
||||
QStringList &files = m_files[filePath];
|
||||
if (!files.contains(accessPath))
|
||||
files.append(accessPath);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
@@ -388,6 +402,24 @@ void QrcParserPrivate::collectFilesInPath(const QString &path, QMap<QString,QStr
|
||||
}
|
||||
}
|
||||
|
||||
void QrcParserPrivate::collectResourceFilesForSourceFile(const QString &sourceFile,
|
||||
QStringList *results,
|
||||
const QLocale *locale) const
|
||||
{
|
||||
QTC_CHECK(sourceFile.startsWith(QLatin1Char('/')));
|
||||
QTC_CHECK(!sourceFile.endsWith(QLatin1Char('/')));
|
||||
QStringList langs = allUiLanguages(locale);
|
||||
SMap::const_iterator file = m_files.find(sourceFile);
|
||||
if (file == m_files.end())
|
||||
return;
|
||||
foreach (const QString &resource, file.value()) {
|
||||
foreach (const QString &language, langs) {
|
||||
if (resource.startsWith(language) && !results->contains(resource))
|
||||
results->append(resource);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QStringList QrcParserPrivate::errorMessages() const
|
||||
{
|
||||
return m_errorMessages;
|
||||
|
||||
Reference in New Issue
Block a user