forked from qt-creator/qt-creator
be more conservative when reindenting code: only change the
actual whitespace when the indentation column changes. Complete whitespace cleanup can still be done either automatically on save (when configured) or when explicitely called with the "Clean Whitespace" action from Edit/Advanced.
This commit is contained in:
@@ -93,7 +93,7 @@ bool BaseTextDocument::save(const QString &fileName)
|
||||
|
||||
cursor.beginEditBlock();
|
||||
if (m_storageSettings.m_cleanWhitespace)
|
||||
cleanWhitespace(cursor, m_storageSettings.m_inEntireDocument);
|
||||
cleanWhitespace(cursor, m_storageSettings.m_cleanIndentation, m_storageSettings.m_inEntireDocument);
|
||||
if (m_storageSettings.m_addFinalNewLine)
|
||||
ensureFinalNewLine(cursor);
|
||||
cursor.endEditBlock();
|
||||
@@ -305,13 +305,12 @@ void BaseTextDocument::cleanWhitespace()
|
||||
{
|
||||
QTextCursor cursor(m_document);
|
||||
cursor.beginEditBlock();
|
||||
cleanWhitespace(cursor, true);
|
||||
if (m_storageSettings.m_addFinalNewLine)
|
||||
ensureFinalNewLine(cursor);
|
||||
cleanWhitespace(cursor, true, true);
|
||||
ensureFinalNewLine(cursor);
|
||||
cursor.endEditBlock();
|
||||
}
|
||||
|
||||
void BaseTextDocument::cleanWhitespace(QTextCursor& cursor, bool inEntireDocument)
|
||||
void BaseTextDocument::cleanWhitespace(QTextCursor& cursor, bool cleanIndentation, bool inEntireDocument)
|
||||
{
|
||||
|
||||
TextEditDocumentLayout *documentLayout = qobject_cast<TextEditDocumentLayout*>(m_document->documentLayout());
|
||||
@@ -327,7 +326,7 @@ void BaseTextDocument::cleanWhitespace(QTextCursor& cursor, bool inEntireDocumen
|
||||
cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor, trailing);
|
||||
cursor.removeSelectedText();
|
||||
}
|
||||
if (m_storageSettings.m_cleanIndentation && !m_tabSettings.isIndentationClean(blockText)) {
|
||||
if (cleanIndentation && !m_tabSettings.isIndentationClean(blockText)) {
|
||||
cursor.setPosition(block.position());
|
||||
int firstNonSpace = m_tabSettings.firstNonSpace(blockText);
|
||||
if (firstNonSpace == blockText.length()) {
|
||||
|
||||
@@ -146,7 +146,7 @@ private:
|
||||
bool m_hasDecodingError;
|
||||
QByteArray m_decodingErrorSample;
|
||||
|
||||
void cleanWhitespace(QTextCursor& cursor, bool onlyInModifiedLines);
|
||||
void cleanWhitespace(QTextCursor& cursor, bool cleanIndentation, bool inEntireDocument);
|
||||
void ensureFinalNewLine(QTextCursor& cursor);
|
||||
};
|
||||
|
||||
|
||||
@@ -116,6 +116,12 @@ QString TabSettings::indentationString(const QString &text) const
|
||||
}
|
||||
|
||||
|
||||
int TabSettings::indentationColumn(const QString &text) const
|
||||
{
|
||||
return columnAt(text, firstNonSpace(text));
|
||||
}
|
||||
|
||||
|
||||
int TabSettings::trailingWhitespaces(const QString &text) const
|
||||
{
|
||||
int i = 0;
|
||||
@@ -231,7 +237,7 @@ void TabSettings::indentLine(QTextBlock block, int newIndent) const
|
||||
const int oldBlockLength = text.size();
|
||||
|
||||
// Quickly check whether indenting is required.
|
||||
if (oldBlockLength == 0 && newIndent == 0)
|
||||
if (indentationColumn(text) == newIndent)
|
||||
return;
|
||||
|
||||
const QString indentString = indentationString(0, newIndent);
|
||||
@@ -240,12 +246,6 @@ void TabSettings::indentLine(QTextBlock block, int newIndent) const
|
||||
if (oldBlockLength == indentString.length() && text == indentString)
|
||||
return;
|
||||
|
||||
if (oldBlockLength > indentString.length() &&
|
||||
text.startsWith(indentString) &&
|
||||
!text.at(indentString.length()).isSpace()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QTextCursor cursor(block);
|
||||
cursor.beginEditBlock();
|
||||
cursor.movePosition(QTextCursor::StartOfBlock);
|
||||
|
||||
@@ -64,6 +64,7 @@ struct TEXTEDITOR_EXPORT TabSettings
|
||||
int indentedColumn(int column, bool doIndent = true) const;
|
||||
QString indentationString(int startColumn, int targetColumn) const;
|
||||
QString indentationString(const QString &text) const;
|
||||
int indentationColumn(const QString &text) const;
|
||||
|
||||
void indentLine(QTextBlock block, int newIndent) const;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user