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