aboutsummaryrefslogtreecommitdiffstats
path: root/tools/qmlformat/qmlformat.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tools/qmlformat/qmlformat.cpp')
-rw-r--r--tools/qmlformat/qmlformat.cpp67
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;
}
}