forked from qt-creator/qt-creator
Clang: Use clang-format for indentation
This is the new experimental plugin based on LibFormat. It replaces the default indenter for CppEditorDocument and applies clang-format after the CR or the set of 'electric' characters. Uses the global .clang-format kept in QtC settings or the one for current project. Both can be configured. For indentation some style modifications and code manipulations are done to prevent line shrinking when it's not expected. Manual indentation uses unmodified style from .clang-format file. Change-Id: I6279b805e418e1804b553efa615f5c843f395a58 Reviewed-by: Marco Bubke <marco.bubke@qt.io>
This commit is contained in:
@@ -83,6 +83,7 @@ public:
|
||||
}
|
||||
|
||||
QTextCursor indentOrUnindent(const QTextCursor &textCursor, bool doIndent,
|
||||
const TabSettings &tabSettings,
|
||||
bool blockSelection = false, int column = 0,
|
||||
int *offset = nullptr);
|
||||
void resetRevisions();
|
||||
@@ -110,17 +111,16 @@ public:
|
||||
};
|
||||
|
||||
QTextCursor TextDocumentPrivate::indentOrUnindent(const QTextCursor &textCursor, bool doIndent,
|
||||
const TabSettings &tabSettings,
|
||||
bool blockSelection, int columnIn, int *offset)
|
||||
{
|
||||
QTextCursor cursor = textCursor;
|
||||
cursor.beginEditBlock();
|
||||
|
||||
TabSettings &ts = m_tabSettings;
|
||||
|
||||
// Indent or unindent the selected lines
|
||||
int pos = cursor.position();
|
||||
int column = blockSelection ? columnIn
|
||||
: ts.columnAt(cursor.block().text(), cursor.positionInBlock());
|
||||
: tabSettings.columnAt(cursor.block().text(), cursor.positionInBlock());
|
||||
int anchor = cursor.anchor();
|
||||
int start = qMin(anchor, pos);
|
||||
int end = qMax(anchor, pos);
|
||||
@@ -140,12 +140,13 @@ QTextCursor TextDocumentPrivate::indentOrUnindent(const QTextCursor &textCursor,
|
||||
if (cursor.hasSelection() && !blockSelection && !oneLinePartial) {
|
||||
for (QTextBlock block = startBlock; block != endBlock; block = block.next()) {
|
||||
const QString text = block.text();
|
||||
int indentPosition = ts.lineIndentPosition(text);
|
||||
int indentPosition = tabSettings.lineIndentPosition(text);
|
||||
if (!doIndent && !indentPosition)
|
||||
indentPosition = ts.firstNonSpace(text);
|
||||
int targetColumn = ts.indentedColumn(ts.columnAt(text, indentPosition), doIndent);
|
||||
indentPosition = tabSettings.firstNonSpace(text);
|
||||
int targetColumn = tabSettings.indentedColumn(
|
||||
tabSettings.columnAt(text, indentPosition), doIndent);
|
||||
cursor.setPosition(block.position() + indentPosition);
|
||||
cursor.insertText(ts.indentationString(0, targetColumn, 0, block));
|
||||
cursor.insertText(tabSettings.indentationString(0, targetColumn, 0, block));
|
||||
cursor.setPosition(block.position());
|
||||
cursor.setPosition(block.position() + indentPosition, QTextCursor::KeepAnchor);
|
||||
cursor.removeSelectedText();
|
||||
@@ -170,27 +171,30 @@ QTextCursor TextDocumentPrivate::indentOrUnindent(const QTextCursor &textCursor,
|
||||
for (QTextBlock block = startBlock; block != endBlock; block = block.next()) {
|
||||
QString text = block.text();
|
||||
|
||||
int blockColumn = ts.columnAt(text, text.size());
|
||||
int blockColumn = tabSettings.columnAt(text, text.size());
|
||||
if (blockColumn < column) {
|
||||
cursor.setPosition(block.position() + text.size());
|
||||
cursor.insertText(ts.indentationString(blockColumn, column, 0, block));
|
||||
cursor.insertText(tabSettings.indentationString(blockColumn, column, 0, block));
|
||||
text = block.text();
|
||||
}
|
||||
|
||||
int indentPosition = ts.positionAtColumn(text, column, nullptr, true);
|
||||
int spaces = ts.spacesLeftFromPosition(text, indentPosition);
|
||||
int startColumn = ts.columnAt(text, indentPosition - spaces);
|
||||
int targetColumn = ts.indentedColumn(ts.columnAt(text, indentPosition), doIndent);
|
||||
int indentPosition = tabSettings.positionAtColumn(text, column, nullptr, true);
|
||||
int spaces = tabSettings.spacesLeftFromPosition(text, indentPosition);
|
||||
int startColumn = tabSettings.columnAt(text, indentPosition - spaces);
|
||||
int targetColumn = tabSettings.indentedColumn(
|
||||
tabSettings.columnAt(text, indentPosition), doIndent);
|
||||
cursor.setPosition(block.position() + indentPosition);
|
||||
cursor.setPosition(block.position() + indentPosition - spaces, QTextCursor::KeepAnchor);
|
||||
cursor.removeSelectedText();
|
||||
cursor.insertText(ts.indentationString(startColumn, targetColumn, 0, block));
|
||||
cursor.insertText(tabSettings.indentationString(startColumn, targetColumn, 0, block));
|
||||
}
|
||||
// Preserve initial anchor of block selection
|
||||
if (blockSelection) {
|
||||
end = cursor.position();
|
||||
if (offset)
|
||||
*offset = ts.columnAt(cursor.block().text(), cursor.positionInBlock()) - column;
|
||||
if (offset) {
|
||||
*offset = tabSettings.columnAt(cursor.block().text(), cursor.positionInBlock())
|
||||
- column;
|
||||
}
|
||||
cursor.setPosition(start);
|
||||
cursor.setPosition(end, QTextCursor::KeepAnchor);
|
||||
}
|
||||
@@ -330,19 +334,19 @@ const StorageSettings &TextDocument::storageSettings() const
|
||||
return d->m_storageSettings;
|
||||
}
|
||||
|
||||
void TextDocument::setTabSettings(const TabSettings &tabSettings)
|
||||
void TextDocument::setTabSettings(const TabSettings &newTabSettings)
|
||||
{
|
||||
if (tabSettings == d->m_tabSettings)
|
||||
if (newTabSettings == d->m_tabSettings)
|
||||
return;
|
||||
d->m_tabSettings = tabSettings;
|
||||
d->m_tabSettings = newTabSettings;
|
||||
|
||||
if (Highlighter *highlighter = qobject_cast<Highlighter *>(d->m_highlighter))
|
||||
highlighter->setTabSettings(tabSettings);
|
||||
highlighter->setTabSettings(tabSettings());
|
||||
|
||||
emit tabSettingsChanged();
|
||||
}
|
||||
|
||||
const TabSettings &TextDocument::tabSettings() const
|
||||
TabSettings TextDocument::tabSettings() const
|
||||
{
|
||||
return d->m_tabSettings;
|
||||
}
|
||||
@@ -412,26 +416,26 @@ void TextDocument::setExtraEncodingSettings(const ExtraEncodingSettings &extraEn
|
||||
d->m_extraEncodingSettings = extraEncodingSettings;
|
||||
}
|
||||
|
||||
void TextDocument::autoIndent(const QTextCursor &cursor, QChar typedChar)
|
||||
void TextDocument::autoIndent(const QTextCursor &cursor, QChar typedChar, bool autoTriggered)
|
||||
{
|
||||
d->m_indenter->indent(&d->m_document, cursor, typedChar, d->m_tabSettings);
|
||||
d->m_indenter->indent(&d->m_document, cursor, typedChar, tabSettings(), autoTriggered);
|
||||
}
|
||||
|
||||
void TextDocument::autoReindent(const QTextCursor &cursor)
|
||||
{
|
||||
d->m_indenter->reindent(&d->m_document, cursor, d->m_tabSettings);
|
||||
d->m_indenter->reindent(&d->m_document, cursor, tabSettings());
|
||||
}
|
||||
|
||||
QTextCursor TextDocument::indent(const QTextCursor &cursor, bool blockSelection, int column,
|
||||
int *offset)
|
||||
{
|
||||
return d->indentOrUnindent(cursor, true, blockSelection, column, offset);
|
||||
return d->indentOrUnindent(cursor, true, tabSettings(), blockSelection, column, offset);
|
||||
}
|
||||
|
||||
QTextCursor TextDocument::unindent(const QTextCursor &cursor, bool blockSelection, int column,
|
||||
int *offset)
|
||||
{
|
||||
return d->indentOrUnindent(cursor, false, blockSelection, column, offset);
|
||||
return d->indentOrUnindent(cursor, false, tabSettings(), blockSelection, column, offset);
|
||||
}
|
||||
|
||||
const ExtraEncodingSettings &TextDocument::extraEncodingSettings() const
|
||||
@@ -814,23 +818,24 @@ void TextDocument::cleanWhitespace(QTextCursor &cursor, bool cleanIndentation, b
|
||||
if (blocks.isEmpty())
|
||||
return;
|
||||
|
||||
const TabSettings currentTabSettings = tabSettings();
|
||||
const IndentationForBlock &indentations =
|
||||
d->m_indenter->indentationForBlocks(blocks, d->m_tabSettings);
|
||||
d->m_indenter->indentationForBlocks(blocks, currentTabSettings);
|
||||
|
||||
foreach (block, blocks) {
|
||||
QString blockText = block.text();
|
||||
d->m_tabSettings.removeTrailingWhitespace(cursor, block);
|
||||
currentTabSettings.removeTrailingWhitespace(cursor, block);
|
||||
const int indent = indentations[block.blockNumber()];
|
||||
if (cleanIndentation && !d->m_tabSettings.isIndentationClean(block, indent)) {
|
||||
if (cleanIndentation && !currentTabSettings.isIndentationClean(block, indent)) {
|
||||
cursor.setPosition(block.position());
|
||||
int firstNonSpace = d->m_tabSettings.firstNonSpace(blockText);
|
||||
int firstNonSpace = currentTabSettings.firstNonSpace(blockText);
|
||||
if (firstNonSpace == blockText.length()) {
|
||||
cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
|
||||
cursor.removeSelectedText();
|
||||
} else {
|
||||
int column = d->m_tabSettings.columnAt(blockText, firstNonSpace);
|
||||
int column = currentTabSettings.columnAt(blockText, firstNonSpace);
|
||||
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, firstNonSpace);
|
||||
QString indentationString = d->m_tabSettings.indentationString(0, column, column - indent, block);
|
||||
QString indentationString = currentTabSettings.indentationString(0, column, column - indent, block);
|
||||
cursor.insertText(indentationString);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user