ClangFormat: Make more readable helper functions

Change-Id: I25828a17104a778d4be156bf2a2879cefbd08691
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
This commit is contained in:
Ivan Donchevskii
2018-10-17 11:00:24 +02:00
parent 075ead9cd9
commit 918713a059
2 changed files with 25 additions and 28 deletions

View File

@@ -145,14 +145,14 @@ int utf8NthLineOffset(const QTextDocument *textDocument, const QByteArray &buffe
if (textDocument->characterCount() == buffer.size() + 1) if (textDocument->characterCount() == buffer.size() + 1)
return textDocument->findBlockByNumber(line - 1).position(); return textDocument->findBlockByNumber(line - 1).position();
int pos = 0; int utf8Offset = 0;
for (int count = 0; count < line - 1; ++count) { for (int count = 0; count < line - 1; ++count) {
pos = buffer.indexOf('\n', pos); utf8Offset = buffer.indexOf('\n', utf8Offset);
if (pos == -1) if (utf8Offset == -1)
return -1; return -1; // The line does not exist.
++pos; ++utf8Offset;
} }
return pos; return utf8Offset;
} }
} // Text } // Text

View File

@@ -133,12 +133,12 @@ void trimFirstNonEmptyBlock(const QTextBlock &currentBlock)
if (!initialText.at(initialText.size() - 1).isSpace()) if (!initialText.at(initialText.size() - 1).isSpace())
return; return;
int extraSpaceCount = 1; auto lastNonSpace = std::find_if_not(initialText.rbegin(),
for (int i = initialText.size() - 2; i >= 0; --i) { initialText.rend(),
if (!initialText.at(i).isSpace()) [](const QChar &letter) {
break; return letter.isSpace();
++extraSpaceCount; });
} const int extraSpaceCount = static_cast<int>(std::distance(initialText.rbegin(), lastNonSpace));
QTextCursor cursor(prevBlock); QTextCursor cursor(prevBlock);
cursor.beginEditBlock(); cursor.beginEditBlock();
@@ -249,28 +249,25 @@ Utils::LineColumn utf16LineColumn(const QTextBlock &block,
const QByteArray &utf8Buffer, const QByteArray &utf8Buffer,
int utf8Offset) int utf8Offset)
{ {
// Do not search if the offset is less - we are not interested.
if (utf8Offset < blockOffsetUtf8 - 1) if (utf8Offset < blockOffsetUtf8 - 1)
return Utils::LineColumn(); return Utils::LineColumn();
// If lastIndexOf('\n') returns -1 then we are fine to add 1 and get 0 offset.
const int lineStartUtf8Offset = utf8Buffer.lastIndexOf('\n', utf8Offset - 1) + 1;
int line = block.blockNumber() + 1; // Init with the line corresponding the block.
if (utf8Offset == blockOffsetUtf8 - 1) { if (utf8Offset == blockOffsetUtf8 - 1) {
const int lineStart = utf8Buffer.lastIndexOf('\n', utf8Offset - 1) + 1; // Our offset is the end of the previous line
const QByteArray lineText = utf8Buffer.mid(lineStart, utf8Offset - lineStart); --line;
return Utils::LineColumn(block.blockNumber(), QString::fromUtf8(lineText).size() + 1); } else {
line += static_cast<int>(std::count(utf8Buffer.begin() + blockOffsetUtf8,
utf8Buffer.begin() + lineStartUtf8Offset,
'\n'));
} }
int pos = blockOffsetUtf8; const QByteArray lineText = utf8Buffer.mid(lineStartUtf8Offset,
int prevPos = pos; utf8Offset - lineStartUtf8Offset);
int line = block.blockNumber(); // Start with previous line.
while (pos != -1 && pos <= utf8Offset) {
// Find the first pos which comes after offset and take the previous line.
++line;
prevPos = pos;
pos = utf8Buffer.indexOf('\n', pos);
if (pos != -1)
++pos;
}
const QByteArray lineText = utf8Buffer.mid(prevPos, utf8Offset - prevPos);
return Utils::LineColumn(line, QString::fromUtf8(lineText).size() + 1); return Utils::LineColumn(line, QString::fromUtf8(lineText).size() + 1);
} }