23 const QQmlJS::Dom::LineWriterOptions::LineEndings defaultEndings =
30 int newlineIndex = code.indexOf(QChar(u'\n'));
31 int crIndex = code.indexOf(QChar(u'\r'));
32 if (newlineIndex >= 0) {
34 if (crIndex + 1 == newlineIndex)
35 return LineEndings::Windows;
37 qWarning().noquote() <<
"Invalid line ending in file, using default";
38 return defaultEndings;
40 return LineEndings::Unix;
43 return LineEndings::OldMacOs;
46 qWarning().noquote() <<
"Unknown line ending in file, using default";
47 return defaultEndings;
74 && settings.isSet(QQmlFormatSettings::s_indentWidthSetting)) {
75 setIndentWidth(settings.value(QQmlFormatSettings::s_indentWidthSetting).toInt()
);
78 if (!isMarked(
Settings::UseTabs) && settings.isSet(QQmlFormatSettings::s_useTabsSetting)) {
79 setTabsEnabled(settings.value(QQmlFormatSettings::s_useTabsSetting).toBool()
);
83 && settings.isSet(QQmlFormatSettings::s_maxColumnWidthSetting)) {
88 && settings.isSet(QQmlFormatSettings::s_normalizeSetting)) {
93 setNewline(QQmlFormatOptions::parseEndings(
94 settings.value(QQmlFormatSettings::s_newlineSetting).toString()));
98 && settings.isSet(QQmlFormatSettings::s_objectsSpacingSetting)) {
103 && settings.isSet(QQmlFormatSettings::s_functionsSpacingSetting)) {
108 && settings.isSet(QQmlFormatSettings::s_sortImportsSetting)) {
109 setSortImports(settings.value(QQmlFormatSettings::s_sortImportsSetting).toBool()
);
116 QCommandLineParser parser;
117 parser.setApplicationDescription(
118 "Formats QML files according to the QML Coding Conventions."_L1);
119 parser.addHelpOption();
120 parser.addVersionOption();
123 QCommandLineOption({
"V"_L1,
"verbose"_L1 },
124 QStringLiteral(
"Verbose mode. Outputs more detailed information.")));
126 QCommandLineOption writeDefaultsOption(
127 QStringList() <<
"write-defaults"_L1,
128 QLatin1String(
"Writes defaults settings to .qmlformat.ini and exits (Warning: This "
129 "will overwrite any existing settings and comments!)"_L1));
130 parser.addOption(writeDefaultsOption);
132 QCommandLineOption ignoreSettings(QStringList() <<
"ignore-settings"_L1,
133 QLatin1String(
"Ignores all settings files and only takes "
134 "command line options into consideration"_L1));
135 parser.addOption(ignoreSettings);
137 parser.addOption(QCommandLineOption(
138 {
"i"_L1,
"inplace"_L1 },
139 QStringLiteral(
"Edit file in-place instead of outputting to stdout.")));
141 parser.addOption(QCommandLineOption({
"f"_L1,
"force"_L1 },
142 QStringLiteral(
"Continue even if an error has occurred.")));
144 parser.addOption(QCommandLineOption({
"t"_L1,
"tabs"_L1 },
145 QStringLiteral(
"Use tabs instead of spaces.")));
147 parser.addOption(QCommandLineOption({
"w"_L1,
"indent-width"_L1 },
148 QStringLiteral(
"How many spaces are used when indenting."),
149 "width"_L1,
"4"_L1));
151 QCommandLineOption columnWidthOption(
152 {
"W"_L1,
"column-width"_L1 },
153 QStringLiteral(
"Breaks the line into multiple lines if exceedes the specified width."
154 "Use -1 to disable line wrapping. (default)"),
155 "width"_L1,
"-1"_L1);
156 parser.addOption(columnWidthOption);
157 parser.addOption(QCommandLineOption({
"n"_L1,
"normalize"_L1 },
158 QStringLiteral(
"Reorders the attributes of the objects "
159 "according to the QML Coding Guidelines.")));
161 QCommandLineOption filesOption(
162 {
"F"_L1,
"files"_L1 },
"Format all files listed in file, in-place"_L1,
"file"_L1);
163 parser.addOption(filesOption);
165 parser.addOption(QCommandLineOption(
166 {
"l"_L1,
"newline"_L1 },
167 QStringLiteral(
"Override the new line format to use (native macos unix windows)."),
168 "newline"_L1,
"native"_L1));
170 parser.addOption(QCommandLineOption(
171 QStringList() <<
"objects-spacing"_L1,
172 QStringLiteral(
"Ensure spaces between objects (only works with normalize option).")));
174 parser.addOption(QCommandLineOption(
175 QStringList() <<
"functions-spacing"_L1,
176 QStringLiteral(
"Ensure spaces between functions (only works with normalize option).")));
179 QCommandLineOption({
"S"_L1,
"sort-imports"_L1 },
180 QStringLiteral(
"Sort imports alphabetically "
181 "(Warning: this might change semantics if a given "
182 "name identifies types in multiple modules!).")));
184 parser.addPositionalArgument(
"filenames"_L1,
"files to be processed by qmlformat"_L1);
186 parser.process(args);
188 if (parser.isSet(writeDefaultsOption)) {
193 if (parser.positionalArguments().empty() && !parser.isSet(filesOption)) {
194 options.addError(
"Error: Expected at least one input file."_L1);
198 bool indentWidthOkay =
false;
199 const int indentWidth = parser.value(
"indent-width"_L1).toInt(&indentWidthOkay);
200 if (!indentWidthOkay) {
201 options.addError(
"Error: Invalid value passed to -w"_L1);
206 if (!parser.value(
"files"_L1).isEmpty()) {
207 const QString path = parser.value(
"files"_L1);
209 if (!file.open(QIODevice::Text | QIODevice::ReadOnly)) {
210 options.addError(
"Error: Could not open file \""_L1 + path +
"\" for option -F."_L1);
214 QTextStream in(&file);
215 while (!in.atEnd()) {
216 QString file = in.readLine();
221 files.push_back(file);
224 if (files.isEmpty()) {
225 options.addError(
"Error: File \""_L1 + path +
"\" for option -F is empty."_L1);
229 for (
const auto &file : std::as_const(files)) {
230 if (!QFile::exists(file)) {
231 options.addError(
"Error: Entry \""_L1 + file +
"\" of file \""_L1 + path
232 +
"\" passed to option -F could not be found."_L1);
237 const auto &args = parser.positionalArguments();
238 for (
const auto &file : args) {
239 if (!QFile::exists(file)) {
240 options.addError(
"Error: Could not find file \""_L1 + file +
"\"."_L1);
246 options.setIsVerbose(parser.isSet(
"verbose"_L1));
247 options.setIsInplace(parser.isSet(
"inplace"_L1));
248 options.setForceEnabled(parser.isSet(
"force"_L1));
249 options.setIgnoreSettingsEnabled(parser.isSet(
"ignore-settings"_L1));
251 if (parser.isSet(
"tabs"_L1)) {
255 if (parser.isSet(
"normalize"_L1)) {
259 if (parser.isSet(
"objects-spacing"_L1)) {
263 if (parser.isSet(
"functions-spacing"_L1)) {
267 if (parser.isSet(
"sort-imports"_L1)) {
271 if (parser.isSet(
"indent-width"_L1)) {
276 if (parser.isSet(
"newline"_L1)) {
278 options.setNewline(QQmlFormatOptions::parseEndings(parser.value(
"newline"_L1)));
280 options.setFiles(files);
281 options.setArguments(parser.positionalArguments());
283 if (parser.isSet(columnWidthOption)) {
284 bool isValidValue =
false;
285 const int maxColumnWidth = parser.value(columnWidthOption).toInt(&isValidValue);
286 if (!isValidValue || maxColumnWidth < -1) {
287 options.addError(
"Error: Invalid value passed to -W. Must be an integer >= -1"_L1);