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
13with 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
24qmlformat can be configured via command line options. There are two groups of options: Those which
25are directly related to formatting, and those which control the behavior of the tool.
26
27
28The 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
68The next group of options controls how files should be formatted, and can additionally also be
69controlled 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
187qmlformat writes the formatted version of the file to stdout.
188If you wish to have your file updated in-place specify the \c{-i} flag.
189
190\section3 Grouping Properties, Functions, and Signals Together
191With \c{-n} or \c{--normalize} flag, \e qmlformat groups all properties, functions,
192and signals together, instead of retaining the order you specified.
193
194\section3 Settings File
195You can configure \e qmlformat by including a settings file \c{.qmlformat.ini} in your
196project source or in the parent directories of your project source folder. A default
197settings file can be obtained by passing the \c{--write-defaults} flag. This generates the
198\c{.qmlformat.ini} file in the current working directory.
199
200\warning \c{--write-defaults} will overwrite any existing settings and comments!
201
202\section3 Formatting a List of Files
203While you can pass a list of files to be formatted as arguments, qmlformat provides
204\c {-F} option to format a set of files stored in a file. In this case, formatting will happen
205inplace.
206
207\code
208 // FileList.txt
209 main.qml
210 mycomponent.qml
211\endcode
212
213Then, use it like
214\code
215 qmlformat -F FileList.txt
216\endcode
217
218\note If the file contains an invalid entry, for example, a file path that
219doesn't exist or a valid file path but the content is an invalid qml document,
220then \c qmlformat will error out for that particular entry. It will still format
221the valid file entries in place.
222
223\warning If you provide -F option, qmlformat will ignore the positional arguments.
224
225\section3 Semicolon Rule
226The \c{--semicolon-rule} option allows you to customize the addition of semicolons at the end of JS statements.
227The following values are accepted:
228\list
229 \li \c{always} - Always add semicolons (default).
230 \li \c{essential} - Remove semicolons unless omitting them would cause issues.
231\endlist
232
233\section2 Disabling Formatting with Comments
234You can temporarily disable qmlformat using special comments.
235\list
236 \li \c {// qmlformat off} turns off formatting from that line onward
237 \li \c {// qmlformat on} turns on formatting after you turned it off
238\endlist
239
240This lets you preserve hand-tuned code or complex structures without \c qmlformat
241changing their layout. Formatting remains off until the next \c {// qmlformat on}
242comment, or until the end of the file if no re-enable is found.
243
244\note Directives must be on their own line.
245
246\note Nested directives are not supported. Only the first \c {// qmlformat off} and
247the next \c {// qmlformat on} are considered. Any additional directives inside a disabled
248region are ignored.
249
250\note The directives are ignored in normalized formatting mode, when \c sortImports is
251enabled, or when any option that reorders the original document is used. In these cases,
252formatting is always applied.
253
254*/