forked from qt-creator/qt-creator
QmlJS: Clean up ViewerContext
The special ctor wasn't used anywhere, the default ctor can be replaced by inline initialization, maybeAddPath is only meaningful in the model manager and the language compatibility check should definitely not pretend to be a general rule either. Change-Id: I11cf25fe1c696550d33b56ce57316100321d564b Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
@@ -70,7 +70,6 @@ SOURCES += \
|
|||||||
$$PWD/qmljssimplereader.cpp \
|
$$PWD/qmljssimplereader.cpp \
|
||||||
$$PWD/persistenttrie.cpp \
|
$$PWD/persistenttrie.cpp \
|
||||||
$$PWD/qmljsimportdependencies.cpp \
|
$$PWD/qmljsimportdependencies.cpp \
|
||||||
$$PWD/qmljsviewercontext.cpp \
|
|
||||||
$$PWD/qmljsdialect.cpp
|
$$PWD/qmljsdialect.cpp
|
||||||
|
|
||||||
contains(QT, gui) {
|
contains(QT, gui) {
|
||||||
|
|||||||
@@ -40,6 +40,34 @@ static Q_LOGGING_CATEGORY(importsLog, "qtc.qmljs.imports", QtWarningMsg)
|
|||||||
|
|
||||||
namespace QmlJS {
|
namespace QmlJS {
|
||||||
|
|
||||||
|
/*
|
||||||
|
which languages might be imported in this context
|
||||||
|
*/
|
||||||
|
static bool languageIsCompatible(Dialect contextLanguage, Dialect importLanguage)
|
||||||
|
{
|
||||||
|
if (importLanguage == Dialect::AnyLanguage && contextLanguage != Dialect::NoLanguage)
|
||||||
|
return true;
|
||||||
|
switch (contextLanguage.dialect()) {
|
||||||
|
case Dialect::JavaScript:
|
||||||
|
case Dialect::Json:
|
||||||
|
case Dialect::QmlProject:
|
||||||
|
case Dialect::QmlQbs:
|
||||||
|
case Dialect::QmlTypeInfo:
|
||||||
|
return contextLanguage == importLanguage;
|
||||||
|
case Dialect::Qml:
|
||||||
|
return importLanguage == Dialect::Qml || importLanguage == Dialect::QmlQtQuick2 || importLanguage == Dialect::JavaScript;
|
||||||
|
case Dialect::QmlQtQuick2:
|
||||||
|
case Dialect::QmlQtQuick2Ui:
|
||||||
|
return importLanguage == Dialect::Qml || importLanguage == Dialect::QmlQtQuick2 || importLanguage == Dialect::QmlQtQuick2Ui
|
||||||
|
|| importLanguage == Dialect::JavaScript;
|
||||||
|
case Dialect::AnyLanguage:
|
||||||
|
return true;
|
||||||
|
case Dialect::NoLanguage:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
ImportKind::Enum toImportKind(ImportType::Enum type)
|
ImportKind::Enum toImportKind(ImportType::Enum type)
|
||||||
{
|
{
|
||||||
switch (type) {
|
switch (type) {
|
||||||
@@ -587,7 +615,7 @@ void ImportDependencies::filter(const ViewerContext &vContext)
|
|||||||
bool hasChanges = false;
|
bool hasChanges = false;
|
||||||
for (auto j = m_coreImports.cbegin(), end = m_coreImports.cend(); j != end; ++j) {
|
for (auto j = m_coreImports.cbegin(), end = m_coreImports.cend(); j != end; ++j) {
|
||||||
const CoreImport &cImport = j.value();
|
const CoreImport &cImport = j.value();
|
||||||
if (vContext.languageIsCompatible(cImport.language)) {
|
if (languageIsCompatible(vContext.language, cImport.language)) {
|
||||||
QList<Export> newExports;
|
QList<Export> newExports;
|
||||||
foreach (const Export &e, cImport.possibleExports) {
|
foreach (const Export &e, cImport.possibleExports) {
|
||||||
if (e.visibleInVContext(vContext)) {
|
if (e.visibleInVContext(vContext)) {
|
||||||
@@ -637,7 +665,7 @@ void ImportDependencies::iterateOnCandidateImports(
|
|||||||
const QStringList imp = m_importCache.value(key.flatKey());
|
const QStringList imp = m_importCache.value(key.flatKey());
|
||||||
foreach (const QString &cImportName, imp) {
|
foreach (const QString &cImportName, imp) {
|
||||||
CoreImport cImport = coreImport(cImportName);
|
CoreImport cImport = coreImport(cImportName);
|
||||||
if (vContext.languageIsCompatible(cImport.language)) {
|
if (languageIsCompatible(vContext.language, cImport.language)) {
|
||||||
foreach (const Export e, cImport.possibleExports) {
|
foreach (const Export e, cImport.possibleExports) {
|
||||||
if (e.visibleInVContext(vContext)) {
|
if (e.visibleInVContext(vContext)) {
|
||||||
ImportMatchStrength m = e.exportName.matchImport(key, vContext);
|
ImportMatchStrength m = e.exportName.matchImport(key, vContext);
|
||||||
@@ -659,7 +687,7 @@ void ImportDependencies::iterateOnCandidateImports(
|
|||||||
if (c == ImportKey::SameDir) {
|
if (c == ImportKey::SameDir) {
|
||||||
foreach (const QString &cImportName, lb.value()) {
|
foreach (const QString &cImportName, lb.value()) {
|
||||||
CoreImport cImport = coreImport(cImportName);
|
CoreImport cImport = coreImport(cImportName);
|
||||||
if (vContext.languageIsCompatible(cImport.language)) {
|
if (languageIsCompatible(vContext.language, cImport.language)) {
|
||||||
foreach (const Export e, cImport.possibleExports) {
|
foreach (const Export e, cImport.possibleExports) {
|
||||||
if (e.visibleInVContext(vContext)) {
|
if (e.visibleInVContext(vContext)) {
|
||||||
ImportMatchStrength m = e.exportName.matchImport(key, vContext);
|
ImportMatchStrength m = e.exportName.matchImport(key, vContext);
|
||||||
@@ -835,7 +863,7 @@ void ImportDependencies::iterateOnLibraryImports(
|
|||||||
qCDebug(importsLog) << "libloop:" << i.key().toString() << i.value();
|
qCDebug(importsLog) << "libloop:" << i.key().toString() << i.value();
|
||||||
foreach (const QString &cImportName, i.value()) {
|
foreach (const QString &cImportName, i.value()) {
|
||||||
CoreImport cImport = coreImport(cImportName);
|
CoreImport cImport = coreImport(cImportName);
|
||||||
if (vContext.languageIsCompatible(cImport.language)) {
|
if (languageIsCompatible(vContext.language, cImport.language)) {
|
||||||
foreach (const Export &e, cImport.possibleExports) {
|
foreach (const Export &e, cImport.possibleExports) {
|
||||||
if (e.visibleInVContext(vContext) && e.exportName.type == ImportType::Library) {
|
if (e.visibleInVContext(vContext) && e.exportName.type == ImportType::Library) {
|
||||||
ImportMatchStrength m = e.exportName.matchImport(i.key(), vContext);
|
ImportMatchStrength m = e.exportName.matchImport(i.key(), vContext);
|
||||||
@@ -869,7 +897,7 @@ void ImportDependencies::iterateOnSubImports(
|
|||||||
break;
|
break;
|
||||||
foreach (const QString &cImportName, i.value()) {
|
foreach (const QString &cImportName, i.value()) {
|
||||||
CoreImport cImport = coreImport(cImportName);
|
CoreImport cImport = coreImport(cImportName);
|
||||||
if (vContext.languageIsCompatible(cImport.language)) {
|
if (languageIsCompatible(vContext.language, cImport.language)) {
|
||||||
foreach (const Export &e, cImport.possibleExports) {
|
foreach (const Export &e, cImport.possibleExports) {
|
||||||
if (e.visibleInVContext(vContext)) {
|
if (e.visibleInVContext(vContext)) {
|
||||||
ImportMatchStrength m = e.exportName.matchImport(i.key(), vContext);
|
ImportMatchStrength m = e.exportName.matchImport(i.key(), vContext);
|
||||||
|
|||||||
@@ -81,6 +81,12 @@ static ModelManagerInterface *g_instance = nullptr;
|
|||||||
|
|
||||||
const char qtQuickUISuffix[] = "ui.qml";
|
const char qtQuickUISuffix[] = "ui.qml";
|
||||||
|
|
||||||
|
static void maybeAddPath(ViewerContext &context, const QString &path)
|
||||||
|
{
|
||||||
|
if (!path.isEmpty() && !context.paths.contains(path))
|
||||||
|
context.paths.append(path);
|
||||||
|
}
|
||||||
|
|
||||||
static QStringList environmentImportPaths()
|
static QStringList environmentImportPaths()
|
||||||
{
|
{
|
||||||
QStringList paths;
|
QStringList paths;
|
||||||
@@ -1355,17 +1361,17 @@ ViewerContext ModelManagerInterface::completeVContext(const ViewerContext &vCtx,
|
|||||||
case ViewerContext::AddAllPaths:
|
case ViewerContext::AddAllPaths:
|
||||||
{
|
{
|
||||||
foreach (const QString &path, defaultVCtx.paths)
|
foreach (const QString &path, defaultVCtx.paths)
|
||||||
res.maybeAddPath(path);
|
maybeAddPath(res, path);
|
||||||
switch (res.language.dialect()) {
|
switch (res.language.dialect()) {
|
||||||
case Dialect::AnyLanguage:
|
case Dialect::AnyLanguage:
|
||||||
case Dialect::Qml:
|
case Dialect::Qml:
|
||||||
res.maybeAddPath(info.qtQmlPath);
|
maybeAddPath(res, info.qtQmlPath);
|
||||||
Q_FALLTHROUGH();
|
Q_FALLTHROUGH();
|
||||||
case Dialect::QmlQtQuick2:
|
case Dialect::QmlQtQuick2:
|
||||||
case Dialect::QmlQtQuick2Ui:
|
case Dialect::QmlQtQuick2Ui:
|
||||||
{
|
{
|
||||||
if (res.language == Dialect::QmlQtQuick2 || res.language == Dialect::QmlQtQuick2Ui)
|
if (res.language == Dialect::QmlQtQuick2 || res.language == Dialect::QmlQtQuick2Ui)
|
||||||
res.maybeAddPath(info.qtQmlPath);
|
maybeAddPath(res, info.qtQmlPath);
|
||||||
QList<ProjectInfo> allProjects;
|
QList<ProjectInfo> allProjects;
|
||||||
{
|
{
|
||||||
QMutexLocker locker(&m_mutex);
|
QMutexLocker locker(&m_mutex);
|
||||||
@@ -1377,11 +1383,11 @@ ViewerContext ModelManagerInterface::completeVContext(const ViewerContext &vCtx,
|
|||||||
for (int i = 0; i< pInfo.importPaths.size(); ++i) {
|
for (int i = 0; i< pInfo.importPaths.size(); ++i) {
|
||||||
PathAndLanguage pAndL = pInfo.importPaths.at(i);
|
PathAndLanguage pAndL = pInfo.importPaths.at(i);
|
||||||
if (languages.contains(pAndL.language()) || pAndL.language().companionLanguages().contains(res.language))
|
if (languages.contains(pAndL.language()) || pAndL.language().companionLanguages().contains(res.language))
|
||||||
res.maybeAddPath(pAndL.path().toString());
|
maybeAddPath(res, pAndL.path().toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
foreach (const QString &path, environmentImportPaths())
|
foreach (const QString &path, environmentImportPaths())
|
||||||
res.maybeAddPath(path);
|
maybeAddPath(res, path);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Dialect::NoLanguage:
|
case Dialect::NoLanguage:
|
||||||
@@ -1399,13 +1405,13 @@ ViewerContext ModelManagerInterface::completeVContext(const ViewerContext &vCtx,
|
|||||||
Q_FALLTHROUGH();
|
Q_FALLTHROUGH();
|
||||||
case ViewerContext::AddDefaultPaths:
|
case ViewerContext::AddDefaultPaths:
|
||||||
foreach (const QString &path, defaultVCtx.paths)
|
foreach (const QString &path, defaultVCtx.paths)
|
||||||
res.maybeAddPath(path);
|
maybeAddPath(res, path);
|
||||||
if (res.language == Dialect::AnyLanguage || res.language == Dialect::Qml)
|
if (res.language == Dialect::AnyLanguage || res.language == Dialect::Qml)
|
||||||
res.maybeAddPath(info.qtQmlPath);
|
maybeAddPath(res, info.qtQmlPath);
|
||||||
if (res.language == Dialect::AnyLanguage || res.language == Dialect::Qml
|
if (res.language == Dialect::AnyLanguage || res.language == Dialect::Qml
|
||||||
|| res.language == Dialect::QmlQtQuick2 || res.language == Dialect::QmlQtQuick2Ui) {
|
|| res.language == Dialect::QmlQtQuick2 || res.language == Dialect::QmlQtQuick2Ui) {
|
||||||
foreach (const QString &path, environmentImportPaths())
|
foreach (const QString &path, environmentImportPaths())
|
||||||
res.maybeAddPath(path);
|
maybeAddPath(res, path);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,84 +0,0 @@
|
|||||||
/****************************************************************************
|
|
||||||
**
|
|
||||||
** Copyright (C) 2016 The Qt Company Ltd.
|
|
||||||
** Contact: https://www.qt.io/licensing/
|
|
||||||
**
|
|
||||||
** 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 The Qt Company. For licensing terms
|
|
||||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
||||||
** information use the contact form at https://www.qt.io/contact-us.
|
|
||||||
**
|
|
||||||
** GNU General Public License Usage
|
|
||||||
** Alternatively, this file may be used under the terms of the GNU
|
|
||||||
** General Public License version 3 as published by the Free Software
|
|
||||||
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
||||||
** included in the packaging of this file. Please review the following
|
|
||||||
** information to ensure the GNU General Public License requirements will
|
|
||||||
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
|
||||||
**
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
#include "qmljsviewercontext.h"
|
|
||||||
|
|
||||||
namespace QmlJS {
|
|
||||||
/*!
|
|
||||||
\class QmlJS::ViewerContext
|
|
||||||
\brief The ViewerContext class encapsulate selector and paths for a given viewer.
|
|
||||||
|
|
||||||
Using a a different viewer context can emulate (the pure qml part) of a device.
|
|
||||||
This allows checking how a given qml would be interpreted on another platform/viewer.
|
|
||||||
|
|
||||||
Screen information will also most likely need to be added here.
|
|
||||||
*/
|
|
||||||
ViewerContext::ViewerContext()
|
|
||||||
: language(Dialect::Qml), flags(AddAllPaths)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
ViewerContext::ViewerContext(const QStringList &selectors, const QStringList &paths,
|
|
||||||
QmlJS::Dialect language,
|
|
||||||
QmlJS::ViewerContext::Flags flags)
|
|
||||||
: selectors(selectors), paths(paths), language(language),
|
|
||||||
flags(flags)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
which languages might be imported in this context
|
|
||||||
*/
|
|
||||||
bool ViewerContext::languageIsCompatible(Dialect l) const
|
|
||||||
{
|
|
||||||
if (l == Dialect::AnyLanguage && language != Dialect::NoLanguage)
|
|
||||||
return true;
|
|
||||||
switch (language.dialect()) {
|
|
||||||
case Dialect::JavaScript:
|
|
||||||
case Dialect::Json:
|
|
||||||
case Dialect::QmlProject:
|
|
||||||
case Dialect::QmlQbs:
|
|
||||||
case Dialect::QmlTypeInfo:
|
|
||||||
return language == l;
|
|
||||||
case Dialect::Qml:
|
|
||||||
return l == Dialect::Qml || l == Dialect::QmlQtQuick2 || l == Dialect::JavaScript;
|
|
||||||
case Dialect::QmlQtQuick2:
|
|
||||||
case Dialect::QmlQtQuick2Ui:
|
|
||||||
return l == Dialect::Qml || l == Dialect::QmlQtQuick2 || l == Dialect::QmlQtQuick2Ui
|
|
||||||
|| l == Dialect::JavaScript;
|
|
||||||
case Dialect::AnyLanguage:
|
|
||||||
return true;
|
|
||||||
case Dialect::NoLanguage:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ViewerContext::maybeAddPath(const QString &path)
|
|
||||||
{
|
|
||||||
if (!path.isEmpty() && !paths.contains(path))
|
|
||||||
paths.append(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace QmlJS
|
|
||||||
@@ -32,9 +32,8 @@
|
|||||||
|
|
||||||
namespace QmlJS {
|
namespace QmlJS {
|
||||||
|
|
||||||
class QMLJS_EXPORT ViewerContext
|
struct QMLJS_EXPORT ViewerContext
|
||||||
{
|
{
|
||||||
public:
|
|
||||||
enum Flags {
|
enum Flags {
|
||||||
Complete,
|
Complete,
|
||||||
AddAllPathsAndDefaultSelectors,
|
AddAllPathsAndDefaultSelectors,
|
||||||
@@ -43,18 +42,10 @@ public:
|
|||||||
AddDefaultPathsAndSelectors
|
AddDefaultPathsAndSelectors
|
||||||
};
|
};
|
||||||
|
|
||||||
ViewerContext();
|
|
||||||
ViewerContext(const QStringList &selectors, const QStringList &paths,
|
|
||||||
Dialect language = Dialect::Qml,
|
|
||||||
Flags flags = AddAllPaths);
|
|
||||||
|
|
||||||
bool languageIsCompatible(Dialect l) const;
|
|
||||||
void maybeAddPath(const QString &path);
|
|
||||||
|
|
||||||
QStringList selectors;
|
QStringList selectors;
|
||||||
QStringList paths;
|
QStringList paths;
|
||||||
Dialect language;
|
Dialect language = Dialect::Qml;
|
||||||
Flags flags;
|
Flags flags = AddAllPaths;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QmlJS
|
} // namespace QmlJS
|
||||||
|
|||||||
@@ -224,7 +224,7 @@ void ModelManager::delayedInitialization()
|
|||||||
|
|
||||||
ViewerContext qbsVContext;
|
ViewerContext qbsVContext;
|
||||||
qbsVContext.language = Dialect::QmlQbs;
|
qbsVContext.language = Dialect::QmlQbs;
|
||||||
qbsVContext.maybeAddPath(ICore::resourcePath() + QLatin1String("/qbs"));
|
qbsVContext.paths.append(ICore::resourcePath() + QLatin1String("/qbs"));
|
||||||
setDefaultVContext(qbsVContext);
|
setDefaultVContext(qbsVContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user