Support configurable trailing-whitespace cleanup

Allow the user to configure how trailing whitespace is handled. In some
file types, for example, Markdown, trailing whitespace is semantically
important. This change allows the user to select, via delimited list of
wildcard filename patterns, which files to ignore for trailing
whitespace cleanup.

Task-number: QTCREATORBUG-13358
Change-Id: Ie6814d8c178bed8e3de78e6d359b9940d2ba0ead
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Junker, Gregory
2020-01-16 14:18:01 -08:00
committed by Gregory Junker
parent 3fe1911d46
commit e7f784ca73
6 changed files with 205 additions and 38 deletions

View File

@@ -101,6 +101,8 @@ BehaviorSettingsWidget::BehaviorSettingsWidget(QWidget *parent)
this, &BehaviorSettingsWidget::slotStorageSettingsChanged); this, &BehaviorSettingsWidget::slotStorageSettingsChanged);
connect(d->m_ui.cleanIndentation, &QAbstractButton::clicked, connect(d->m_ui.cleanIndentation, &QAbstractButton::clicked,
this, &BehaviorSettingsWidget::slotStorageSettingsChanged); this, &BehaviorSettingsWidget::slotStorageSettingsChanged);
connect(d->m_ui.skipTrailingWhitespace, &QAbstractButton::clicked,
this, &BehaviorSettingsWidget::slotStorageSettingsChanged);
connect(d->m_ui.mouseHiding, &QAbstractButton::clicked, connect(d->m_ui.mouseHiding, &QAbstractButton::clicked,
this, &BehaviorSettingsWidget::slotBehaviorSettingsChanged); this, &BehaviorSettingsWidget::slotBehaviorSettingsChanged);
connect(d->m_ui.mouseNavigation, &QAbstractButton::clicked, connect(d->m_ui.mouseNavigation, &QAbstractButton::clicked,
@@ -190,6 +192,9 @@ void BehaviorSettingsWidget::setAssignedStorageSettings(const StorageSettings &s
d->m_ui.inEntireDocument->setChecked(storageSettings.m_inEntireDocument); d->m_ui.inEntireDocument->setChecked(storageSettings.m_inEntireDocument);
d->m_ui.cleanIndentation->setChecked(storageSettings.m_cleanIndentation); d->m_ui.cleanIndentation->setChecked(storageSettings.m_cleanIndentation);
d->m_ui.addFinalNewLine->setChecked(storageSettings.m_addFinalNewLine); d->m_ui.addFinalNewLine->setChecked(storageSettings.m_addFinalNewLine);
d->m_ui.skipTrailingWhitespace->setChecked(storageSettings.m_skipTrailingWhitespace);
d->m_ui.ignoreFileTypes->setText(storageSettings.m_ignoreFileTypes);
d->m_ui.ignoreFileTypes->setEnabled(d->m_ui.skipTrailingWhitespace->isChecked());
} }
void BehaviorSettingsWidget::assignedStorageSettings(StorageSettings *storageSettings) const void BehaviorSettingsWidget::assignedStorageSettings(StorageSettings *storageSettings) const
@@ -198,6 +203,8 @@ void BehaviorSettingsWidget::assignedStorageSettings(StorageSettings *storageSet
storageSettings->m_inEntireDocument = d->m_ui.inEntireDocument->isChecked(); storageSettings->m_inEntireDocument = d->m_ui.inEntireDocument->isChecked();
storageSettings->m_cleanIndentation = d->m_ui.cleanIndentation->isChecked(); storageSettings->m_cleanIndentation = d->m_ui.cleanIndentation->isChecked();
storageSettings->m_addFinalNewLine = d->m_ui.addFinalNewLine->isChecked(); storageSettings->m_addFinalNewLine = d->m_ui.addFinalNewLine->isChecked();
storageSettings->m_skipTrailingWhitespace = d->m_ui.skipTrailingWhitespace->isChecked();
storageSettings->m_ignoreFileTypes = d->m_ui.ignoreFileTypes->text();
} }
void BehaviorSettingsWidget::updateConstrainTooltipsBoxTooltip() const void BehaviorSettingsWidget::updateConstrainTooltipsBoxTooltip() const
@@ -273,6 +280,10 @@ void BehaviorSettingsWidget::slotStorageSettingsChanged()
{ {
StorageSettings settings; StorageSettings settings;
assignedStorageSettings(&settings); assignedStorageSettings(&settings);
bool ignoreFileTypesEnabled = d->m_ui.cleanWhitespace->isChecked() && d->m_ui.skipTrailingWhitespace->isChecked();
d->m_ui.ignoreFileTypes->setEnabled(ignoreFileTypesEnabled);
emit storageSettingsChanged(settings); emit storageSettingsChanged(settings);
} }

