QString QmlMarkupVisitor::markedUpCode()
{
    if (int(cursor) < source.length())
        addExtra(cursor, source.length());

    return output;
}
void QmlMarkupVisitor::addMarkedUpToken(
        QQmlJS::AST::SourceLocation &location, const QString &tagName,
        const QHash<QString, QString> &attributes)
{
    if (!location.isValid())
        return;

    if (cursor < location.offset)
        addExtra(cursor, location.offset);
    else if (cursor > location.offset)
        return;

    output += QString(QLatin1String("<@%1")).arg(tagName);
    foreach (const QString &key, attributes)
        output += QString(QLatin1String(" %1=\"%2\"")).arg(key).arg(attributes[key]);
    output += QString(QLatin1String(">%2</@%3>")).arg(protect(sourceText(location)), tagName);
    cursor += location.length;
}
void QmlMarkupVisitor::addVerbatim(QQmlJS::AST::SourceLocation first,
                                   QQmlJS::AST::SourceLocation last)
{
    if (!first.isValid())
        return;

    quint32 start = first.begin();
    quint32 finish;
    if (last.isValid())
        finish = last.end();
    else
        finish = first.end();

    if (cursor < start)
        addExtra(cursor, start);
    else if (cursor > start)
        return;

    QString text = source.mid(start, finish - start);
    output += protect(text);
    cursor = finish;
}
Example #4
0
    OptionalEntity ComponentsMaker::makeProperty(const Tokens &tokens)
    {
        Q_ASSERT(!tokens.isEmpty() && tokens[int(PropGroupNames::Type)]->isSingle() &&
                 !tokens[int(PropGroupNames::Type)]->token().isEmpty() &&
                 tokens[int(PropGroupNames::Name)]->isSingle() &&
                 !tokens[int(PropGroupNames::Name)]->token().isEmpty());

        Q_ASSERT(checkCommonState());

        entity::SharedProperty newProperty = std::make_shared<entity::Property>();
        newProperty->setTypeSearcher(m_Model->globalDatabase());

        // Add name
        newProperty->setName(tokens[int(PropGroupNames::Name)]->token());

        // Add type
        // Not support namespaces for now, so check only in global database.
        // Work with namespaces and custom types in project will be hard due to Qt meta-stuff.
        // And also required more detail work on current code generation functionality. TODO: implement!
        const entity::SharedScope &globasScope = m_Model->globalDatabase()->scope(common::ID::globalScopeID());
        if (!globasScope)
            return {tr("Cannot find global scope."), nullptr};

        const auto &typeName = tokens[int(PropGroupNames::Type)]->token();
        auto type = globasScope->type(typeName);
        if (!type)
            return {tr("Wrong type: %1.").arg(typeName), nullptr};
        newProperty->setTypeId(type->id());

        // Member (supports only "m_" prefix for now)
        const auto &member = tokens[int(PropGroupNames::Member)]->token();
        if (!member.isEmpty()) {
            const bool hasPrefix = member.startsWith("m_");
            const auto name(hasPrefix ? QString(member).remove(0, 2) : member);
            const auto prefix(hasPrefix ? "m_" : "");
            newProperty->addField(name).field()->setPrefix(prefix);
            newProperty->setMember(true);
        }

        // Common methods
        addCommon(tokens, PropGroupNames::Getter,   &entity::Property::addGetter,   newProperty);
        addCommon(tokens, PropGroupNames::Setter,   &entity::Property::addSetter,   newProperty);
        addCommon(tokens, PropGroupNames::Resetter, &entity::Property::addResetter, newProperty);
        addCommon(tokens, PropGroupNames::Notifier, &entity::Property::addNotifier, newProperty);

        // Revision
        Q_ASSERT(tokens[int(PropGroupNames::Revision)]->isSingle());
        const auto &revision = tokens[int(PropGroupNames::Revision)]->token();
        if (!revision.isEmpty()) {
            bool ok = false;
            int rev = revision.toInt(&ok);

            if (ok)
                newProperty->setRevision(rev);
            else
                return {tr("Wrong revision: %1.").arg(revision), nullptr};
        }

        // Add designable and scriptable
        addDS(tokens, PropGroupNames::Designable, &entity::Property::addDesignableGetter,
              &entity::Property::setDesignable, newProperty);
        addDS(tokens, PropGroupNames::Scriptable, &entity::Property::addScriptableGetter,
              &entity::Property::setScriptable, newProperty);

        // Add stroed, user, const and final options
        addExtra(tokens, PropGroupNames::Stored,   &entity::Property::setStored,   newProperty);
        addExtra(tokens, PropGroupNames::User,     &entity::Property::setUser,     newProperty);
        addExtra(tokens, PropGroupNames::Constant, &entity::Property::setConstant, newProperty);
        addExtra(tokens, PropGroupNames::Final,    &entity::Property::setFinal,    newProperty);

        return {"", newProperty};
    }