diff --git a/src/plugins/texteditor/basetextdocument.cpp b/src/plugins/texteditor/basetextdocument.cpp index 8de81100d8cd9c8b4425cc999864b76478dfce82..b2b0e75eeea1e06f87fdc20fcd9e70a53f1f7fa0 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 88e1c609a132fa5dc5f3ae6af81616ac9230f7e6..24dabd7a0aae064a070af48a60d637f6f9b672fa 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 1c20ab7b9040b39928e070d825b02a9ebf51cc1e..393434d90852ab7ca4431f98f674fa1c7713b6f0 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 2fbede49502d1c78c33536edbce927c017abae61..f26e0bb5ad99dae0824bc74d1268f051ed849fb7 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;