forked from qt-creator/qt-creator
Mimetypes v3: Re-implement MimeType setPreferredSuffix
Used by the CppEditor to set the preferred suffix for C/C++ files according to Options > C++ > File Naming. Change-Id: I1b0c862a17febf742202433ee677fcccc1aa511b Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io> Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
@@ -54,6 +54,17 @@
|
||||
|
||||
namespace Utils {
|
||||
|
||||
static QString suffixFromPattern(const QString &pattern)
|
||||
{
|
||||
// Not a simple suffix if it looks like: README or *. or *.* or *.JP*G or *.JP?
|
||||
if (pattern.startsWith(QLatin1String("*.")) &&
|
||||
pattern.length() > 2 &&
|
||||
pattern.indexOf(QLatin1Char('*'), 2) < 0 && pattern.indexOf(QLatin1Char('?'), 2) < 0) {
|
||||
return pattern.mid(2);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
MimeTypePrivate::MimeTypePrivate()
|
||||
: loaded(false), fromCache(false)
|
||||
{}
|
||||
@@ -445,13 +456,9 @@ QStringList MimeType::suffixes() const
|
||||
|
||||
QStringList result;
|
||||
for (const QString &pattern : qAsConst(d->globPatterns)) {
|
||||
// Not a simple suffix if it looks like: README or *. or *.* or *.JP*G or *.JP?
|
||||
if (pattern.startsWith(QLatin1String("*.")) &&
|
||||
pattern.length() > 2 &&
|
||||
pattern.indexOf(QLatin1Char('*'), 2) < 0 && pattern.indexOf(QLatin1Char('?'), 2) < 0) {
|
||||
const QString suffix = pattern.mid(2);
|
||||
const QString suffix = suffixFromPattern(pattern);
|
||||
if (!suffix.isEmpty())
|
||||
result.append(suffix);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -533,15 +540,16 @@ bool MimeType::matchesName(const QString &nameOrAlias) const
|
||||
*/
|
||||
void MimeType::setPreferredSuffix(const QString &suffix)
|
||||
{
|
||||
// MimeDatabasePrivate::instance()->provider()->loadMimeTypePrivate(*d);
|
||||
MimeDatabasePrivate::instance()->loadMimeTypePrivate(const_cast<MimeTypePrivate&>(*d));
|
||||
|
||||
// auto it = std::find_if(d->globPatterns.begin(), d->globPatterns.end(),
|
||||
// [suffix](const QString &pattern) {
|
||||
// return suffixFromPattern(pattern) == suffix;
|
||||
// });
|
||||
// if (it != d->globPatterns.end())
|
||||
// d->globPatterns.erase(it);
|
||||
// d->globPatterns.prepend(QLatin1String("*.") + suffix);
|
||||
auto it = std::find_if(d->globPatterns.begin(),
|
||||
d->globPatterns.end(),
|
||||
[suffix](const QString &pattern) {
|
||||
return suffixFromPattern(pattern) == suffix;
|
||||
});
|
||||
if (it != d->globPatterns.end())
|
||||
d->globPatterns.erase(it);
|
||||
d->globPatterns.prepend(QLatin1String("*.") + suffix);
|
||||
}
|
||||
|
||||
} // namespace Utils
|
||||
|
||||
Reference in New Issue
Block a user