TextEditor: Fix clean whitespace action

This is a partial revert of: e7f784ca73

Fixes: QTCREATORBUG-24565
Change-Id: Iffa149e0f97c315355f211f6ae3856fad08f4f3d
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
David Schulz
2020-09-07 10:52:54 +02:00
parent d64ebc0f8b
commit 5299eb5bec
2 changed files with 13 additions and 11 deletions

View File

@@ -622,8 +622,11 @@ bool TextDocument::save(QString *errorString, const QString &saveFileName, bool
cursor.beginEditBlock(); cursor.beginEditBlock();
cursor.movePosition(QTextCursor::Start); cursor.movePosition(QTextCursor::Start);
if (d->m_storageSettings.m_cleanWhitespace) if (d->m_storageSettings.m_cleanWhitespace) {
cleanWhitespace(cursor, d->m_storageSettings); cleanWhitespace(cursor,
d->m_storageSettings.m_inEntireDocument,
d->m_storageSettings.m_cleanIndentation);
}
if (d->m_storageSettings.m_addFinalNewLine) if (d->m_storageSettings.m_addFinalNewLine)
ensureFinalNewLine(cursor); ensureFinalNewLine(cursor);
cursor.endEditBlock(); cursor.endEditBlock();
@@ -885,7 +888,7 @@ void TextDocument::cleanWhitespace(const QTextCursor &cursor)
copyCursor.setVisualNavigation(false); copyCursor.setVisualNavigation(false);
copyCursor.beginEditBlock(); copyCursor.beginEditBlock();
cleanWhitespace(copyCursor, d->m_storageSettings); cleanWhitespace(copyCursor, true, true);
if (!hasSelection) if (!hasSelection)
ensureFinalNewLine(copyCursor); ensureFinalNewLine(copyCursor);
@@ -893,11 +896,9 @@ void TextDocument::cleanWhitespace(const QTextCursor &cursor)
copyCursor.endEditBlock(); copyCursor.endEditBlock();
} }
void TextDocument::cleanWhitespace(QTextCursor &cursor, const StorageSettings &storageSettings) void TextDocument::cleanWhitespace(QTextCursor &cursor, bool inEntireDocument,
bool cleanIndentation)
{ {
if (!d->m_storageSettings.m_cleanWhitespace)
return;
const QString fileName(filePath().fileName()); const QString fileName(filePath().fileName());
auto documentLayout = qobject_cast<TextDocumentLayout*>(d->m_document.documentLayout()); auto documentLayout = qobject_cast<TextDocumentLayout*>(d->m_document.documentLayout());
@@ -910,8 +911,9 @@ void TextDocument::cleanWhitespace(QTextCursor &cursor, const StorageSettings &s
QVector<QTextBlock> blocks; QVector<QTextBlock> blocks;
while (block.isValid() && block != end) { while (block.isValid() && block != end) {
if (storageSettings.m_inEntireDocument || block.revision() != documentLayout->lastSaveRevision) if (inEntireDocument || block.revision() != documentLayout->lastSaveRevision) {
blocks.append(block); blocks.append(block);
}
block = block.next(); block = block.next();
} }
if (blocks.isEmpty()) if (blocks.isEmpty())
@@ -924,11 +926,11 @@ void TextDocument::cleanWhitespace(QTextCursor &cursor, const StorageSettings &s
foreach (block, blocks) { foreach (block, blocks) {
QString blockText = block.text(); QString blockText = block.text();
if (storageSettings.removeTrailingWhitespace(fileName)) if (d->m_storageSettings.removeTrailingWhitespace(fileName))
currentTabSettings.removeTrailingWhitespace(cursor, block); currentTabSettings.removeTrailingWhitespace(cursor, block);
const int indent = indentations[block.blockNumber()]; const int indent = indentations[block.blockNumber()];
if (storageSettings.m_cleanIndentation && !currentTabSettings.isIndentationClean(block, indent)) { if (cleanIndentation && !currentTabSettings.isIndentationClean(block, indent)) {
cursor.setPosition(block.position()); cursor.setPosition(block.position());
int firstNonSpace = currentTabSettings.firstNonSpace(blockText); int firstNonSpace = currentTabSettings.firstNonSpace(blockText);
if (firstNonSpace == blockText.length()) { if (firstNonSpace == blockText.length()) {

View File

@@ -170,7 +170,7 @@ protected:
private: private:
OpenResult openImpl(QString *errorString, const QString &fileName, const QString &realFileName, OpenResult openImpl(QString *errorString, const QString &fileName, const QString &realFileName,
bool reload); bool reload);
void cleanWhitespace(QTextCursor &cursor, const StorageSettings &storageSettings); void cleanWhitespace(QTextCursor &cursor, bool inEntireDocument, bool cleanIndentation);
void ensureFinalNewLine(QTextCursor &cursor); void ensureFinalNewLine(QTextCursor &cursor);
void modificationChanged(bool modified); void modificationChanged(bool modified);
void updateLayout() const; void updateLayout() const;