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