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>
|
||||
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));
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
|
||||
namespace Utils {
|
||||
|
||||
namespace Internal {
|
||||
|
||||
@@ -48,6 +48,7 @@ public:
|
||||
/* */
|
||||
|
||||
using TypeName = QByteArray;
|
||||
using TypeNameView = QByteArrayView;
|
||||
using PropertyTypeList = QList<PropertyName>;
|
||||
using IdName = QByteArray;
|
||||
class Model;
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "bindingproperty.h"
|
||||
#include "internalnode_p.h"
|
||||
#include "model_p.h"
|
||||
#include "modelutils.h"
|
||||
#include "nodeabstractproperty.h"
|
||||
#include "nodelistproperty.h"
|
||||
#include "nodeproperty.h"
|
||||
@@ -19,12 +20,15 @@
|
||||
#include <rewriterview.h>
|
||||
|
||||
#include <utils/algorithm.h>
|
||||
#include <utils/array.h>
|
||||
|
||||
#include <QHash>
|
||||
#include <QRegularExpression>
|
||||
#include <QSet>
|
||||
#include <QTextStream>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace QmlDesigner {
|
||||
using namespace QmlDesigner::Internal;
|
||||
|
||||
@@ -84,44 +88,42 @@ QString ModelNode::validId()
|
||||
return id();
|
||||
}
|
||||
|
||||
static bool idIsQmlKeyWord(const QString &id)
|
||||
namespace {
|
||||
bool isQmlKeyWord(QStringView id)
|
||||
{
|
||||
static const QSet<QString> keywords = {"as", "break", "case", "catch",
|
||||
"continue", "debugger", "default", "delete",
|
||||
"do", "else", "finally", "for",
|
||||
"function", "if", "import", "in",
|
||||
"instanceof", "new", "print", "return",
|
||||
"switch", "this", "throw", "try",
|
||||
"typeof", "var", "void", "while",
|
||||
"with"};
|
||||
static constexpr auto keywords = Utils::to_array<std::u16string_view>(
|
||||
{u"as", u"break", u"case", u"catch", u"continue", u"debugger",
|
||||
u"default", u"delete", u"do", u"else", u"finally", u"for",
|
||||
u"function", u"if", u"import", u"in", u"instanceof", u"new",
|
||||
u"print", u"return", u"switch", u"this", u"throw", u"try",
|
||||
u"typeof", u"var", u"void", u"while", u"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",
|
||||
"width", "height", "x", "y",
|
||||
"opacity", "parent", "item", "flow",
|
||||
"color", "margin", "padding", "border",
|
||||
"font", "text", "source", "state",
|
||||
"visible", "focus", "data", "clip",
|
||||
"layer", "scale", "enabled", "anchors",
|
||||
"texture", "shaderInfo", "sprite", "spriteSequence",
|
||||
"baseState", "rect"};
|
||||
static constexpr auto token = Utils::to_array<std::u16string_view>(
|
||||
{u"anchors", u"baseState", u"border", u"bottom", u"clip", u"color",
|
||||
u"data", u"enabled", u"flow", u"focus", u"font", u"height",
|
||||
u"item", u"layer", u"left", u"margin", u"opacity", u"padding",
|
||||
u"parent", u"rect", u"right", u"scale", u"shaderInfo", u"source",
|
||||
u"sprite", u"spriteSequence", u"state", u"text", u"texture", u"top",
|
||||
u"visible", u"width", u"x", u"y"});
|
||||
|
||||
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_]*$"));
|
||||
return !id.contains(idExpr);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
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)
|
||||
@@ -138,7 +140,7 @@ QString ModelNode::getIdValidityErrorMessage(const QString &id)
|
||||
if (id.contains(' '))
|
||||
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);
|
||||
|
||||
if (isIdToAvoid(id))
|
||||
|
||||
@@ -5,10 +5,15 @@
|
||||
|
||||
#include "qmldesignercorelib_global.h"
|
||||
|
||||
#include <utils/algorithm.h>
|
||||
|
||||
#include <import.h>
|
||||
#include <model.h>
|
||||
|
||||
#include <QStringView>
|
||||
|
||||
#include <functional>
|
||||
#include <string_view>
|
||||
|
||||
namespace QmlDesigner {
|
||||
class PropertyMetaInfo;
|
||||
@@ -41,4 +46,9 @@ QMLDESIGNERCORE_EXPORT QList<ModelNode> allModelNodesWithId(AbstractView *view);
|
||||
QMLDESIGNERCORE_EXPORT bool isThisOrAncestorLocked(const ModelNode &node);
|
||||
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
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
|
||||
#include <externaldependenciesinterface.h>
|
||||
#include <import.h>
|
||||
#include <model/modelutils.h>
|
||||
#include <projectstorage/modulescanner.h>
|
||||
#include <rewritingexception.h>
|
||||
|
||||
@@ -37,6 +38,7 @@
|
||||
#include <qmljs/qmljsvalueowner.h>
|
||||
|
||||
#include <utils/algorithm.h>
|
||||
#include <utils/array.h>
|
||||
#include <utils/qrcparser.h>
|
||||
#include <utils/qtcassert.h>
|
||||
|
||||
@@ -78,43 +80,46 @@ bool isSupportedVersion(QmlDesigner::Version version)
|
||||
|
||||
bool isGlobalQtEnums(QStringView value)
|
||||
{
|
||||
static constexpr QLatin1StringView list[] = {
|
||||
"Horizontal"_L1, "Vertical"_L1, "AlignVCenter"_L1, "AlignLeft"_L1,
|
||||
"LeftToRight"_L1, "RightToLeft"_L1, "AlignHCenter"_L1, "AlignRight"_L1,
|
||||
"AlignBottom"_L1, "AlignBaseline"_L1, "AlignTop"_L1, "BottomLeft"_L1,
|
||||
"LeftEdge"_L1, "RightEdge"_L1, "BottomEdge"_L1, "TopEdge"_L1,
|
||||
"TabFocus"_L1, "ClickFocus"_L1, "StrongFocus"_L1, "WheelFocus"_L1,
|
||||
"NoFocus"_L1, "ArrowCursor"_L1, "UpArrowCursor"_L1, "CrossCursor"_L1,
|
||||
"WaitCursor"_L1, "IBeamCursor"_L1, "SizeVerCursor"_L1, "SizeHorCursor"_L1,
|
||||
"SizeBDiagCursor"_L1, "SizeFDiagCursor"_L1, "SizeAllCursor"_L1, "BlankCursor"_L1,
|
||||
"SplitVCursor"_L1, "SplitHCursor"_L1, "PointingHandCursor"_L1, "ForbiddenCursor"_L1,
|
||||
"WhatsThisCursor"_L1, "BusyCursor"_L1, "OpenHandCursor"_L1, "ClosedHandCursor"_L1,
|
||||
"DragCopyCursor"_L1, "DragMoveCursor"_L1, "DragLinkCursor"_L1, "TopToBottom"_L1,
|
||||
"LeftButton"_L1, "RightButton"_L1, "MiddleButton"_L1, "BackButton"_L1,
|
||||
"ForwardButton"_L1, "AllButtons"_L1};
|
||||
static constexpr auto list = Utils::to_array<std::u16string_view>(
|
||||
{u"AlignBaseline", u"AlignBottom", u"AlignHCenter", u"AlignLeft",
|
||||
u"AlignRight", u"AlignTop", u"AlignVCenter", u"AllButtons",
|
||||
u"ArrowCursor", u"BackButton", u"BlankCursor", u"BottomEdge",
|
||||
u"BottomLeft", u"BusyCursor", u"ClickFocus", u"ClosedHandCursor",
|
||||
u"CrossCursor", u"DragCopyCursor", u"DragLinkCursor", u"DragMoveCursor",
|
||||
u"ForbiddenCursor", u"ForwardButton", u"Horizontal", u"IBeamCursor",
|
||||
u"LeftButton", u"LeftEdge", u"LeftToRight", u"MiddleButton",
|
||||
u"NoFocus", u"OpenHandCursor", u"PointingHandCursor", u"RightButton",
|
||||
u"RightEdge", u"RightToLeft", u"SizeAllCursor", u"SizeBDiagCursor",
|
||||
u"SizeFDiagCursor", u"SizeHorCursor", u"SizeVerCursor", u"SplitHCursor",
|
||||
u"SplitVCursor", u"StrongFocus", u"TabFocus", u"TopEdge",
|
||||
u"TopToBottom", u"UpArrowCursor", u"Vertical", u"WaitCursor",
|
||||
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)
|
||||
{
|
||||
static constexpr QLatin1StringView list[] = {"TextInput"_L1,
|
||||
"TextEdit"_L1,
|
||||
"Material"_L1,
|
||||
"Universal"_L1,
|
||||
"Font"_L1,
|
||||
"Shape"_L1,
|
||||
"ShapePath"_L1,
|
||||
"AbstractButton"_L1,
|
||||
"Text"_L1,
|
||||
"ShaderEffectSource"_L1,
|
||||
"Grid"_L1,
|
||||
"ItemLayer"_L1,
|
||||
"ImageLayer"_L1,
|
||||
"SpriteLayer"_L1,
|
||||
"Light"_L1};
|
||||
static constexpr auto list = Utils::to_array<std::u16string_view>({u"TextInput",
|
||||
u"TextEdit",
|
||||
u"Material",
|
||||
u"Universal",
|
||||
u"Font",
|
||||
u"Shape",
|
||||
u"ShapePath",
|
||||
u"AbstractButton",
|
||||
u"Text",
|
||||
u"ShaderEffectSource",
|
||||
u"Grid",
|
||||
u"ItemLayer",
|
||||
u"ImageLayer",
|
||||
u"SpriteLayer",
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user