aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorMaximilian Goldstein <max.goldstein@qt.io>2020-09-02 12:34:29 +0200
committerMaximilian Goldstein <max.goldstein@qt.io>2020-11-24 15:05:13 +0100
commitdb0b7cfcb2923aa4f80ed4d2c2e4d2052d52f96a (patch)
tree477790e5ba9069123baaab4c7b63a5234fb05604 /tools
parentd2d8e90e9f218103d60737e1273ab5322834d9ec (diff)
qmlformat: Add indent options
Adds the ability to use tabs or any amount of spaces for indentation instead of the default of 4 spaces. [ChangeLog][QML][qmlformat] Added option to customize indentation. Fixes: QTBUG-86413 Change-Id: I3c370dda2d0069ef61202a2d862ce15bc513e55e Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tools')
-rw-r--r--tools/qmlformat/dumpastvisitor.cpp11
-rw-r--r--tools/qmlformat/dumpastvisitor.h8
-rw-r--r--tools/qmlformat/main.cpp34
3 files changed, 41 insertions, 12 deletions
diff --git a/tools/qmlformat/dumpastvisitor.cpp b/tools/qmlformat/dumpastvisitor.cpp
index 24286ba69b..771814ffd5 100644
--- a/tools/qmlformat/dumpastvisitor.cpp
+++ b/tools/qmlformat/dumpastvisitor.cpp
@@ -30,8 +30,9 @@
#include <QtQml/private/qqmljslexer_p.h>
-DumpAstVisitor::DumpAstVisitor(QQmlJS::Engine *engine, Node *rootNode, CommentAstVisitor *comment)
- : m_engine(engine), m_comment(comment)
+DumpAstVisitor::DumpAstVisitor(QQmlJS::Engine *engine, Node *rootNode, CommentAstVisitor *comment,
+ int indentWidth, DumpAstVisitor::Indentation indentation)
+ : m_engine(engine), m_comment(comment), m_indentWidth(indentWidth), m_indentation(indentation)
{
// Add all completely orphaned comments
m_result += getOrphanedComments(nullptr);
@@ -992,11 +993,9 @@ bool DumpAstVisitor::visit(UiPublicMember *node) {
return true;
}
-static QString generateIndent(int indentLevel)
+QString DumpAstVisitor::generateIndent(int indentLevel) const
{
- constexpr int IDENT_WIDTH = 4;
-
- return QString(IDENT_WIDTH * indentLevel, ' ');
+ return QString(m_indentWidth * indentLevel, m_indentation == Indentation::Tabs ? '\t' : ' ');
}
QString DumpAstVisitor::formatLine(QString line, bool newline) const
diff --git a/tools/qmlformat/dumpastvisitor.h b/tools/qmlformat/dumpastvisitor.h
index 729e15702a..657592f403 100644
--- a/tools/qmlformat/dumpastvisitor.h
+++ b/tools/qmlformat/dumpastvisitor.h
@@ -43,7 +43,10 @@ using namespace QQmlJS;
class DumpAstVisitor : protected Visitor
{
public:
- DumpAstVisitor(QQmlJS::Engine *engine, Node *rootNode, CommentAstVisitor *comment);
+ enum Indentation { Tabs, Spaces };
+
+ DumpAstVisitor(QQmlJS::Engine *engine, Node *rootNode, CommentAstVisitor *comment,
+ int indentWidth, Indentation indentation);
QString toString() const { return m_result; }
@@ -94,6 +97,7 @@ private:
QHash<QString, UiObjectMember*> m_bindings;
};
+ QString generateIndent(int indentLevel) const;
QString formatLine(QString line, bool newline = true) const;
QString formatPartlyFormatedLines(QString line, bool newline = true) const;
@@ -150,6 +154,8 @@ private:
QString m_component_name = "";
QQmlJS::Engine *m_engine;
CommentAstVisitor *m_comment;
+ int m_indentWidth;
+ Indentation m_indentation;
};
#endif // DUMPAST_H
diff --git a/tools/qmlformat/main.cpp b/tools/qmlformat/main.cpp
index f9afd10a2c..a021971710 100644
--- a/tools/qmlformat/main.cpp
+++ b/tools/qmlformat/main.cpp
@@ -44,7 +44,8 @@
#include "dumpastvisitor.h"
#include "restructureastvisitor.h"
-bool parseFile(const QString& filename, bool inplace, bool verbose, bool sortImports, bool force, const QString& newline)
+bool parseFile(const QString &filename, bool inplace, bool verbose, bool sortImports, bool force,
+ int indentWidth, bool tabs, const QString &newline)
{
QFile file(filename);
@@ -100,7 +101,9 @@ bool parseFile(const QString& filename, bool inplace, bool verbose, bool sortImp
if (verbose)
qWarning().noquote() << "Dumping" << filename;
- DumpAstVisitor dump(&engine, parser.rootNode(), &comment);
+ DumpAstVisitor dump(&engine, parser.rootNode(), &comment, tabs ? 1 : indentWidth,
+ tabs ? DumpAstVisitor::Indentation::Tabs
+ : DumpAstVisitor::Indentation::Spaces);
QString dumpCode = dump.toString();
@@ -191,6 +194,13 @@ int main(int argc, char *argv[])
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"));
+
parser.addOption(QCommandLineOption(
{ "F", "files" }, QStringLiteral("Format all files listed in file, in-place"), "file"));
@@ -212,6 +222,19 @@ int main(int argc, char *argv[])
return -1;
}
+ if (parser.isSet("indent-width") && parser.isSet("tabs")) {
+ qWarning() << "Error: Cannot use --indent-width with --tabs";
+ return -1;
+ }
+
+ bool indentWidthOkay = false;
+ int indentWidth = parser.value("indent-width").toInt(&indentWidthOkay);
+
+ if (!indentWidthOkay) {
+ qWarning() << "Error: Invalid value passed to -w";
+ return -1;
+ }
+
if (parser.isSet("files")) {
if (!positionalArguments.isEmpty())
qWarning() << "Warning: Positional arguments are ignored when -F is used";
@@ -226,14 +249,15 @@ int main(int argc, char *argv[])
continue;
if (!parseFile(file, true, parser.isSet("verbose"), !parser.isSet("no-sort"),
- parser.isSet("force"), parser.value("newline")))
+ parser.isSet("force"), indentWidth, parser.isSet("tabs"),
+ parser.value("newline")))
success = false;
}
} else {
for (const QString &file : parser.positionalArguments()) {
if (!parseFile(file, parser.isSet("inplace"), parser.isSet("verbose"),
- !parser.isSet("no-sort"), parser.isSet("force"),
- parser.value("newline")))
+ !parser.isSet("no-sort"), parser.isSet("force"), indentWidth,
+ parser.isSet("tabs"), parser.value("newline")))
success = false;
}
}