2019-01-16 09:37:54 +01:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2019 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "clangformatbaseindenter.h"
|
|
|
|
|
|
|
|
|
|
#include <clang/Tooling/Core/Replacement.h>
|
|
|
|
|
|
2019-01-28 08:13:33 +01:00
|
|
|
#include <utils/algorithm.h>
|
2019-01-16 09:37:54 +01:00
|
|
|
#include <utils/fileutils.h>
|
|
|
|
|
#include <utils/textutils.h>
|
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
|
|
|
|
#include <QTextDocument>
|
2019-03-06 12:47:45 +01:00
|
|
|
#include <QDebug>
|
2019-01-16 09:37:54 +01:00
|
|
|
|
|
|
|
|
namespace ClangFormat {
|
|
|
|
|
|
2019-02-19 14:30:52 +01:00
|
|
|
namespace {
|
|
|
|
|
void adjustFormatStyleForLineBreak(clang::format::FormatStyle &style,
|
|
|
|
|
ReplacementsToKeep replacementsToKeep)
|
2019-01-16 09:37:54 +01:00
|
|
|
{
|
2019-02-19 12:46:38 +01:00
|
|
|
style.MaxEmptyLinesToKeep = 100;
|
2019-02-08 12:31:53 +01:00
|
|
|
style.SortIncludes = false;
|
|
|
|
|
style.SortUsingDeclarations = false;
|
|
|
|
|
|
|
|
|
|
// This is a separate pass, don't do it unless it's the full formatting.
|
|
|
|
|
style.FixNamespaceComments = false;
|
|
|
|
|
|
|
|
|
|
if (replacementsToKeep == ReplacementsToKeep::IndentAndBefore)
|
|
|
|
|
return;
|
|
|
|
|
|
2019-01-16 09:37:54 +01:00
|
|
|
style.DisableFormat = false;
|
|
|
|
|
style.ColumnLimit = 0;
|
|
|
|
|
#ifdef KEEP_LINE_BREAKS_FOR_NON_EMPTY_LINES_BACKPORTED
|
|
|
|
|
style.KeepLineBreaksForNonEmptyLines = true;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-19 14:30:52 +01:00
|
|
|
llvm::StringRef clearExtraNewline(llvm::StringRef text)
|
2019-01-16 09:37:54 +01:00
|
|
|
{
|
|
|
|
|
while (text.startswith("\n\n"))
|
|
|
|
|
text = text.drop_front();
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-19 14:30:52 +01:00
|
|
|
clang::tooling::Replacements filteredReplacements(const QByteArray &buffer,
|
|
|
|
|
const clang::tooling::Replacements &replacements,
|
|
|
|
|
int utf8Offset,
|
|
|
|
|
int utf8Length,
|
|
|
|
|
ReplacementsToKeep replacementsToKeep)
|
2019-01-16 09:37:54 +01:00
|
|
|
{
|
|
|
|
|
clang::tooling::Replacements filtered;
|
|
|
|
|
for (const clang::tooling::Replacement &replacement : replacements) {
|
|
|
|
|
int replacementOffset = static_cast<int>(replacement.getOffset());
|
2019-02-19 14:30:52 +01:00
|
|
|
|
|
|
|
|
// Skip everything after.
|
|
|
|
|
if (replacementOffset >= utf8Offset + utf8Length)
|
|
|
|
|
return filtered;
|
|
|
|
|
|
|
|
|
|
const bool isNotIndentOrInRange = replacementOffset < utf8Offset - 1
|
|
|
|
|
|| buffer.at(replacementOffset) != '\n';
|
|
|
|
|
if (isNotIndentOrInRange && replacementsToKeep == ReplacementsToKeep::OnlyIndent)
|
2019-01-16 09:37:54 +01:00
|
|
|
continue;
|
|
|
|
|
|
2019-01-28 08:13:33 +01:00
|
|
|
llvm::StringRef text = replacementsToKeep == ReplacementsToKeep::OnlyIndent
|
|
|
|
|
? clearExtraNewline(replacement.getReplacementText())
|
|
|
|
|
: replacement.getReplacementText();
|
2019-01-16 09:37:54 +01:00
|
|
|
|
|
|
|
|
llvm::Error error = filtered.add(
|
|
|
|
|
clang::tooling::Replacement(replacement.getFilePath(),
|
|
|
|
|
static_cast<unsigned int>(replacementOffset),
|
|
|
|
|
replacement.getLength(),
|
|
|
|
|
text));
|
|
|
|
|
// Throws if error is not checked.
|
2019-01-28 08:13:33 +01:00
|
|
|
if (error) {
|
|
|
|
|
error = llvm::handleErrors(std::move(error),
|
|
|
|
|
[](const llvm::ErrorInfoBase &) -> llvm::Error {
|
|
|
|
|
return llvm::Error::success();
|
|
|
|
|
});
|
|
|
|
|
QTC_CHECK(!error && "Error must be a \"success\" at this point");
|
2019-01-16 09:37:54 +01:00
|
|
|
break;
|
2019-01-28 08:13:33 +01:00
|
|
|
}
|
2019-01-16 09:37:54 +01:00
|
|
|
}
|
|
|
|
|
return filtered;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-19 14:30:52 +01:00
|
|
|
void trimRHSWhitespace(const QTextBlock &block)
|
2019-01-16 09:37:54 +01:00
|
|
|
{
|
2019-02-19 14:30:52 +01:00
|
|
|
const QString initialText = block.text();
|
|
|
|
|
if (!initialText.rbegin()->isSpace())
|
2019-01-16 09:37:54 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
auto lastNonSpace = std::find_if_not(initialText.rbegin(),
|
|
|
|
|
initialText.rend(),
|
|
|
|
|
[](const QChar &letter) { return letter.isSpace(); });
|
|
|
|
|
const int extraSpaceCount = static_cast<int>(std::distance(initialText.rbegin(), lastNonSpace));
|
|
|
|
|
|
2019-02-19 14:30:52 +01:00
|
|
|
QTextCursor cursor(block);
|
2019-01-16 09:37:54 +01:00
|
|
|
cursor.beginEditBlock();
|
|
|
|
|
cursor.movePosition(QTextCursor::Right,
|
|
|
|
|
QTextCursor::MoveAnchor,
|
|
|
|
|
initialText.size() - extraSpaceCount);
|
|
|
|
|
cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, extraSpaceCount);
|
|
|
|
|
cursor.removeSelectedText();
|
|
|
|
|
cursor.endEditBlock();
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-07 12:29:10 +01:00
|
|
|
// We don't need other types so far.
|
|
|
|
|
enum class CharacterType { OpeningParen, OpeningBrace, Invalid };
|
|
|
|
|
|
|
|
|
|
CharacterType firstOpeningParenOrBraceBeforeBlock(const QTextBlock &block)
|
|
|
|
|
{
|
|
|
|
|
if (block.text().trimmed().startsWith(')'))
|
|
|
|
|
return CharacterType::OpeningParen;
|
|
|
|
|
|
|
|
|
|
QTextCursor cursor(block);
|
|
|
|
|
const QTextDocument *doc = block.document();
|
|
|
|
|
|
|
|
|
|
cursor.movePosition(QTextCursor::PreviousCharacter);
|
|
|
|
|
QChar currentChar = doc->characterAt(cursor.position());
|
|
|
|
|
|
|
|
|
|
int parenCount = 0;
|
|
|
|
|
int braceCount = 0;
|
|
|
|
|
|
|
|
|
|
while (cursor.position() > 0 && parenCount <= 0 && braceCount <= 0) {
|
|
|
|
|
cursor.movePosition(QTextCursor::PreviousCharacter);
|
|
|
|
|
currentChar = doc->characterAt(cursor.position());
|
|
|
|
|
if (currentChar == '(')
|
|
|
|
|
++parenCount;
|
|
|
|
|
else if (currentChar == ')')
|
|
|
|
|
--parenCount;
|
|
|
|
|
else if (currentChar == '{')
|
|
|
|
|
++braceCount;
|
|
|
|
|
else if (currentChar == '}')
|
|
|
|
|
--braceCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (braceCount > 0)
|
|
|
|
|
return CharacterType::OpeningBrace;
|
|
|
|
|
if (parenCount > 0)
|
|
|
|
|
return CharacterType::OpeningParen;
|
|
|
|
|
|
|
|
|
|
return CharacterType::Invalid;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-19 14:30:52 +01:00
|
|
|
// Add extra text in case of the empty line or the line starting with ')'.
|
|
|
|
|
// Track such extra pieces of text in isInsideModifiedLine().
|
2019-03-06 14:05:31 +01:00
|
|
|
int forceIndentWithExtraText(QByteArray &buffer,
|
|
|
|
|
QByteArray &dummyText,
|
|
|
|
|
const QTextBlock &block,
|
|
|
|
|
bool secondTry)
|
2019-01-16 09:37:54 +01:00
|
|
|
{
|
2019-01-28 08:13:33 +01:00
|
|
|
const QString blockText = block.text();
|
|
|
|
|
int firstNonWhitespace = Utils::indexOf(blockText,
|
|
|
|
|
[](const QChar &ch) { return !ch.isSpace(); });
|
2019-02-19 12:46:38 +01:00
|
|
|
int utf8Offset = Utils::Text::utf8NthLineOffset(block.document(),
|
|
|
|
|
buffer,
|
|
|
|
|
block.blockNumber() + 1);
|
2019-02-20 08:43:29 +01:00
|
|
|
if (firstNonWhitespace >= 0)
|
2019-02-18 15:56:50 +01:00
|
|
|
utf8Offset += firstNonWhitespace;
|
2019-02-19 12:46:38 +01:00
|
|
|
else
|
|
|
|
|
utf8Offset += blockText.length();
|
2019-01-28 08:13:33 +01:00
|
|
|
|
|
|
|
|
const bool closingParenBlock = firstNonWhitespace >= 0
|
|
|
|
|
&& blockText.at(firstNonWhitespace) == ')';
|
2019-03-07 12:29:10 +01:00
|
|
|
|
2019-02-19 12:46:38 +01:00
|
|
|
int extraLength = 0;
|
2019-01-28 08:13:33 +01:00
|
|
|
if (firstNonWhitespace < 0 || closingParenBlock) {
|
2019-03-06 14:05:31 +01:00
|
|
|
if (dummyText.isEmpty()) {
|
2019-03-07 12:29:10 +01:00
|
|
|
const CharacterType charType = firstOpeningParenOrBraceBeforeBlock(block);
|
2019-03-06 14:05:31 +01:00
|
|
|
// If we don't know yet the dummy text, let's guess it and use for this line and before.
|
2019-03-07 12:29:10 +01:00
|
|
|
if (charType != CharacterType::OpeningParen) {
|
|
|
|
|
// Use the complete statement if we are not inside parenthesis.
|
|
|
|
|
dummyText = "a;a;";
|
|
|
|
|
} else {
|
|
|
|
|
// Search for previous character
|
|
|
|
|
QTextBlock prevBlock = block.previous();
|
|
|
|
|
bool prevBlockIsEmpty = prevBlock.position() > 0
|
|
|
|
|
&& prevBlock.text().trimmed().isEmpty();
|
|
|
|
|
while (prevBlockIsEmpty) {
|
|
|
|
|
prevBlock = prevBlock.previous();
|
|
|
|
|
prevBlockIsEmpty = prevBlock.position() > 0
|
|
|
|
|
&& prevBlock.text().trimmed().isEmpty();
|
|
|
|
|
}
|
|
|
|
|
dummyText = prevBlock.text().endsWith(',') ? "&& a," : "&& a";
|
2019-03-06 14:05:31 +01:00
|
|
|
}
|
2019-01-16 09:37:54 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-18 15:56:50 +01:00
|
|
|
buffer.insert(utf8Offset, dummyText);
|
2019-02-19 12:46:38 +01:00
|
|
|
extraLength += dummyText.length();
|
2019-01-16 09:37:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (secondTry) {
|
2019-02-18 15:56:50 +01:00
|
|
|
int nextLinePos = buffer.indexOf('\n', utf8Offset);
|
2019-01-28 08:13:33 +01:00
|
|
|
if (nextLinePos < 0)
|
|
|
|
|
nextLinePos = buffer.size() - 1;
|
|
|
|
|
|
2019-01-16 09:37:54 +01:00
|
|
|
if (nextLinePos > 0) {
|
|
|
|
|
// If first try was not successful try to put ')' in the end of the line to close possibly
|
2019-03-07 12:29:10 +01:00
|
|
|
// unclosed parenthesis.
|
2019-01-16 09:37:54 +01:00
|
|
|
// TODO: Does it help to add different endings depending on the context?
|
|
|
|
|
buffer.insert(nextLinePos, ')');
|
2019-02-19 12:46:38 +01:00
|
|
|
extraLength += 1;
|
2019-01-16 09:37:54 +01:00
|
|
|
}
|
|
|
|
|
}
|
2019-02-19 12:46:38 +01:00
|
|
|
|
|
|
|
|
return extraLength;
|
2019-01-16 09:37:54 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-19 14:30:52 +01:00
|
|
|
bool isInsideModifiedLine(const QString &originalLine, const QString &modifiedLine, int column)
|
2019-02-19 12:46:38 +01:00
|
|
|
{
|
2019-02-19 14:30:52 +01:00
|
|
|
// Detect the cases when we have inserted extra text into the line to get the indentation.
|
2019-02-19 12:46:38 +01:00
|
|
|
return originalLine.length() < modifiedLine.length() && column != modifiedLine.length() + 1
|
|
|
|
|
&& (column > originalLine.length() || originalLine.trimmed().isEmpty()
|
|
|
|
|
|| !modifiedLine.startsWith(originalLine));
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-19 14:30:52 +01:00
|
|
|
TextEditor::Replacements utf16Replacements(const QTextDocument *doc,
|
|
|
|
|
const QByteArray &utf8Buffer,
|
|
|
|
|
const clang::tooling::Replacements &replacements)
|
2019-01-16 09:37:54 +01:00
|
|
|
{
|
|
|
|
|
TextEditor::Replacements convertedReplacements;
|
|
|
|
|
convertedReplacements.reserve(replacements.size());
|
2019-02-19 12:46:38 +01:00
|
|
|
|
2019-01-16 09:37:54 +01:00
|
|
|
for (const clang::tooling::Replacement &replacement : replacements) {
|
2019-02-19 14:30:52 +01:00
|
|
|
Utils::LineColumn lineColUtf16 = Utils::Text::utf16LineColumn(utf8Buffer,
|
|
|
|
|
static_cast<int>(
|
|
|
|
|
replacement.getOffset()));
|
2019-01-16 09:37:54 +01:00
|
|
|
if (!lineColUtf16.isValid())
|
|
|
|
|
continue;
|
2019-02-19 12:46:38 +01:00
|
|
|
|
|
|
|
|
const QString lineText = doc->findBlockByNumber(lineColUtf16.line - 1).text();
|
2019-02-19 14:30:52 +01:00
|
|
|
const QString bufferLineText
|
|
|
|
|
= Utils::Text::utf16LineTextInUtf8Buffer(utf8Buffer,
|
|
|
|
|
static_cast<int>(replacement.getOffset()));
|
2019-02-19 12:46:38 +01:00
|
|
|
if (isInsideModifiedLine(lineText, bufferLineText, lineColUtf16.column))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
lineColUtf16.column = std::min(lineColUtf16.column, lineText.length() + 1);
|
|
|
|
|
|
|
|
|
|
const int utf16Offset = Utils::Text::positionInText(doc,
|
2019-01-16 09:37:54 +01:00
|
|
|
lineColUtf16.line,
|
|
|
|
|
lineColUtf16.column);
|
|
|
|
|
const int utf16Length = QString::fromUtf8(
|
|
|
|
|
utf8Buffer.mid(static_cast<int>(replacement.getOffset()),
|
|
|
|
|
static_cast<int>(replacement.getLength())))
|
|
|
|
|
.size();
|
|
|
|
|
convertedReplacements.emplace_back(utf16Offset,
|
|
|
|
|
utf16Length,
|
|
|
|
|
QString::fromStdString(replacement.getReplacementText()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return convertedReplacements;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-19 14:30:52 +01:00
|
|
|
void applyReplacements(QTextDocument *doc, const TextEditor::Replacements &replacements)
|
2019-01-16 09:37:54 +01:00
|
|
|
{
|
|
|
|
|
if (replacements.empty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
int fullOffsetShift = 0;
|
2019-02-18 15:56:50 +01:00
|
|
|
QTextCursor editCursor(doc);
|
2019-01-16 09:37:54 +01:00
|
|
|
for (const TextEditor::Replacement &replacement : replacements) {
|
|
|
|
|
editCursor.beginEditBlock();
|
|
|
|
|
editCursor.setPosition(replacement.offset + fullOffsetShift);
|
|
|
|
|
editCursor.movePosition(QTextCursor::NextCharacter,
|
|
|
|
|
QTextCursor::KeepAnchor,
|
|
|
|
|
replacement.length);
|
|
|
|
|
editCursor.removeSelectedText();
|
|
|
|
|
editCursor.insertText(replacement.text);
|
|
|
|
|
editCursor.endEditBlock();
|
|
|
|
|
fullOffsetShift += replacement.text.length() - replacement.length;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-19 14:30:52 +01:00
|
|
|
QString selectedLines(QTextDocument *doc, const QTextBlock &startBlock, const QTextBlock &endBlock)
|
2019-01-16 09:37:54 +01:00
|
|
|
{
|
2019-02-19 12:46:38 +01:00
|
|
|
return Utils::Text::textAt(QTextCursor(doc),
|
|
|
|
|
startBlock.position(),
|
|
|
|
|
std::max(0,
|
|
|
|
|
endBlock.position() + endBlock.length()
|
|
|
|
|
- startBlock.position() - 1));
|
2019-01-16 09:37:54 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-19 14:30:52 +01:00
|
|
|
int indentationForBlock(const TextEditor::Replacements &toReplace,
|
|
|
|
|
const QByteArray &buffer,
|
|
|
|
|
const QTextBlock ¤tBlock)
|
|
|
|
|
{
|
|
|
|
|
const int utf8Offset = Utils::Text::utf8NthLineOffset(currentBlock.document(),
|
|
|
|
|
buffer,
|
|
|
|
|
currentBlock.blockNumber() + 1);
|
|
|
|
|
auto replacementIt = std::find_if(toReplace.begin(),
|
|
|
|
|
toReplace.end(),
|
|
|
|
|
[utf8Offset](const TextEditor::Replacement &replacement) {
|
|
|
|
|
return replacement.offset == utf8Offset - 1;
|
|
|
|
|
});
|
|
|
|
|
if (replacementIt == toReplace.end())
|
|
|
|
|
return -1;
|
2019-01-16 09:37:54 +01:00
|
|
|
|
2019-02-19 14:30:52 +01:00
|
|
|
int afterLineBreak = replacementIt->text.lastIndexOf('\n');
|
|
|
|
|
afterLineBreak = (afterLineBreak < 0) ? 0 : afterLineBreak + 1;
|
|
|
|
|
return static_cast<int>(replacementIt->text.size() - afterLineBreak);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool doNotIndentInContext(QTextDocument *doc, int pos)
|
2019-01-16 09:37:54 +01:00
|
|
|
{
|
2019-02-19 14:30:52 +01:00
|
|
|
const QChar character = doc->characterAt(pos);
|
|
|
|
|
const QTextBlock currentBlock = doc->findBlock(pos);
|
|
|
|
|
const QString text = currentBlock.text().left(pos - currentBlock.position());
|
|
|
|
|
// NOTE: check if "<<" and ">>" always work correctly.
|
|
|
|
|
switch (character.toLatin1()) {
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
case ':':
|
|
|
|
|
// Do not indent when it's the first ':' and it's not the 'case' line.
|
|
|
|
|
if (text.contains(QLatin1String("case")) || text.contains(QLatin1String("default"))
|
|
|
|
|
|| text.contains(QLatin1String("public")) || text.contains(QLatin1String("private"))
|
|
|
|
|
|| text.contains(QLatin1String("protected")) || text.contains(QLatin1String("signals"))
|
|
|
|
|
|| text.contains(QLatin1String("Q_SIGNALS"))) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (pos > 0 && doc->characterAt(pos - 1) != ':')
|
|
|
|
|
return true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2019-01-16 09:37:54 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-19 14:30:52 +01:00
|
|
|
QTextBlock reverseFindLastEmptyBlock(QTextBlock start)
|
2019-01-16 09:37:54 +01:00
|
|
|
{
|
2019-02-19 14:30:52 +01:00
|
|
|
if (start.position() > 0) {
|
|
|
|
|
start = start.previous();
|
|
|
|
|
while (start.position() > 0 && start.text().trimmed().isEmpty())
|
|
|
|
|
start = start.previous();
|
|
|
|
|
if (!start.text().trimmed().isEmpty())
|
|
|
|
|
start = start.next();
|
2019-01-16 09:37:54 +01:00
|
|
|
}
|
2019-02-19 14:30:52 +01:00
|
|
|
return start;
|
2019-01-16 09:37:54 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-19 14:30:52 +01:00
|
|
|
int formattingRangeStart(const QTextBlock ¤tBlock,
|
|
|
|
|
const QByteArray &buffer,
|
|
|
|
|
int documentRevision)
|
2019-01-16 09:37:54 +01:00
|
|
|
{
|
2019-02-19 14:30:52 +01:00
|
|
|
QTextBlock prevBlock = currentBlock.previous();
|
|
|
|
|
while ((prevBlock.position() > 0 || prevBlock.length() > 0)
|
|
|
|
|
&& prevBlock.revision() != documentRevision) {
|
|
|
|
|
// Find the first block with not matching revision.
|
|
|
|
|
prevBlock = prevBlock.previous();
|
|
|
|
|
}
|
|
|
|
|
if (prevBlock.revision() == documentRevision)
|
|
|
|
|
prevBlock = prevBlock.next();
|
|
|
|
|
|
|
|
|
|
return Utils::Text::utf8NthLineOffset(prevBlock.document(), buffer, prevBlock.blockNumber() + 1);
|
2019-01-16 09:37:54 +01:00
|
|
|
}
|
2019-02-19 14:30:52 +01:00
|
|
|
} // namespace
|
2019-01-16 09:37:54 +01:00
|
|
|
|
2019-02-19 14:30:52 +01:00
|
|
|
ClangFormatBaseIndenter::ClangFormatBaseIndenter(QTextDocument *doc)
|
|
|
|
|
: TextEditor::Indenter(doc)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
TextEditor::Replacements ClangFormatBaseIndenter::replacements(QByteArray buffer,
|
|
|
|
|
const QTextBlock &startBlock,
|
|
|
|
|
const QTextBlock &endBlock,
|
2019-02-20 08:43:29 +01:00
|
|
|
int cursorPositionInEditor,
|
2019-02-19 14:30:52 +01:00
|
|
|
ReplacementsToKeep replacementsToKeep,
|
|
|
|
|
const QChar &typedChar,
|
|
|
|
|
bool secondTry) const
|
2019-01-16 09:37:54 +01:00
|
|
|
{
|
2019-02-19 14:30:52 +01:00
|
|
|
QTC_ASSERT(replacementsToKeep != ReplacementsToKeep::All, return TextEditor::Replacements());
|
|
|
|
|
|
|
|
|
|
clang::format::FormatStyle style = styleForFile();
|
|
|
|
|
QByteArray originalBuffer = buffer;
|
|
|
|
|
|
|
|
|
|
int utf8Offset = Utils::Text::utf8NthLineOffset(m_doc, buffer, startBlock.blockNumber() + 1);
|
|
|
|
|
QTC_ASSERT(utf8Offset >= 0, return TextEditor::Replacements(););
|
|
|
|
|
int utf8Length = selectedLines(m_doc, startBlock, endBlock).toUtf8().size();
|
|
|
|
|
|
|
|
|
|
int rangeStart = 0;
|
|
|
|
|
if (replacementsToKeep == ReplacementsToKeep::IndentAndBefore)
|
|
|
|
|
rangeStart = formattingRangeStart(startBlock, buffer, lastSaveRevision());
|
|
|
|
|
|
|
|
|
|
adjustFormatStyleForLineBreak(style, replacementsToKeep);
|
2019-03-05 17:16:29 +01:00
|
|
|
if (replacementsToKeep == ReplacementsToKeep::OnlyIndent) {
|
2019-03-06 14:05:31 +01:00
|
|
|
QByteArray dummyText;
|
|
|
|
|
// Iterate backwards to reuse the same dummy text for all empty lines.
|
|
|
|
|
for (int index = endBlock.blockNumber(); index >= startBlock.blockNumber(); --index) {
|
2019-02-19 14:30:52 +01:00
|
|
|
utf8Length += forceIndentWithExtraText(buffer,
|
2019-03-06 14:05:31 +01:00
|
|
|
dummyText,
|
2019-03-05 17:16:29 +01:00
|
|
|
m_doc->findBlockByNumber(index),
|
2019-02-19 14:30:52 +01:00
|
|
|
secondTry);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (replacementsToKeep != ReplacementsToKeep::IndentAndBefore || utf8Offset < rangeStart)
|
|
|
|
|
rangeStart = utf8Offset;
|
|
|
|
|
|
|
|
|
|
unsigned int rangeLength = static_cast<unsigned int>(utf8Offset + utf8Length - rangeStart);
|
|
|
|
|
std::vector<clang::tooling::Range> ranges{{static_cast<unsigned int>(rangeStart), rangeLength}};
|
|
|
|
|
|
|
|
|
|
clang::format::FormattingAttemptStatus status;
|
|
|
|
|
clang::tooling::Replacements clangReplacements = reformat(style,
|
|
|
|
|
buffer.data(),
|
|
|
|
|
ranges,
|
|
|
|
|
m_fileName.toString().toStdString(),
|
|
|
|
|
&status);
|
|
|
|
|
|
|
|
|
|
clang::tooling::Replacements filtered;
|
|
|
|
|
if (status.FormatComplete) {
|
|
|
|
|
filtered = filteredReplacements(buffer,
|
|
|
|
|
clangReplacements,
|
|
|
|
|
utf8Offset,
|
|
|
|
|
utf8Length,
|
|
|
|
|
replacementsToKeep);
|
|
|
|
|
}
|
|
|
|
|
const bool canTryAgain = replacementsToKeep == ReplacementsToKeep::OnlyIndent
|
|
|
|
|
&& typedChar == QChar::Null && !secondTry;
|
|
|
|
|
if (canTryAgain && filtered.empty()) {
|
|
|
|
|
return replacements(originalBuffer,
|
|
|
|
|
startBlock,
|
|
|
|
|
endBlock,
|
2019-02-20 08:43:29 +01:00
|
|
|
cursorPositionInEditor,
|
2019-02-19 14:30:52 +01:00
|
|
|
replacementsToKeep,
|
|
|
|
|
typedChar,
|
|
|
|
|
true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return utf16Replacements(m_doc, buffer, filtered);
|
2019-01-16 09:37:54 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-13 14:17:21 +01:00
|
|
|
TextEditor::Replacements ClangFormatBaseIndenter::format(
|
|
|
|
|
const TextEditor::RangesInLines &rangesInLines)
|
2019-01-16 09:37:54 +01:00
|
|
|
{
|
2019-02-13 14:17:21 +01:00
|
|
|
if (rangesInLines.empty())
|
|
|
|
|
return TextEditor::Replacements();
|
|
|
|
|
|
2019-01-16 09:37:54 +01:00
|
|
|
const QByteArray buffer = m_doc->toPlainText().toUtf8();
|
2019-02-13 14:17:21 +01:00
|
|
|
std::vector<clang::tooling::Range> ranges;
|
|
|
|
|
ranges.reserve(rangesInLines.size());
|
|
|
|
|
|
|
|
|
|
for (auto &range : rangesInLines) {
|
|
|
|
|
const int utf8StartOffset = Utils::Text::utf8NthLineOffset(m_doc, buffer, range.startLine);
|
2019-02-19 14:30:52 +01:00
|
|
|
int utf8RangeLength = m_doc->findBlockByNumber(range.endLine - 1).text().toUtf8().size();
|
2019-02-13 14:17:21 +01:00
|
|
|
if (range.endLine > range.startLine) {
|
|
|
|
|
utf8RangeLength += Utils::Text::utf8NthLineOffset(m_doc, buffer, range.endLine)
|
|
|
|
|
- utf8StartOffset;
|
|
|
|
|
}
|
|
|
|
|
ranges.emplace_back(static_cast<unsigned int>(utf8StartOffset),
|
|
|
|
|
static_cast<unsigned int>(utf8RangeLength));
|
2019-01-16 09:37:54 +01:00
|
|
|
}
|
|
|
|
|
|
2019-03-06 12:47:45 +01:00
|
|
|
clang::format::FormatStyle style = styleForFile();
|
|
|
|
|
const std::string assumedFileName = m_fileName.toString().toStdString();
|
|
|
|
|
clang::tooling::Replacements clangReplacements = clang::format::sortIncludes(style,
|
|
|
|
|
buffer.data(),
|
|
|
|
|
ranges,
|
|
|
|
|
assumedFileName);
|
|
|
|
|
auto changedCode = clang::tooling::applyAllReplacements(buffer.data(), clangReplacements);
|
|
|
|
|
QTC_ASSERT(changedCode, {
|
|
|
|
|
qDebug() << QString::fromStdString(llvm::toString(changedCode.takeError()));
|
|
|
|
|
return TextEditor::Replacements();
|
|
|
|
|
});
|
|
|
|
|
ranges = clang::tooling::calculateRangesAfterReplacements(clangReplacements, ranges);
|
|
|
|
|
|
2019-02-13 14:17:21 +01:00
|
|
|
clang::format::FormattingAttemptStatus status;
|
2019-03-06 12:47:45 +01:00
|
|
|
const clang::tooling::Replacements formatReplacements
|
|
|
|
|
= reformat(style, *changedCode, ranges, m_fileName.toString().toStdString(), &status);
|
|
|
|
|
clangReplacements = clangReplacements.merge(formatReplacements);
|
|
|
|
|
|
2019-02-19 12:46:38 +01:00
|
|
|
const TextEditor::Replacements toReplace = utf16Replacements(m_doc, buffer, clangReplacements);
|
2019-02-18 15:56:50 +01:00
|
|
|
applyReplacements(m_doc, toReplace);
|
2019-01-16 09:37:54 +01:00
|
|
|
|
|
|
|
|
return toReplace;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-19 14:30:52 +01:00
|
|
|
TextEditor::Replacements ClangFormatBaseIndenter::indentsFor(QTextBlock startBlock,
|
|
|
|
|
const QTextBlock &endBlock,
|
|
|
|
|
const QChar &typedChar,
|
|
|
|
|
int cursorPositionInEditor)
|
2019-01-16 09:37:54 +01:00
|
|
|
{
|
2019-02-08 09:48:49 +01:00
|
|
|
if (typedChar != QChar::Null && cursorPositionInEditor > 0
|
|
|
|
|
&& m_doc->characterAt(cursorPositionInEditor - 1) == typedChar
|
|
|
|
|
&& doNotIndentInContext(m_doc, cursorPositionInEditor - 1)) {
|
2019-02-19 14:30:52 +01:00
|
|
|
return TextEditor::Replacements();
|
2019-02-08 09:48:49 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-19 14:30:52 +01:00
|
|
|
startBlock = reverseFindLastEmptyBlock(startBlock);
|
|
|
|
|
const int startBlockPosition = startBlock.position();
|
2019-02-19 12:46:38 +01:00
|
|
|
if (startBlock.position() > 0) {
|
2019-02-19 14:30:52 +01:00
|
|
|
trimRHSWhitespace(startBlock.previous());
|
|
|
|
|
if (cursorPositionInEditor >= 0)
|
|
|
|
|
cursorPositionInEditor += startBlock.position() - startBlockPosition;
|
2019-02-19 12:46:38 +01:00
|
|
|
}
|
|
|
|
|
|
2019-03-06 14:05:31 +01:00
|
|
|
const QByteArray buffer = m_doc->toPlainText().toUtf8();
|
|
|
|
|
|
2019-02-18 15:56:50 +01:00
|
|
|
ReplacementsToKeep replacementsToKeep = ReplacementsToKeep::OnlyIndent;
|
2019-01-28 08:13:33 +01:00
|
|
|
if (formatWhileTyping()
|
2019-02-18 15:56:50 +01:00
|
|
|
&& (cursorPositionInEditor == -1 || cursorPositionInEditor >= startBlockPosition)
|
2019-03-04 13:40:20 +01:00
|
|
|
&& (typedChar == ';' || typedChar == '}')) {
|
2019-01-28 08:13:33 +01:00
|
|
|
// Format before current position only in case the cursor is inside the indented block.
|
|
|
|
|
// So if cursor position is less then the block position then the current line is before
|
|
|
|
|
// the indented block - don't trigger extra formatting in this case.
|
|
|
|
|
// cursorPositionInEditor == -1 means the consition matches automatically.
|
2019-02-08 09:48:49 +01:00
|
|
|
|
|
|
|
|
// Format only before newline or complete statement not to break code.
|
2019-02-18 15:56:50 +01:00
|
|
|
replacementsToKeep = ReplacementsToKeep::IndentAndBefore;
|
2019-01-28 08:13:33 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-19 14:30:52 +01:00
|
|
|
return replacements(buffer,
|
|
|
|
|
startBlock,
|
|
|
|
|
endBlock,
|
2019-02-20 08:43:29 +01:00
|
|
|
cursorPositionInEditor,
|
2019-02-19 14:30:52 +01:00
|
|
|
replacementsToKeep,
|
|
|
|
|
typedChar);
|
|
|
|
|
}
|
2019-01-16 09:37:54 +01:00
|
|
|
|
2019-02-19 14:30:52 +01:00
|
|
|
void ClangFormatBaseIndenter::indentBlocks(const QTextBlock &startBlock,
|
|
|
|
|
const QTextBlock &endBlock,
|
|
|
|
|
const QChar &typedChar,
|
|
|
|
|
int cursorPositionInEditor)
|
|
|
|
|
{
|
2019-03-06 14:05:31 +01:00
|
|
|
applyReplacements(m_doc, indentsFor(startBlock, endBlock, typedChar, cursorPositionInEditor));
|
2019-02-19 14:30:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangFormatBaseIndenter::indent(const QTextCursor &cursor,
|
|
|
|
|
const QChar &typedChar,
|
|
|
|
|
int cursorPositionInEditor)
|
|
|
|
|
{
|
|
|
|
|
if (cursor.hasSelection()) {
|
|
|
|
|
indentBlocks(m_doc->findBlock(cursor.selectionStart()),
|
|
|
|
|
m_doc->findBlock(cursor.selectionEnd()),
|
|
|
|
|
typedChar,
|
|
|
|
|
cursorPositionInEditor);
|
|
|
|
|
} else {
|
|
|
|
|
indentBlocks(cursor.block(), cursor.block(), typedChar, cursorPositionInEditor);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangFormatBaseIndenter::indent(const QTextCursor &cursor,
|
|
|
|
|
const QChar &typedChar,
|
|
|
|
|
const TextEditor::TabSettings & /*tabSettings*/,
|
|
|
|
|
int cursorPositionInEditor)
|
|
|
|
|
{
|
|
|
|
|
indent(cursor, typedChar, cursorPositionInEditor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangFormatBaseIndenter::reindent(const QTextCursor &cursor,
|
|
|
|
|
const TextEditor::TabSettings & /*tabSettings*/,
|
|
|
|
|
int cursorPositionInEditor)
|
|
|
|
|
{
|
|
|
|
|
indent(cursor, QChar::Null, cursorPositionInEditor);
|
2019-01-16 09:37:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangFormatBaseIndenter::indentBlock(const QTextBlock &block,
|
|
|
|
|
const QChar &typedChar,
|
2019-01-28 08:11:20 +01:00
|
|
|
const TextEditor::TabSettings & /*tabSettings*/,
|
|
|
|
|
int cursorPositionInEditor)
|
2019-01-16 09:37:54 +01:00
|
|
|
{
|
2019-02-18 15:56:50 +01:00
|
|
|
indentBlocks(block, block, typedChar, cursorPositionInEditor);
|
2019-01-16 09:37:54 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-19 14:30:52 +01:00
|
|
|
int ClangFormatBaseIndenter::indentFor(const QTextBlock &block,
|
|
|
|
|
const TextEditor::TabSettings & /*tabSettings*/,
|
|
|
|
|
int cursorPositionInEditor)
|
2019-01-16 09:37:54 +01:00
|
|
|
{
|
2019-02-19 14:30:52 +01:00
|
|
|
TextEditor::Replacements toReplace = indentsFor(block,
|
|
|
|
|
block,
|
|
|
|
|
QChar::Null,
|
|
|
|
|
cursorPositionInEditor);
|
2019-01-16 09:37:54 +01:00
|
|
|
if (toReplace.empty())
|
|
|
|
|
return -1;
|
|
|
|
|
|
2019-03-06 14:05:31 +01:00
|
|
|
const QByteArray buffer = m_doc->toPlainText().toUtf8();
|
2019-02-19 14:30:52 +01:00
|
|
|
return indentationForBlock(toReplace, buffer, block);
|
2019-01-16 09:37:54 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-19 14:30:52 +01:00
|
|
|
TextEditor::IndentationForBlock ClangFormatBaseIndenter::indentationForBlocks(
|
|
|
|
|
const QVector<QTextBlock> &blocks,
|
|
|
|
|
const TextEditor::TabSettings & /*tabSettings*/,
|
|
|
|
|
int cursorPositionInEditor)
|
2019-01-16 09:37:54 +01:00
|
|
|
{
|
2019-02-19 14:30:52 +01:00
|
|
|
TextEditor::IndentationForBlock ret;
|
|
|
|
|
if (blocks.isEmpty())
|
|
|
|
|
return ret;
|
|
|
|
|
TextEditor::Replacements toReplace = indentsFor(blocks.front(),
|
|
|
|
|
blocks.back(),
|
|
|
|
|
QChar::Null,
|
|
|
|
|
cursorPositionInEditor);
|
|
|
|
|
|
2019-03-06 14:05:31 +01:00
|
|
|
const QByteArray buffer = m_doc->toPlainText().toUtf8();
|
2019-02-19 14:30:52 +01:00
|
|
|
for (const QTextBlock &block : blocks)
|
|
|
|
|
ret.insert(block.blockNumber(), indentationForBlock(toReplace, buffer, block));
|
|
|
|
|
return ret;
|
2019-01-16 09:37:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ClangFormatBaseIndenter::isElectricCharacter(const QChar &ch) const
|
|
|
|
|
{
|
|
|
|
|
switch (ch.toLatin1()) {
|
|
|
|
|
case '{':
|
|
|
|
|
case '}':
|
|
|
|
|
case ':':
|
|
|
|
|
case '#':
|
|
|
|
|
case '<':
|
|
|
|
|
case '>':
|
|
|
|
|
case ';':
|
|
|
|
|
case '(':
|
|
|
|
|
case ')':
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-28 07:54:05 +01:00
|
|
|
void ClangFormatBaseIndenter::formatOrIndent(const QTextCursor &cursor,
|
|
|
|
|
const TextEditor::TabSettings & /*tabSettings*/,
|
2019-01-28 08:11:20 +01:00
|
|
|
int cursorPositionInEditor)
|
2019-01-28 07:54:05 +01:00
|
|
|
{
|
2019-02-13 14:17:21 +01:00
|
|
|
if (formatCodeInsteadOfIndent()) {
|
|
|
|
|
QTextBlock start;
|
|
|
|
|
QTextBlock end;
|
|
|
|
|
if (cursor.hasSelection()) {
|
|
|
|
|
start = m_doc->findBlock(cursor.selectionStart());
|
|
|
|
|
end = m_doc->findBlock(cursor.selectionEnd());
|
|
|
|
|
} else {
|
|
|
|
|
start = end = cursor.block();
|
|
|
|
|
}
|
|
|
|
|
format({{start.blockNumber() + 1, end.blockNumber() + 1}});
|
|
|
|
|
} else {
|
2019-01-28 08:11:20 +01:00
|
|
|
indent(cursor, QChar::Null, cursorPositionInEditor);
|
2019-02-13 14:17:21 +01:00
|
|
|
}
|
2019-01-28 07:54:05 +01:00
|
|
|
}
|
|
|
|
|
|
2019-01-16 09:37:54 +01:00
|
|
|
clang::format::FormatStyle ClangFormatBaseIndenter::styleForFile() const
|
|
|
|
|
{
|
|
|
|
|
llvm::Expected<clang::format::FormatStyle> style
|
|
|
|
|
= clang::format::getStyle("file", m_fileName.toString().toStdString(), "none");
|
|
|
|
|
if (style)
|
|
|
|
|
return *style;
|
|
|
|
|
|
|
|
|
|
handleAllErrors(style.takeError(), [](const llvm::ErrorInfoBase &) {
|
|
|
|
|
// do nothing
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return clang::format::getLLVMStyle();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace ClangFormat
|