aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/texteditor/storagesettings.cpp
diff options
context:
space:
mode:
authorJunker, Gregory <gregory.junker@intel.com>2020-01-16 14:18:01 -0800
committerGregory Junker <gregory.junker@intel.com>2020-02-19 18:09:13 +0000
commite7f784ca7393bddb60cbb111d3bdb7cd0e6d122e (patch)
treeb037333fd0c281ff96075389d34c8819b1636eb9 /src/plugins/texteditor/storagesettings.cpp
parent3fe1911d4616a1eeafc538745381dba1270f38cf (diff)
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>
Diffstat (limited to 'src/plugins/texteditor/storagesettings.cpp')
-rw-r--r--src/plugins/texteditor/storagesettings.cpp52
1 files changed, 49 insertions, 3 deletions
diff --git a/src/plugins/texteditor/storagesettings.cpp b/src/plugins/texteditor/storagesettings.cpp
index a1c4645b0c..2053ffa440 100644
--- a/src/plugins/texteditor/storagesettings.cpp
+++ b/src/plugins/texteditor/storagesettings.cpp
@@ -27,6 +27,7 @@
#include <utils/settingsutils.h>
+#include <QRegularExpression>
#include <QSettings>
#include <QString>
@@ -36,13 +37,18 @@ static const char cleanWhitespaceKey[] = "cleanWhitespace";
static const char inEntireDocumentKey[] = "inEntireDocument";
static const char addFinalNewLineKey[] = "addFinalNewLine";
static const char cleanIndentationKey[] = "cleanIndentation";
+static const char skipTrailingWhitespaceKey[] = "skipTrailingWhitespace";
+static const char ignoreFileTypesKey[] = "ignoreFileTypes";
static const char groupPostfix[] = "StorageSettings";
+static const char defaultTrailingWhitespaceBlacklist[] = "*.md, *.MD, Makefile";
StorageSettings::StorageSettings()
- : m_cleanWhitespace(true),
+ : m_ignoreFileTypes(defaultTrailingWhitespaceBlacklist),
+ m_cleanWhitespace(true),
m_inEntireDocument(false),
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(addFinalNewLineKey), m_addFinalNewLine);
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)
@@ -75,6 +83,42 @@ void StorageSettings::fromMap(const QString &prefix, const QVariantMap &map)
map.value(prefix + QLatin1String(addFinalNewLineKey), m_addFinalNewLine).toBool();
m_cleanIndentation =
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
@@ -82,7 +126,9 @@ bool StorageSettings::equals(const StorageSettings &ts) const
return m_addFinalNewLine == ts.m_addFinalNewLine
&& m_cleanWhitespace == ts.m_cleanWhitespace
&& 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