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