diff options
author | Fawzi Mohamed <fawzi.mohamed@qt.io> | 2021-03-23 15:37:32 +0100 |
---|---|---|
committer | Fawzi Mohamed <fawzi.mohamed@qt.io> | 2021-06-05 00:07:48 +0200 |
commit | de3d65009adf3c7ce0be6da77ee74b0a2610c9c5 (patch) | |
tree | b9c03fc2b5e2c7845c4b4070c403eae66e5df3ef /tools | |
parent | 6cf15ad4e359223bb19c997f0dd279f9aea842d7 (diff) |
qmldom: writeOut, write reformatted Qml
Adding writeOut: support for reformatted Qml
- linewriter: write line by line with caching, callbacks,
SourceLoaction updating
- outwriter: write to line writer, and keep track of updated file
locations and reformatted ScriptExpressions
- reformatter: reformat javascript
Change-Id: I4bdc393fb2d9b5a3db944a850719c24ef8726d15
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/qmldom/qmldomtool.cpp | 51 |
1 files changed, 50 insertions, 1 deletions
diff --git a/tools/qmldom/qmldomtool.cpp b/tools/qmldom/qmldomtool.cpp index 760d43222e..1ab9dedc6a 100644 --- a/tools/qmldom/qmldomtool.cpp +++ b/tools/qmldom/qmldomtool.cpp @@ -36,6 +36,7 @@ #include <QtQmlDom/private/qqmldomtop_p.h> #include <QtQmlDom/private/qqmldomfilewriter_p.h> +#include <QtQmlDom/private/qqmldomoutwriter_p.h> #include <QtQmlDom/private/qqmldomelements_p.h> #include <QtQmlDom/private/qqmldomfieldfilter_p.h> @@ -75,6 +76,10 @@ int main(int argc, char *argv[]) << "dump", QLatin1String("Dumps the code model")); parser.addOption(dumpOption); + QCommandLineOption reformatOption(QStringList() << "r" + << "reformat", + QLatin1String("reformats the files explicitly passed in")); + parser.addOption(reformatOption); QCommandLineOption filterOption( QStringList() << "f" @@ -106,6 +111,14 @@ int main(int argc, char *argv[]) QLatin1String("pathToDump")); parser.addOption(pathToDumpOption); + QCommandLineOption reformatDirOption( + QStringList() << "reformat-dir", + QLatin1String( + "Target directory for the reformatted files, " + "if not given the files are reformatted in place (but backup files are kept)"), + QLatin1String("reformatDir")); + parser.addOption(reformatDirOption); + QCommandLineOption nBackupsOption( QStringList() << "backups", QLatin1String("Number of backup files to generate (default is 2, the oldest, " @@ -195,7 +208,43 @@ int main(int argc, char *argv[]) env.loadFile(s, QString(), nullptr, LoadOption::DefaultLoad); } envPtr->loadPendingDependencies(env); - { + if (parser.isSet(reformatOption)) { + for (auto s : positionalArguments) { + DomItem qmlFile = env.path(Paths::qmldirFilePath(s)); + if (qmlFile) { + qDebug() << "reformatting" << s; + FileWriter fw; + QString target = s; + QString rDir = parser.value(reformatDirOption); + if (!rDir.isEmpty()) { + QFileInfo f(s); + QDir d(rDir); + target = d.filePath(f.fileName()); + } + switch (fw.write( + target, + [&qmlFile, target](QTextStream &ts) { + LineWriter lw([&ts](QStringView s) { ts << s; }, target); + OutWriter ow(lw); + qmlFile.writeOut(ow); + ow.eof(); + return true; + }, + nBackups)) { + case FileWriter::Status::ShouldWrite: + case FileWriter::Status::SkippedDueToFailure: + qWarning() << "failure reformatting " << s; + break; + case FileWriter::Status::DidWrite: + qDebug() << "success"; + break; + case FileWriter::Status::SkippedEqual: + qDebug() << "no change"; + } + } + } + } + if (parser.isSet(dumpOption) || !parser.isSet(reformatOption)) { qDebug() << "will dump\n"; QTextStream ts(stdout); auto sink = [&ts](QStringView v) { |