forked from qt-creator/qt-creator
Editor: Remove trailing whitespaces in generated files.
Task-number: QTCREATORBUG-9833 Change-Id: I7440d1e7c8ef303db1b6abfa8cd12325b934b0d2 Reviewed-by: Nikolai Kosjar <nikolai.kosjar@digia.com> Reviewed-by: Jarek Kobus <jaroslaw.kobus@digia.com>
This commit is contained in:
@@ -48,6 +48,7 @@
|
|||||||
#include <texteditor/icodestylepreferencesfactory.h>
|
#include <texteditor/icodestylepreferencesfactory.h>
|
||||||
#include <texteditor/normalindenter.h>
|
#include <texteditor/normalindenter.h>
|
||||||
#include <texteditor/tabsettings.h>
|
#include <texteditor/tabsettings.h>
|
||||||
|
#include <texteditor/storagesettings.h>
|
||||||
#include <projectexplorer/project.h>
|
#include <projectexplorer/project.h>
|
||||||
#include <projectexplorer/editorconfiguration.h>
|
#include <projectexplorer/editorconfiguration.h>
|
||||||
|
|
||||||
@@ -580,13 +581,19 @@ void ProjectFileWizardExtension::applyCodeStyle(Core::GeneratedFile *file) const
|
|||||||
|
|
||||||
TextEditor::ICodeStylePreferences *codeStylePrefs = codeStylePreferences(baseProject, languageId);
|
TextEditor::ICodeStylePreferences *codeStylePrefs = codeStylePreferences(baseProject, languageId);
|
||||||
indenter->setCodeStylePreferences(codeStylePrefs);
|
indenter->setCodeStylePreferences(codeStylePrefs);
|
||||||
|
|
||||||
QTextDocument doc(file->contents());
|
QTextDocument doc(file->contents());
|
||||||
QTextCursor cursor(&doc);
|
QTextCursor cursor(&doc);
|
||||||
cursor.select(QTextCursor::Document);
|
cursor.select(QTextCursor::Document);
|
||||||
indenter->indent(&doc, cursor, QChar::Null, codeStylePrefs->currentTabSettings());
|
indenter->indent(&doc, cursor, QChar::Null, codeStylePrefs->currentTabSettings());
|
||||||
file->setContents(doc.toPlainText());
|
|
||||||
delete indenter;
|
delete indenter;
|
||||||
|
if (TextEditor::TextEditorSettings::instance()->storageSettings().m_cleanWhitespace) {
|
||||||
|
QTextBlock block = doc.firstBlock();
|
||||||
|
while (block.isValid()) {
|
||||||
|
codeStylePrefs->currentTabSettings().removeTrailingWhitespace(cursor, block);
|
||||||
|
block = block.next();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
file->setContents(doc.toPlainText());
|
||||||
}
|
}
|
||||||
|
|
||||||
QStringList ProjectFileWizardExtension::getProjectChoices() const
|
QStringList ProjectFileWizardExtension::getProjectChoices() const
|
||||||
|
|||||||
@@ -469,11 +469,7 @@ void BaseTextDocument::cleanWhitespace(QTextCursor &cursor, bool cleanIndentatio
|
|||||||
if (inEntireDocument || block.revision() != documentLayout->lastSaveRevision) {
|
if (inEntireDocument || block.revision() != documentLayout->lastSaveRevision) {
|
||||||
|
|
||||||
QString blockText = block.text();
|
QString blockText = block.text();
|
||||||
if (int trailing = d->m_tabSettings.trailingWhitespaces(blockText)) {
|
d->m_tabSettings.removeTrailingWhitespace(cursor, block);
|
||||||
cursor.setPosition(block.position() + block.length() - 1);
|
|
||||||
cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor, trailing);
|
|
||||||
cursor.removeSelectedText();
|
|
||||||
}
|
|
||||||
if (cleanIndentation && !d->m_tabSettings.isIndentationClean(block)) {
|
if (cleanIndentation && !d->m_tabSettings.isIndentationClean(block)) {
|
||||||
cursor.setPosition(block.position());
|
cursor.setPosition(block.position());
|
||||||
int firstNonSpace = d->m_tabSettings.firstNonSpace(blockText);
|
int firstNonSpace = d->m_tabSettings.firstNonSpace(blockText);
|
||||||
|
|||||||
@@ -152,6 +152,15 @@ int TabSettings::trailingWhitespaces(const QString &text) const
|
|||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TabSettings::removeTrailingWhitespace(QTextCursor cursor, QTextBlock &block) const
|
||||||
|
{
|
||||||
|
if (const int trailing = trailingWhitespaces(block.text())) {
|
||||||
|
cursor.setPosition(block.position() + block.length() - 1);
|
||||||
|
cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor, trailing);
|
||||||
|
cursor.removeSelectedText();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool TabSettings::isIndentationClean(const QTextBlock &block) const
|
bool TabSettings::isIndentationClean(const QTextBlock &block) const
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|||||||
@@ -85,6 +85,7 @@ public:
|
|||||||
void reindentLine(QTextBlock block, int delta) const;
|
void reindentLine(QTextBlock block, int delta) const;
|
||||||
|
|
||||||
int trailingWhitespaces(const QString &text) const;
|
int trailingWhitespaces(const QString &text) const;
|
||||||
|
void removeTrailingWhitespace(QTextCursor cursor, QTextBlock &block) const;
|
||||||
bool isIndentationClean(const QTextBlock &block) const;
|
bool isIndentationClean(const QTextBlock &block) const;
|
||||||
bool guessSpacesForTabs(const QTextBlock &block) const;
|
bool guessSpacesForTabs(const QTextBlock &block) const;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user