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 "qqmldomattachedinfo_p.h"
21#include "qqmldomlinewriter_p.h"
22
23#include <QtCore/QLoggingCategory>
24
25QT_BEGIN_NAMESPACE
26
27namespace QQmlJS {
28namespace Dom {
29
30class QMLDOM_EXPORT OutWriterState
31{
32public:
33 OutWriterState(const Path &itPath, const DomItem &it, const FileLocations::Tree &fLoc);
34
35 void closeState(OutWriter &);
36
37 Path itemCanonicalPath;
38 DomItem item;
39 PendingSourceLocationId fullRegionId;
40 FileLocations::Tree currentMap;
41 QMap<FileLocationRegion, PendingSourceLocationId> pendingRegions;
42 QMap<FileLocationRegion, CommentedElement> pendingComments;
43};
44
45class QMLDOM_EXPORT OutWriter
46{
47public:
48 int indent = 0;
49 int indenterId = -1;
50 bool indentNextlines = false;
51 bool skipComments = false;
52 LineWriter &lineWriter;
53 Path currentPath;
54 FileLocations::Tree topLocation;
55 QString writtenStr;
56 UpdatedScriptExpression::Tree reformattedScriptExpressions;
57 QList<OutWriterState> states;
58
59 explicit OutWriter(LineWriter &lw)
60 : lineWriter(lw),
61 topLocation(FileLocations::createTree(basePath: Path())),
62 reformattedScriptExpressions(UpdatedScriptExpression::createTree(basePath: Path()))
63 {
64 lineWriter.addInnerSink(s: [this](QStringView s) { writtenStr.append(v: s); });
65 indenterId =
66 lineWriter.addTextAddCallback(callback: [this](LineWriter &, LineWriter::TextAddType tt) {
67 if (indentNextlines && tt == LineWriter::TextAddType::Normal
68 && QStringView(lineWriter.currentLine()).trimmed().isEmpty())
69 lineWriter.setLineIndent(indent);
70 return true;
71 });
72 }
73
74 OutWriterState &state(int i = 0);
75
76 int increaseIndent(int level = 1)
77 {
78 int oldIndent = indent;
79 indent += lineWriter.options().formatOptions.indentSize * level;
80 return oldIndent;
81 }
82 int decreaseIndent(int level = 1, int expectedIndent = -1)
83 {
84 indent -= lineWriter.options().formatOptions.indentSize * level;
85 Q_ASSERT(expectedIndent < 0 || expectedIndent == indent);
86 return indent;
87 }
88
89 void itemStart(const DomItem &it);
90 void itemEnd(const DomItem &it);
91 void regionStart(FileLocationRegion region);
92 void regionEnd(FileLocationRegion regino);
93
94 quint32 counter() const { return lineWriter.counter(); }
95 OutWriter &writeRegion(FileLocationRegion region, QStringView toWrite);
96 OutWriter &writeRegion(FileLocationRegion region);
97 OutWriter &ensureNewline(int nNewlines = 1)
98 {
99 lineWriter.ensureNewline(nNewlines);
100 return *this;
101 }
102 OutWriter &ensureSpace()
103 {
104 lineWriter.ensureSpace();
105 return *this;
106 }
107 OutWriter &ensureSpace(QStringView space)
108 {
109 lineWriter.ensureSpace(space);
110 return *this;
111 }
112 OutWriter &newline()
113 {
114 lineWriter.newline();
115 return *this;
116 }
117 OutWriter &space()
118 {
119 lineWriter.space();
120 return *this;
121 }
122 OutWriter &write(QStringView v, LineWriter::TextAddType t = LineWriter::TextAddType::Normal)
123 {
124 lineWriter.write(v, tType: t);
125 return *this;
126 }
127 OutWriter &write(QStringView v, SourceLocation *toUpdate)
128 {
129 lineWriter.write(v, toUpdate);
130 return *this;
131 }
132 void flush() { lineWriter.flush(); }
133 void eof(bool ensureNewline = true) { lineWriter.eof(ensureNewline); }
134 int addNewlinesAutospacerCallback(int nLines)
135 {
136 return lineWriter.addNewlinesAutospacerCallback(nLines);
137 }
138 int addTextAddCallback(std::function<bool(LineWriter &, LineWriter::TextAddType)> callback)
139 {
140 return lineWriter.addTextAddCallback(callback);
141 }
142 bool removeTextAddCallback(int i) { return lineWriter.removeTextAddCallback(i); }
143 void addReformattedScriptExpression(const Path &p, const std::shared_ptr<ScriptExpression> &exp)
144 {
145 if (auto updExp = UpdatedScriptExpression::ensure(base: reformattedScriptExpressions, basePath: p,
146 pType: AttachedInfo::PathType::Canonical)) {
147 updExp->info().expr = exp;
148 }
149 }
150 DomItem restoreWrittenFileItem(const DomItem &fileItem);
151
152private:
153 DomItem writtenQmlFileItem(const DomItem &fileItem, const Path &filePath);
154 DomItem writtenJsFileItem(const DomItem &fileItem, const Path &filePath);
155 static void logScriptExprUpdateSkipped(
156 const DomItem &exprItem, const Path &exprPath,
157 const std::shared_ptr<ScriptExpression> &formattedExpr);
158};
159
160} // end namespace Dom
161} // end namespace QQmlJS
162
163QT_END_NAMESPACE
164#endif // QMLDOMOUTWRITER_P_H
165

Provided by KDAB

Privacy Policy
Learn Advanced QML with KDAB
Find out more

source code of qtdeclarative/src/qmldom/qqmldomoutwriter_p.h