Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtqml-tooling-qmlformat.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2023 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4
/*!
5
\page qtqml-tooling-qmlformat.html
6
\title qmlformat
7
\brief A tool that automatically formats QML files according to QML coding convention.
8
\ingroup qtqml-tooling
9
\ingroup qtqml-tooling-devtools
10
11
\section1 qmlformat
12
\e qmlformat is a tool that automatically formats QML files in accordance
13
with the \l{QML Coding Conventions}.
14
15
\table
16
\header
17
\li Usage:
18
\row
19
\li qmlformat [\l{options}] \l{arguments}
20
\endtable
21
22
\section2 Options and settings
23
\target options
24
qmlformat can be configured via command line options. There are two groups of options: Those which
25
are directly related to formatting, and those which control the behavior of the tool.
26
27
28
The following options only affect the tool behavior:
29
\table
30
\header
31
\li Command Line Option
32
\li Description
33
\row
34
\li \c{-h}, \c{--help}
35
\li Displays help on commandline options.
36
\row
37
\li \c{--help-all}
38
\li Displays help, including generic Qt options.
39
40
\row
41
\li \c{-v}, \c{--version}
42
\li Displays version information.
43
\row
44
\li \c{-V}, \c{--verbose}
45
\li Verbose mode. Outputs more detailed information.
46
\row
47
\li \c{--write-defaults}
48
\li Writes defaults settings to .qmlformat.ini and exits
49
(Warning: This will overwrite any existing settings and comments!)
50
\row
51
\li \c{--output-options}
52
\li
53
\li Output all available options, their default value and a hint of values or types
54
\row
55
\li \c{--ignore-settings}
56
\li Ignores all settings files and only takes command line options into consideration
57
\row
58
\li \c{-i}, \c{--inplace}
59
\li Edit file in-place instead of outputting to stdout.
60
\row
61
\li \c{-f}, \c{--force}
62
\li Continue even if an error has occurred.
63
\row
64
\li \c{-F}, \c{--files <file>}
65
\li Format all files listed in file, in-place
66
\endtable
67
68
The next group of options controls how files should be formatted, and can additionally also be
69
controlled via a \l{Settings File}{settings file}.:
70
\table
71
\header
72
\li Command Line Option
73
\li Setting Name
74
\li Default State/Value
75
\li Description
76
\row
77
\li \c{-t}, \c{--tabs}
78
\li UseTabs
79
\li disabled/false
80
\li Use tabs instead of spaces.
81
82
In a command line invocation, the behavior can be enabled by passing the flag.
83
84
In a settings file, the behavior can be enabled by setting the
85
relevant variable to "true".
86
\row
87
\li \c{-w}, \c{--indent-width <width>}
88
\li IndentWidth
89
\li 4
90
\li How many spaces are used when indenting.
91
\row
92
\li \c{-W}, \c{--column-width <width>}
93
\li MaxColumnWidth
94
\li -1
95
\li Breaks the line into multiple lines if it exceeds
96
the specified width. Use -1 to disable line
97
wrapping. (default).
98
\row
99
\li \c{-n}, \c{--normalize}
100
\li NormalizeOrder
101
\li disabled/false
102
\li Reorders and sorts the attributes of the objects according to the QML Coding Guidelines.
103
Incompatible with \c{--group-attributes-together}.
104
105
In a command line invocation, the behavior can be enabled by passing the flag.
106
107
In a settings file, the behavior can be enabled by setting the
108
relevant variable to "true".
109
\row
110
\li \c{-l}, \c{--newline <newline>}
111
\li NewlineType
112
\li native
113
\li Overrides the new line format to use (\c {native}, \c {macos}, \c {unix}, \c {windows}).
114
\row
115
\li \c{-S}, \c{--sort-imports}
116
\li SortImports
117
\li disabled/false
118
\li Sort imports alphabetically
119
(Warning: this might change semantics if a given name identifies types in multiple modules!).
120
121
In a command line invocation, the behavior can be enabled by passing the flag.
122
123
In a settings file, the behavior can be enabled by setting the
124
relevant variable to "true".
125
\row
126
\li \c{--objects-spacing}
127
\li ObjectsSpacing
128
\li disabled/false
129
\li Ensure spaces between objects (only works with \c{normalize} or \c{group-attributes-together}).
130
131
In a command line invocation, the behavior can be enabled by passing the flag.
132
133
In a settings file, the behavior can be enabled by setting the
134
relevant variable to "true".
135
\row
136
\li \c{--functions-spacing}
137
\li FunctionsSpacing
138
\li disabled/false
139
\li Ensure spaces between functions (only works with \c{normalize} or \c{group-attributes-together}).
140
141
In a command line invocation, the behavior can be enabled by passing the flag.
142
143
In a settings file, the behavior can be enabled by setting the
144
relevant variable to "true".
145
\row
146
\li \c{--group-attributes-together}
147
\li GroupAttributesTogether
148
\li disabled/false
149
\li Reorders but does not sort the attributes of the objects according to the QML Coding Guidelines.
150
Incompatible with \c{--normalize}.
151
152
In a command line invocation, the behavior can be enabled by passing the flag.
153
154
In a settings file, the behavior can be enabled by setting the
155
relevant variable to "true".
156
\row
157
\li \c{--single-line-empty-objects}
158
\li SingleLineEmptyObjects
159
\li disabled/false
160
\li Write empty objects on a single line (only works with \c{normalize} or \c{group-attributes-together}).
161
162
In a command line invocation, the behavior can be enabled by passing the flag.
163
164
In a settings file, the behavior can be enabled by setting the
165
relevant variable to "true".
166
\row
167
\li \c{--semicolon-rule}
168
\li SemicolonRule
169
\li always
170
\li Customizes the addition of semicolons at the end of JS statements (\c {always}, \c {essential}).
171
\note See \l{Semicolon Rule} for more details.
172
\endtable
173
174
\section2 Arguments
175
\target arguments
176
\table
177
\header
178
\li Arguments:
179
\row
180
\li filenames
181
\endtable
182
183
\section2 Details
184
\e qmlformat is flexible and can be configured according to your needs.
185
186
\section3 Output
187
qmlformat writes the formatted version of the file to stdout.
188
If you wish to have your file updated in-place specify the \c{-i} flag.
189
190
\section3 Grouping Properties, Functions, and Signals Together
191
With \c{-n} or \c{--normalize} flag, \e qmlformat groups and sorts all properties, functions,
192
and signals by name, instead of retaining the existing order.
193
For example:
194
\qml
195
import QtQuick
196
197
QtObject {
198
signal s2()
199
property int h
200
function z() {}
201
property int w
202
function y() {}
203
id: asdf
204
signal s1()
205
206
property Item myItem2: Item {
207
TextEdit {}
208
Rectangle {}
209
}
210
property Item myItem: Item {
211
Rectangle {}
212
TextEdit {}
213
}
214
}
215
\endqml
216
217
will be formatted to:
218
\qml
219
import QtQuick
220
221
QtObject {
222
id: asdf
223
224
property int h
225
property Item myItem: Item {
226
Rectangle {
227
}
228
TextEdit {
229
}
230
}
231
property Item myItem2: Item {
232
TextEdit {
233
}
234
Rectangle {
235
}
236
}
237
property int w
238
239
signal s1
240
signal s2
241
242
function y() {
243
}
244
function z() {
245
}
246
}
247
\enqml
248
249
To group the attributes without sorting by name, use \c{--group-attributes-together} instead.
250
This formats the previous snippet into:
251
\qml
252
import QtQuick
253
254
QtObject {
255
id: asdf
256
257
property int h
258
property int w
259
property Item myItem2: Item {
260
TextEdit {
261
}
262
Rectangle {
263
}
264
}
265
property Item myItem: Item {
266
Rectangle {
267
}
268
TextEdit {
269
}
270
}
271
272
signal s2
273
signal s1
274
275
function z() {
276
}
277
function y() {
278
}
279
}
280
\endqml
281
282
Note that this option takes precedence over \c{--normalize}.
283
284
285
\section3 Settings File
286
You can configure \e qmlformat by including a settings file \c{.qmlformat.ini} in your
287
project source or in the parent directories of your project source folder. A default
288
settings file can be obtained by passing the \c{--write-defaults} flag. This generates the
289
\c{.qmlformat.ini} file in the current working directory.
290
291
\warning \c{--write-defaults} will overwrite any existing settings and comments!
292
293
\section3 Formatting a List of Files
294
While you can pass a list of files to be formatted as arguments, qmlformat provides
295
\c {-F} option to format a set of files stored in a file. In this case, formatting will happen
296
inplace.
297
298
\code
299
// FileList.txt
300
main.qml
301
mycomponent.qml
302
\endcode
303
304
Then, use it like
305
\code
306
qmlformat -F FileList.txt
307
\endcode
308
309
\note If the file contains an invalid entry, for example, a file path that
310
doesn't exist or a valid file path but the content is an invalid qml document,
311
then \c qmlformat will error out for that particular entry. It will still format
312
the valid file entries in place.
313
314
\warning If you provide -F option, qmlformat will ignore the positional arguments.
315
316
\section3 Semicolon Rule
317
The \c{--semicolon-rule} option allows you to customize the addition of semicolons at the end of JS statements.
318
The following values are accepted:
319
\list
320
\li \c{always} - Always add semicolons (default).
321
\li \c{essential} - Remove semicolons unless omitting them would cause issues.
322
\endlist
323
324
\section2 Disabling Formatting with Comments
325
You can temporarily disable qmlformat using special comments.
326
\list
327
\li \c {// qmlformat off} turns off formatting from that line onward
328
\li \c {// qmlformat on} turns on formatting after you turned it off
329
\endlist
330
331
This lets you preserve hand-tuned code or complex structures without \c qmlformat
332
changing their layout. Formatting remains off until the next \c {// qmlformat on}
333
comment, or until the end of the file if no re-enable is found.
334
335
\note Directives must be on their own line.
336
337
\note Nested directives are not supported. Only the first \c {// qmlformat off} and
338
the next \c {// qmlformat on} are considered. Any additional directives inside a disabled
339
region are ignored.
340
341
\note The directives are ignored in normalized formatting mode, when \c sortImports is
342
enabled, or when any option that reorders the original document is used. In these cases,
343
formatting is always applied.
344
345
*/
qtdeclarative
src
qml
doc
src
tools
qtqml-tooling-qmlformat.qdoc
Generated on
for Qt by
1.16.1