forked from qt-creator/qt-creator
QmlDesigner: Modernize the code a little bit
Change-Id: I24dffa459ad8a948fd58d83249cb07c827a6343f Reviewed-by: Qt CI Patch Build Bot <ci_patchbuild_bot@qt.io> Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
@@ -1410,13 +1410,13 @@ OutputContainer setUnionMerge(InputContainer1 &&input1,
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename Container>
|
template<typename Container>
|
||||||
auto usize(const Container &container)
|
constexpr auto usize(const Container &container)
|
||||||
{
|
{
|
||||||
return static_cast<std::make_unsigned_t<decltype(std::size(container))>>(std::size(container));
|
return static_cast<std::make_unsigned_t<decltype(std::size(container))>>(std::size(container));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Container>
|
template<typename Container>
|
||||||
auto ssize(const Container &container)
|
constexpr auto ssize(const Container &container)
|
||||||
{
|
{
|
||||||
return static_cast<std::make_signed_t<decltype(std::size(container))>>(std::size(container));
|
return static_cast<std::make_signed_t<decltype(std::size(container))>>(std::size(container));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
|
||||||
namespace Utils {
|
namespace Utils {
|
||||||
|
|
||||||
namespace Internal {
|
namespace Internal {
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ public:
|
|||||||
/* */
|
/* */
|
||||||
|
|
||||||
using TypeName = QByteArray;
|
using TypeName = QByteArray;
|
||||||
|
using TypeNameView = QByteArrayView;
|
||||||
using PropertyTypeList = QList<PropertyName>;
|
using PropertyTypeList = QList<PropertyName>;
|
||||||
using IdName = QByteArray;
|
using IdName = QByteArray;
|
||||||
class Model;
|
class Model;
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
#include "bindingproperty.h"
|
#include "bindingproperty.h"
|
||||||
#include "internalnode_p.h"
|
#include "internalnode_p.h"
|
||||||
#include "model_p.h"
|
#include "model_p.h"
|
||||||
|
#include "modelutils.h"
|
||||||
#include "nodeabstractproperty.h"
|
#include "nodeabstractproperty.h"
|
||||||
#include "nodelistproperty.h"
|
#include "nodelistproperty.h"
|
||||||
#include "nodeproperty.h"
|
#include "nodeproperty.h"
|
||||||
@@ -19,12 +20,15 @@
|
|||||||
#include <rewriterview.h>
|
#include <rewriterview.h>
|
||||||
|
|
||||||
#include <utils/algorithm.h>
|
#include <utils/algorithm.h>
|
||||||
|
#include <utils/array.h>
|
||||||
|
|
||||||
#include <QHash>
|
#include <QHash>
|
||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
#include <QSet>
|
#include <QSet>
|
||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
namespace QmlDesigner {
|
namespace QmlDesigner {
|
||||||
using namespace QmlDesigner::Internal;
|
using namespace QmlDesigner::Internal;
|
||||||
|
|
||||||
@@ -84,44 +88,42 @@ QString ModelNode::validId()
|
|||||||
return id();
|
return id();
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool idIsQmlKeyWord(const QString &id)
|
namespace {
|
||||||
|
bool isQmlKeyWord(QStringView id)
|
||||||
{
|
{
|
||||||
static const QSet<QString> keywords = {"as", "break", "case", "catch",
|
static constexpr auto keywords = Utils::to_array<std::u16string_view>(
|
||||||
"continue", "debugger", "default", "delete",
|
{u"as", u"break", u"case", u"catch", u"continue", u"debugger",
|
||||||
"do", "else", "finally", "for",
|
u"default", u"delete", u"do", u"else", u"finally", u"for",
|
||||||
"function", "if", "import", "in",
|
u"function", u"if", u"import", u"in", u"instanceof", u"new",
|
||||||
"instanceof", "new", "print", "return",
|
u"print", u"return", u"switch", u"this", u"throw", u"try",
|
||||||
"switch", "this", "throw", "try",
|
u"typeof", u"var", u"void", u"while", u"with"});
|
||||||
"typeof", "var", "void", "while",
|
|
||||||
"with"};
|
|
||||||
|
|
||||||
return keywords.contains(id);
|
return std::binary_search(keywords.begin(), keywords.end(), ModelUtils::toStdStringView(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool isIdToAvoid(const QString &id)
|
bool isIdToAvoid(QStringView id)
|
||||||
{
|
{
|
||||||
static const QSet<QString> ids = {"top", "bottom", "left", "right",
|
static constexpr auto token = Utils::to_array<std::u16string_view>(
|
||||||
"width", "height", "x", "y",
|
{u"anchors", u"baseState", u"border", u"bottom", u"clip", u"color",
|
||||||
"opacity", "parent", "item", "flow",
|
u"data", u"enabled", u"flow", u"focus", u"font", u"height",
|
||||||
"color", "margin", "padding", "border",
|
u"item", u"layer", u"left", u"margin", u"opacity", u"padding",
|
||||||
"font", "text", "source", "state",
|
u"parent", u"rect", u"right", u"scale", u"shaderInfo", u"source",
|
||||||
"visible", "focus", "data", "clip",
|
u"sprite", u"spriteSequence", u"state", u"text", u"texture", u"top",
|
||||||
"layer", "scale", "enabled", "anchors",
|
u"visible", u"width", u"x", u"y"});
|
||||||
"texture", "shaderInfo", "sprite", "spriteSequence",
|
|
||||||
"baseState", "rect"};
|
|
||||||
|
|
||||||
return ids.contains(id);
|
return std::binary_search(token.begin(), token.end(), ModelUtils::toStdStringView(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool idContainsWrongLetter(const QString &id)
|
bool idContainsWrongLetter(const QString &id)
|
||||||
{
|
{
|
||||||
static QRegularExpression idExpr(QStringLiteral("^[a-z_][a-zA-Z0-9_]*$"));
|
static QRegularExpression idExpr(QStringLiteral("^[a-z_][a-zA-Z0-9_]*$"));
|
||||||
return !id.contains(idExpr);
|
return !id.contains(idExpr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
bool ModelNode::isValidId(const QString &id)
|
bool ModelNode::isValidId(const QString &id)
|
||||||
{
|
{
|
||||||
return id.isEmpty() || (!idContainsWrongLetter(id) && !idIsQmlKeyWord(id) && !isIdToAvoid(id));
|
return id.isEmpty() || (!idContainsWrongLetter(id) && !isQmlKeyWord(id) && !isIdToAvoid(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
QString ModelNode::getIdValidityErrorMessage(const QString &id)
|
QString ModelNode::getIdValidityErrorMessage(const QString &id)
|
||||||
@@ -138,7 +140,7 @@ QString ModelNode::getIdValidityErrorMessage(const QString &id)
|
|||||||
if (id.contains(' '))
|
if (id.contains(' '))
|
||||||
return QObject::tr("ID cannot include whitespace (%1).").arg(id);
|
return QObject::tr("ID cannot include whitespace (%1).").arg(id);
|
||||||
|
|
||||||
if (idIsQmlKeyWord(id))
|
if (isQmlKeyWord(id))
|
||||||
return QObject::tr("%1 is a reserved QML keyword.").arg(id);
|
return QObject::tr("%1 is a reserved QML keyword.").arg(id);
|
||||||
|
|
||||||
if (isIdToAvoid(id))
|
if (isIdToAvoid(id))
|
||||||
|
|||||||
@@ -5,10 +5,15 @@
|
|||||||
|
|
||||||
#include "qmldesignercorelib_global.h"
|
#include "qmldesignercorelib_global.h"
|
||||||
|
|
||||||
|
#include <utils/algorithm.h>
|
||||||
|
|
||||||
#include <import.h>
|
#include <import.h>
|
||||||
#include <model.h>
|
#include <model.h>
|
||||||
|
|
||||||
|
#include <QStringView>
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
namespace QmlDesigner {
|
namespace QmlDesigner {
|
||||||
class PropertyMetaInfo;
|
class PropertyMetaInfo;
|
||||||
@@ -41,4 +46,9 @@ QMLDESIGNERCORE_EXPORT QList<ModelNode> allModelNodesWithId(AbstractView *view);
|
|||||||
QMLDESIGNERCORE_EXPORT bool isThisOrAncestorLocked(const ModelNode &node);
|
QMLDESIGNERCORE_EXPORT bool isThisOrAncestorLocked(const ModelNode &node);
|
||||||
QMLDESIGNERCORE_EXPORT ModelNode lowestCommonAncestor(Utils::span<const ModelNode> nodes);
|
QMLDESIGNERCORE_EXPORT ModelNode lowestCommonAncestor(Utils::span<const ModelNode> nodes);
|
||||||
|
|
||||||
|
constexpr std::u16string_view toStdStringView(QStringView view)
|
||||||
|
{
|
||||||
|
return {view.utf16(), Utils::usize(view)};
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace QmlDesigner::ModelUtils
|
} // namespace QmlDesigner::ModelUtils
|
||||||
|
|||||||
@@ -22,6 +22,7 @@
|
|||||||
|
|
||||||
#include <externaldependenciesinterface.h>
|
#include <externaldependenciesinterface.h>
|
||||||
#include <import.h>
|
#include <import.h>
|
||||||
|
#include <model/modelutils.h>
|
||||||
#include <projectstorage/modulescanner.h>
|
#include <projectstorage/modulescanner.h>
|
||||||
#include <rewritingexception.h>
|
#include <rewritingexception.h>
|
||||||
|
|
||||||
@@ -37,6 +38,7 @@
|
|||||||
#include <qmljs/qmljsvalueowner.h>
|
#include <qmljs/qmljsvalueowner.h>
|
||||||
|
|
||||||
#include <utils/algorithm.h>
|
#include <utils/algorithm.h>
|
||||||
|
#include <utils/array.h>
|
||||||
#include <utils/qrcparser.h>
|
#include <utils/qrcparser.h>
|
||||||
#include <utils/qtcassert.h>
|
#include <utils/qtcassert.h>
|
||||||
|
|
||||||
@@ -78,43 +80,46 @@ bool isSupportedVersion(QmlDesigner::Version version)
|
|||||||
|
|
||||||
bool isGlobalQtEnums(QStringView value)
|
bool isGlobalQtEnums(QStringView value)
|
||||||
{
|
{
|
||||||
static constexpr QLatin1StringView list[] = {
|
static constexpr auto list = Utils::to_array<std::u16string_view>(
|
||||||
"Horizontal"_L1, "Vertical"_L1, "AlignVCenter"_L1, "AlignLeft"_L1,
|
{u"AlignBaseline", u"AlignBottom", u"AlignHCenter", u"AlignLeft",
|
||||||
"LeftToRight"_L1, "RightToLeft"_L1, "AlignHCenter"_L1, "AlignRight"_L1,
|
u"AlignRight", u"AlignTop", u"AlignVCenter", u"AllButtons",
|
||||||
"AlignBottom"_L1, "AlignBaseline"_L1, "AlignTop"_L1, "BottomLeft"_L1,
|
u"ArrowCursor", u"BackButton", u"BlankCursor", u"BottomEdge",
|
||||||
"LeftEdge"_L1, "RightEdge"_L1, "BottomEdge"_L1, "TopEdge"_L1,
|
u"BottomLeft", u"BusyCursor", u"ClickFocus", u"ClosedHandCursor",
|
||||||
"TabFocus"_L1, "ClickFocus"_L1, "StrongFocus"_L1, "WheelFocus"_L1,
|
u"CrossCursor", u"DragCopyCursor", u"DragLinkCursor", u"DragMoveCursor",
|
||||||
"NoFocus"_L1, "ArrowCursor"_L1, "UpArrowCursor"_L1, "CrossCursor"_L1,
|
u"ForbiddenCursor", u"ForwardButton", u"Horizontal", u"IBeamCursor",
|
||||||
"WaitCursor"_L1, "IBeamCursor"_L1, "SizeVerCursor"_L1, "SizeHorCursor"_L1,
|
u"LeftButton", u"LeftEdge", u"LeftToRight", u"MiddleButton",
|
||||||
"SizeBDiagCursor"_L1, "SizeFDiagCursor"_L1, "SizeAllCursor"_L1, "BlankCursor"_L1,
|
u"NoFocus", u"OpenHandCursor", u"PointingHandCursor", u"RightButton",
|
||||||
"SplitVCursor"_L1, "SplitHCursor"_L1, "PointingHandCursor"_L1, "ForbiddenCursor"_L1,
|
u"RightEdge", u"RightToLeft", u"SizeAllCursor", u"SizeBDiagCursor",
|
||||||
"WhatsThisCursor"_L1, "BusyCursor"_L1, "OpenHandCursor"_L1, "ClosedHandCursor"_L1,
|
u"SizeFDiagCursor", u"SizeHorCursor", u"SizeVerCursor", u"SplitHCursor",
|
||||||
"DragCopyCursor"_L1, "DragMoveCursor"_L1, "DragLinkCursor"_L1, "TopToBottom"_L1,
|
u"SplitVCursor", u"StrongFocus", u"TabFocus", u"TopEdge",
|
||||||
"LeftButton"_L1, "RightButton"_L1, "MiddleButton"_L1, "BackButton"_L1,
|
u"TopToBottom", u"UpArrowCursor", u"Vertical", u"WaitCursor",
|
||||||
"ForwardButton"_L1, "AllButtons"_L1};
|
u"WhatsThisCursor", u"WheelFocus"});
|
||||||
|
|
||||||
return std::find(std::begin(list), std::end(list), value) != std::end(list);
|
return std::binary_search(std::begin(list),
|
||||||
|
std::end(list),
|
||||||
|
QmlDesigner::ModelUtils::toStdStringView(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isKnownEnumScopes(QStringView value)
|
bool isKnownEnumScopes(QStringView value)
|
||||||
{
|
{
|
||||||
static constexpr QLatin1StringView list[] = {"TextInput"_L1,
|
static constexpr auto list = Utils::to_array<std::u16string_view>({u"TextInput",
|
||||||
"TextEdit"_L1,
|
u"TextEdit",
|
||||||
"Material"_L1,
|
u"Material",
|
||||||
"Universal"_L1,
|
u"Universal",
|
||||||
"Font"_L1,
|
u"Font",
|
||||||
"Shape"_L1,
|
u"Shape",
|
||||||
"ShapePath"_L1,
|
u"ShapePath",
|
||||||
"AbstractButton"_L1,
|
u"AbstractButton",
|
||||||
"Text"_L1,
|
u"Text",
|
||||||
"ShaderEffectSource"_L1,
|
u"ShaderEffectSource",
|
||||||
"Grid"_L1,
|
u"Grid",
|
||||||
"ItemLayer"_L1,
|
u"ItemLayer",
|
||||||
"ImageLayer"_L1,
|
u"ImageLayer",
|
||||||
"SpriteLayer"_L1,
|
u"SpriteLayer",
|
||||||
"Light"_L1};
|
u"Light"});
|
||||||
|
|
||||||
return std::find(std::begin(list), std::end(list), value) != std::end(list);
|
return std::find(std::begin(list), std::end(list), QmlDesigner::ModelUtils::toStdStringView(value))
|
||||||
|
!= std::end(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool supportedQtQuickVersion(const QmlDesigner::Import &import)
|
bool supportedQtQuickVersion(const QmlDesigner::Import &import)
|
||||||
|
|||||||
Reference in New Issue
Block a user