Commit 2cd07174 authored by Igor Kushnir's avatar Igor Kushnir
Browse files

Source formatters: deduplicate and share a common MIME type list

The text/x-csrc MIME type was two positions lower in
CustomScriptPlugin::predefinedStyle()'s version of the list, but this
shouldn't make a difference because:
* text/x-chdr corresponds to the same language as text/x-csrc - C;
* text/x-c++hdr is a subclass of text/x-chdr, which is still above it.

Sharing one common list instance is faster than constructing a new list
each time it is needed.
parent 97bc6339
......@@ -186,6 +186,22 @@ QMap<QString, QVariant> ISourceFormatter::stringToOptionMap(const QString &optio
return map;
}
SourceFormatterStyle::MimeList ISourceFormatter::mimeTypesSupportedByBuiltInStyles()
{
static const SourceFormatterStyle::MimeList list = {
{QStringLiteral("text/x-c++src"), QStringLiteral("C++")},
{QStringLiteral("text/x-csrc"), QStringLiteral("C")},
{QStringLiteral("text/x-chdr"), QStringLiteral("C")},
{QStringLiteral("text/x-c++hdr"), QStringLiteral("C++")},
{QStringLiteral("text/x-java"), QStringLiteral("Java")},
{QStringLiteral("text/x-csharp"), QStringLiteral("C#")},
{QStringLiteral("text/x-objcsrc"), QStringLiteral("Objective-C")},
{QStringLiteral("text/x-objc++src"), QStringLiteral("Objective-C++")},
{QStringLiteral("text/x-objchdr"), QStringLiteral("Objective-C")},
};
return list;
}
QString ISourceFormatter::missingExecutableMessage(const QString &name)
{
return i18n("The executable %1 cannot be found. Please make sure"
......
......@@ -211,6 +211,10 @@ class KDEVPLATFORMINTERFACES_EXPORT ISourceFormatter
*/
static QMap<QString, QVariant> stringToOptionMap(const QString &option);
/** \return The shared list of MIME types supported by each built-in style.
*/
static SourceFormatterStyle::MimeList mimeTypesSupportedByBuiltInStyles();
/** \return A message to display when an executable needed by a
* plugin is missing. This should be returned as description
* if a needed executable is not found.
......
......@@ -258,21 +258,6 @@ QString AStylePlugin::formatSource(const QString& text, const QUrl &url, const Q
return formatSourceWithStyle(style, text, url, mime, leftContext, rightContext);
}
static SourceFormatterStyle::MimeList supportedMimeTypes()
{
return {
{QStringLiteral("text/x-c++src"), QStringLiteral("C++")},
{QStringLiteral("text/x-csrc"), QStringLiteral("C")},
{QStringLiteral("text/x-chdr"), QStringLiteral("C")},
{QStringLiteral("text/x-c++hdr"), QStringLiteral("C++")},
{QStringLiteral("text/x-java"), QStringLiteral("Java")},
{QStringLiteral("text/x-csharp"), QStringLiteral("C#")},
{QStringLiteral("text/x-objcsrc"), QStringLiteral("Objective-C")},
{QStringLiteral("text/x-objc++src"), QStringLiteral("Objective-C++")},
{QStringLiteral("text/x-objchdr"), QStringLiteral("Objective-C")},
};
}
SourceFormatterStyle predefinedStyle(const QString& name, const QString& caption = QString())
{
SourceFormatterStyle st = SourceFormatterStyle( name );
......@@ -280,7 +265,7 @@ SourceFormatterStyle predefinedStyle(const QString& name, const QString& caption
AStyleFormatter fmt;
fmt.predefinedStyle( name );
st.setContent( fmt.saveStyle() );
st.setMimeTypes(supportedMimeTypes());
st.setMimeTypes(ISourceFormatter::mimeTypesSupportedByBuiltInStyles());
st.setUsePreview(true);
return st;
}
......
......@@ -271,17 +271,8 @@ KDevelop::SourceFormatterStyle CustomScriptPlugin::predefinedStyle(const QString
"This will reformat all files in subdirectory <b>subdir</b> using the <b>uncrustify</b> "
"tool with the config-file <b>uncrustify.config</b>."));
}
result.setMimeTypes({
{QStringLiteral("text/x-c++src"), QStringLiteral("C++")},
{QStringLiteral("text/x-chdr"), QStringLiteral("C")},
{QStringLiteral("text/x-c++hdr"), QStringLiteral("C++")},
{QStringLiteral("text/x-csrc"), QStringLiteral("C")},
{QStringLiteral("text/x-java"), QStringLiteral("Java")},
{QStringLiteral("text/x-csharp"), QStringLiteral("C#")},
{QStringLiteral("text/x-objcsrc"), QStringLiteral("Objective-C")},
{QStringLiteral("text/x-objc++src"), QStringLiteral("Objective-C++")},
{QStringLiteral("text/x-objchdr"), QStringLiteral("Objective-C")},
});
result.setMimeTypes(ISourceFormatter::mimeTypesSupportedByBuiltInStyles());
return result;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment