Commit 14659c09 authored by Thomas Hartmann's avatar Thomas Hartmann

QmlDesigner: Get imports from the code model

Change-Id: I96928025aede350bf4ff084555f612568d9389d8
Reviewed-by: default avatarFawzi Mohamed <fawzi.mohamed@digia.com>
parent f10dfe86
......@@ -99,6 +99,8 @@ public:
QList<Import> possibleImports() const;
QList<Import> usedImports() const;
void changeImports(const QList<Import> &importsToBeAdded, const QList<Import> &importsToBeRemoved);
void setPossibleImports(const QList<Import> &possibleImports);
void setUsedImports(const QList<Import> &usedImports);
bool hasImport(const Import &import, bool ignoreAlias = true, bool allowHigherVersion = false);
QString pathForImport(const Import &import);
......
......@@ -1731,25 +1731,12 @@ QList<Import> Model::imports() const
QList<Import> Model::possibleImports() const
{
QList<Import> possibleImportList;
possibleImportList.append(Import::createLibraryImport("QtQuick.Controls", "1.0"));
possibleImportList.append(Import::createLibraryImport("QtQuick.Layouts", "1.0"));
possibleImportList.append(Import::createLibraryImport("QtQuick.Window", "2.0"));
return possibleImportList;
return d->m_possibleImportList;
}
QList<Import> Model::usedImports() const
{
QList<Import> usedImportList;
usedImportList.append(Import::createLibraryImport("QtQuick", "1.0"));
usedImportList.append(Import::createLibraryImport("QtQuick", "1.1"));
usedImportList.append(Import::createLibraryImport("QtQuick", "2.0"));
usedImportList.append(Import::createLibraryImport("QtQuick", "2.1"));
return usedImportList;
return d->m_usedImportList;
}
void Model::changeImports(const QList<Import> &importsToBeAdded, const QList<Import> &importsToBeRemoved)
......@@ -1757,6 +1744,16 @@ void Model::changeImports(const QList<Import> &importsToBeAdded, const QList<Imp
d->changeImports(importsToBeAdded, importsToBeRemoved);
}
void Model::setPossibleImports(const QList<Import> &possibleImports)
{
d->m_possibleImportList = possibleImports;
}
void Model::setUsedImports(const QList<Import> &usedImports)
{
d->m_usedImportList = usedImports;
}
static bool compareVersions(const QString &version1, const QString &version2, bool allowHigherVersion)
{
......
......@@ -236,6 +236,8 @@ private:
Model *m_q;
MetaInfo m_metaInfo;
QList<Import> m_imports;
QList<Import> m_possibleImportList;
QList<Import> m_usedImportList;
QList<QWeakPointer<AbstractView> > m_viewList;
QList<InternalNodePointer> m_selectedInternalNodeList;
QHash<QString,InternalNodePointer> m_idNodeHash;
......
......@@ -48,6 +48,7 @@
#include <qmljs/qmljsutils.h>
#include <qmljs/qmljsmodelmanagerinterface.h>
#include <qmljs/qmljsqrcparser.h>
#include <qmljs/qmljsinterpreter.h>
#include <utils/qtcassert.h>
......@@ -726,6 +727,67 @@ void TextToModelMerger::setupImports(const Document::Ptr &doc,
differenceHandler.importAbsentInQMl(import);
}
void TextToModelMerger::setupPossibleImports(const QmlJS::Snapshot &snapshot, const QmlJS::ViewerContext &viewContext)
{
QList<Import> possibleImports;
QSet<ImportKey> possibleImportKeys = snapshot.importDependencies()->libraryImports(viewContext);
QHash<QString, ImportKey> filteredPossibleImportKeys;
foreach (const ImportKey &importKey, possibleImportKeys) {
if (!filteredPossibleImportKeys.contains(importKey.path())
|| filteredPossibleImportKeys.value(importKey.path()).majorVersion < importKey.majorVersion
|| (filteredPossibleImportKeys.value(importKey.path()).majorVersion == importKey.majorVersion
&& filteredPossibleImportKeys.value(importKey.path()).minorVersion < importKey.minorVersion))
filteredPossibleImportKeys.insert(importKey.path(), importKey);
}
filteredPossibleImportKeys.remove(QLatin1String("<cpp>"));
filteredPossibleImportKeys.remove(QLatin1String("QML"));
filteredPossibleImportKeys.remove(QLatin1String("QtQml"));
filteredPossibleImportKeys.remove(QLatin1String("QtQuick/PrivateWidgets"));
QList<QmlJS::Import> allImports = m_scopeChain->context()->imports(m_document.data())->all();
foreach (const QmlJS::Import &import, allImports) {
filteredPossibleImportKeys.remove(import.info.path());
}
foreach (const ImportKey &importKey, filteredPossibleImportKeys) {
QString libraryName = importKey.splitPath.join(QLatin1Char('.'));
QString version = QString(QStringLiteral("%1.%2").arg(importKey.majorVersion).arg(importKey.minorVersion));
possibleImports.append(Import::createLibraryImport(libraryName, version));
}
if ( m_rewriterView->isAttached())
m_rewriterView->model()->setPossibleImports(possibleImports);
}
void TextToModelMerger::setupUsedImports()
{
QList<QmlJS::Import> allImports = m_scopeChain->context()->imports(m_document.data())->all();
QList<Import> usedImports;
foreach (const QmlJS::Import &import, allImports) {
if (import.used) {
if (import.info.type() == ImportType::Library) {
usedImports.append(Import::createLibraryImport(import.info.name(), import.info.version().toString(), import.info.as()));
} else if (import.info.type() == ImportType::Directory || import.info.type() == ImportType::File) {
usedImports.append(Import::createFileImport(import.info.name(), import.info.version().toString(), import.info.as()));
}
}
}
// even if not explicitly used we probably want to keep QtQuick imports
usedImports.append(Import::createLibraryImport("QtQuick", "1.0"));
usedImports.append(Import::createLibraryImport("QtQuick", "1.1"));
usedImports.append(Import::createLibraryImport("QtQuick", "2.0"));
usedImports.append(Import::createLibraryImport("QtQuick", "2.1"));
if (m_rewriterView->isAttached())
m_rewriterView->model()->setUsedImports(usedImports);
}
bool TextToModelMerger::load(const QString &data, DifferenceHandler &differenceHandler)
{
// qDebug() << "TextToModelMerger::load with data:" << data;
......@@ -768,6 +830,8 @@ bool TextToModelMerger::load(const QString &data, DifferenceHandler &differenceH
}
setupImports(doc, differenceHandler);
setupPossibleImports(snapshot, vContext);
setupUsedImports();
if (m_rewriterView->model()->imports().isEmpty()) {
const QmlJS::DiagnosticMessage diagnosticMessage(QmlJS::Severity::Error, AST::SourceLocation(0, 0, 0, 0), QCoreApplication::translate("QmlDesigner::TextToModelMerger", "No import statements found"));
......
......@@ -59,6 +59,8 @@ public:
bool isActive() const;
void setupImports(const QmlJS::Document::Ptr &doc, DifferenceHandler &differenceHandler);
void setupPossibleImports(const QmlJS::Snapshot &snapshot, const QmlJS::ViewerContext &viewContext);
void setupUsedImports();
bool load(const QString &data, DifferenceHandler &differenceHandler);
RewriterView *view() const
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment