forked from qt-creator/qt-creator
Editor: Collect indentation for a list of text blocks
Allows the indenter to reuse code formatter structures. Task-number: QTCREATORBUG-16420 Change-Id: Ie906d7fdcb50798da02ef5b750fb03ae752aadc1 Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
This commit is contained in:
@@ -186,3 +186,21 @@ CppCodeStyleSettings CppQtStyleIndenter::codeStyleSettings() const
|
||||
return m_cppCodeStylePreferences->currentCodeStyleSettings();
|
||||
return CppCodeStyleSettings();
|
||||
}
|
||||
|
||||
TextEditor::IndentationForBlock
|
||||
CppQtStyleIndenter::indentationForBlocks(const QVector<QTextBlock> &blocks,
|
||||
const TextEditor::TabSettings &tabSettings)
|
||||
{
|
||||
QtStyleCodeFormatter codeFormatter(tabSettings, codeStyleSettings());
|
||||
|
||||
codeFormatter.updateStateUntil(blocks.last());
|
||||
|
||||
TextEditor::IndentationForBlock ret;
|
||||
foreach (QTextBlock block, blocks) {
|
||||
int indent;
|
||||
int padding;
|
||||
codeFormatter.indentFor(block, &indent, &padding);
|
||||
ret.insert(block.blockNumber(), indent);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
@@ -58,6 +58,8 @@ public:
|
||||
void setCodeStylePreferences(TextEditor::ICodeStylePreferences *preferences) override;
|
||||
void invalidateCache(QTextDocument *doc) override;
|
||||
int indentFor(const QTextBlock &block, const TextEditor::TabSettings &tabSettings) override;
|
||||
TextEditor::IndentationForBlock indentationForBlocks(const QVector<QTextBlock> &blocks,
|
||||
const TextEditor::TabSettings &tabSettings) override;
|
||||
private:
|
||||
CppCodeStyleSettings codeStyleSettings() const;
|
||||
CppCodeStylePreferences *m_cppCodeStylePreferences;
|
||||
|
@@ -124,5 +124,24 @@ int GlslIndenter::indentFor(const QTextBlock &block, const TextEditor::TabSettin
|
||||
return indent;
|
||||
}
|
||||
|
||||
TextEditor::IndentationForBlock
|
||||
GlslIndenter::indentationForBlocks(const QVector<QTextBlock> &blocks,
|
||||
const TextEditor::TabSettings &tabSettings)
|
||||
{
|
||||
CppTools::QtStyleCodeFormatter codeFormatter(tabSettings,
|
||||
CppTools::CppToolsSettings::instance()->cppCodeStyle()->codeStyleSettings());
|
||||
|
||||
codeFormatter.updateStateUntil(blocks.last());
|
||||
|
||||
TextEditor::IndentationForBlock ret;
|
||||
foreach (QTextBlock block, blocks) {
|
||||
int indent;
|
||||
int padding;
|
||||
codeFormatter.indentFor(block, &indent, &padding);
|
||||
ret.insert(block.blockNumber(), indent);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
} // namespace Internal
|
||||
} // namespace GlslEditor
|
||||
|
@@ -48,6 +48,8 @@ public:
|
||||
const TextEditor::TabSettings &tabSettings) override;
|
||||
|
||||
int indentFor(const QTextBlock &block, const TextEditor::TabSettings &tabSettings) override;
|
||||
TextEditor::IndentationForBlock indentationForBlocks(const QVector<QTextBlock> &blocks,
|
||||
const TextEditor::TabSettings &tabSettings) override;
|
||||
};
|
||||
|
||||
} // namespace Internal
|
||||
|
@@ -90,3 +90,19 @@ int Indenter::indentFor(const QTextBlock &block,
|
||||
codeFormatter.updateStateUntil(block);
|
||||
return codeFormatter.indentFor(block);
|
||||
}
|
||||
|
||||
|
||||
TextEditor::IndentationForBlock
|
||||
Indenter::indentationForBlocks(const QVector<QTextBlock> &blocks,
|
||||
const TextEditor::TabSettings &tabSettings)
|
||||
{
|
||||
QmlJSTools::CreatorCodeFormatter codeFormatter(tabSettings);
|
||||
|
||||
|
||||
codeFormatter.updateStateUntil(blocks.last());
|
||||
|
||||
TextEditor::IndentationForBlock ret;
|
||||
foreach (QTextBlock block, blocks)
|
||||
ret.insert(block.blockNumber(), codeFormatter.indentFor(block));
|
||||
return ret;
|
||||
}
|
||||
|
@@ -46,6 +46,8 @@ public:
|
||||
void invalidateCache(QTextDocument *doc) override;
|
||||
|
||||
int indentFor(const QTextBlock &block, const TextEditor::TabSettings &tabSettings) override;
|
||||
TextEditor::IndentationForBlock indentationForBlocks(const QVector<QTextBlock> &blocks,
|
||||
const TextEditor::TabSettings &tabSettings) override;
|
||||
};
|
||||
|
||||
} // Internal
|
||||
|
@@ -118,3 +118,12 @@ int Indenter::indentFor(const QTextBlock &block, const TabSettings &tabSettings)
|
||||
Q_UNUSED(tabSettings)
|
||||
return -1;
|
||||
}
|
||||
|
||||
IndentationForBlock Indenter::indentationForBlocks(const QVector<QTextBlock> &blocks,
|
||||
const TabSettings &tabSettings)
|
||||
{
|
||||
IndentationForBlock ret;
|
||||
foreach (QTextBlock block, blocks)
|
||||
ret.insert(block.blockNumber(), indentFor(block, tabSettings));
|
||||
return ret;
|
||||
}
|
||||
|
@@ -27,6 +27,8 @@
|
||||
|
||||
#include "texteditor_global.h"
|
||||
|
||||
#include <QMap>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QTextDocument;
|
||||
class QTextCursor;
|
||||
@@ -39,6 +41,8 @@ namespace TextEditor {
|
||||
class ICodeStylePreferences;
|
||||
class TabSettings;
|
||||
|
||||
using IndentationForBlock = QMap<int, int>;
|
||||
|
||||
class TEXTEDITOR_EXPORT Indenter
|
||||
{
|
||||
public:
|
||||
@@ -69,6 +73,10 @@ public:
|
||||
virtual void invalidateCache(QTextDocument *doc);
|
||||
|
||||
virtual int indentFor(const QTextBlock &block, const TextEditor::TabSettings &tabSettings);
|
||||
|
||||
// Expects a list of blocks in order of occurrence in the document.
|
||||
virtual IndentationForBlock indentationForBlocks(const QVector<QTextBlock> &blocks,
|
||||
const TextEditor::TabSettings &tabSettings);
|
||||
};
|
||||
|
||||
} // namespace TextEditor
|
||||
|
@@ -764,29 +764,35 @@ void TextDocument::cleanWhitespace(QTextCursor &cursor, bool cleanIndentation, b
|
||||
if (cursor.hasSelection())
|
||||
end = d->m_document.findBlock(cursor.selectionEnd()-1).next();
|
||||
|
||||
QVector<QTextBlock> blocks;
|
||||
while (block.isValid() && block != end) {
|
||||
if (inEntireDocument || block.revision() != documentLayout->lastSaveRevision)
|
||||
blocks.append(block);
|
||||
block = block.next();
|
||||
}
|
||||
if (blocks.isEmpty())
|
||||
return;
|
||||
|
||||
if (inEntireDocument || block.revision() != documentLayout->lastSaveRevision) {
|
||||
const IndentationForBlock &indentations =
|
||||
d->m_indenter->indentationForBlocks(blocks, d->m_tabSettings);
|
||||
|
||||
QString blockText = block.text();
|
||||
d->m_tabSettings.removeTrailingWhitespace(cursor, block);
|
||||
const int indent = d->m_indenter->indentFor(block, d->m_tabSettings);
|
||||
if (cleanIndentation && !d->m_tabSettings.isIndentationClean(block, indent)) {
|
||||
cursor.setPosition(block.position());
|
||||
int firstNonSpace = d->m_tabSettings.firstNonSpace(blockText);
|
||||
if (firstNonSpace == blockText.length()) {
|
||||
cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
|
||||
cursor.removeSelectedText();
|
||||
} else {
|
||||
int column = d->m_tabSettings.columnAt(blockText, firstNonSpace);
|
||||
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, firstNonSpace);
|
||||
QString indentationString = d->m_tabSettings.indentationString(0, column, column - indent, block);
|
||||
cursor.insertText(indentationString);
|
||||
}
|
||||
foreach (block, blocks) {
|
||||
QString blockText = block.text();
|
||||
d->m_tabSettings.removeTrailingWhitespace(cursor, block);
|
||||
const int indent = indentations[block.blockNumber()];
|
||||
if (cleanIndentation && !d->m_tabSettings.isIndentationClean(block, indent)) {
|
||||
cursor.setPosition(block.position());
|
||||
int firstNonSpace = d->m_tabSettings.firstNonSpace(blockText);
|
||||
if (firstNonSpace == blockText.length()) {
|
||||
cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
|
||||
cursor.removeSelectedText();
|
||||
} else {
|
||||
int column = d->m_tabSettings.columnAt(blockText, firstNonSpace);
|
||||
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, firstNonSpace);
|
||||
QString indentationString = d->m_tabSettings.indentationString(0, column, column - indent, block);
|
||||
cursor.insertText(indentationString);
|
||||
}
|
||||
}
|
||||
|
||||
block = block.next();
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user