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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "tabsettings.h"
#include <QDebug>
#include <QRandomGenerator>
#include <QTextCursor>
#include <QTextDocument>
static const char spacesForTabsKey[] = "SpacesForTabs";
static const char autoDetectKey[] = "AutoDetect";
static const char tabSizeKey[] = "TabSize";
static const char indentSizeKey[] = "IndentSize";
static const char paddingModeKey[] = "PaddingMode";
using namespace Utils;
namespace TextEditor {
TabSettings::TabSettings(TabSettings::TabPolicy tabPolicy,
int tabSize,
int indentSize,
TabSettings::ContinuationAlignBehavior continuationAlignBehavior)
: m_tabPolicy(tabPolicy)
, m_tabSize(tabSize)
, m_indentSize(indentSize)
, m_continuationAlignBehavior(continuationAlignBehavior)
{
}
Store TabSettings::toMap() const
{
return {
{spacesForTabsKey, m_tabPolicy != TabsOnlyTabPolicy},
{autoDetectKey, m_autoDetect},
{tabSizeKey, m_tabSize},
{indentSizeKey, m_indentSize},
{paddingModeKey, m_continuationAlignBehavior}
};
}
void TabSettings::fromMap(const Store &map)
{
const bool spacesForTabs = map.value(spacesForTabsKey, true).toBool();
m_autoDetect = map.value(autoDetectKey, true).toBool();
m_tabPolicy = spacesForTabs ? SpacesOnlyTabPolicy : TabsOnlyTabPolicy;
m_tabSize = map.value(tabSizeKey, m_tabSize).toInt();
m_indentSize = map.value(indentSizeKey, m_indentSize).toInt();
m_continuationAlignBehavior = (ContinuationAlignBehavior)
map.value(paddingModeKey, m_continuationAlignBehavior).toInt();
}
TabSettings TabSettings::autoDetect(const QTextDocument *document) const
{
QTC_ASSERT(document, return *this);
if (!m_autoDetect)
return *this;
int totalIndentations = 0;
int indentationWithTabs = 0;
QMap<int, int> indentCount;
QMap<int, int> deltaCount;
auto checkText =
[this, document, &totalIndentations, &indentCount, &indentationWithTabs](const QTextBlock &block) {
if (block.length() == 0)
return 0;
int pos = block.position();
bool hasTabs = false;
int indentation = 0;
// iterate ove the characters in the document is faster since we do not have to allocate
// a string for each block text when we are only interested in the first few characters
QChar c = document->characterAt(pos);
while (c.isSpace() && c != QChar::ParagraphSeparator) {
if (c == QChar::Tabulation) {
hasTabs = true;
indentation += m_tabSize;
} else {
++indentation;
}
c = document->characterAt(++pos);
}
// only track indentations that are at least 2 columns wide
if (indentation > 1) {
if (hasTabs)
++indentationWithTabs;
++indentCount[indentation];
++totalIndentations;
return indentation;
}
return 0;
};
const int blockCount = document->blockCount();
bool useDefault = true;
if (blockCount < 400) {
// check the indentation of all blocks if the document is shorter than 400 lines
int previousIndentation = 0;
for (QTextBlock block = document->firstBlock(); block.isValid(); block = block.next()) {
const int indentation = checkText(block);
const int delta = indentation - previousIndentation;
if (delta != 0)
++deltaCount[qAbs(delta)];
previousIndentation = indentation;
}
// We checked all, so if we find any indented line, it makes sense to use it:
useDefault = totalIndentations == 0;
} else {
// scanning the first and last 25 lines specifically since those most probably contain
// different indentations
const int startEndDelta = 25;
int previousStartIndentation = 0;
int previousEndIndentation = 0;
for (int i = 0; i < startEndDelta; ++i) {
int indentation = checkText(document->findBlockByNumber(i));
int delta = indentation - previousStartIndentation;
if (delta != 0)
++deltaCount[qAbs(delta)];
previousStartIndentation = indentation;
indentation = checkText(document->findBlockByNumber(blockCount - 1 - i));
delta = indentation - previousEndIndentation;
if (delta != 0)
++deltaCount[qAbs(delta)];
previousEndIndentation = indentation;
}
// scan random lines until we have 200 indentations or checked a maximum of 2000 lines
// to limit the number of checks for large documents
QRandomGenerator gen(QDateTime::currentMSecsSinceEpoch());
int checks = 0;
QMap<int, int> alreadyCheckedIndentations;
while (totalIndentations < 200) {
++checks;
if (checks > 2000)
break;
const int blockNumber = gen.bounded(startEndDelta + 1, blockCount - startEndDelta - 12);
auto it = alreadyCheckedIndentations.find(blockNumber);
bool alreadyChecked = it != alreadyCheckedIndentations.end();
if (!alreadyChecked) {
it = alreadyCheckedIndentations
.insert(blockNumber, checkText(document->findBlockByNumber(blockNumber)));
};
int previousIndentation = it.value();
for (int i = 1; i <= 10; ++i) {
auto it = alreadyCheckedIndentations.find(blockNumber + i);
if (it != alreadyCheckedIndentations.end()) {
if (alreadyChecked)
continue;
alreadyChecked = true;
} else {
it = alreadyCheckedIndentations.insert(
blockNumber + i, checkText(document->findBlockByNumber(blockNumber + i)));
}
const int indentation = it.value();
const int delta = indentation - previousIndentation;
if (delta != 0)
++deltaCount[qAbs(delta)];
previousIndentation = indentation;
}
}
// Don't determine indentation for the whole file from few actually indented lines that we
// managed to find:
useDefault = totalIndentations < 3;
}
if (useDefault)
return *this;
// find the most common delta
int mostCommonDelta = 0;
int mostCommonDeltaCount = 0;
for (auto it = deltaCount.cbegin(); it != deltaCount.cend(); ++it) {
if (const int count = it.value(); count > mostCommonDeltaCount) {
mostCommonDeltaCount = count;
mostCommonDelta = it.key();
}
}
// find the most common indent
int mostCommonIndent = 0;
int mostCommonIndentCount = 0;
for (auto it = indentCount.cbegin(); it != indentCount.cend(); ++it) {
if (const int count = it.value(); count > mostCommonIndentCount) {
mostCommonIndentCount = count;
mostCommonIndent = it.key();
}
}
// check whether the most common delta is a fraction of the most common indent
if (mostCommonDeltaCount >= 4 && mostCommonIndent % mostCommonDelta == 0) {
TabSettings result = *this;
result.m_indentSize = mostCommonDelta;
double relativeTabCount = double(indentationWithTabs) / double(totalIndentations);
result.m_tabPolicy = relativeTabCount > 0.5 ? TabSettings::TabsOnlyTabPolicy
: TabSettings::SpacesOnlyTabPolicy;
return result;
}
for (auto it = indentCount.cbegin(); it != indentCount.cend(); ++it) {
// check whether the smallest indent is a fraction of the most common indent
// to filter out some false positives
if (mostCommonIndent % it.key() == 0) {
TabSettings result = *this;
result.m_indentSize = it.key();
double relativeTabCount = double(indentationWithTabs) / double(totalIndentations);
result.m_tabPolicy = relativeTabCount > 0.5 ? TabSettings::TabsOnlyTabPolicy
: TabSettings::SpacesOnlyTabPolicy;
return result;
}
}
return *this;
}
bool TabSettings::cursorIsAtBeginningOfLine(const QTextCursor &cursor)
{
QString text = cursor.block().text();
int fns = firstNonSpace(text);
return (cursor.position() - cursor.block().position() <= fns);
}
int TabSettings::lineIndentPosition(const QString &text) const
{
int i = 0;
while (i < text.size()) {
if (!text.at(i).isSpace())
break;
++i;
}
int column = columnAt(text, i);
return i - (column % m_indentSize);
}
int TabSettings::firstNonSpace(const QString &text)
{
int i = 0;
while (i < text.size()) {
if (!text.at(i).isSpace())
return i;
++i;
}
return i;
}
QString TabSettings::indentationString(const QString &text)
{
return text.left(firstNonSpace(text));
}
int TabSettings::indentationColumn(const QString &text) const
{
return columnAt(text, firstNonSpace(text));
}
int TabSettings::maximumPadding(const QString &text)
{
int fns = firstNonSpace(text);
int i = fns;
while (i > 0) {
if (text.at(i-1) != QLatin1Char(' '))
break;
--i;
}
return fns - i;
}
int TabSettings::trailingWhitespaces(const QString &text)
{
int i = 0;
while (i < text.size()) {
if (!text.at(text.size()-1-i).isSpace())
return i;
++i;
}
return i;
}
void TabSettings::removeTrailingWhitespace(const QTextBlock &block)
{
if (const int trailing = trailingWhitespaces(block.text())) {
QTextCursor cursor(block);
cursor.setPosition(block.position() + block.length() - 1);
cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor, trailing);
cursor.removeSelectedText();
}
}
bool TabSettings::isIndentationClean(const QTextBlock &block, const int indent) const
{
int i = 0;
int spaceCount = 0;
QString text = block.text();
while (i < text.size()) {
QChar c = text.at(i);
if (!c.isSpace())
return true;
if (c == QLatin1Char(' ')) {
++spaceCount;
if (spaceCount == m_tabSize)
if (m_tabPolicy == TabsOnlyTabPolicy)
if ((m_continuationAlignBehavior != ContinuationAlignWithSpaces) || (i < indent))
return false;
if (spaceCount > indent && m_continuationAlignBehavior == NoContinuationAlign)
return false;
} else if (c == QLatin1Char('\t')) {
if (m_tabPolicy == SpacesOnlyTabPolicy || (spaceCount != 0))
return false;
if ((m_continuationAlignBehavior != ContinuationAlignWithIndent) && ((i + 1) * m_tabSize > indent))
return false;
}
++i;
}
return true;
}
int TabSettings::columnAt(const QString &text, int position) const
{
int column = 0;
for (int i = 0; i < position; ++i) {
if (text.at(i) == QLatin1Char('\t'))
column = column - (column % m_tabSize) + m_tabSize;
else
++column;
}
return column;
}
int TabSettings::columnAtCursorPosition(const QTextCursor &cursor) const
{
return columnAt(cursor.block().text(), cursor.positionInBlock());
}
int TabSettings::positionAtColumn(const QString &text, int column, int *offset, bool allowOverstep) const
{
int col = 0;
int i = 0;
int textSize = text.size();
while ((i < textSize || allowOverstep) && col < column) {
if (i < textSize && text.at(i) == QLatin1Char('\t'))
col = col - (col % m_tabSize) + m_tabSize;
else
++col;
++i;
}
if (offset)
*offset = column - col;
return i;
}
int TabSettings::columnCountForText(const QString &text, int startColumn) const
{
int column = startColumn;
for (auto c : text) {
if (c == QLatin1Char('\t'))
column = column - (column % m_tabSize) + m_tabSize;
else
++column;
}
return column - startColumn;
}
int TabSettings::spacesLeftFromPosition(const QString &text, int position)
{
if (position > text.size())
return 0;
int i = position;
while (i > 0) {
if (!text.at(i-1).isSpace())
break;
--i;
}
return position - i;
}
int TabSettings::indentedColumn(int column, bool doIndent) const
{
int aligned = (column / m_indentSize) * m_indentSize;
if (doIndent)
return aligned + m_indentSize;
if (aligned < column)
return aligned;
return qMax(0, aligned - m_indentSize);
}
QString TabSettings::indentationString(int startColumn, int targetColumn, int padding) const
{
targetColumn = qMax(startColumn, targetColumn);
if (m_tabPolicy == SpacesOnlyTabPolicy)
return QString(targetColumn - startColumn, QLatin1Char(' '));
QString s;
int alignedStart = startColumn == 0 ? 0 : startColumn - (startColumn % m_tabSize) + m_tabSize;
if (alignedStart > startColumn && alignedStart <= targetColumn) {
s += QLatin1Char('\t');
startColumn = alignedStart;
}
if (m_continuationAlignBehavior == NoContinuationAlign) {
targetColumn -= padding;
padding = 0;
} else if (m_continuationAlignBehavior == ContinuationAlignWithIndent) {
padding = 0;
}
const int columns = targetColumn - padding - startColumn;
const int tabs = columns / m_tabSize;
s += QString(tabs, QLatin1Char('\t'));
s += QString(targetColumn - startColumn - tabs * m_tabSize, QLatin1Char(' '));
return s;
}
void TabSettings::indentLine(const QTextBlock &block, int newIndent, int padding) const
{
const QString text = block.text();
const int oldBlockLength = text.size();
if (m_continuationAlignBehavior == NoContinuationAlign) {
newIndent -= padding;
padding = 0;
} else if (m_continuationAlignBehavior == ContinuationAlignWithIndent) {
padding = 0;
}
// Quickly check whether indenting is required.
// fixme: after changing "use spaces for tabs" the change was not reflected
// because of the following optimisation. Commenting it out for now.
// if (indentationColumn(text) == newIndent)
// return;
const QString indentString = indentationString(0, newIndent, padding);
if (oldBlockLength == indentString.length() && text == indentString)
return;
QTextCursor cursor(block);
cursor.beginEditBlock();
cursor.movePosition(QTextCursor::StartOfBlock);
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, firstNonSpace(text));
cursor.removeSelectedText();
cursor.insertText(indentString);
cursor.endEditBlock();
}
void TabSettings::reindentLine(QTextBlock block, int delta) const
{
const QString text = block.text();
const int oldBlockLength = text.size();
int oldIndent = indentationColumn(text);
int newIndent = qMax(oldIndent + delta, 0);
if (oldIndent == newIndent)
return;
int padding = 0;
// user likes tabs for spaces and uses tabs for indentation, preserve padding
if (m_tabPolicy == TabsOnlyTabPolicy && m_tabSize == m_indentSize)
padding = qMin(maximumPadding(text), newIndent);
const QString indentString = indentationString(0, newIndent, padding);
if (oldBlockLength == indentString.length() && text == indentString)
return;
QTextCursor cursor(block);
cursor.beginEditBlock();
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, firstNonSpace(text));
cursor.removeSelectedText();
cursor.insertText(indentString);
cursor.endEditBlock();
}
bool TabSettings::equals(const TabSettings &ts) const
{
return m_autoDetect == ts.m_autoDetect
&& m_tabPolicy == ts.m_tabPolicy
&& m_tabSize == ts.m_tabSize
&& m_indentSize == ts.m_indentSize
&& m_continuationAlignBehavior == ts.m_continuationAlignBehavior;
}
static TabSettings::Retriever g_retriever = [](const FilePath &) { return TabSettings{}; };
void TabSettings::setRetriever(const Retriever &retriever) { g_retriever = retriever; }
TabSettings TabSettings::settingsForFile(const Utils::FilePath &filePath)
{
return g_retriever(filePath);
}
} // namespace TextEditor
|