-
Notifications
You must be signed in to change notification settings - Fork 7
/
EditorConfig.cpp
431 lines (353 loc) · 12.5 KB
/
EditorConfig.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
* MIT License
*
* Copyright (c) 2017 Justin Dailey
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <QDir>
#include <QFile>
#include <QRegularExpression>
#include <QTextStream>
#include <QVector>
#include <QMap>
#include <QFileInfo>
#include <QException>
#include <QDebug>
#include "EditorConfig.h"
const int MAX_SECTION_NAME_LENGTH = 4096;
const int MAX_KEY_LENGTH = 50;
const int MAX_VALUE_LENGTH = 255;
class EditorConfigFileNotFound : public QException
{
public:
void raise() const override { throw *this; }
EditorConfigFileNotFound *clone() const override { return new EditorConfigFileNotFound(*this); }
};
static QStringList getAllPossibleConfigFileLocations(const QString &path, const QString &filename)
{
// Given a path e.g. C:/a/b/c and a config file name e.g. .editorconfig
// Generate all paths that may contain a config file:
// C:/a/b/c/.editorconfig
// C:/a/b/.editorconfig
// C:/a/.editorconfig
// C:/.editorconfig
QString curPath = path;
QStringList filenames;
Q_ASSERT(!curPath.endsWith("/"));
do {
curPath.chop(curPath.length() - curPath.lastIndexOf("/"));
filenames.append(curPath + "/" + filename);
} while(curPath.length() > 0 && curPath.contains("/"));
return filenames;
}
static int findNextUnescapedCharacter(QStringView pattern, const QChar c)
{
int index = pattern.indexOf(c, 1);
while (index != -1) {
// Check if previous char is not the escape sequence
if (pattern[index - 1] != '\\') {
return index;
}
else {
index = pattern.indexOf(c, index + 1);
}
}
return index;
}
static bool isNextElementLiteral(QStringView pattern)
{
return pattern.startsWith('\\') && pattern.size() > 1;
}
static bool isNextElementOptionalDirectory(QStringView pattern)
{
return pattern.startsWith(QLatin1String("/**/"));
}
static bool isNextElementAll(QStringView pattern)
{
return pattern.startsWith(QLatin1String("**"));
}
static bool isNextElementAllExceptSlash(QStringView pattern)
{
return pattern.startsWith('*');
}
static bool isNextElementBrackets(QStringView pattern)
{
if (pattern.startsWith(QLatin1String("[!")) || pattern.startsWith(QLatin1String("["))) {
int nextClosingBracket = findNextUnescapedCharacter(pattern, ']');
if (nextClosingBracket != -1) {
// NOTE: spec doesn't specify that if / appears in the sequence, then treat the entire thing as literal
QString sequence(pattern.data(), nextClosingBracket);
return !sequence.contains('/');
}
}
return false;
}
static bool isNextElementBraces(QStringView pattern)
{
if (pattern.startsWith(QLatin1String("{"))) {
int nextClosingBrace = findNextUnescapedCharacter(pattern, '}');
if (nextClosingBrace != -1) {
// NOTE: spec doesn't specify that if ',' doesnt appears in the sequence, then treat the entire thing as literal
QString stringList(pattern.data(), nextClosingBrace);
return stringList.contains(',');
}
}
return false;
}
static int findMatchingBrace(QStringView pattern)
{
int braceCount = 0;
int i = 0;
while (i < pattern.size()) {
if (isNextElementLiteral(&pattern.data()[i])) {
// Skip any literals
i += 2;
}
else if (pattern[i] == '{') {
braceCount++;
i++;
}
else if (pattern[i] == '}') {
braceCount--;
if (braceCount == 0)
return i;
i++;
}
else {
i++;
}
}
return -1; // Not found
}
static QString patternToRegex(QString &pattern)
{
QString regex;
const QChar *data = pattern.data();
while (!data->isNull()) {
if (isNextElementLiteral(data)) {
regex.append('\\' + data[1]);
data += 2;
}
else if (isNextElementOptionalDirectory(data)) {
regex.append("(\\/|\\/.*\\/)");
data += 4;
}
else if (isNextElementAll(data)) {
regex.append(".*?");
data += 2;
}
else if (isNextElementAllExceptSlash(data)) {
regex.append("[^/]*");
data++;
}
else if (data[0] == '?') {
regex.append("[^/]");
data++;
}
else if (isNextElementBrackets(data)) {
regex.append('[');
data++;
if (data[0] == '!') {
regex.append('^');
data++;
}
int len = findNextUnescapedCharacter(data, ']');
regex.append(data, len);
data += len;
regex.append(']');
data++;
}
else if (isNextElementBraces(data)) {
int len = findMatchingBrace(data);
if (len == -1) {
// No matching brace was found, so use the rest of the string as a literal
regex.append(QRegularExpression::escape(QString(data)));
break;
}
else {
len--; // Drop off the last }
regex.append('(');
data++;
QString rawList(data, len);
data += len;
QStringList stringList = rawList.split(',', Qt::KeepEmptyParts);
bool hasEmptyElement = stringList.removeAll(QString("")) > 0;
regex.append(stringList.join('|'));
regex.append(')');
if (hasEmptyElement) {
regex.append('?');
}
data++;
}
}
else {
regex.append(QRegularExpression::escape(data[0]));
data++;
}
}
return regex;
}
static bool isComment(QStringView line)
{
return line[0] == '#' || line[0] == ';';
}
static bool isSectionHeader(QStringView line)
{
return line.startsWith('[') && line.endsWith(']');
}
static bool isKeyValuePair(QStringView line)
{
return line.contains('=');
}
static bool isPatternRelativeToConfigFile(QStringView pattern)
{
// If the pattern contains an unescaped / then it is relative to the config file
return findNextUnescapedCharacter(pattern, '/') != -1;
}
EditorConfigSettings settingsFromConfigFile(const QString &ecFilePath, const QString &absoluteFilePath)
{
// The file may not exist
if (!QFileInfo::exists(ecFilePath)) {
throw EditorConfigFileNotFound();
}
QFile inputFile(ecFilePath);
inputFile.open(QIODevice::ReadOnly | QIODevice::Text);
bool isInApplicableSection = false;
bool isInPreamble = true;
EditorConfigSettings settings;
QTextStream in(&inputFile);
while (!in.atEnd()) {
const QString line = in.readLine().trimmed();
if (line.isEmpty()) {
continue;
}
else if (isComment(line)) {
continue;
}
else if (isSectionHeader(line)) {
// We've hit the first section so not in the preamble any more
isInPreamble = false;
// Drop leading [ and trailing ]
QString sectionName = line.mid(1, line.length() - 2);
if (sectionName.length() <= MAX_SECTION_NAME_LENGTH) {
if (isPatternRelativeToConfigFile(sectionName)) {
// Make sure it starts with /
if (!sectionName.startsWith('/')) {
sectionName.prepend('/');
}
}
else {
// It can be anywhere within the directory
sectionName.prepend("/**/");
}
QString regexString = QRegularExpression::escape(QFileInfo(inputFile).dir().canonicalPath());
regexString += patternToRegex(sectionName) + '$';
QRegularExpression re(regexString);
if (re.isValid()) {
isInApplicableSection = re.match(absoluteFilePath).hasMatch();
}
else {
isInApplicableSection = false;
}
}
else {
isInApplicableSection = false;
}
}
else if (isKeyValuePair(line)) {
QString key = line
.left(line.indexOf('='))
.trimmed()
.toLower();
QString value = line
.right(line.size() - line.indexOf('=') - 1) // Extra 1 for the =
.trimmed();
if (key.length() <= MAX_KEY_LENGTH && value.length() <= MAX_VALUE_LENGTH) {
if (isInPreamble) {
if (key == "root") {
settings.insert(key, value.toLower());
}
// ignore everything else in the preamble
}
else if (isInApplicableSection) {
settings.insert(key, value);
}
}
}
else {
// Unknown line. No sane way to recover so clear all settings and stop
settings.clear();
break;
}
}
inputFile.close();
return settings;
}
static EditorConfigSettings mergeMaps(QVector<EditorConfigSettings> &maps)
{
EditorConfigSettings mergedMap;
for (const auto &map : maps) {
for (const auto &kv : map.keys()) {
mergedMap.insert(kv, map[kv]);
}
}
return mergedMap;
}
static void postProcessSettings(EditorConfigSettings &settings)
{
// if indent_style == "tab" and !indent_size: indent_size = "tab"
if (settings.contains("indent_style") && settings["indent_style"] == "tab" && !settings.contains("indent_size")) {
settings["indent_size"] = "tab";
}
// if indent_size != "tab" and !tab_width: tab_width = indent_size
if (settings.contains("indent_size") && settings["indent_size"] != "tab" && !settings.contains("tab_width")) {
settings["tab_width"] = settings["indent_size"];
}
// if indent_size == "tab": indent_size = tab_width
if (settings.contains("indent_size") && settings["indent_size"] == "tab" && settings.contains("tab_width")) {
settings["indent_size"] = settings["tab_width"];
}
// Don't need the "root" key saved any more
settings.remove("root");
}
EditorConfigSettings EditorConfig::getFileSettings(const QString &filePath, const QString &configName)
{
const QString absoluteFilePath = QDir(filePath).absolutePath();
QStringList locationsToCheck = getAllPossibleConfigFileLocations(absoluteFilePath, configName);
QVector<EditorConfigSettings> individualConfigFileSettings;
foreach (const QString &ecFilePath, locationsToCheck) {
try {
EditorConfigSettings configFileSettings = settingsFromConfigFile(ecFilePath, absoluteFilePath);
// Config files higher up in the directory tree are applied first and can get overriden
// by settings in lower (closer) config files, so prepend settings as we go up the directory tree.
individualConfigFileSettings.prepend(configFileSettings);
// Check to see if we are done yet
if (configFileSettings.contains("root") && configFileSettings["root"] == "true") {
break;
}
}
catch (const EditorConfigFileNotFound &e) {
Q_UNUSED(e);
}
}
EditorConfigSettings settings = mergeMaps(individualConfigFileSettings);
postProcessSettings(settings);
return settings;
}