2018-08-29 15:58:13 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2018 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 "clangformatindenter.h"
|
|
|
|
|
|
2018-10-11 14:11:43 +02:00
|
|
|
#include "clangformatutils.h"
|
|
|
|
|
|
2018-08-29 15:58:13 +02:00
|
|
|
#include <clang/Format/Format.h>
|
|
|
|
|
#include <clang/Tooling/Core/Replacement.h>
|
|
|
|
|
|
2018-10-11 14:11:43 +02:00
|
|
|
#include <cpptools/cppmodelmanager.h>
|
2018-08-29 15:58:13 +02:00
|
|
|
#include <texteditor/textdocument.h>
|
|
|
|
|
#include <texteditor/texteditor.h>
|
|
|
|
|
|
|
|
|
|
#include <utils/hostosinfo.h>
|
2018-10-11 14:11:43 +02:00
|
|
|
#include <utils/textutils.h>
|
|
|
|
|
#include <utils/qtcassert.h>
|
2018-08-29 15:58:13 +02:00
|
|
|
|
2018-10-02 10:53:31 +02:00
|
|
|
#include <llvm/Config/llvm-config.h>
|
|
|
|
|
|
2018-08-29 15:58:13 +02:00
|
|
|
#include <QDir>
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
#include <QTextBlock>
|
|
|
|
|
|
2018-10-11 14:11:43 +02:00
|
|
|
#include <fstream>
|
|
|
|
|
|
2018-11-21 14:54:47 +01:00
|
|
|
using ClangReplacement = clang::tooling::Replacement;
|
|
|
|
|
using ClangReplacements = clang::tooling::Replacements;
|
|
|
|
|
using QtReplacement = TextEditor::Replacement;
|
|
|
|
|
using QtReplacements = TextEditor::Replacements;
|
|
|
|
|
|
2018-08-29 15:58:13 +02:00
|
|
|
using namespace clang;
|
|
|
|
|
using namespace format;
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
using namespace tooling;
|
|
|
|
|
using namespace ProjectExplorer;
|
|
|
|
|
using namespace TextEditor;
|
|
|
|
|
|
|
|
|
|
namespace ClangFormat {
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
2018-10-11 14:11:43 +02:00
|
|
|
void adjustFormatStyleForLineBreak(format::FormatStyle &style)
|
2018-08-29 15:58:13 +02:00
|
|
|
{
|
2018-10-11 14:11:43 +02:00
|
|
|
style.DisableFormat = false;
|
|
|
|
|
style.ColumnLimit = 0;
|
|
|
|
|
#ifdef KEEP_LINE_BREAKS_FOR_NON_EMPTY_LINES_BACKPORTED
|
|
|
|
|
style.KeepLineBreaksForNonEmptyLines = true;
|
2018-10-02 10:53:31 +02:00
|
|
|
#endif
|
2018-10-11 14:11:43 +02:00
|
|
|
style.MaxEmptyLinesToKeep = 2;
|
2018-11-13 07:29:21 +01:00
|
|
|
style.SortIncludes = false;
|
|
|
|
|
style.SortUsingDeclarations = false;
|
2018-08-29 15:58:13 +02:00
|
|
|
}
|
|
|
|
|
|
2018-11-26 10:50:07 +01:00
|
|
|
StringRef clearExtraNewline(StringRef text)
|
|
|
|
|
{
|
|
|
|
|
while (text.startswith("\n\n"))
|
|
|
|
|
text = text.drop_front();
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-21 14:54:47 +01:00
|
|
|
ClangReplacements filteredReplacements(const ClangReplacements &replacements,
|
|
|
|
|
int offset,
|
|
|
|
|
int extraOffsetToAdd,
|
|
|
|
|
bool onlyIndention)
|
2018-08-29 15:58:13 +02:00
|
|
|
{
|
2018-11-21 14:54:47 +01:00
|
|
|
ClangReplacements filtered;
|
|
|
|
|
for (const ClangReplacement &replacement : replacements) {
|
2018-10-11 14:11:43 +02:00
|
|
|
int replacementOffset = static_cast<int>(replacement.getOffset());
|
2018-11-13 07:29:21 +01:00
|
|
|
if (onlyIndention && replacementOffset != offset - 1)
|
|
|
|
|
continue;
|
2018-08-29 15:58:13 +02:00
|
|
|
|
|
|
|
|
if (replacementOffset + 1 >= offset)
|
2018-10-11 14:11:43 +02:00
|
|
|
replacementOffset += extraOffsetToAdd;
|
2018-08-29 15:58:13 +02:00
|
|
|
|
2018-11-26 10:50:07 +01:00
|
|
|
StringRef text = onlyIndention ? clearExtraNewline(replacement.getReplacementText())
|
|
|
|
|
: replacement.getReplacementText();
|
|
|
|
|
|
2018-11-21 14:54:47 +01:00
|
|
|
Error error = filtered.add(ClangReplacement(replacement.getFilePath(),
|
|
|
|
|
static_cast<unsigned int>(replacementOffset),
|
|
|
|
|
replacement.getLength(),
|
|
|
|
|
text));
|
2018-08-29 15:58:13 +02:00
|
|
|
// Throws if error is not checked.
|
|
|
|
|
if (error)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return filtered;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-11 14:11:43 +02:00
|
|
|
void trimFirstNonEmptyBlock(const QTextBlock ¤tBlock)
|
|
|
|
|
{
|
|
|
|
|
QTextBlock prevBlock = currentBlock.previous();
|
|
|
|
|
while (prevBlock.position() > 0 && prevBlock.text().trimmed().isEmpty())
|
|
|
|
|
prevBlock = prevBlock.previous();
|
2018-08-29 15:58:13 +02:00
|
|
|
|
2018-10-11 14:11:43 +02:00
|
|
|
if (prevBlock.text().trimmed().isEmpty())
|
|
|
|
|
return;
|
2018-08-29 15:58:13 +02:00
|
|
|
|
2018-10-11 14:11:43 +02:00
|
|
|
const QString initialText = prevBlock.text();
|
|
|
|
|
if (!initialText.at(initialText.size() - 1).isSpace())
|
|
|
|
|
return;
|
2018-08-29 15:58:13 +02:00
|
|
|
|
2018-10-17 11:00:24 +02:00
|
|
|
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));
|
2018-08-29 15:58:13 +02:00
|
|
|
|
2018-10-11 14:11:43 +02:00
|
|
|
QTextCursor cursor(prevBlock);
|
|
|
|
|
cursor.beginEditBlock();
|
|
|
|
|
cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor,
|
|
|
|
|
initialText.size() - extraSpaceCount);
|
|
|
|
|
cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, extraSpaceCount);
|
|
|
|
|
cursor.removeSelectedText();
|
|
|
|
|
cursor.endEditBlock();
|
2018-08-29 15:58:13 +02:00
|
|
|
}
|
|
|
|
|
|
2018-11-26 12:44:44 +01:00
|
|
|
void trimCurrentBlock(const QTextBlock ¤tBlock)
|
|
|
|
|
{
|
|
|
|
|
if (currentBlock.text().trimmed().isEmpty()) {
|
|
|
|
|
// Clear the block containing only spaces
|
|
|
|
|
QTextCursor cursor(currentBlock);
|
|
|
|
|
cursor.beginEditBlock();
|
|
|
|
|
cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
|
|
|
|
|
cursor.removeSelectedText();
|
|
|
|
|
cursor.endEditBlock();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-11 14:11:43 +02:00
|
|
|
// Returns the total langth of previous lines with pure whitespace.
|
|
|
|
|
int previousEmptyLinesLength(const QTextBlock ¤tBlock)
|
2018-08-29 15:58:13 +02:00
|
|
|
{
|
2018-10-11 14:11:43 +02:00
|
|
|
int length{0};
|
|
|
|
|
QTextBlock prevBlock = currentBlock.previous();
|
|
|
|
|
while (prevBlock.position() > 0 && prevBlock.text().trimmed().isEmpty()) {
|
|
|
|
|
length += prevBlock.text().length() + 1;
|
|
|
|
|
prevBlock = prevBlock.previous();
|
2018-08-29 15:58:13 +02:00
|
|
|
}
|
2018-10-11 14:11:43 +02:00
|
|
|
|
|
|
|
|
return length;
|
2018-08-29 15:58:13 +02:00
|
|
|
}
|
|
|
|
|
|
2019-01-09 16:28:41 +01:00
|
|
|
void modifyToIndentEmptyLines(
|
|
|
|
|
QByteArray &buffer, int offset, int &length, const QTextBlock &block, bool secondTry)
|
2018-08-29 15:58:13 +02:00
|
|
|
{
|
2018-10-11 14:11:43 +02:00
|
|
|
const QString blockText = block.text().trimmed();
|
|
|
|
|
const bool closingParenBlock = blockText.startsWith(')');
|
2019-01-09 16:28:41 +01:00
|
|
|
if (blockText.isEmpty() || closingParenBlock) {
|
|
|
|
|
//This extra text works for the most cases.
|
|
|
|
|
QByteArray dummyText("a;");
|
|
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
}
|
|
|
|
|
if (prevBlock.text().endsWith(','))
|
|
|
|
|
dummyText = "int a";
|
2018-08-29 15:58:13 +02:00
|
|
|
|
2019-01-09 16:28:41 +01:00
|
|
|
if (closingParenBlock) {
|
|
|
|
|
if (prevBlock.text().endsWith(','))
|
|
|
|
|
dummyText = "int a";
|
|
|
|
|
else
|
|
|
|
|
dummyText = "&& a";
|
|
|
|
|
}
|
2018-08-29 15:58:13 +02:00
|
|
|
|
2019-01-09 16:28:41 +01:00
|
|
|
length += dummyText.length();
|
|
|
|
|
buffer.insert(offset, dummyText);
|
2018-08-29 15:58:13 +02:00
|
|
|
}
|
|
|
|
|
|
2019-01-09 16:28:41 +01:00
|
|
|
if (secondTry) {
|
|
|
|
|
int nextLinePos = buffer.indexOf('\n', offset);
|
|
|
|
|
if (nextLinePos > 0) {
|
|
|
|
|
// If first try was not successful try to put ')' in the end of the line to close possibly
|
|
|
|
|
// unclosed parentheses.
|
|
|
|
|
// TODO: Does it help to add different endings depending on the context?
|
|
|
|
|
buffer.insert(nextLinePos, ')');
|
|
|
|
|
length += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-29 15:58:13 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-11 14:11:43 +02:00
|
|
|
static const int kMaxLinesFromCurrentBlock = 200;
|
|
|
|
|
|
|
|
|
|
Utils::LineColumn utf16LineColumn(const QTextBlock &block,
|
|
|
|
|
int blockOffsetUtf8,
|
|
|
|
|
const QByteArray &utf8Buffer,
|
|
|
|
|
int utf8Offset)
|
2018-08-29 15:58:13 +02:00
|
|
|
{
|
2018-10-17 11:00:24 +02:00
|
|
|
// 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.
|
2018-11-13 07:29:21 +01:00
|
|
|
|
|
|
|
|
if (utf8Offset < blockOffsetUtf8) {
|
|
|
|
|
line -= static_cast<int>(std::count(utf8Buffer.begin() + lineStartUtf8Offset,
|
|
|
|
|
utf8Buffer.begin() + blockOffsetUtf8,
|
|
|
|
|
'\n'));
|
2018-10-17 11:00:24 +02:00
|
|
|
} else {
|
|
|
|
|
line += static_cast<int>(std::count(utf8Buffer.begin() + blockOffsetUtf8,
|
|
|
|
|
utf8Buffer.begin() + lineStartUtf8Offset,
|
|
|
|
|
'\n'));
|
2018-10-11 14:11:43 +02:00
|
|
|
}
|
2018-08-29 15:58:13 +02:00
|
|
|
|
2018-10-17 11:00:24 +02:00
|
|
|
const QByteArray lineText = utf8Buffer.mid(lineStartUtf8Offset,
|
|
|
|
|
utf8Offset - lineStartUtf8Offset);
|
2018-10-11 14:11:43 +02:00
|
|
|
return Utils::LineColumn(line, QString::fromUtf8(lineText).size() + 1);
|
|
|
|
|
}
|
2018-08-29 15:58:13 +02:00
|
|
|
|
2018-11-21 14:54:47 +01:00
|
|
|
QtReplacements utf16Replacements(const QTextBlock &block,
|
|
|
|
|
int blockOffsetUtf8,
|
|
|
|
|
const QByteArray &utf8Buffer,
|
|
|
|
|
const ClangReplacements &replacements)
|
2018-08-29 15:58:13 +02:00
|
|
|
{
|
2018-11-21 14:54:47 +01:00
|
|
|
QtReplacements convertedReplacements;
|
|
|
|
|
convertedReplacements.reserve(replacements.size());
|
|
|
|
|
for (const ClangReplacement &replacement : replacements) {
|
|
|
|
|
const Utils::LineColumn lineColUtf16 = utf16LineColumn(block,
|
|
|
|
|
blockOffsetUtf8,
|
|
|
|
|
utf8Buffer,
|
|
|
|
|
static_cast<int>(
|
|
|
|
|
replacement.getOffset()));
|
2018-10-11 14:11:43 +02:00
|
|
|
if (!lineColUtf16.isValid())
|
|
|
|
|
continue;
|
|
|
|
|
const int utf16Offset = Utils::Text::positionInText(block.document(),
|
|
|
|
|
lineColUtf16.line,
|
|
|
|
|
lineColUtf16.column);
|
|
|
|
|
const int utf16Length = QString::fromUtf8(
|
2018-11-21 14:54:47 +01:00
|
|
|
utf8Buffer.mid(static_cast<int>(replacement.getOffset()),
|
|
|
|
|
static_cast<int>(replacement.getLength())))
|
|
|
|
|
.size();
|
|
|
|
|
convertedReplacements.emplace_back(utf16Offset,
|
|
|
|
|
utf16Length,
|
|
|
|
|
QString::fromStdString(replacement.getReplacementText()));
|
2018-08-29 15:58:13 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-11 14:11:43 +02:00
|
|
|
return convertedReplacements;
|
2018-08-29 15:58:13 +02:00
|
|
|
}
|
|
|
|
|
|
2018-11-21 14:54:47 +01:00
|
|
|
QtReplacements replacements(const Utils::FileName &fileName,
|
|
|
|
|
QByteArray buffer,
|
|
|
|
|
int utf8Offset,
|
|
|
|
|
int utf8Length,
|
|
|
|
|
const QTextBlock &block,
|
|
|
|
|
const QChar &typedChar = QChar::Null,
|
2019-01-09 16:28:41 +01:00
|
|
|
bool onlyIndention = true,
|
|
|
|
|
bool secondTry = false)
|
2018-11-21 14:54:47 +01:00
|
|
|
{
|
|
|
|
|
FormatStyle style = styleForFile(fileName);
|
|
|
|
|
|
2019-01-09 16:28:41 +01:00
|
|
|
int originalOffsetUtf8 = utf8Offset;
|
|
|
|
|
int originalLengthUtf8 = utf8Length;
|
2018-11-21 14:54:47 +01:00
|
|
|
QByteArray originalBuffer = buffer;
|
|
|
|
|
|
|
|
|
|
int extraOffset = 0;
|
|
|
|
|
if (onlyIndention) {
|
|
|
|
|
if (block.blockNumber() > kMaxLinesFromCurrentBlock) {
|
|
|
|
|
extraOffset = Utils::Text::utf8NthLineOffset(
|
|
|
|
|
block.document(), buffer, block.blockNumber() - kMaxLinesFromCurrentBlock);
|
|
|
|
|
}
|
|
|
|
|
int endOffset = Utils::Text::utf8NthLineOffset(
|
|
|
|
|
block.document(), buffer, block.blockNumber() + kMaxLinesFromCurrentBlock);
|
|
|
|
|
if (endOffset == -1)
|
|
|
|
|
endOffset = buffer.size();
|
|
|
|
|
|
|
|
|
|
buffer = buffer.mid(extraOffset, endOffset - extraOffset);
|
|
|
|
|
utf8Offset -= extraOffset;
|
|
|
|
|
|
|
|
|
|
const int emptySpaceLength = previousEmptyLinesLength(block);
|
|
|
|
|
utf8Offset -= emptySpaceLength;
|
|
|
|
|
buffer.remove(utf8Offset, emptySpaceLength);
|
|
|
|
|
|
|
|
|
|
extraOffset += emptySpaceLength;
|
|
|
|
|
|
|
|
|
|
adjustFormatStyleForLineBreak(style);
|
|
|
|
|
if (typedChar == QChar::Null)
|
2019-01-09 16:28:41 +01:00
|
|
|
modifyToIndentEmptyLines(buffer, utf8Offset, utf8Length, block, secondTry);
|
2018-11-21 14:54:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<Range> ranges{{static_cast<unsigned int>(utf8Offset),
|
|
|
|
|
static_cast<unsigned int>(utf8Length)}};
|
|
|
|
|
FormattingAttemptStatus status;
|
|
|
|
|
|
2019-01-09 16:28:41 +01:00
|
|
|
ClangReplacements clangReplacements = reformat(style,
|
|
|
|
|
buffer.data(),
|
|
|
|
|
ranges,
|
|
|
|
|
fileName.toString().toStdString(),
|
|
|
|
|
&status);
|
2018-11-21 14:54:47 +01:00
|
|
|
|
|
|
|
|
if (!status.FormatComplete)
|
|
|
|
|
QtReplacements();
|
|
|
|
|
|
2019-01-09 16:28:41 +01:00
|
|
|
const ClangReplacements filtered = filteredReplacements(clangReplacements,
|
2018-11-21 14:54:47 +01:00
|
|
|
utf8Offset,
|
|
|
|
|
extraOffset,
|
|
|
|
|
onlyIndention);
|
2019-01-09 16:28:41 +01:00
|
|
|
|
|
|
|
|
const bool canTryAgain = onlyIndention && typedChar == QChar::Null && !secondTry;
|
|
|
|
|
if (canTryAgain && filtered.empty()) {
|
|
|
|
|
return replacements(fileName,
|
|
|
|
|
originalBuffer,
|
|
|
|
|
originalOffsetUtf8,
|
|
|
|
|
originalLengthUtf8,
|
|
|
|
|
block,
|
|
|
|
|
typedChar,
|
|
|
|
|
onlyIndention,
|
|
|
|
|
true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return utf16Replacements(block, originalOffsetUtf8, originalBuffer, filtered);
|
2018-11-21 14:54:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void applyReplacements(const QTextBlock &block, const QtReplacements &replacements)
|
2018-08-29 15:58:13 +02:00
|
|
|
{
|
2018-10-11 14:11:43 +02:00
|
|
|
if (replacements.empty())
|
|
|
|
|
return;
|
2018-08-29 15:58:13 +02:00
|
|
|
|
2018-10-11 14:11:43 +02:00
|
|
|
int fullOffsetShift = 0;
|
|
|
|
|
QTextCursor editCursor(block);
|
2018-11-21 14:54:47 +01:00
|
|
|
for (const QtReplacement &replacement : replacements) {
|
2018-10-11 14:11:43 +02:00
|
|
|
editCursor.beginEditBlock();
|
2018-11-21 14:54:47 +01:00
|
|
|
editCursor.setPosition(replacement.offset + fullOffsetShift);
|
|
|
|
|
editCursor.movePosition(QTextCursor::NextCharacter,
|
|
|
|
|
QTextCursor::KeepAnchor,
|
|
|
|
|
replacement.length);
|
2018-10-11 14:11:43 +02:00
|
|
|
editCursor.removeSelectedText();
|
2018-11-21 14:54:47 +01:00
|
|
|
editCursor.insertText(replacement.text);
|
2018-10-11 14:11:43 +02:00
|
|
|
editCursor.endEditBlock();
|
2018-11-21 14:54:47 +01:00
|
|
|
fullOffsetShift += replacement.text.length() - replacement.length;
|
2018-10-11 14:11:43 +02:00
|
|
|
}
|
2018-08-29 15:58:13 +02:00
|
|
|
}
|
|
|
|
|
|
2018-11-07 09:27:00 +01:00
|
|
|
QString selectedLines(QTextDocument *doc, const QTextBlock &startBlock, const QTextBlock &endBlock)
|
|
|
|
|
{
|
|
|
|
|
QString text = Utils::Text::textAt(
|
|
|
|
|
QTextCursor(doc),
|
|
|
|
|
startBlock.position(),
|
|
|
|
|
std::max(0, endBlock.position() + endBlock.length() - startBlock.position() - 1));
|
|
|
|
|
while (!text.isEmpty() && text.rbegin()->isSpace())
|
|
|
|
|
text.chop(1);
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-29 15:58:13 +02:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
|
|
bool ClangFormatIndenter::isElectricCharacter(const QChar &ch) const
|
|
|
|
|
{
|
|
|
|
|
switch (ch.toLatin1()) {
|
|
|
|
|
case '{':
|
|
|
|
|
case '}':
|
|
|
|
|
case ':':
|
|
|
|
|
case '#':
|
|
|
|
|
case '<':
|
|
|
|
|
case '>':
|
|
|
|
|
case ';':
|
|
|
|
|
case '(':
|
|
|
|
|
case ')':
|
|
|
|
|
case ',':
|
|
|
|
|
case '.':
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangFormatIndenter::indent(QTextDocument *doc,
|
|
|
|
|
const QTextCursor &cursor,
|
|
|
|
|
const QChar &typedChar,
|
|
|
|
|
const TabSettings &tabSettings,
|
2018-11-13 07:29:21 +01:00
|
|
|
bool /*autoTriggered*/)
|
2018-08-29 15:58:13 +02:00
|
|
|
{
|
2018-11-13 07:29:21 +01:00
|
|
|
if (cursor.hasSelection()) {
|
2018-11-26 10:50:07 +01:00
|
|
|
// Calling currentBlock.next() might be unsafe because we change the document.
|
|
|
|
|
// Let's operate with block numbers instead.
|
|
|
|
|
const int startNumber = doc->findBlock(cursor.selectionStart()).blockNumber();
|
|
|
|
|
const int endNumber = doc->findBlock(cursor.selectionEnd()).blockNumber();
|
|
|
|
|
for (int currentBlockNumber = startNumber; currentBlockNumber <= endNumber;
|
|
|
|
|
++currentBlockNumber) {
|
|
|
|
|
const QTextBlock currentBlock = doc->findBlockByNumber(currentBlockNumber);
|
|
|
|
|
if (currentBlock.isValid()) {
|
|
|
|
|
const int blocksAmount = doc->blockCount();
|
|
|
|
|
indentBlock(doc, currentBlock, typedChar, tabSettings);
|
|
|
|
|
QTC_CHECK(blocksAmount == doc->blockCount()
|
|
|
|
|
&& "ClangFormat plugin indentation changed the amount of blocks.");
|
|
|
|
|
}
|
2018-08-29 15:58:13 +02:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
indentBlock(doc, cursor.block(), typedChar, tabSettings);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-21 14:54:47 +01:00
|
|
|
QtReplacements ClangFormatIndenter::format(QTextDocument *doc,
|
2018-11-09 11:07:54 +01:00
|
|
|
const Utils::FileName &fileName,
|
2018-11-21 14:54:47 +01:00
|
|
|
const QTextCursor &cursor,
|
|
|
|
|
const TextEditor::TabSettings & /*tabSettings*/)
|
2018-11-13 07:29:21 +01:00
|
|
|
{
|
|
|
|
|
int utf8Offset;
|
|
|
|
|
int utf8Length;
|
|
|
|
|
const QByteArray buffer = doc->toPlainText().toUtf8();
|
2018-11-21 14:54:47 +01:00
|
|
|
QTextBlock block = cursor.block();
|
2018-11-13 07:29:21 +01:00
|
|
|
if (cursor.hasSelection()) {
|
2018-11-21 14:54:47 +01:00
|
|
|
block = doc->findBlock(cursor.selectionStart());
|
2018-11-13 07:29:21 +01:00
|
|
|
const QTextBlock end = doc->findBlock(cursor.selectionEnd());
|
2018-11-21 14:54:47 +01:00
|
|
|
utf8Offset = Utils::Text::utf8NthLineOffset(doc, buffer, block.blockNumber() + 1);
|
|
|
|
|
QTC_ASSERT(utf8Offset >= 0, return QtReplacements(););
|
|
|
|
|
utf8Length = selectedLines(doc, block, end).toUtf8().size();
|
|
|
|
|
|
2018-11-13 07:29:21 +01:00
|
|
|
} else {
|
|
|
|
|
const QTextBlock block = cursor.block();
|
|
|
|
|
utf8Offset = Utils::Text::utf8NthLineOffset(doc, buffer, block.blockNumber() + 1);
|
2018-11-21 14:54:47 +01:00
|
|
|
QTC_ASSERT(utf8Offset >= 0, return QtReplacements(););
|
2018-11-13 07:29:21 +01:00
|
|
|
utf8Length = block.text().toUtf8().size();
|
|
|
|
|
}
|
2018-11-21 14:54:47 +01:00
|
|
|
|
|
|
|
|
const QtReplacements toReplace
|
|
|
|
|
= replacements(fileName, buffer, utf8Offset, utf8Length, block, QChar::Null, false);
|
|
|
|
|
applyReplacements(block, toReplace);
|
|
|
|
|
|
|
|
|
|
return toReplace;
|
2018-11-13 07:29:21 +01:00
|
|
|
}
|
|
|
|
|
|
2018-08-29 15:58:13 +02:00
|
|
|
void ClangFormatIndenter::reindent(QTextDocument *doc,
|
|
|
|
|
const QTextCursor &cursor,
|
|
|
|
|
const TabSettings &tabSettings)
|
|
|
|
|
{
|
|
|
|
|
indent(doc, cursor, QChar::Null, tabSettings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangFormatIndenter::indentBlock(QTextDocument *doc,
|
|
|
|
|
const QTextBlock &block,
|
|
|
|
|
const QChar &typedChar,
|
|
|
|
|
const TabSettings &tabSettings)
|
|
|
|
|
{
|
2018-09-12 13:02:46 +02:00
|
|
|
Q_UNUSED(tabSettings);
|
|
|
|
|
|
2018-08-29 15:58:13 +02:00
|
|
|
TextEditorWidget *editor = TextEditorWidget::currentTextEditorWidget();
|
|
|
|
|
if (!editor)
|
|
|
|
|
return;
|
|
|
|
|
|
2018-11-13 11:47:02 +01:00
|
|
|
const Utils::FileName fileName = editor->textDocument()->filePath();
|
2018-10-11 14:11:43 +02:00
|
|
|
trimFirstNonEmptyBlock(block);
|
2018-11-26 12:44:44 +01:00
|
|
|
trimCurrentBlock(block);
|
2018-10-11 14:11:43 +02:00
|
|
|
const QByteArray buffer = doc->toPlainText().toUtf8();
|
|
|
|
|
const int utf8Offset = Utils::Text::utf8NthLineOffset(doc, buffer, block.blockNumber() + 1);
|
|
|
|
|
QTC_ASSERT(utf8Offset >= 0, return;);
|
2018-08-29 15:58:13 +02:00
|
|
|
|
2018-10-11 14:11:43 +02:00
|
|
|
applyReplacements(block,
|
2018-11-21 14:54:47 +01:00
|
|
|
replacements(fileName, buffer, utf8Offset, 0, block, typedChar));
|
2018-08-29 15:58:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ClangFormatIndenter::indentFor(const QTextBlock &block, const TextEditor::TabSettings &)
|
|
|
|
|
{
|
|
|
|
|
TextEditorWidget *editor = TextEditorWidget::currentTextEditorWidget();
|
|
|
|
|
if (!editor)
|
|
|
|
|
return -1;
|
|
|
|
|
|
2018-11-13 11:47:02 +01:00
|
|
|
const Utils::FileName fileName = editor->textDocument()->filePath();
|
2018-10-11 14:11:43 +02:00
|
|
|
trimFirstNonEmptyBlock(block);
|
2018-11-26 12:44:44 +01:00
|
|
|
trimCurrentBlock(block);
|
2018-10-11 14:11:43 +02:00
|
|
|
const QTextDocument *doc = block.document();
|
|
|
|
|
const QByteArray buffer = doc->toPlainText().toUtf8();
|
|
|
|
|
const int utf8Offset = Utils::Text::utf8NthLineOffset(doc, buffer, block.blockNumber() + 1);
|
|
|
|
|
QTC_ASSERT(utf8Offset >= 0, return 0;);
|
|
|
|
|
|
2018-11-21 14:54:47 +01:00
|
|
|
const QtReplacements toReplace = replacements(fileName, buffer, utf8Offset, 0, block);
|
2018-08-29 15:58:13 +02:00
|
|
|
|
|
|
|
|
if (toReplace.empty())
|
|
|
|
|
return -1;
|
|
|
|
|
|
2018-11-21 14:54:47 +01:00
|
|
|
const QtReplacement &replacement = toReplace.front();
|
|
|
|
|
int afterLineBreak = replacement.text.lastIndexOf('\n');
|
|
|
|
|
afterLineBreak = (afterLineBreak < 0) ? 0 : afterLineBreak + 1;
|
|
|
|
|
return static_cast<int>(replacement.text.size() - afterLineBreak);
|
2018-08-29 15:58:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TabSettings ClangFormatIndenter::tabSettings() const
|
|
|
|
|
{
|
2018-11-13 11:47:02 +01:00
|
|
|
FormatStyle style = currentProjectStyle();
|
2018-08-29 15:58:13 +02:00
|
|
|
TabSettings tabSettings;
|
|
|
|
|
|
|
|
|
|
switch (style.UseTab) {
|
|
|
|
|
case FormatStyle::UT_Never:
|
|
|
|
|
tabSettings.m_tabPolicy = TabSettings::SpacesOnlyTabPolicy;
|
2018-09-12 12:57:08 +02:00
|
|
|
break;
|
2018-08-29 15:58:13 +02:00
|
|
|
case FormatStyle::UT_Always:
|
|
|
|
|
tabSettings.m_tabPolicy = TabSettings::TabsOnlyTabPolicy;
|
2018-09-12 12:57:08 +02:00
|
|
|
break;
|
2018-08-29 15:58:13 +02:00
|
|
|
default:
|
|
|
|
|
tabSettings.m_tabPolicy = TabSettings::MixedTabPolicy;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-11 14:11:43 +02:00
|
|
|
tabSettings.m_tabSize = static_cast<int>(style.TabWidth);
|
|
|
|
|
tabSettings.m_indentSize = static_cast<int>(style.IndentWidth);
|
2018-08-29 15:58:13 +02:00
|
|
|
|
2018-11-08 10:35:23 +01:00
|
|
|
if (style.AlignAfterOpenBracket == FormatStyle::BAS_DontAlign)
|
|
|
|
|
tabSettings.m_continuationAlignBehavior = TabSettings::NoContinuationAlign;
|
2018-08-29 15:58:13 +02:00
|
|
|
else
|
|
|
|
|
tabSettings.m_continuationAlignBehavior = TabSettings::ContinuationAlignWithIndent;
|
|
|
|
|
|
|
|
|
|
return tabSettings;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace ClangFormat
|