Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qqmldomoutwriter_p.h
Go to the documentation of this file.
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"
21#include "qqmldomlinewriter_p.h"
22
23#include <QtCore/QLoggingCategory>
24
26
27namespace QQmlJS {
28namespace Dom {
29
31{
32public:
34
35 void closeState(OutWriter &);
36
43};
44
46{
47public:
48 int indent = 0;
49 int indenterId = -1;
50 bool indentNextlines = false;
51 bool skipComments = false;
58
59 explicit OutWriter(LineWriter &lw)
60 : lineWriter(lw),
61 topLocation(FileLocations::createTree(Path())),
62 reformattedScriptExpressions(UpdatedScriptExpression::createTree(Path()))
63 {
64 lineWriter.addInnerSink([this](QStringView s) { writtenStr.append(s); });
65 indenterId =
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(DomItem &it);
90 void itemEnd(DomItem &it);
91 void regionStart(QString rName);
92 void regionStart(QStringView rName) { regionStart(rName.toString()); }
93 void regionEnd(QString rName);
94 void regionEnd(QStringView rName) { regionEnd(rName.toString()); }
95
96 quint32 counter() const { return lineWriter.counter(); }
97 OutWriter &writeRegion(QString rName, QStringView toWrite);
99 {
100 return writeRegion(rName.toString(), toWrite);
101 }
103 OutWriter &writeRegion(QStringView t) { return writeRegion(t.toString(), t); }
104 OutWriter &ensureNewline(int nNewlines = 1)
105 {
106 lineWriter.ensureNewline(nNewlines);
107 return *this;
108 }
110 {
111 lineWriter.ensureSpace();
112 return *this;
113 }
115 {
116 lineWriter.ensureSpace(space);
117 return *this;
118 }
120 {
121 lineWriter.newline();
122 return *this;
123 }
125 {
126 lineWriter.space();
127 return *this;
128 }
129 OutWriter &write(QStringView v, LineWriter::TextAddType t = LineWriter::TextAddType::Normal)
130 {
131 lineWriter.write(v, t);
132 return *this;
133 }
135 {
136 lineWriter.write(v, toUpdate);
137 return *this;
138 }
139 void flush() { lineWriter.flush(); }
140 void eof(bool ensureNewline = true) { lineWriter.eof(ensureNewline); }
142 {
143 return lineWriter.addNewlinesAutospacerCallback(nLines);
144 }
145 int addTextAddCallback(std::function<bool(LineWriter &, LineWriter::TextAddType)> callback)
146 {
147 return lineWriter.addTextAddCallback(callback);
148 }
149 bool removeTextAddCallback(int i) { return lineWriter.removeTextAddCallback(i); }
150 void addReformattedScriptExpression(Path p, std::shared_ptr<ScriptExpression> exp)
151 {
152 if (auto updExp = UpdatedScriptExpression::ensure(reformattedScriptExpressions, p,
153 AttachedInfo::PathType::Canonical)) {
154 updExp->info().expr = exp;
155 }
156 }
157 DomItem updatedFile(DomItem &qmlFile);
158};
159
160} // end namespace Dom
161} // end namespace QQmlJS
162
164#endif // QMLDOMOUTWRITER_P_H
\inmodule QtCore
Definition qatomic.h:112
Definition qlist.h:74
Definition qmap.h:186
Represents and maintains a mapping between elements and their location in a file.
std::shared_ptr< AttachedInfoT< FileLocations > > Tree
const QString & currentLine() const
LineWriter & write(QStringView v, TextAddType tType=TextAddType::Normal)
LineWriter & ensureNewline(int nNewlines=1, TextAddType t=TextAddType::Extra)
LineWriter & ensureSpace(TextAddType t=TextAddType::Extra)
const LineWriterOptions & options() const
void eof(bool ensureNewline=true)
int addNewlinesAutospacerCallback(int nLines)
void setLineIndent(int indentAmount)
int addTextAddCallback(std::function< bool(LineWriter &, TextAddType)> callback)
QMap< QString, PendingSourceLocationId > pendingRegions
PendingSourceLocationId fullRegionId
QMap< QString, CommentedElement > pendingComments
OutWriter & ensureNewline(int nNewlines=1)
int increaseIndent(int level=1)
OutWriter & writeRegion(QString t)
QList< OutWriterState > states
OutWriter & writeRegion(QStringView rName, QStringView toWrite)
OutWriter & write(QStringView v, SourceLocation *toUpdate)
OutWriter & writeRegion(QStringView t)
int decreaseIndent(int level=1, int expectedIndent=-1)
OutWriter & ensureSpace(QStringView space)
void eof(bool ensureNewline=true)
UpdatedScriptExpression::Tree reformattedScriptExpressions
void regionStart(QStringView rName)
void regionEnd(QStringView rName)
FileLocations::Tree topLocation
int addNewlinesAutospacerCallback(int nLines)
int addTextAddCallback(std::function< bool(LineWriter &, LineWriter::TextAddType)> callback)
void addReformattedScriptExpression(Path p, std::shared_ptr< ScriptExpression > exp)
OutWriter & write(QStringView v, LineWriter::TextAddType t=LineWriter::TextAddType::Normal)
std::shared_ptr< AttachedInfoT< UpdatedScriptExpression > > Tree
\inmodule QtCore
Definition qstringview.h:76
constexpr bool isEmpty() const noexcept
Returns whether this string view is empty - that is, whether {size() == 0}.
QString toString() const
Returns a deep copy of this string view's data as a QString.
Definition qstring.h:1014
QStringView trimmed() const noexcept
Strips leading and trailing whitespace and returns the result.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
QString & append(QChar c)
Definition qstring.cpp:3227
QSet< QString >::iterator it
else opt state
[0]
Combined button and popup list for selecting options.
GLsizei const GLfloat * v
[13]
GLenum GLuint GLint level
GLdouble GLdouble t
Definition qopenglext.h:243
GLdouble s
[6]
Definition qopenglext.h:235
GLfloat GLfloat p
[1]
#define QMLDOM_EXPORT
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
unsigned int quint32
Definition qtypes.h:45