Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmlformatsettings.cpp
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4
6
7#include <QCommandLineParser>
8#include <QJsonArray>
9#include <QJsonDocument>
10#include <QJsonObject>
11
12using namespace Qt::StringLiterals;
13
14QQmlFormatSettings::QQmlFormatSettings(const QString &toolName) : QQmlToolingSettings(toolName)
15{
16 addOption(s_useTabsSetting, false);
17 addOption(s_indentWidthSetting, 4);
18 addOption(s_maxColumnWidthSetting, -1);
19 addOption(s_normalizeSetting, false);
20 addOption(s_newlineSetting, u"native"_s,
21 QStringList { u"unix"_s, u"windows"_s, u"macos"_s, u"native"_s });
22 addOption(s_objectsSpacingSetting, false);
23 addOption(s_functionsSpacingSetting, false);
24 addOption(s_sortImportsSetting, false);
25 addOption(s_singleLineEmptyObjectsSetting, false);
26 addOption(s_semiColonRuleSetting, u"always"_s, QStringList{ u"always"_s, u"essential"_s });
27}
28
29void QQmlFormatSettings::addOption(const QString &name, const QVariant &defaultValue, const QStringList &allowedValues)
30{
31 QQmlToolingSettings::addOption(name, defaultValue);
32 if (defaultValue.typeId() == QMetaType::QString) {
33 Q_ASSERT(!allowedValues.isEmpty());
34 m_allowedValues[name] = allowedValues;
35 }
36}
37
38bool QQmlFormatSettings::outputOptions() const
39{
40 QJsonObject root;
41 QJsonArray optionsArray;
42 for (auto it = m_values.constBegin(); it != m_values.constEnd(); ++it) {
43 QJsonObject option;
44 option[u"name"] = it.key();
45 option[u"value"] = QJsonValue::fromVariant(it.value());
46 option[u"hint"] = QString::fromUtf8(it.value().typeName());
47
48 if (it.value().typeId() == QMetaType::QString)
49 option[u"hint"] = m_allowedValues[it.key()].join(u',');
50
51 optionsArray.append(option);
52 }
53 root[u"options"] = optionsArray;
54
55 QCommandLineParser::showMessageAndExit(
56 QCommandLineParser::MessageType::Information,
57 QString::fromUtf8(QJsonDocument(root).toJson()), EXIT_SUCCESS);
58 return true;
59}