| 1 | // Copyright (C) 2020 The Qt Company Ltd. |
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QMLDOMOUTWRITER_P_H |
| 5 | #define QMLDOMOUTWRITER_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include "qqmldom_global.h" |
| 19 | #include "qqmldom_fwd_p.h" |
| 20 | #include "qqmldomlinewriter_p.h" |
| 21 | #include "qqmldomcomments_p.h" |
| 22 | |
| 23 | #include <QtCore/QLoggingCategory> |
| 24 | |
| 25 | QT_BEGIN_NAMESPACE |
| 26 | |
| 27 | namespace QQmlJS { |
| 28 | namespace Dom { |
| 29 | |
| 30 | class QMLDOM_EXPORT OutWriter |
| 31 | { |
| 32 | public: |
| 33 | int indent = 0; |
| 34 | int indenterId = -1; |
| 35 | bool indentNextlines = false; |
| 36 | bool skipComments = false; |
| 37 | LineWriter &lineWriter; |
| 38 | Path currentPath; |
| 39 | QString writtenStr; |
| 40 | using RegionToCommentMap = QMap<FileLocationRegion, CommentedElement>; |
| 41 | QStack<RegionToCommentMap> pendingComments; |
| 42 | |
| 43 | explicit OutWriter(LineWriter &lw) : lineWriter(lw) |
| 44 | { |
| 45 | lineWriter.addInnerSink(s: [this](QStringView s) { writtenStr.append(v: s); }); |
| 46 | indenterId = |
| 47 | lineWriter.addTextAddCallback(callback: [this](LineWriter &, LineWriter::TextAddType tt) { |
| 48 | if (indentNextlines && tt == LineWriter::TextAddType::Normal |
| 49 | && QStringView(lineWriter.currentLine()).trimmed().isEmpty()) |
| 50 | lineWriter.setLineIndent(indent); |
| 51 | return true; |
| 52 | }); |
| 53 | } |
| 54 | |
| 55 | int increaseIndent(int level = 1) |
| 56 | { |
| 57 | int oldIndent = indent; |
| 58 | indent += lineWriter.options().formatOptions.indentSize * level; |
| 59 | return oldIndent; |
| 60 | } |
| 61 | int decreaseIndent(int level = 1, int expectedIndent = -1) |
| 62 | { |
| 63 | indent -= lineWriter.options().formatOptions.indentSize * level; |
| 64 | Q_ASSERT(expectedIndent < 0 || expectedIndent == indent); |
| 65 | return indent; |
| 66 | } |
| 67 | |
| 68 | void itemStart(const DomItem &it); |
| 69 | void itemEnd(); |
| 70 | void regionStart(FileLocationRegion region); |
| 71 | void regionEnd(FileLocationRegion regino); |
| 72 | |
| 73 | quint32 counter() const { return lineWriter.counter(); } |
| 74 | OutWriter &writeRegion(FileLocationRegion region, QStringView toWrite); |
| 75 | OutWriter &writeRegion(FileLocationRegion region); |
| 76 | OutWriter &ensureNewline(int nNewlines = 1) |
| 77 | { |
| 78 | lineWriter.ensureNewline(nNewlines); |
| 79 | return *this; |
| 80 | } |
| 81 | OutWriter &ensureSpace() |
| 82 | { |
| 83 | lineWriter.ensureSpace(); |
| 84 | return *this; |
| 85 | } |
| 86 | OutWriter &ensureSpace(QStringView space) |
| 87 | { |
| 88 | lineWriter.ensureSpace(space); |
| 89 | return *this; |
| 90 | } |
| 91 | OutWriter &newline() |
| 92 | { |
| 93 | lineWriter.newline(); |
| 94 | return *this; |
| 95 | } |
| 96 | OutWriter &write(QStringView v, LineWriter::TextAddType t = LineWriter::TextAddType::Normal) |
| 97 | { |
| 98 | lineWriter.write(v, tType: t); |
| 99 | return *this; |
| 100 | } |
| 101 | void flush() { lineWriter.flush(); } |
| 102 | void eof(bool ensureNewline = true) { lineWriter.eof(ensureNewline); } |
| 103 | int addNewlinesAutospacerCallback(int nLines) |
| 104 | { |
| 105 | return lineWriter.addNewlinesAutospacerCallback(nLines); |
| 106 | } |
| 107 | int addTextAddCallback(std::function<bool(LineWriter &, LineWriter::TextAddType)> callback) |
| 108 | { |
| 109 | return lineWriter.addTextAddCallback(callback); |
| 110 | } |
| 111 | bool removeTextAddCallback(int i) { return lineWriter.removeTextAddCallback(i); } |
| 112 | |
| 113 | }; |
| 114 | |
| 115 | } // end namespace Dom |
| 116 | } // end namespace QQmlJS |
| 117 | |
| 118 | QT_END_NAMESPACE |
| 119 | #endif // QMLDOMOUTWRITER_P_H |
| 120 |
