Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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:
33 OutWriterState(const Path &itPath, const DomItem &it, const FileLocations::Tree &fLoc);
34
35 void closeState(OutWriter &);
36
41 QMap<FileLocationRegion, PendingSourceLocationId> pendingRegions;
42 QMap<FileLocationRegion, CommentedElement> pendingComments;
43};
44
46{
47public:
48 int indent = 0;
49 int indenterId = -1;
50 bool indentNextlines = false;
51 bool skipComments = false;
57 QList<OutWriterState> states;
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(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 }
103 {
104 lineWriter.ensureSpace();
105 return *this;
106 }
108 {
109 lineWriter.ensureSpace(space);
110 return *this;
111 }
113 {
114 lineWriter.newline();
115 return *this;
116 }
118 {
119 lineWriter.space();
120 return *this;
121 }
122 OutWriter &write(QStringView v, LineWriter::TextAddType t = LineWriter::TextAddType::Normal)
123 {
124 lineWriter.write(v, t);
125 return *this;
126 }
128 {
129 lineWriter.write(v, toUpdate);
130 return *this;
131 }
132 void flush() { lineWriter.flush(); }
133 void eof(bool ensureNewline = true) { lineWriter.eof(ensureNewline); }
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(reformattedScriptExpressions, p,
146 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
164#endif // QMLDOMOUTWRITER_P_H
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)
void addInnerSink(const SinkF &s)
int addNewlinesAutospacerCallback(int nLines)
void setLineIndent(int indentAmount)
int addTextAddCallback(std::function< bool(LineWriter &, TextAddType)> callback)
QMap< FileLocationRegion, PendingSourceLocationId > pendingRegions
PendingSourceLocationId fullRegionId
QMap< FileLocationRegion, CommentedElement > pendingComments
OutWriter & ensureNewline(int nNewlines=1)
int increaseIndent(int level=1)
void addReformattedScriptExpression(const Path &p, const std::shared_ptr< ScriptExpression > &exp)
QList< OutWriterState > states
OutWriter & write(QStringView v, SourceLocation *toUpdate)
int decreaseIndent(int level=1, int expectedIndent=-1)
OutWriter & ensureSpace(QStringView space)
void eof(bool ensureNewline=true)
UpdatedScriptExpression::Tree reformattedScriptExpressions
FileLocations::Tree topLocation
int addNewlinesAutospacerCallback(int nLines)
int addTextAddCallback(std::function< bool(LineWriter &, LineWriter::TextAddType)> callback)
OutWriter & write(QStringView v, LineWriter::TextAddType t=LineWriter::TextAddType::Normal)
std::shared_ptr< AttachedInfoT< UpdatedScriptExpression > > Tree
\inmodule QtCore
Definition qstringview.h:78
constexpr bool isEmpty() const noexcept
Returns whether this string view is empty - that is, whether {size() == 0}.
QStringView trimmed() const noexcept
Strips leading and trailing whitespace and returns the result.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QString & append(QChar c)
Definition qstring.cpp:3252
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 s
[6]
Definition qopenglext.h:235
GLdouble GLdouble t
Definition qopenglext.h:243
GLfloat GLfloat p
[1]
#define QMLDOM_EXPORT
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
unsigned int quint32
Definition qtypes.h:50