-
Notifications
You must be signed in to change notification settings - Fork 18
/
qmlfmt.cpp
170 lines (149 loc) · 6 KB
/
qmlfmt.cpp
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
/*
Copyright (c) 2015-2020, Jesper Hellesø Hansen
jesperhh@gmail.com
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <time.h>
#include <QFileInfo>
#include <QDir>
#include <QDirIterator>
#include <QRegExp>
#include <qmljs/parser/qmljsengine_p.h>
#include <qmljs/qmljsdocument.h>
#include <qmljs/qmljsmodelmanagerinterface.h>
#include <qmljs/qmljsreformatter.h>
#include <diff_match_patch.h>
#include "qmlfmt.h"
// Listing files with incorrect formatting, overwriting files with formatted content and printing diffs generate no output when files are identical.
static const QmlFmt::Options SkipIdenticalFilesMask = QmlFmt::Option::ListFileName | QmlFmt::Option::OverwriteFile | QmlFmt::Option::PrintDiff;
int QmlFmt::InternalRun(QIODevice& input, const QString& path)
{
QTextStream qstdout(stdout);
QTextStream qstderr(stderr);
const Utils::FilePath filePath = Utils::FilePath::fromString(path);
const QString source = QString::fromUtf8(input.readAll());
const QmlJS::Dialect dialect = QmlJS::ModelManagerInterface::guessLanguageOfFile(filePath);
QmlJS::Document::MutablePtr document = QmlJS::Document::create(filePath, dialect);
document->setSource(source);
document->parse();
if (!document->diagnosticMessages().isEmpty())
{
if (this->m_options.testFlag(Option::PrintError))
{
for (const QmlJS::DiagnosticMessage& msg : document->diagnosticMessages())
{
qstderr << (msg.isError() ? "Error:" : "Warning:");
qstderr << msg.loc.startLine << ':' << msg.loc.startColumn << ':';
qstderr << ' ' << msg.message << "\n";
}
}
return 1;
}
const QString reformatted = QmlJS::reformat(document, m_indentSize, m_tabSize, m_lineLength);
// Only continue if we are printing to stdout, in that case we should always print the file content,
// changed or not. If we are printing diff/overwriting/listing files there will be nothing to do,
// so we can just skip this.
if (source == reformatted && (this->m_options & SkipIdenticalFilesMask) != 0)
return 0;
if (this->m_options.testFlag(Option::ListFileName))
{
// List filename
qstdout << path << "\n";
}
else if (this->m_options.testFlag(Option::PrintDiff))
{
// Create and print diff
diff_match_patch differ;
const QList<Patch> patches = differ.patch_make(source, reformatted);
qstdout << differ.patch_toText(patches);
}
else
{
// Print reformatted file to stdout/original file
QFile outFile;
if (this->m_options.testFlag(Option::OverwriteFile))
{
outFile.setFileName(path);
outFile.open(QFile::WriteOnly | QFile::Text | QFile::Truncate);
}
else
{
outFile.open(stdout, QFile::WriteOnly | QFile::Text);
}
const QByteArray bytes = reformatted.toUtf8();
outFile.write(bytes);
}
return 0;
}
QmlFmt::QmlFmt(Options options, int indentSize, int tabSize, int lineLength)
: m_options(options)
, m_indentSize(indentSize)
, m_tabSize(tabSize)
, m_lineLength(lineLength)
{
new QmlJS::ModelManagerInterface();
}
int QmlFmt::Run()
{
QFile file;
file.open(stdin, QFile::ReadOnly | QFile::Text);
return this->InternalRun(file, "stdin.qml");
}
int QmlFmt::Run(QStringList paths)
{
if (paths.count() == 0)
{
return Run();
}
int returnValue = 0;
for (const QString& fileOrDir : paths)
{
QFileInfo fileInfo(fileOrDir);
if (fileInfo.isFile())
{
QFile file(fileOrDir);
file.open(QFile::ReadOnly | QFile::Text);
returnValue |= this->InternalRun(file, fileOrDir);
}
else if (fileInfo.isDir())
{
QDirIterator iter(
fileOrDir,
QStringList{ "*.qml" },
QDir::Filter::Files,
QDirIterator::IteratorFlag::Subdirectories);
while (iter.hasNext())
{
QFile file(iter.next());
file.open(QFile::ReadOnly | QFile::Text);
returnValue |= this->InternalRun(file, file.fileName());
}
}
else
{
QTextStream(stderr) << "Path is not valid file or directory: " << fileOrDir << "\n";
returnValue |= 1;
}
}
return returnValue;
}