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
qqmldomreformatter_p.h
Go to the documentation of this file.
1// Copyright (C) 2021 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 QQMLDOMREFORMATTER_P
5#define QQMLDOMREFORMATTER_P
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
20#include "qqmldomoutwriter_p.h"
21#include "qqmldom_fwd_p.h"
22#include "qqmldomcomments_p.h"
23
24#include <QtQml/private/qqmljsast_p.h>
25
27namespace QQmlJS {
28namespace Dom {
29
30class ScriptFormatter final : protected AST::JSVisitor
31{
32public:
33 // TODO QTBUG-121988
34 ScriptFormatter(OutWriter &lw, const std::shared_ptr<AstComments> &comments,
35 const std::function<QStringView(SourceLocation)> &loc2Str, AST::Node *node)
36 : lw(lw), comments(comments), loc2Str(loc2Str)
37 {
38 accept(node);
39 }
40
41protected:
42 inline void out(const char *str) { lw.write(QString::fromLatin1(str)); }
43 inline void out(QStringView str) { lw.write(str); }
44 inline void out(const SourceLocation &loc)
45 {
46 if (loc.length != 0)
47 out(loc2Str(loc));
48 }
49 inline void newLine(quint32 count = 1) { lw.ensureNewline(count); }
50
51 inline void accept(AST::Node *node) { AST::Node::accept(node, this); }
52 void lnAcceptIndented(AST::Node *node);
53 bool acceptBlockOrIndented(AST::Node *ast, bool finishWithSpaceOrNewline = false);
54
55 bool preVisit(AST::Node *n) override;
56 void postVisit(AST::Node *n) override;
57
58 bool visit(AST::ThisExpression *ast) override;
59 bool visit(AST::NullExpression *ast) override;
60 bool visit(AST::TrueLiteral *ast) override;
61 bool visit(AST::FalseLiteral *ast) override;
62 bool visit(AST::IdentifierExpression *ast) override;
63 bool visit(AST::StringLiteral *ast) override;
64 bool visit(AST::NumericLiteral *ast) override;
65 bool visit(AST::RegExpLiteral *ast) override;
66
67 bool visit(AST::ArrayPattern *ast) override;
68
69 bool visit(AST::ObjectPattern *ast) override;
70
71 bool visit(AST::PatternElementList *ast) override;
72
73 bool visit(AST::PatternPropertyList *ast) override;
74 bool visit(AST::PatternProperty *property) override;
75
76 bool visit(AST::NestedExpression *ast) override;
77 bool visit(AST::IdentifierPropertyName *ast) override;
78 bool visit(AST::StringLiteralPropertyName *ast) override;
79 bool visit(AST::NumericLiteralPropertyName *ast) override;
80
81 bool visit(AST::TemplateLiteral *ast) override;
82 bool visit(AST::ArrayMemberExpression *ast) override;
83
84 bool visit(AST::FieldMemberExpression *ast) override;
85
86 bool visit(AST::NewMemberExpression *ast) override;
87
88 bool visit(AST::NewExpression *ast) override;
89
90 bool visit(AST::CallExpression *ast) override;
91
92 bool visit(AST::PostIncrementExpression *ast) override;
93
94 bool visit(AST::PostDecrementExpression *ast) override;
95 bool visit(AST::PreIncrementExpression *ast) override;
96
97 bool visit(AST::PreDecrementExpression *ast) override;
98
99 bool visit(AST::DeleteExpression *ast) override;
100
101 bool visit(AST::VoidExpression *ast) override;
102 bool visit(AST::TypeOfExpression *ast) override;
103
104 bool visit(AST::UnaryPlusExpression *ast) override;
105
106 bool visit(AST::UnaryMinusExpression *ast) override;
107
108 bool visit(AST::TildeExpression *ast) override;
109
110 bool visit(AST::NotExpression *ast) override;
111
112 bool visit(AST::BinaryExpression *ast) override;
113
114 bool visit(AST::ConditionalExpression *ast) override;
115
116 bool visit(AST::Block *ast) override;
117
118 bool visit(AST::VariableStatement *ast) override;
119
120 bool visit(AST::PatternElement *ast) override;
121
122 bool visit(AST::EmptyStatement *ast) override;
123
124 bool visit(AST::IfStatement *ast) override;
125 bool visit(AST::DoWhileStatement *ast) override;
126
127 bool visit(AST::WhileStatement *ast) override;
128
129 bool visit(AST::ForStatement *ast) override;
130
131 bool visit(AST::ForEachStatement *ast) override;
132
133 bool visit(AST::ContinueStatement *ast) override;
134 bool visit(AST::BreakStatement *ast) override;
135
136 bool visit(AST::ReturnStatement *ast) override;
137 bool visit(AST::ThrowStatement *ast) override;
138 bool visit(AST::WithStatement *ast) override;
139
140 bool visit(AST::SwitchStatement *ast) override;
141
142 bool visit(AST::CaseBlock *ast) override;
143
144 bool visit(AST::CaseClause *ast) override;
145
146 bool visit(AST::DefaultClause *ast) override;
147
148 bool visit(AST::LabelledStatement *ast) override;
149
150 bool visit(AST::TryStatement *ast) override;
151
152 bool visit(AST::Catch *ast) override;
153
154 bool visit(AST::Finally *ast) override;
155
156 bool visit(AST::FunctionDeclaration *ast) override;
157
158 bool visit(AST::FunctionExpression *ast) override;
159
160 bool visit(AST::Elision *ast) override;
161
162 bool visit(AST::ArgumentList *ast) override;
163
164 bool visit(AST::StatementList *ast) override;
165
166 bool visit(AST::VariableDeclarationList *ast) override;
167
168 bool visit(AST::CaseClauses *ast) override;
169
170 bool visit(AST::FormalParameterList *ast) override;
171
172 bool visit(AST::SuperLiteral *) override;
173 bool visit(AST::ComputedPropertyName *) override;
174 bool visit(AST::Expression *el) override;
175 bool visit(AST::ExpressionStatement *el) override;
176
177 bool visit(AST::ClassDeclaration *ast) override;
178
179 bool visit(AST::ImportDeclaration *ast) override;
180 bool visit(AST::ImportSpecifier *ast) override;
181 bool visit(AST::NameSpaceImport *ast) override;
182 bool visit(AST::ImportsList *ast) override;
183 bool visit(AST::NamedImports *ast) override;
184 bool visit(AST::ImportClause *ast) override;
185
186 bool visit(AST::ExportDeclaration *ast) override;
187 bool visit(AST::ExportClause *ast) override;
188 bool visit(AST::ExportSpecifier *ast) override;
189 bool visit(AST::ExportsList *ast) override;
190
191 bool visit(AST::FromClause *ast) override;
192
193 void endVisit(AST::ComputedPropertyName *) override;
194
195 void endVisit(AST::ExportDeclaration *ast) override;
196 void endVisit(AST::ExportClause *ast) override;
197
198 void endVisit(AST::ImportDeclaration *ast) override;
199 void endVisit(AST::NamedImports *ast) override;
200
201 void throwRecursionDepthError() override;
202
203private:
204 bool addSemicolons() const { return expressionDepth > 0; }
205
206 OutWriter &lw;
207 std::shared_ptr<AstComments> comments;
208 std::function<QStringView(SourceLocation)> loc2Str;
209 QHash<AST::Node *, QList<std::function<void()>>> postOps;
210 int expressionDepth = 0;
211};
212
214 OutWriter &lw, const std::shared_ptr<AstComments> &comments,
215 const std::function<QStringView(SourceLocation)> &loc2Str, AST::Node *n);
216
217} // namespace Dom
218} // namespace QQmlJS
220
221#endif // QQMLDOMREFORMATTER_P
\inmodule QtCore
Definition qhash.h:820
Definition qlist.h:75
void accept(BaseVisitor *visitor)
OutWriter & ensureNewline(int nNewlines=1)
OutWriter & write(QStringView v, LineWriter::TextAddType t=LineWriter::TextAddType::Normal)
void lnAcceptIndented(AST::Node *node)
ScriptFormatter(OutWriter &lw, const std::shared_ptr< AstComments > &comments, const std::function< QStringView(SourceLocation)> &loc2Str, AST::Node *node)
bool visit(AST::ThisExpression *ast) override
bool preVisit(AST::Node *n) override
void out(const SourceLocation &loc)
void endVisit(AST::ComputedPropertyName *) override
bool acceptBlockOrIndented(AST::Node *ast, bool finishWithSpaceOrNewline=false)
void postVisit(AST::Node *n) override
\inmodule QtCore
Definition qstringview.h:78
static QString fromLatin1(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5871
QString str
[2]
void reformatAst(OutWriter &lw, const std::shared_ptr< AstComments > &comments, const std::function< QStringView(SourceLocation)> &loc2Str, AST::Node *n)
Combined button and popup list for selecting options.
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage void
GLenum GLenum GLsizei count
GLfloat n
#define QMLDOM_EXPORT
unsigned int quint32
Definition qtypes.h:50
const char property[13]
Definition qwizard.cpp:101
QTextStream out(stdout)
[7]
QStringView el