diff options
| author | Maximilian Goldstein <max.goldstein@qt.io> | 2021-10-25 18:30:26 +0200 |
|---|---|---|
| committer | Maximilian Goldstein <max.goldstein@qt.io> | 2021-10-26 12:03:40 +0200 |
| commit | e14d13bea68a5bf40a5475e5065651f750d4af04 (patch) | |
| tree | 360afa7ca59445a0e960df18d716ef9666a47a9c /tools/qmlformat/qmlformat.cpp | |
| parent | e2532e8773e0ee123d2b27e978026386b7ebc19d (diff) | |
qmlformat: Implement settings file
Implements controlling qmlformat via a settings file as can be done for qmllint.
[ChangeLog][General][qmlformat] Adds the ability to set linting options
via a settings file rather than using command line parameters. Use
--write-defaults to generate a template with default values for editing.
Use --ignore-settings to disable this feature.
Fixes: QTBUG-86415
Change-Id: I282c3b994ca6cc491a27b45f531f1ba1c2652ef7
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tools/qmlformat/qmlformat.cpp')
| -rw-r--r-- | tools/qmlformat/qmlformat.cpp | 67 |
1 files changed, 65 insertions, 2 deletions
diff --git a/tools/qmlformat/qmlformat.cpp b/tools/qmlformat/qmlformat.cpp index dab1ee42c7..75120b3ab0 100644 --- a/tools/qmlformat/qmlformat.cpp +++ b/tools/qmlformat/qmlformat.cpp @@ -44,6 +44,8 @@ # include <QCommandLineParser> #endif +#include "../shared/qqmltoolingsettings.h" + using namespace QQmlJS::Dom; struct Options @@ -54,6 +56,8 @@ struct Options bool tabs = false; bool valid = false; bool normalize = false; + bool ignoreSettings = false; + bool writeDefaultSettings = false; int indentWidth = 4; bool indentWidthSet = false; @@ -168,6 +172,17 @@ Options buildCommandLineOptions(const QCoreApplication &app) 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."))); @@ -198,6 +213,13 @@ Options buildCommandLineOptions(const QCoreApplication &app) parser.process(app); + if (parser.isSet(writeDefaultsOption)) { + Options options; + options.writeDefaultSettings = true; + options.valid = true; + return options; + } + bool indentWidthOkay = false; const int indentWidth = parser.value("indent-width").toInt(&indentWidthOkay); if (!indentWidthOkay) { @@ -229,6 +251,7 @@ Options buildCommandLineOptions(const QCoreApplication &app) options.force = parser.isSet("force"); options.tabs = parser.isSet("tabs"); options.normalize = parser.isSet("normalize"); + options.ignoreSettings = parser.isSet("ignore-settings"); options.valid = true; options.indentWidth = indentWidth; @@ -248,6 +271,20 @@ int main(int argc, char *argv[]) QCoreApplication::setApplicationName("qmlformat"); QCoreApplication::setApplicationVersion(QT_VERSION_STR); + QQmlToolingSettings settings(QLatin1String("qmlformat")); + + const QString &useTabsSetting = QStringLiteral("UseTabs"); + settings.addOption(useTabsSetting); + + const QString &indentWidthSetting = QStringLiteral("IndentWidth"); + settings.addOption(indentWidthSetting, 4); + + const QString &normalizeSetting = QStringLiteral("NormalizeOrder"); + settings.addOption(normalizeSetting); + + const QString &newlineSetting = QStringLiteral("NewlineType"); + settings.addOption(newlineSetting, QStringLiteral("native")); + const auto options = buildCommandLineOptions(app); if (!options.valid) { for (const auto &error : options.errors) { @@ -257,6 +294,32 @@ int main(int argc, char *argv[]) return -1; } + if (options.writeDefaultSettings) + return settings.writeDefaults() ? 0 : -1; + + auto getSettings = [&](const QString &file, Options options) { + if (options.ignoreSettings || !settings.search(file)) + return options; + + Options perFileOptions = options; + + // Allow for tab settings to be overwritten by the command line + if (!options.indentWidthSet) { + if (settings.isSet(indentWidthSetting)) + perFileOptions.indentWidth = settings.value(indentWidthSetting).toInt(); + if (settings.isSet(useTabsSetting)) + perFileOptions.tabs = settings.value(useTabsSetting).toBool(); + } + + if (settings.isSet(normalizeSetting)) + perFileOptions.normalize = settings.value(normalizeSetting).toBool(); + + if (settings.isSet(newlineSetting)) + perFileOptions.newline = settings.value(newlineSetting).toString(); + + return perFileOptions; + }; + bool success = true; if (!options.files.isEmpty()) { if (!options.arguments.isEmpty()) @@ -265,12 +328,12 @@ int main(int argc, char *argv[]) for (const QString &file : options.files) { Q_ASSERT(!file.isEmpty()); - if (!parseFile(file, options)) + if (!parseFile(file, getSettings(file, options))) success = false; } } else { for (const QString &file : options.arguments) { - if (!parseFile(file, options)) + if (!parseFile(file, getSettings(file, options))) success = false; } } |
