aboutsummaryrefslogtreecommitdiffstats
path: root/tools/qmlformat/qmlformat.cpp
diff options
context:
space:
mode:
authorSemih Yavuz <semih.yavuz@qt.io>2025-01-27 12:06:07 +0100
committerSemih Yavuz <semih.yavuz@qt.io>2025-01-29 20:37:46 +0100
commit6dfcdfd90dfd53c24bbe67e1391af00a420fa282 (patch)
treef9b446bc7357c2007003b02cd5e89c5fb9ac16b8 /tools/qmlformat/qmlformat.cpp
parentdf13ff9749afd883ca1740f2564ec646816bd8b4 (diff)
qmlformat: move configuration functions into library
...Then we can add tests for them. More tests to be added in the next commit. Pick-to: 6.8 6.9 Task-number: QTBUG-133225 Change-Id: I3f40ddc3d0895a785d6bce359c63ad6ad4d70606 Reviewed-by: Sami Shalayel <sami.shalayel@qt.io>
Diffstat (limited to 'tools/qmlformat/qmlformat.cpp')
-rw-r--r--tools/qmlformat/qmlformat.cpp146
1 files changed, 3 insertions, 143 deletions
diff --git a/tools/qmlformat/qmlformat.cpp b/tools/qmlformat/qmlformat.cpp
index 9d60d17f66..6cdfa907c3 100644
--- a/tools/qmlformat/qmlformat.cpp
+++ b/tools/qmlformat/qmlformat.cpp
@@ -120,132 +120,6 @@ static bool parseFile(const QString &filename, const QQmlFormatOptions &options)
return res;
}
-QQmlFormatOptions buildCommandLineOptions(const QCoreApplication &app)
-{
- QQmlFormatOptions options;
-#if QT_CONFIG(commandlineparser)
- QCommandLineParser parser;
- parser.setApplicationDescription("Formats QML files according to the QML Coding Conventions.");
- parser.addHelpOption();
- parser.addVersionOption();
-
- parser.addOption(
- QCommandLineOption({ "V", "verbose" },
- QStringLiteral("Verbose mode. Outputs more detailed information.")));
-
- QCommandLineOption writeDefaultsOption(
- QStringList() << "write-defaults",
- QLatin1String("Writes defaults settings to .qmlformat.ini and exits (Warning: This "
- "will overwrite any existing settings and comments!)"));
- parser.addOption(writeDefaultsOption);
-
- QCommandLineOption ignoreSettings(QStringList() << "ignore-settings",
- QLatin1String("Ignores all settings files and only takes "
- "command line options into consideration"));
- parser.addOption(ignoreSettings);
-
- parser.addOption(QCommandLineOption(
- { "i", "inplace" },
- QStringLiteral("Edit file in-place instead of outputting to stdout.")));
-
- parser.addOption(QCommandLineOption({ "f", "force" },
- QStringLiteral("Continue even if an error has occurred.")));
-
- parser.addOption(
- QCommandLineOption({ "t", "tabs" }, QStringLiteral("Use tabs instead of spaces.")));
-
- parser.addOption(QCommandLineOption({ "w", "indent-width" },
- QStringLiteral("How many spaces are used when indenting."),
- "width", "4"));
-
- QCommandLineOption columnWidthOption(
- { "W", "column-width" },
- QStringLiteral("Breaks the line into multiple lines if exceedes the specified width."
- "Use -1 to disable line wrapping. (default)"),
- "width", "-1");
- parser.addOption(columnWidthOption);
- parser.addOption(QCommandLineOption({ "n", "normalize" },
- QStringLiteral("Reorders the attributes of the objects "
- "according to the QML Coding Guidelines.")));
-
- parser.addOption(QCommandLineOption(
- { "F", "files" }, QStringLiteral("Format all files listed in file, in-place"), "file"));
-
- parser.addOption(QCommandLineOption(
- { "l", "newline" },
- QStringLiteral("Override the new line format to use (native macos unix windows)."),
- "newline", "native"));
-
- parser.addOption(QCommandLineOption(QStringList() << "objects-spacing", QStringLiteral("Ensure spaces between objects (only works with normalize option).")));
-
- parser.addOption(QCommandLineOption(QStringList() << "functions-spacing", QStringLiteral("Ensure spaces between functions (only works with normalize option).")));
-
- parser.addOption(
- QCommandLineOption({ "S", "sort-imports" },
- QStringLiteral("Sort imports alphabetically "
- "(Warning: this might change semantics if a given name identifies types in multiple modules!).")));
-
- parser.addPositionalArgument("filenames", "files to be processed by qmlformat");
-
- parser.process(app);
-
- if (parser.isSet(writeDefaultsOption)) {
- options.setWriteDefaultSettingsEnabled(true);
- return options;
- }
-
- bool indentWidthOkay = false;
- const int indentWidth = parser.value("indent-width").toInt(&indentWidthOkay);
- if (!indentWidthOkay) {
- options.addError("Error: Invalid value passed to -w");
- return options;
- }
-
- QStringList files;
- if (!parser.value("files").isEmpty()) {
- QFile file(parser.value("files"));
- if (file.open(QIODevice::Text | QIODevice::ReadOnly)) {
- QTextStream in(&file);
- while (!in.atEnd()) {
- QString file = in.readLine();
-
- if (file.isEmpty())
- continue;
-
- files.push_back(file);
- }
- }
- }
-
- options.setIsVerbose(parser.isSet("verbose"));
- options.setIsInplace(parser.isSet("inplace"));
- options.setForceEnabled(parser.isSet("force"));
- options.setTabsEnabled(parser.isSet("tabs"));
- options.setIgnoreSettingsEnabled(parser.isSet("ignore-settings"));
- options.setNormalizeEnabled(parser.isSet("normalize"));
- options.setObjectsSpacing(parser.isSet("objects-spacing"));
- options.setFunctionsSpacing(parser.isSet("functions-spacing"));
- options.setSortImports(parser.isSet("sort-imports"));
-
- options.setIndentWidth(indentWidth);
- options.setIndentWidthSet(parser.isSet("indent-width"));
- options.setNewline(QQmlFormatOptions::parseEndings(parser.value("newline"))); // TODO
- options.setFiles(files);
- options.setArguments(parser.positionalArguments());
-
- if (parser.isSet(columnWidthOption)) {
- bool isValidValue = false;
- const int maxColumnWidth = parser.value(columnWidthOption).toInt(&isValidValue);
- if (!isValidValue || maxColumnWidth < -1) {
- options.addError("Error: Invalid value passed to -W. Must be an integer >= -1");
- return options;
- }
- options.setMaxColumnWidth(maxColumnWidth);
- }
-#endif
- return options;
-}
-
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
@@ -253,8 +127,7 @@ int main(int argc, char *argv[])
QCoreApplication::setApplicationVersion(QT_VERSION_STR);
QQmlFormatSettings settings(QLatin1String("qmlformat"));
-
- const auto &options = buildCommandLineOptions(app);
+ const auto &options = QQmlFormatOptions::buildCommandLineOptions(app.arguments());
if (!options.isValid()) {
for (const auto &error : options.errors()) {
qWarning().noquote() << error;
@@ -266,19 +139,6 @@ int main(int argc, char *argv[])
if (options.writeDefaultSettingsEnabled())
return settings.writeDefaults() ? 0 : -1;
- auto getSettings = [&](const QString &file, const QQmlFormatOptions &options) {
- // Perform formatting inplace if --files option is set.
- const bool hasFiles = !options.files().isEmpty();
- QQmlFormatOptions perFileOptions = options;
- if (hasFiles)
- perFileOptions.setIsInplace(true);
-
- if (!options.ignoreSettingsEnabled() && settings.search(file))
- perFileOptions.applySettings(settings);
-
- return perFileOptions;
- };
-
bool success = true;
if (!options.files().isEmpty()) {
if (!options.arguments().isEmpty())
@@ -287,12 +147,12 @@ int main(int argc, char *argv[])
for (const QString &file : options.files()) {
Q_ASSERT(!file.isEmpty());
- if (!parseFile(file, getSettings(file, options)))
+ if (!parseFile(file, options.optionsForFile(file, &settings)))
success = false;
}
} else {
for (const QString &file : options.arguments()) {
- if (!parseFile(file, getSettings(file, options)))
+ if (!parseFile(file, options.optionsForFile(file, &settings)))
success = false;
}
}