diff options
author | Semih Yavuz <[email protected]> | 2024-11-08 16:57:22 +0100 |
---|---|---|
committer | Semih Yavuz <[email protected]> | 2024-12-05 23:24:30 +0100 |
commit | 3bb4962254498504c135ca0243c0be5bdf6defad (patch) | |
tree | 2a721b800dea110cfb0f564b0ddff882a7ad3493 | |
parent | 9f1fca90e7b760f54e5fb5689e662defb669c64b (diff) |
qmlformat: use indenting line writer if column break is in use
Task-number: QTBUG-113590
Change-Id: I24e1d8df81736b63d8b2eb25aa8625b9e22b211a
Reviewed-by: Fabian Kosmale <[email protected]>
-rw-r--r-- | src/qmldom/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/qmldom/qqmldomitem.cpp | 5 | ||||
-rw-r--r-- | src/qmldom/qqmldomlinewriterfactory_p.h | 39 | ||||
-rw-r--r-- | src/qmlformat/qqmlformatoptions_p.h | 1 | ||||
-rw-r--r-- | tools/qmlformat/qmlformat.cpp | 11 |
5 files changed, 50 insertions, 7 deletions
diff --git a/src/qmldom/CMakeLists.txt b/src/qmldom/CMakeLists.txt index eb7664fae2..a1c506f85a 100644 --- a/src/qmldom/CMakeLists.txt +++ b/src/qmldom/CMakeLists.txt @@ -41,6 +41,7 @@ qt_internal_add_module(QmlDomPrivate qqmldomtop.cpp qqmldomtop_p.h qqmldomtypesreader.cpp qqmldomtypesreader_p.h qqmldomscriptelements_p.h qqmldomscriptelements.cpp + qqmldomlinewriterfactory_p.h DEFINES QMLDOM_LIBRARY PUBLIC_LIBRARIES diff --git a/src/qmldom/qqmldomitem.cpp b/src/qmldom/qqmldomitem.cpp index 7f6be8911c..127e7d95fd 100644 --- a/src/qmldom/qqmldomitem.cpp +++ b/src/qmldom/qqmldomitem.cpp @@ -17,6 +17,7 @@ #include "qqmldomlinewriter_p.h" #include "qqmldom_utils_p.h" #include "qqmldomscriptelements_p.h" +#include <qqmldomlinewriterfactory_p.h> #include <QtQml/private/qqmljslexer_p.h> #include <QtQml/private/qqmljsparser_p.h> @@ -1338,8 +1339,8 @@ bool DomItem::writeOut(const QString &path, int nBackups, const LineWriterOption auto status = fw->write( path, [this, path, &options, extraChecks](QTextStream &ts) { - LineWriter lw([&ts](QStringView s) { ts << s; }, path, options); - OutWriter ow(lw); + auto lw = createLineWriter([&ts](QStringView s) { ts << s; }, path, options); + OutWriter ow(*lw); return writeOutForFile(ow, extraChecks); }, nBackups); diff --git a/src/qmldom/qqmldomlinewriterfactory_p.h b/src/qmldom/qqmldomlinewriterfactory_p.h new file mode 100644 index 0000000000..ddac3523b7 --- /dev/null +++ b/src/qmldom/qqmldomlinewriterfactory_p.h @@ -0,0 +1,39 @@ +// Copyright (C) 2024 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#ifndef QQMLDOMLINEWRITERFACTORY_P_H +#define QQMLDOMLINEWRITERFACTORY_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include "qqmldomlinewriter_p.h" +#include "qqmldomindentinglinewriter_p.h" + +QT_BEGIN_NAMESPACE + +namespace QQmlJS { +namespace Dom { + +inline std::unique_ptr<LineWriter> createLineWriter(const SinkF &innerSink, const QString &fileName, + const LineWriterOptions &options) +{ + return options.maxLineLength > 0 + ? std::make_unique<IndentingLineWriter>(innerSink, fileName, options) + : std::make_unique<LineWriter>(innerSink, fileName, options); +} + +} // namespace Dom +} // namespace QQmlJS + +QT_END_NAMESPACE + +#endif // QQMLDOMLINEWRITERFACTORY_P_H diff --git a/src/qmlformat/qqmlformatoptions_p.h b/src/qmlformat/qqmlformatoptions_p.h index 8f8960bbf2..ea66de5d7f 100644 --- a/src/qmlformat/qqmlformatoptions_p.h +++ b/src/qmlformat/qqmlformatoptions_p.h @@ -78,6 +78,7 @@ public: int maxColumnWidth() const { return m_options.maxLineLength; } void setMaxColumnWidth(int width) { m_options.maxLineLength = width; } + bool isMaxColumnWidthSet() const { return m_options.maxLineLength > 0; } QQmlJS::Dom::LineWriterOptions optionsForCode(const QString &code) const { diff --git a/tools/qmlformat/qmlformat.cpp b/tools/qmlformat/qmlformat.cpp index 82cca36ab1..e960072322 100644 --- a/tools/qmlformat/qmlformat.cpp +++ b/tools/qmlformat/qmlformat.cpp @@ -14,6 +14,7 @@ #include <QtQmlDom/private/qqmldomexternalitems_p.h> #include <QtQmlDom/private/qqmldomtop_p.h> #include <QtQmlDom/private/qqmldomoutwriter_p.h> +#include <QtQmlDom/private/qqmldomlinewriterfactory_p.h> #if QT_CONFIG(commandlineparser) # include <QCommandLineParser> @@ -92,9 +93,8 @@ static bool parseFile(const QString &filename, const QQmlFormatOptions &options) auto lwOptions = options.optionsForCode(code); WriteOutChecks checks = WriteOutCheck::Default; //Disable writeOutChecks for some usecases - if (options.forceEnabled() || - code.size() > 32000 || - fileItem.internalKind() == DomType::JsFile) { + if (options.forceEnabled() || options.isMaxColumnWidthSet() || code.size() > 32000 + || fileItem.internalKind() == DomType::JsFile) { checks = WriteOutCheck::None; } @@ -108,8 +108,9 @@ static bool parseFile(const QString &filename, const QQmlFormatOptions &options) } else { QFile out; if (out.open(stdout, QIODevice::WriteOnly)) { - LineWriter lw([&out](QStringView s) { out.write(s.toUtf8()); }, filename, lwOptions); - OutWriter ow(lw); + auto lw = createLineWriter([&out](QStringView s) { out.write(s.toUtf8()); }, filename, + lwOptions); + OutWriter ow(*lw); res = fileItem.writeOutForFile(ow, checks); ow.flush(); } else { |