View File

@@ -7,7 +7,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>801</width> <width>801</width>
<height>547</height> <height>693</height>
</rect> </rect>
</property> </property>
<layout class="QHBoxLayout" name="horizontalLayout"> <layout class="QHBoxLayout" name="horizontalLayout">
@@ -169,13 +169,72 @@ Specifies how backspace interacts with indentation.
<string>Cleanups Upon Saving</string> <string>Cleanups Upon Saving</string>
</property> </property>
<layout class="QGridLayout" name="gridLayout_2"> <layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0" colspan="2"> <item row="5" column="1">
<widget class="QCheckBox" name="cleanWhitespace"> <widget class="QWidget" name="widget" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QCheckBox" name="skipTrailingWhitespace">
<property name="enabled">
<bool>false</bool>
</property>
<property name="toolTip">
<string>For the file patterns listed, do not trim trailing whitespace.</string>
</property>
<property name="text">
<string>Skip clean whitespace for file types:</string>
</property>
<property name="checked">
<bool>false</bool>
</property>
<property name="tristate">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="ignoreFileTypes">
<property name="enabled">
<bool>false</bool>
</property>
<property name="acceptDrops">
<bool>false</bool>
</property>
<property name="toolTip">
<string>List of wildcard-aware file patterns, separated by commas or semicolons.</string>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="9" column="0" colspan="2">
<widget class="QCheckBox" name="addFinalNewLine">
<property name="enabled">
<bool>true</bool>
</property>
<property name="toolTip"> <property name="toolTip">
<string>Removes trailing whitespace upon saving.</string> <string>Always writes a newline character at the end of the file.</string>
</property> </property>
<property name="text"> <property name="text">
<string>&amp;Clean whitespace</string> <string>&amp;Ensure newline at end of file</string>
</property> </property>
</widget> </widget>
</item> </item>
@@ -195,6 +254,29 @@ Specifies how backspace interacts with indentation.
</property> </property>
</spacer> </spacer>
</item> </item>
<item row="0" column="0" colspan="2">
<widget class="QCheckBox" name="cleanWhitespace">
<property name="toolTip">
<string>Removes trailing whitespace upon saving.</string>
</property>
<property name="text">
<string>&amp;Clean whitespace</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QCheckBox" name="cleanIndentation">
<property name="enabled">
<bool>false</bool>
</property>
<property name="toolTip">
<string>Corrects leading whitespace according to tab settings.</string>
</property>
<property name="text">
<string>Clean indentation</string>
</property>
</widget>
</item>
<item row="1" column="1"> <item row="1" column="1">
<widget class="QCheckBox" name="inEntireDocument"> <widget class="QCheckBox" name="inEntireDocument">
<property name="enabled"> <property name="enabled">
@@ -214,29 +296,6 @@ Specifies how backspace interacts with indentation.
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="1">
<widget class="QCheckBox" name="cleanIndentation">
<property name="enabled">
<bool>false</bool>
</property>
<property name="toolTip">
<string>Corrects leading whitespace according to tab settings.</string>
</property>
<property name="text">
<string>Clean indentation</string>
</property>
</widget>
</item>
<item row="3" column="0" colspan="2">
<widget class="QCheckBox" name="addFinalNewLine">
<property name="toolTip">
<string>Always writes a newline character at the end of the file.</string>
</property>
<property name="text">
<string>&amp;Ensure newline at end of file</string>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
</item> </item>
@@ -521,5 +580,37 @@ Specifies how backspace interacts with indentation.
</hint> </hint>
</hints> </hints>
</connection> </connection>
<connection>
<sender>cleanWhitespace</sender>
<signal>toggled(bool)</signal>
<receiver>skipTrailingWhitespace</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>530</x>
<y>48</y>
</hint>
<hint type="destinationlabel">
<x>548</x>
<y>144</y>
</hint>
</hints>
</connection>
<connection>
<sender>cleanWhitespace</sender>
<signal>toggled(bool)</signal>
<receiver>ignoreFileTypes</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>530</x>
<y>48</y>
</hint>
<hint type="destinationlabel">
<x>556</x>
<y>177</y>
</hint>
</hints>
</connection>
</connections> </connections>
</ui> </ui>

