aboutsummaryrefslogtreecommitdiffstats
path: root/tools/qmlformat/main.cpp
blob: a7e6c64bf3d9e266f52bd23facdb043cc56341a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/****************************************************************************
**
** Copyright (C) 2019 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the tools applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QCoreApplication>
#include <QFile>
#include <QTextStream>

#include <QtQml/private/qqmljslexer_p.h>
#include <QtQml/private/qqmljsparser_p.h>
#include <QtQml/private/qqmljsengine_p.h>
#include <QtQml/private/qqmljsastvisitor_p.h>
#include <QtQml/private/qqmljsast_p.h>

#if QT_CONFIG(commandlineparser)
#include <QCommandLineParser>
#endif

#include "commentastvisitor.h"
#include "dumpastvisitor.h"
#include "restructureastvisitor.h"

bool parseFile(const QString &filename, bool inplace, bool verbose, bool force,
               int indentWidth, bool tabs, const QString &newline)
{
    QFile file(filename);

    if (!file.open(QIODevice::Text | QIODevice::ReadOnly)) {
        qWarning().noquote() << "Failed to open" << filename << "for reading.";
        return false;
    }

    QString code = QString::fromUtf8(file.readAll());
    file.close();

    QQmlJS::Engine engine;
    QQmlJS::Lexer lexer(&engine);

    lexer.setCode(code, 1, true);
    QQmlJS::Parser parser(&engine);

    bool success = parser.parse();

    if (!success) {
        const auto diagnosticMessages = parser.diagnosticMessages();
        for (const QQmlJS::DiagnosticMessage &m : diagnosticMessages) {
            qWarning().noquote() << QString::fromLatin1("%1:%2 : %3")
                                    .arg(filename).arg(m.loc.startLine).arg(m.message);
        }

        qWarning().noquote() << "Failed to parse" << filename;
        return false;
    }

    // Try to attach comments to AST nodes
    CommentAstVisitor comment(&engine, parser.rootNode());

    if (verbose)
        qWarning().noquote() << comment.attachedComments().size() << "comment(s) attached.";

    if (verbose) {
        int orphaned = 0;

        for (const auto& orphanList : comment.orphanComments().values())
            orphaned += orphanList.size();

        qWarning().noquote() << orphaned << "comments are orphans.";
    }

    // Do the actual restructuring
    RestructureAstVisitor restructure(parser.rootNode());

    // Turn AST back into source code
    if (verbose)
        qWarning().noquote() << "Dumping" << filename;

    DumpAstVisitor dump(&engine, parser.rootNode(), &comment, tabs ? 1 : indentWidth,
                        tabs ? DumpAstVisitor::Indentation::Tabs
                             : DumpAstVisitor::Indentation::Spaces);

    QString dumpCode = dump.toString();

    lexer.setCode(dumpCode, 1, true);

    bool dumpSuccess = parser.parse();

    if (!dumpSuccess) {
        if (verbose) {
            const auto diagnosticMessages = parser.diagnosticMessages();
            for (const QQmlJS::DiagnosticMessage &m : diagnosticMessages) {
              qWarning().noquote() << QString::fromLatin1("<formatted>:%2 : %3")
                                      .arg(m.loc.startLine).arg(m.message);
            }
        }

        qWarning().noquote() << "Failed to parse formatted code.";
    }

    if (dump.error() || !dumpSuccess) {
        if (force) {
            qWarning().noquote() << "An error has occurred. The output may not be reliable.";
        } else {
            qWarning().noquote() << "An error has occurred. Aborting.";
            return false;
        }
   }


    const bool native = newline == "native";

    if (!native) {
        if (newline == "macos") {
            dumpCode = dumpCode.replace("\n","\r");
        } else if (newline == "windows") {
            dumpCode = dumpCode.replace("\n", "\r\n");
        } else if (newline == "unix") {
            // Nothing needs to be done for unix line-endings
        } else {
            qWarning().noquote() << "Unknown line ending type" << newline;
            return false;
        }
    }

    if (inplace) {
        if (verbose)
            qWarning().noquote() << "Writing to file" << filename;

        if (!file.open(native ? QIODevice::WriteOnly | QIODevice::Text : QIODevice::WriteOnly))
        {
            qWarning().noquote() << "Failed to open" << filename << "for writing";
            return false;
        }

        file.write(dumpCode.toUtf8());
        file.close();
    } else {
        QFile out;
        out.open(stdout, QIODevice::WriteOnly);
        out.write(dumpCode.toUtf8());
    }

    return true;
}

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    QCoreApplication::setApplicationName("qmlformat");
    QCoreApplication::setApplicationVersion("1.0");

    bool success = true;
#if QT_CONFIG(commandlineparser)
    QCommandLineParser parser;
    parser.setApplicationDescription("Formats QML files according to the QML Coding Conventions.");
    parser.addHelpOption();
    parser.addVersionOption();

    parser.addOption(QCommandLineOption({"V", "verbose"},
                     QStringLiteral("Verbose mode. Outputs more detailed information.")));

    parser.addOption(QCommandLineOption({"i", "inplace"},
                     QStringLiteral("Edit file in-place instead of outputting to stdout.")));

    parser.addOption(QCommandLineOption({"f", "force"},
                     QStringLiteral("Continue even if an error has occurred.")));

    parser.addOption(
            QCommandLineOption({ "t", "tabs" }, QStringLiteral("Use tabs instead of spaces.")));

    parser.addOption(QCommandLineOption({ "w", "indent-width" },
                                        QStringLiteral("How many spaces are used when indenting."),
                                        "width", "4"));

    parser.addOption(QCommandLineOption(
            { "F", "files" }, QStringLiteral("Format all files listed in file, in-place"), "file"));

    parser.addOption(QCommandLineOption({"l", "newline"},
                     QStringLiteral("Override the new line format to use (native macos unix windows)."),
                     "newline", "native"));

    parser.addPositionalArgument("filenames", "files to be processed by qmlformat");

    parser.process(app);

    const auto positionalArguments = parser.positionalArguments();

    if (positionalArguments.isEmpty() && !parser.isSet("files"))
        parser.showHelp(-1);

    if (!parser.isSet("inplace") && parser.value("newline") != "native") {
        qWarning() << "Error: The -l option can only be used with -i";
        return -1;
    }

    if (parser.isSet("indent-width") && parser.isSet("tabs")) {
        qWarning() << "Error: Cannot use --indent-width with --tabs";
        return -1;
    }

    bool indentWidthOkay = false;
    int indentWidth = parser.value("indent-width").toInt(&indentWidthOkay);

    if (!indentWidthOkay) {
        qWarning() << "Error: Invalid value passed to -w";
        return -1;
    }

    if (parser.isSet("files")) {
        if (!positionalArguments.isEmpty())
            qWarning() << "Warning: Positional arguments are ignored when -F is used";

        QFile file(parser.value("files"));
        file.open(QIODevice::Text | QIODevice::ReadOnly);
        QTextStream in(&file);
        while (!in.atEnd()) {
            QString file = in.readLine();

            if (file.isEmpty())
                continue;

            if (!parseFile(file, true, parser.isSet("verbose"),
                           parser.isSet("force"), indentWidth, parser.isSet("tabs"),
                           parser.value("newline")))
                success = false;
        }
    } else {
        for (const QString &file : parser.positionalArguments()) {
            if (!parseFile(file, parser.isSet("inplace"), parser.isSet("verbose"),
                           parser.isSet("force"), indentWidth,
                           parser.isSet("tabs"), parser.value("newline")))
                success = false;
        }
    }
#endif

    return success ? 0 : 1;
}