1 | // Copyright (C) 2019 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
3 | |
4 | #include <QCoreApplication> |
5 | #include <QFile> |
6 | #include <QTextStream> |
7 | |
8 | #include <QtQml/private/qqmljslexer_p.h> |
9 | #include <QtQml/private/qqmljsparser_p.h> |
10 | #include <QtQml/private/qqmljsengine_p.h> |
11 | #include <QtQml/private/qqmljsastvisitor_p.h> |
12 | #include <QtQml/private/qqmljsast_p.h> |
13 | #include <QtQmlDom/private/qqmldomitem_p.h> |
14 | #include <QtQmlDom/private/qqmldomexternalitems_p.h> |
15 | #include <QtQmlDom/private/qqmldomtop_p.h> |
16 | #include <QtQmlDom/private/qqmldomoutwriter_p.h> |
17 | |
18 | #if QT_CONFIG(commandlineparser) |
19 | # include <QCommandLineParser> |
20 | #endif |
21 | |
22 | #include <QtQmlToolingSettings/private/qqmltoolingsettings_p.h> |
23 | |
24 | |
25 | using namespace QQmlJS::Dom; |
26 | |
27 | struct Options |
28 | { |
29 | bool verbose = false; |
30 | bool inplace = false; |
31 | bool force = false; |
32 | bool tabs = false; |
33 | bool valid = false; |
34 | bool normalize = false; |
35 | bool ignoreSettings = false; |
36 | bool writeDefaultSettings = false; |
37 | bool objectsSpacing = false; |
38 | bool functionsSpacing = false; |
39 | |
40 | int indentWidth = 4; |
41 | bool indentWidthSet = false; |
42 | QString newline = "native" ; |
43 | |
44 | QStringList files; |
45 | QStringList arguments; |
46 | QStringList errors; |
47 | }; |
48 | |
49 | // TODO refactor |
50 | // Move out to the LineWriterOptions class / helper |
51 | static LineWriterOptions composeLwOptions(const Options &options, QStringView code) |
52 | { |
53 | LineWriterOptions lwOptions; |
54 | lwOptions.formatOptions.indentSize = options.indentWidth; |
55 | lwOptions.formatOptions.useTabs = options.tabs; |
56 | lwOptions.updateOptions = LineWriterOptions::Update::None; |
57 | if (options.newline == "native" ) { |
58 | // find out current line endings... |
59 | int newlineIndex = code.indexOf(c: QChar(u'\n')); |
60 | int crIndex = code.indexOf(c: QChar(u'\r')); |
61 | if (newlineIndex >= 0) { |
62 | if (crIndex >= 0) { |
63 | if (crIndex + 1 == newlineIndex) |
64 | lwOptions.lineEndings = LineWriterOptions::LineEndings::Windows; |
65 | else |
66 | qWarning().noquote() << "Invalid line ending in file, using default" ; |
67 | |
68 | } else { |
69 | lwOptions.lineEndings = LineWriterOptions::LineEndings::Unix; |
70 | } |
71 | } else if (crIndex >= 0) { |
72 | lwOptions.lineEndings = LineWriterOptions::LineEndings::OldMacOs; |
73 | } else { |
74 | qWarning().noquote() << "Unknown line ending in file, using default" ; |
75 | } |
76 | } else if (options.newline == "macos" ) { |
77 | lwOptions.lineEndings = LineWriterOptions::LineEndings::OldMacOs; |
78 | } else if (options.newline == "windows" ) { |
79 | lwOptions.lineEndings = LineWriterOptions::LineEndings::Windows; |
80 | } else if (options.newline == "unix" ) { |
81 | lwOptions.lineEndings = LineWriterOptions::LineEndings::Unix; |
82 | } else { |
83 | qWarning().noquote() << "Unknown line ending type" << options.newline << ", using default" ; |
84 | } |
85 | |
86 | if (options.normalize) |
87 | lwOptions.attributesSequence = LineWriterOptions::AttributesSequence::Normalize; |
88 | else |
89 | lwOptions.attributesSequence = LineWriterOptions::AttributesSequence::Preserve; |
90 | |
91 | lwOptions.objectsSpacing = options.objectsSpacing; |
92 | lwOptions.functionsSpacing = options.functionsSpacing; |
93 | return lwOptions; |
94 | } |
95 | |
96 | static void logParsingErrors(const DomItem &fileItem, const QString &filename) |
97 | { |
98 | fileItem.iterateErrors( |
99 | visitor: [](const DomItem &, const ErrorMessage &msg) { |
100 | errorToQDebug(msg); |
101 | return true; |
102 | }, |
103 | iterate: true); |
104 | qWarning().noquote() << "Failed to parse" << filename; |
105 | } |
106 | |
107 | // TODO |
108 | // refactor this workaround. ExternalOWningItem is not recognized as an owning type |
109 | // in ownerAs. |
110 | static std::shared_ptr<ExternalOwningItem> getFileItemOwner(const DomItem &fileItem) |
111 | { |
112 | std::shared_ptr<ExternalOwningItem> filePtr = nullptr; |
113 | switch (fileItem.internalKind()) { |
114 | case DomType::JsFile: |
115 | filePtr = fileItem.ownerAs<JsFile>(); |
116 | break; |
117 | default: |
118 | filePtr = fileItem.ownerAs<QmlFile>(); |
119 | break; |
120 | } |
121 | return filePtr; |
122 | } |
123 | |
124 | // TODO refactor |
125 | // Introduce better encapsulation and separation of concerns and move to DOM API |
126 | // returns a DomItem corresponding to the loaded file and bool indicating the validity of the file |
127 | static std::pair<DomItem, bool> parse(const QString &filename) |
128 | { |
129 | auto envPtr = |
130 | DomEnvironment::create(loadPaths: QStringList(), |
131 | options: QQmlJS::Dom::DomEnvironment::Option::SingleThreaded |
132 | | QQmlJS::Dom::DomEnvironment::Option::NoDependencies); |
133 | // placeholder for a node |
134 | // containing metadata (ExternalItemInfo) about the loaded file |
135 | DomItem fMetadataItem; |
136 | envPtr->loadFile(file: FileToLoad::fromFileSystem(environment: envPtr, canonicalPath: filename), |
137 | // callback called when everything is loaded that receives the |
138 | // loaded external file pair (path, oldValue, newValue) |
139 | callback: [&fMetadataItem](Path, const DomItem &, const DomItem &extItemInfo) { |
140 | fMetadataItem = extItemInfo; |
141 | }); |
142 | auto fItem = fMetadataItem.fileObject(); |
143 | auto filePtr = getFileItemOwner(fileItem: fItem); |
144 | return { fItem, filePtr && filePtr->isValid() }; |
145 | } |
146 | |
147 | static bool parseFile(const QString &filename, const Options &options) |
148 | { |
149 | const auto [fileItem, validFile] = parse(filename); |
150 | if (!validFile) { |
151 | logParsingErrors(fileItem, filename); |
152 | return false; |
153 | } |
154 | |
155 | // Turn AST back into source code |
156 | if (options.verbose) |
157 | qWarning().noquote() << "Dumping" << filename; |
158 | |
159 | const auto &code = getFileItemOwner(fileItem)->code(); |
160 | auto lwOptions = composeLwOptions(options, code); |
161 | WriteOutChecks checks = WriteOutCheck::Default; |
162 | //Disable writeOutChecks for some usecases |
163 | if (options.force || |
164 | code.size() > 32000 || |
165 | fileItem.internalKind() == DomType::JsFile) { |
166 | checks = WriteOutCheck::None; |
167 | } |
168 | |
169 | bool res = false; |
170 | if (options.inplace) { |
171 | if (options.verbose) |
172 | qWarning().noquote() << "Writing to file" << filename; |
173 | FileWriter fw; |
174 | const unsigned numberOfBackupFiles = 0; |
175 | res = fileItem.writeOut(path: filename, nBackups: numberOfBackupFiles, opt: lwOptions, fw: &fw, extraChecks: checks); |
176 | } else { |
177 | QFile out; |
178 | if (out.open(stdout, ioFlags: QIODevice::WriteOnly)) { |
179 | LineWriter lw([&out](QStringView s) { out.write(data: s.toUtf8()); }, filename, lwOptions); |
180 | OutWriter ow(lw); |
181 | res = fileItem.writeOutForFile(ow, extraChecks: checks); |
182 | ow.flush(); |
183 | } else { |
184 | res = false; |
185 | } |
186 | } |
187 | return res; |
188 | } |
189 | |
190 | Options buildCommandLineOptions(const QCoreApplication &app) |
191 | { |
192 | #if QT_CONFIG(commandlineparser) |
193 | QCommandLineParser parser; |
194 | parser.setApplicationDescription("Formats QML files according to the QML Coding Conventions." ); |
195 | parser.addHelpOption(); |
196 | parser.addVersionOption(); |
197 | |
198 | parser.addOption( |
199 | commandLineOption: QCommandLineOption({ "V" , "verbose" }, |
200 | QStringLiteral("Verbose mode. Outputs more detailed information." ))); |
201 | |
202 | QCommandLineOption writeDefaultsOption( |
203 | QStringList() << "write-defaults" , |
204 | QLatin1String("Writes defaults settings to .qmlformat.ini and exits (Warning: This " |
205 | "will overwrite any existing settings and comments!)" )); |
206 | parser.addOption(commandLineOption: writeDefaultsOption); |
207 | |
208 | QCommandLineOption ignoreSettings(QStringList() << "ignore-settings" , |
209 | QLatin1String("Ignores all settings files and only takes " |
210 | "command line options into consideration" )); |
211 | parser.addOption(commandLineOption: ignoreSettings); |
212 | |
213 | parser.addOption(commandLineOption: QCommandLineOption( |
214 | { "i" , "inplace" }, |
215 | QStringLiteral("Edit file in-place instead of outputting to stdout." ))); |
216 | |
217 | parser.addOption(commandLineOption: QCommandLineOption({ "f" , "force" }, |
218 | QStringLiteral("Continue even if an error has occurred." ))); |
219 | |
220 | parser.addOption( |
221 | commandLineOption: QCommandLineOption({ "t" , "tabs" }, QStringLiteral("Use tabs instead of spaces." ))); |
222 | |
223 | parser.addOption(commandLineOption: QCommandLineOption({ "w" , "indent-width" }, |
224 | QStringLiteral("How many spaces are used when indenting." ), |
225 | "width" , "4" )); |
226 | |
227 | parser.addOption(commandLineOption: QCommandLineOption({ "n" , "normalize" }, |
228 | QStringLiteral("Reorders the attributes of the objects " |
229 | "according to the QML Coding Guidelines." ))); |
230 | |
231 | QCommandLineOption filesOption( |
232 | { "F"_L1 , "files"_L1 }, "Format all files listed in file, in-place"_L1 , "file"_L1 ); |
233 | parser.addOption(commandLineOption: filesOption); |
234 | |
235 | parser.addOption(commandLineOption: QCommandLineOption( |
236 | { "l" , "newline" }, |
237 | QStringLiteral("Override the new line format to use (native macos unix windows)." ), |
238 | "newline" , "native" )); |
239 | |
240 | parser.addOption(commandLineOption: QCommandLineOption(QStringList() << "objects-spacing" , QStringLiteral("Ensure spaces between objects (only works with normalize option)." ))); |
241 | |
242 | parser.addOption(commandLineOption: QCommandLineOption(QStringList() << "functions-spacing" , QStringLiteral("Ensure spaces between functions (only works with normalize option)." ))); |
243 | |
244 | parser.addPositionalArgument(name: "filenames" , description: "files to be processed by qmlformat" ); |
245 | |
246 | parser.process(app); |
247 | |
248 | if (parser.isSet(option: writeDefaultsOption)) { |
249 | Options options; |
250 | options.writeDefaultSettings = true; |
251 | options.valid = true; |
252 | return options; |
253 | } |
254 | |
255 | if (parser.positionalArguments().empty() && !parser.isSet(option: filesOption)) { |
256 | Options options; |
257 | options.errors.push_back(t: "Error: Expected at least one input file."_L1 ); |
258 | return options; |
259 | } |
260 | |
261 | bool indentWidthOkay = false; |
262 | const int indentWidth = parser.value(name: "indent-width" ).toInt(ok: &indentWidthOkay); |
263 | if (!indentWidthOkay) { |
264 | Options options; |
265 | options.errors.push_back(t: "Error: Invalid value passed to -w" ); |
266 | return options; |
267 | } |
268 | |
269 | QStringList files; |
270 | if (!parser.value(name: "files"_L1 ).isEmpty()) { |
271 | const QString path = parser.value(name: "files"_L1 ); |
272 | QFile file(path); |
273 | if (!file.open(flags: QIODevice::Text | QIODevice::ReadOnly)) { |
274 | Options options; |
275 | options.errors.push_back( |
276 | t: "Error: Could not open file \"" + path + "\" for option -F."_L1 ); |
277 | return options; |
278 | } |
279 | |
280 | QTextStream in(&file); |
281 | while (!in.atEnd()) { |
282 | QString file = in.readLine(); |
283 | |
284 | if (file.isEmpty()) |
285 | continue; |
286 | |
287 | files.push_back(t: file); |
288 | } |
289 | |
290 | if (files.isEmpty()) { |
291 | Options options; |
292 | options.errors.push_back(t: "Error: File \""_L1 + path + "\" for option -F is empty."_L1 ); |
293 | return options; |
294 | } |
295 | |
296 | for (const auto &file : std::as_const(t&: files)) { |
297 | if (!QFile::exists(fileName: file)) { |
298 | Options options; |
299 | options.errors.push_back(t: "Error: Entry \"" + file + "\" of file \"" + path |
300 | + "\" passed to option -F could not be found." ); |
301 | return options; |
302 | } |
303 | } |
304 | } else { |
305 | const auto &args = parser.positionalArguments(); |
306 | for (const auto &file : args) { |
307 | if (!QFile::exists(fileName: file)) { |
308 | Options options; |
309 | options.errors.push_back(t: "Error: Could not find file \"" + file + "\"." ); |
310 | return options; |
311 | } |
312 | } |
313 | } |
314 | |
315 | Options options; |
316 | options.verbose = parser.isSet(name: "verbose" ); |
317 | options.inplace = parser.isSet(name: "inplace" ); |
318 | options.force = parser.isSet(name: "force" ); |
319 | options.tabs = parser.isSet(name: "tabs" ); |
320 | options.normalize = parser.isSet(name: "normalize" ); |
321 | options.ignoreSettings = parser.isSet(name: "ignore-settings" ); |
322 | options.objectsSpacing = parser.isSet(name: "objects-spacing" ); |
323 | options.functionsSpacing = parser.isSet(name: "functions-spacing" ); |
324 | options.valid = true; |
325 | |
326 | options.indentWidth = indentWidth; |
327 | options.indentWidthSet = parser.isSet(name: "indent-width" ); |
328 | options.newline = parser.value(name: "newline" ); |
329 | options.files = files; |
330 | options.arguments = parser.positionalArguments(); |
331 | return options; |
332 | #else |
333 | return Options {}; |
334 | #endif |
335 | } |
336 | |
337 | int main(int argc, char *argv[]) |
338 | { |
339 | QCoreApplication app(argc, argv); |
340 | QCoreApplication::setApplicationName("qmlformat" ); |
341 | QCoreApplication::setApplicationVersion(QT_VERSION_STR); |
342 | |
343 | QQmlToolingSettings settings(QLatin1String("qmlformat" )); |
344 | |
345 | const QString &useTabsSetting = QStringLiteral("UseTabs" ); |
346 | settings.addOption(name: useTabsSetting); |
347 | |
348 | const QString &indentWidthSetting = QStringLiteral("IndentWidth" ); |
349 | settings.addOption(name: indentWidthSetting, defaultValue: 4); |
350 | |
351 | const QString &normalizeSetting = QStringLiteral("NormalizeOrder" ); |
352 | settings.addOption(name: normalizeSetting); |
353 | |
354 | const QString &newlineSetting = QStringLiteral("NewlineType" ); |
355 | settings.addOption(name: newlineSetting, QStringLiteral("native" )); |
356 | |
357 | const QString &objectsSpacingSetting = QStringLiteral("ObjectsSpacing" ); |
358 | settings.addOption(name: objectsSpacingSetting); |
359 | |
360 | const QString &functionsSpacingSetting = QStringLiteral("FunctionsSpacing" ); |
361 | settings.addOption(name: functionsSpacingSetting); |
362 | |
363 | const auto options = buildCommandLineOptions(app); |
364 | if (!options.valid) { |
365 | for (const auto &error : options.errors) { |
366 | qWarning().noquote() << error; |
367 | } |
368 | |
369 | return -1; |
370 | } |
371 | |
372 | if (options.writeDefaultSettings) |
373 | return settings.writeDefaults() ? 0 : -1; |
374 | |
375 | auto getSettings = [&](const QString &file, Options options) { |
376 | // Perform formatting inplace if --files option is set. |
377 | if (!options.files.isEmpty()) |
378 | options.inplace = true; |
379 | |
380 | if (options.ignoreSettings || !settings.search(path: file)) |
381 | return options; |
382 | |
383 | Options perFileOptions = options; |
384 | |
385 | // Allow for tab settings to be overwritten by the command line |
386 | if (!options.indentWidthSet) { |
387 | if (settings.isSet(name: indentWidthSetting)) |
388 | perFileOptions.indentWidth = settings.value(name: indentWidthSetting).toInt(); |
389 | if (settings.isSet(name: useTabsSetting)) |
390 | perFileOptions.tabs = settings.value(name: useTabsSetting).toBool(); |
391 | } |
392 | |
393 | if (settings.isSet(name: normalizeSetting)) |
394 | perFileOptions.normalize = settings.value(name: normalizeSetting).toBool(); |
395 | |
396 | if (settings.isSet(name: newlineSetting)) |
397 | perFileOptions.newline = settings.value(name: newlineSetting).toString(); |
398 | |
399 | if (settings.isSet(name: objectsSpacingSetting)) |
400 | perFileOptions.objectsSpacing = settings.value(name: objectsSpacingSetting).toBool(); |
401 | |
402 | if (settings.isSet(name: functionsSpacingSetting)) |
403 | perFileOptions.functionsSpacing = settings.value(name: functionsSpacingSetting).toBool(); |
404 | |
405 | return perFileOptions; |
406 | }; |
407 | |
408 | bool success = true; |
409 | if (!options.files.isEmpty()) { |
410 | if (!options.arguments.isEmpty()) |
411 | qWarning() << "Warning: Positional arguments are ignored when -F is used" ; |
412 | |
413 | for (const QString &file : options.files) { |
414 | Q_ASSERT(!file.isEmpty()); |
415 | |
416 | if (!parseFile(filename: file, options: getSettings(file, options))) |
417 | success = false; |
418 | } |
419 | } else { |
420 | for (const QString &file : options.arguments) { |
421 | if (!parseFile(filename: file, options: getSettings(file, options))) |
422 | success = false; |
423 | } |
424 | } |
425 | |
426 | return success ? 0 : 1; |
427 | } |
428 | |