View File

@@ -27,6 +27,7 @@
#include <utils/settingsutils.h> #include <utils/settingsutils.h>
#include <QRegularExpression>
#include <QSettings> #include <QSettings>
#include <QString> #include <QString>
@@ -36,13 +37,18 @@ static const char cleanWhitespaceKey[] = "cleanWhitespace";
static const char inEntireDocumentKey[] = "inEntireDocument"; static const char inEntireDocumentKey[] = "inEntireDocument";
static const char addFinalNewLineKey[] = "addFinalNewLine"; static const char addFinalNewLineKey[] = "addFinalNewLine";
static const char cleanIndentationKey[] = "cleanIndentation"; static const char cleanIndentationKey[] = "cleanIndentation";
static const char skipTrailingWhitespaceKey[] = "skipTrailingWhitespace";
static const char ignoreFileTypesKey[] = "ignoreFileTypes";
static const char groupPostfix[] = "StorageSettings"; static const char groupPostfix[] = "StorageSettings";
static const char defaultTrailingWhitespaceBlacklist[] = "*.md, *.MD, Makefile";
StorageSettings::StorageSettings() StorageSettings::StorageSettings()
: m_cleanWhitespace(true), : m_ignoreFileTypes(defaultTrailingWhitespaceBlacklist),
m_cleanWhitespace(true),
m_inEntireDocument(false), m_inEntireDocument(false),
m_addFinalNewLine(true), m_addFinalNewLine(true),
m_cleanIndentation(true) m_cleanIndentation(true),
m_skipTrailingWhitespace(true)
{ {
} }
@@ -63,6 +69,8 @@ void StorageSettings::toMap(const QString &prefix, QVariantMap *map) const
map->insert(prefix + QLatin1String(inEntireDocumentKey), m_inEntireDocument); map->insert(prefix + QLatin1String(inEntireDocumentKey), m_inEntireDocument);
map->insert(prefix + QLatin1String(addFinalNewLineKey), m_addFinalNewLine); map->insert(prefix + QLatin1String(addFinalNewLineKey), m_addFinalNewLine);
map->insert(prefix + QLatin1String(cleanIndentationKey), m_cleanIndentation); map->insert(prefix + QLatin1String(cleanIndentationKey), m_cleanIndentation);
map->insert(prefix + QLatin1String(skipTrailingWhitespaceKey), m_skipTrailingWhitespace);
map->insert(prefix + QLatin1String(ignoreFileTypesKey), m_ignoreFileTypes.toLatin1().data());
} }
void StorageSettings::fromMap(const QString &prefix, const QVariantMap &map) void StorageSettings::fromMap(const QString &prefix, const QVariantMap &map)
@@ -75,6 +83,42 @@ void StorageSettings::fromMap(const QString &prefix, const QVariantMap &map)
map.value(prefix + QLatin1String(addFinalNewLineKey), m_addFinalNewLine).toBool(); map.value(prefix + QLatin1String(addFinalNewLineKey), m_addFinalNewLine).toBool();
m_cleanIndentation = m_cleanIndentation =
map.value(prefix + QLatin1String(cleanIndentationKey), m_cleanIndentation).toBool(); map.value(prefix + QLatin1String(cleanIndentationKey), m_cleanIndentation).toBool();
m_skipTrailingWhitespace =
map.value(prefix + QLatin1String(skipTrailingWhitespaceKey), m_skipTrailingWhitespace).toBool();
m_ignoreFileTypes =
map.value(prefix + QLatin1String(ignoreFileTypesKey), m_ignoreFileTypes).toString();
}
bool StorageSettings::removeTrailingWhitespace(const QString &fileName) const
{
// if the user has elected not to trim trailing whitespace altogether, then
// early out here
if (!m_skipTrailingWhitespace) {
return true;
}
const QString ignoreFileTypesRegExp(R"(\s*((?>\*\.)?[\w\d\.\*]+)[,;]?\s*)");
// use the ignore-files regex to extract the specified file patterns
QRegularExpression re(ignoreFileTypesRegExp);
QRegularExpressionMatchIterator iter = re.globalMatch(m_ignoreFileTypes);
while (iter.hasNext()) {
QRegularExpressionMatch match = iter.next();
QString pattern = match.captured(1);
QString wildcardRegExp = QRegularExpression::wildcardToRegularExpression(pattern);
QRegularExpression patternRegExp(wildcardRegExp);
QRegularExpressionMatch patternMatch = patternRegExp.match(fileName);
if (patternMatch.hasMatch()) {
// if the filename has a pattern we want to ignore, then we need to return
// false ("don't remove trailing whitespace")
return false;
}
}
// the supplied pattern does not match, so we want to remove trailing whitespace
return true;
} }
bool StorageSettings::equals(const StorageSettings &ts) const bool StorageSettings::equals(const StorageSettings &ts) const
@@ -82,7 +126,9 @@ bool StorageSettings::equals(const StorageSettings &ts) const
return m_addFinalNewLine == ts.m_addFinalNewLine return m_addFinalNewLine == ts.m_addFinalNewLine
&& m_cleanWhitespace == ts.m_cleanWhitespace && m_cleanWhitespace == ts.m_cleanWhitespace
&& m_inEntireDocument == ts.m_inEntireDocument && m_inEntireDocument == ts.m_inEntireDocument
&& m_cleanIndentation == ts.m_cleanIndentation; && m_cleanIndentation == ts.m_cleanIndentation
&& m_skipTrailingWhitespace == ts.m_skipTrailingWhitespace
&& m_ignoreFileTypes == ts.m_ignoreFileTypes;
} }
} // namespace TextEditor } // namespace TextEditor

