From 16e7f0c16f57e984310cd1bba838a69221f4a18a Mon Sep 17 00:00:00 2001 From: mae <qt-info@nokia.com> Date: Wed, 9 Sep 2009 16:16:46 +0200 Subject: [PATCH] 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. --- src/plugins/texteditor/basetextdocument.cpp | 11 +++++------ src/plugins/texteditor/basetextdocument.h | 2 +- src/plugins/texteditor/tabsettings.cpp | 14 +++++++------- src/plugins/texteditor/tabsettings.h | 1 + 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/plugins/texteditor/basetextdocument.cpp b/src/plugins/texteditor/basetextdocument.cpp index 8de81100d8c..b2b0e75eeea 100644 --- a/src/plugins/texteditor/basetextdocument.cpp +++ b/src/plugins/texteditor/basetextdocument.cpp @@ -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()) { diff --git a/src/plugins/texteditor/basetextdocument.h b/src/plugins/texteditor/basetextdocument.h index 88e1c609a13..24dabd7a0aa 100644 --- a/src/plugins/texteditor/basetextdocument.h +++ b/src/plugins/texteditor/basetextdocument.h @@ -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); }; diff --git a/src/plugins/texteditor/tabsettings.cpp b/src/plugins/texteditor/tabsettings.cpp index 1c20ab7b904..393434d9085 100644 --- a/src/plugins/texteditor/tabsettings.cpp +++ b/src/plugins/texteditor/tabsettings.cpp @@ -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); diff --git a/src/plugins/texteditor/tabsettings.h b/src/plugins/texteditor/tabsettings.h index 2fbede49502..f26e0bb5ad9 100644 --- a/src/plugins/texteditor/tabsettings.h +++ b/src/plugins/texteditor/tabsettings.h @@ -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; -- GitLab