diff options
| author | Semih Yavuz <semih.yavuz@qt.io> | 2025-01-27 12:06:07 +0100 |
|---|---|---|
| committer | Semih Yavuz <semih.yavuz@qt.io> | 2025-01-29 20:37:46 +0100 |
| commit | 6dfcdfd90dfd53c24bbe67e1391af00a420fa282 (patch) | |
| tree | f9b446bc7357c2007003b02cd5e89c5fb9ac16b8 /src/qmlformat/qqmlformatoptions.cpp | |
| parent | df13ff9749afd883ca1740f2564ec646816bd8b4 (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 'src/qmlformat/qqmlformatoptions.cpp')
| -rw-r--r-- | src/qmlformat/qqmlformatoptions.cpp | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/src/qmlformat/qqmlformatoptions.cpp b/src/qmlformat/qqmlformatoptions.cpp index 80820974c9..86bf50c3d3 100644 --- a/src/qmlformat/qqmlformatoptions.cpp +++ b/src/qmlformat/qqmlformatoptions.cpp @@ -3,6 +3,13 @@ #include "qqmlformatoptions_p.h" +#if QT_CONFIG(commandlineparser) +# include <QCommandLineParser> +# include <QCommandLineOption> +#endif + +using namespace Qt::StringLiterals; + QQmlFormatOptions::QQmlFormatOptions() { setTabsEnabled(false); @@ -92,3 +99,151 @@ void QQmlFormatOptions::applySettings(const QQmlFormatSettings &settings) if (settings.isSet(QQmlFormatSettings::s_sortImportsSetting)) setSortImports(settings.value(QQmlFormatSettings::s_sortImportsSetting).toBool()); } + +QQmlFormatOptions QQmlFormatOptions::buildCommandLineOptions(const QStringList &args) +{ + QQmlFormatOptions options; +#if QT_CONFIG(commandlineparser) + QCommandLineParser parser; + parser.setApplicationDescription( + "Formats QML files according to the QML Coding Conventions."_L1); + parser.addHelpOption(); + parser.addVersionOption(); + + parser.addOption( + QCommandLineOption({ "V"_L1, "verbose"_L1 }, + QStringLiteral("Verbose mode. Outputs more detailed information."))); + + QCommandLineOption writeDefaultsOption( + QStringList() << "write-defaults"_L1, + QLatin1String("Writes defaults settings to .qmlformat.ini and exits (Warning: This " + "will overwrite any existing settings and comments!)"_L1)); + parser.addOption(writeDefaultsOption); + + QCommandLineOption ignoreSettings(QStringList() << "ignore-settings"_L1, + QLatin1String("Ignores all settings files and only takes " + "command line options into consideration"_L1)); + parser.addOption(ignoreSettings); + + parser.addOption(QCommandLineOption( + { "i"_L1, "inplace"_L1 }, + QStringLiteral("Edit file in-place instead of outputting to stdout."))); + + parser.addOption(QCommandLineOption({ "f"_L1, "force"_L1 }, + QStringLiteral("Continue even if an error has occurred."))); + + parser.addOption(QCommandLineOption({ "t"_L1, "tabs"_L1 }, + QStringLiteral("Use tabs instead of spaces."))); + + parser.addOption(QCommandLineOption({ "w"_L1, "indent-width"_L1 }, + QStringLiteral("How many spaces are used when indenting."), + "width"_L1, "4"_L1)); + + QCommandLineOption columnWidthOption( + { "W"_L1, "column-width"_L1 }, + QStringLiteral("Breaks the line into multiple lines if exceedes the specified width." + "Use -1 to disable line wrapping. (default)"), + "width"_L1, "-1"_L1); + parser.addOption(columnWidthOption); + parser.addOption(QCommandLineOption({ "n"_L1, "normalize"_L1 }, + QStringLiteral("Reorders the attributes of the objects " + "according to the QML Coding Guidelines."))); + + parser.addOption(QCommandLineOption({ "F"_L1, "files"_L1 }, + QStringLiteral("Format all files listed in file, in-place"), + "file"_L1)); + + parser.addOption(QCommandLineOption( + { "l"_L1, "newline"_L1 }, + QStringLiteral("Override the new line format to use (native macos unix windows)."), + "newline"_L1, "native"_L1)); + + parser.addOption(QCommandLineOption( + QStringList() << "objects-spacing"_L1, + QStringLiteral("Ensure spaces between objects (only works with normalize option)."))); + + parser.addOption(QCommandLineOption( + QStringList() << "functions-spacing"_L1, + QStringLiteral("Ensure spaces between functions (only works with normalize option)."))); + + parser.addOption( + QCommandLineOption({ "S"_L1, "sort-imports"_L1 }, + QStringLiteral("Sort imports alphabetically " + "(Warning: this might change semantics if a given " + "name identifies types in multiple modules!)."))); + + parser.addPositionalArgument("filenames"_L1, "files to be processed by qmlformat"_L1); + + parser.process(args); + + if (parser.isSet(writeDefaultsOption)) { + options.setWriteDefaultSettingsEnabled(true); + return options; + } + + bool indentWidthOkay = false; + const int indentWidth = parser.value("indent-width"_L1).toInt(&indentWidthOkay); + if (!indentWidthOkay) { + options.addError("Error: Invalid value passed to -w"_L1); + return options; + } + + QStringList files; + if (!parser.value("files"_L1).isEmpty()) { + QFile file(parser.value("files"_L1)); + 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"_L1)); + options.setIsInplace(parser.isSet("inplace"_L1)); + options.setForceEnabled(parser.isSet("force"_L1)); + options.setTabsEnabled(parser.isSet("tabs"_L1)); + options.setIgnoreSettingsEnabled(parser.isSet("ignore-settings"_L1)); + options.setNormalizeEnabled(parser.isSet("normalize"_L1)); + options.setObjectsSpacing(parser.isSet("objects-spacing"_L1)); + options.setFunctionsSpacing(parser.isSet("functions-spacing"_L1)); + options.setSortImports(parser.isSet("sort-imports"_L1)); + + options.setIndentWidth(indentWidth); + options.setIndentWidthSet(parser.isSet("indent-width"_L1)); + options.setNewline(QQmlFormatOptions::parseEndings(parser.value("newline"_L1))); // 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"_L1); + return options; + } + options.setMaxColumnWidth(maxColumnWidth); + } +#endif + return options; +} + +QQmlFormatOptions QQmlFormatOptions::optionsForFile(const QString &fileName, + QQmlFormatSettings *settings) const +{ + // Perform formatting inplace if --files option is set. + const bool hasFiles = !files().isEmpty(); + QQmlFormatOptions perFileOptions = *this; + if (hasFiles) + perFileOptions.setIsInplace(true); + + if (!ignoreSettingsEnabled() && settings->search(fileName)) + perFileOptions.applySettings(*settings); + + return perFileOptions; +} |