View File

@@ -46,12 +46,17 @@ public:
void toMap(const QString &prefix, QVariantMap *map) const; void toMap(const QString &prefix, QVariantMap *map) const;
void fromMap(const QString &prefix, const QVariantMap &map); void fromMap(const QString &prefix, const QVariantMap &map);
// calculated based on boolean setting plus file type blacklist examination
bool removeTrailingWhitespace(const QString &filePattern) const;
bool equals(const StorageSettings &ts) const; bool equals(const StorageSettings &ts) const;
QString m_ignoreFileTypes;
bool m_cleanWhitespace; bool m_cleanWhitespace;
bool m_inEntireDocument; bool m_inEntireDocument;
bool m_addFinalNewLine; bool m_addFinalNewLine;
bool m_cleanIndentation; bool m_cleanIndentation;
bool m_skipTrailingWhitespace;
}; };
inline bool operator==(const StorageSettings &t1, const StorageSettings &t2) { return t1.equals(t2); } inline bool operator==(const StorageSettings &t1, const StorageSettings &t2) { return t1.equals(t2); }

View File

@@ -622,7 +622,7 @@ bool TextDocument::save(QString *errorString, const QString &saveFileName, bool
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.m_cleanIndentation, d->m_storageSettings.m_inEntireDocument); cleanWhitespace(cursor, d->m_storageSettings);
if (d->m_storageSettings.m_addFinalNewLine) if (d->m_storageSettings.m_addFinalNewLine)
ensureFinalNewLine(cursor); ensureFinalNewLine(cursor);
cursor.endEditBlock(); cursor.endEditBlock();
@@ -883,14 +883,22 @@ void TextDocument::cleanWhitespace(const QTextCursor &cursor)
QTextCursor copyCursor = cursor; QTextCursor copyCursor = cursor;
copyCursor.setVisualNavigation(false); copyCursor.setVisualNavigation(false);
copyCursor.beginEditBlock(); copyCursor.beginEditBlock();
cleanWhitespace(copyCursor, true, true);
cleanWhitespace(copyCursor, d->m_storageSettings);
if (!hasSelection) if (!hasSelection)
ensureFinalNewLine(copyCursor); ensureFinalNewLine(copyCursor);
copyCursor.endEditBlock(); copyCursor.endEditBlock();
} }
void TextDocument::cleanWhitespace(QTextCursor &cursor, bool cleanIndentation, bool inEntireDocument) void TextDocument::cleanWhitespace(QTextCursor &cursor, const StorageSettings &storageSettings)
{ {
if (!d->m_storageSettings.m_cleanWhitespace)
return;
const QString fileName(filePath().fileName());
auto documentLayout = qobject_cast<TextDocumentLayout*>(d->m_document.documentLayout()); auto documentLayout = qobject_cast<TextDocumentLayout*>(d->m_document.documentLayout());
Q_ASSERT(cursor.visualNavigation() == false); Q_ASSERT(cursor.visualNavigation() == false);
@@ -901,7 +909,7 @@ void TextDocument::cleanWhitespace(QTextCursor &cursor, bool cleanIndentation, b
QVector<QTextBlock> blocks; QVector<QTextBlock> blocks;
while (block.isValid() && block != end) { while (block.isValid() && block != end) {
if (inEntireDocument || block.revision() != documentLayout->lastSaveRevision) if (storageSettings.m_inEntireDocument || block.revision() != documentLayout->lastSaveRevision)
blocks.append(block); blocks.append(block);
block = block.next(); block = block.next();
} }
@@ -914,9 +922,12 @@ void TextDocument::cleanWhitespace(QTextCursor &cursor, bool cleanIndentation, b
foreach (block, blocks) { foreach (block, blocks) {
QString blockText = block.text(); QString blockText = block.text();
currentTabSettings.removeTrailingWhitespace(cursor, block);
if (storageSettings.removeTrailingWhitespace(fileName))
currentTabSettings.removeTrailingWhitespace(cursor, block);
const int indent = indentations[block.blockNumber()]; const int indent = indentations[block.blockNumber()];
if (cleanIndentation && !currentTabSettings.isIndentationClean(block, indent)) { if (storageSettings.m_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()) {
@@ -934,6 +945,9 @@ void TextDocument::cleanWhitespace(QTextCursor &cursor, bool cleanIndentation, b
void TextDocument::ensureFinalNewLine(QTextCursor& cursor) void TextDocument::ensureFinalNewLine(QTextCursor& cursor)
{ {
if (!d->m_storageSettings.m_addFinalNewLine)
return;
cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
bool emptyFile = !cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor); bool emptyFile = !cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor);

View File

@@ -169,7 +169,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, bool cleanIndentation, bool inEntireDocument); void cleanWhitespace(QTextCursor &cursor, const StorageSettings &storageSettings);
void ensureFinalNewLine(QTextCursor &cursor); void ensureFinalNewLine(QTextCursor &cursor);
void modificationChanged(bool modified); void modificationChanged(bool modified);
void updateLayout() const; void updateLayout() const;