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 the attributes of the objects according to the QML Coding Guidelines.
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 normalize option).
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 normalize option).
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{--single-line-empty-objects}
146
\li SingleLineEmptyObjects
147
\li disabled/false
148
\li Write empty objects on a single line (only works with normalize option).
149
150
In a command line invocation, the behavior can be enabled by passing the flag.
151
152
In a settings file, the behavior can be enabled by setting the
153
relevant variable to "true".
154
\row
155
\li \c{--semicolon-rule}
156
\li SemicolonRule
157
\li always
158
\li Customizes the addition of semicolons at the end of JS statements (\c {always}, \c {essential}).
159
\note See \l{Semicolon Rule} for more details.
160
\endtable
161
162
\section2 Arguments
163
\target arguments
164
\table
165
\header
166
\li Arguments:
167
\row
168
\li filenames
169
\endtable
170
171
\section2 Details
172
\e qmlformat is flexible and can be configured according to your needs.
173
174
\section3 Output
175
qmlformat writes the formatted version of the file to stdout.
176
If you wish to have your file updated in-place specify the \c{-i} flag.
177
178
\section3 Grouping Properties, Functions, and Signals Together
179
With \c{-n} or \c{--normalize} flag, \e qmlformat groups all properties, functions,
180
and signals together, instead of retaining the order you specified.
181
182
\section3 Settings File
183
You can configure \e qmlformat by including a settings file \c{.qmlformat.ini} in your
184
project source or in the parent directories of your project source folder. A default
185
settings file can be obtained by passing the \c{--write-defaults} flag. This generates the
186
\c{.qmlformat.ini} file in the current working directory.
187
188
\warning \c{--write-defaults} will overwrite any existing settings and comments!
189
190
\section3 Formatting a List of Files
191
While you can pass a list of files to be formatted as arguments, qmlformat provides
192
\c {-F} option to format a set of files stored in a file. In this case, formatting will happen
193
inplace.
194
195
\code
196
// FileList.txt
197
main.qml
198
mycomponent.qml
199
\endcode
200
201
Then, use it like
202
\code
203
qmlformat -F FileList.txt
204
\endcode
205
206
\note If the file contains an invalid entry, for example, a file path that
207
doesn't exist or a valid file path but the content is an invalid qml document,
208
then \c qmlformat will error out for that particular entry. It will still format
209
the valid file entries in place.
210
211
\warning If you provide -F option, qmlformat will ignore the positional arguments.
212
213
\section3 Semicolon Rule
214
The \c{--semicolon-rule} option allows you to customize the addition of semicolons at the end of JS statements.
215
The following values are accepted:
216
\list
217
\li \c{always} - Always add semicolons (default).
218
\li \c{essential} - Remove semicolons unless omitting them would cause issues.
219
\endlist
220
221
\section2 Disabling Formatting with Comments
222
You can temporarily disable qmlformat using special comments.
223
\list
224
\li \c {// qmlformat off} turns off formatting from that line onward
225
\li \c {// qmlformat on} turns on formatting after you turned it off
226
\endlist
227
228
This lets you preserve hand-tuned code or complex structures without \c qmlformat
229
changing their layout. Formatting remains off until the next \c {// qmlformat on}
230
comment, or until the end of the file if no re-enable is found.
231
232
\note Directives must be on their own line.
233
234
\note Nested directives are not supported. Only the first \c {// qmlformat off} and
235
the next \c {// qmlformat on} are considered. Any additional directives inside a disabled
236
region are ignored.
237
238
\note The directives are ignored in normalized formatting mode, when \c sortImports is
239
enabled, or when any option that reorders the original document is used. In these cases,
240
formatting is always applied.
241
242
*/
qtdeclarative
src
qml
doc
src
tools
qtqml-tooling-qmlformat.qdoc
Generated on
for Qt by
1.14.0