Commit e4ea3642 authored by Igor Kushnir's avatar Igor Kushnir
Browse files

Don't use the first available formatter if none is configured

The first formatter in the list and its default style are not likely to
match a user's preferred style. Users who care about formatting should
configure it first.

Remove no longer necessary
SourceFormatterController::findFirstFormatterForMimeType(),
SourceFormatter::supportedMimeTypes() and
ISourceFormatterController::isMimeTypeSupported().
parent 72ac821f
......@@ -47,9 +47,6 @@ class KDEVPLATFORMINTERFACES_EXPORT ISourceFormatterController : public QObject
virtual ISourceFormatter* formatterForUrl(const QUrl& url, const QMimeType& mime) = 0;
///\return @c true if there are formatters at all, @c false otherwise
virtual bool hasFormatters() const = 0;
/** \return Whether this mime type is supported by any plugin.
*/
virtual bool isMimeTypeSupported(const QMimeType &mime) = 0;
virtual KDevelop::SourceFormatterStyle styleForUrl(const QUrl& url, const QMimeType& mime) = 0;
......
......@@ -310,28 +310,6 @@ KConfigGroup SourceFormatterController::globalConfig() const
return KSharedConfig::openConfig()->group( Strings::SourceFormatter() );
}
ISourceFormatter* SourceFormatterController::findFirstFormatterForMimeType(const QMimeType& mime ) const
{
Q_D(const SourceFormatterController);
static QHash<QString, ISourceFormatter*> knownFormatters;
const auto formatterIt = knownFormatters.constFind(mime.name());
if (formatterIt != knownFormatters.constEnd())
return *formatterIt;
auto it = std::find_if(d->sourceFormatters.constBegin(), d->sourceFormatters.constEnd(),
[&](ISourceFormatter* iformatter) {
QSharedPointer<SourceFormatter> formatter(createFormatterForPlugin(iformatter));
return (formatter->supportedMimeTypes().contains(mime.name()));
});
ISourceFormatter* iformatter = (it != d->sourceFormatters.constEnd()) ? *it : nullptr;
// cache result in any case
knownFormatters.insert(mime.name(), iformatter);
return iformatter;
}
static void populateStyleFromConfigGroup(SourceFormatterStyle* s, const KConfigGroup& stylegrp)
{
s->setCaption( stylegrp.readEntry( SourceFormatterController::styleCaptionKey(), QString() ) );
......@@ -369,7 +347,7 @@ ISourceFormatter* SourceFormatterController::formatterForUrl(const QUrl& url, co
{
Q_D(SourceFormatterController);
if (!d->enabled || !isMimeTypeSupported(mime)) {
if (!d->enabled) {
return nullptr;
}
......@@ -377,20 +355,12 @@ ISourceFormatter* SourceFormatterController::formatterForUrl(const QUrl& url, co
if( formatter.isEmpty() )
{
return findFirstFormatterForMimeType( mime );
return nullptr;
}
return d->formatterForConfigEntry(formatter, mime.name());
}
bool SourceFormatterController::isMimeTypeSupported(const QMimeType& mime)
{
if( findFirstFormatterForMimeType( mime ) ) {
return true;
}
return false;
}
QString SourceFormatterController::indentationMode(const QMimeType& mime)
{
if (mime.inherits(QStringLiteral("text/x-c++src")) || mime.inherits(QStringLiteral("text/x-chdr")) ||
......@@ -403,9 +373,6 @@ QString SourceFormatterController::indentationMode(const QMimeType& mime)
QString SourceFormatterController::addModelineForCurrentLang(QString input, const QUrl& url, const QMimeType& mime)
{
if( !isMimeTypeSupported(mime) )
return input;
QRegExp kateModelineWithNewline(QStringLiteral("\\s*\\n//\\s*kate:(.*)$"));
// If there already is a modeline in the document, adapt it while formatting, even
......@@ -414,7 +381,11 @@ QString SourceFormatterController::addModelineForCurrentLang(QString input, cons
kateModelineWithNewline.indexIn( input ) == -1 )
return input;
ISourceFormatter* fmt = formatterForUrl(url, mime);
const ISourceFormatter* const fmt = formatterForUrl(url, mime);
if (!fmt) {
return input;
}
ISourceFormatter::Indentation indentation = fmt->indentation(url);
if( !indentation.isValid() )
......@@ -424,9 +395,6 @@ QString SourceFormatterController::addModelineForCurrentLang(QString input, cons
QTextStream os(&output, QIODevice::WriteOnly);
QTextStream is(&input, QIODevice::ReadOnly);
Q_ASSERT(fmt);
QString modeline(QStringLiteral("// kate: ")
+ QLatin1String("indent-mode ") + indentationMode(mime) + QLatin1String("; "));
......
......@@ -11,7 +11,6 @@
#include <interfaces/isourceformattercontroller.h>
#include <interfaces/isourceformatter.h>
#include <QSet>
#include <QVector>
#include <QMimeType>
......@@ -46,18 +45,7 @@ struct SourceFormatter
// style name -> style. style objects owned by this
using StyleMap = QMap<QString,KDevelop::SourceFormatterStyle*>;
StyleMap styles;
// Get a list of supported mime types from the style map.
QSet<QString> supportedMimeTypes() const
{
QSet<QString> supported;
for ( auto style: styles ) {
const auto mimeTypes = style->mimeTypes();
for (auto& item : mimeTypes) {
supported.insert(item.mimeType);
}
}
return supported;
}
SourceFormatter() = default;
~SourceFormatter()
{
......@@ -99,9 +87,6 @@ public:
*/
ISourceFormatter* formatterForUrl(const QUrl& url, const QMimeType& mime) override;
bool hasFormatters() const override;
/** \return Whether this mime type is supported by any plugin.
*/
bool isMimeTypeSupported(const QMimeType& mime) override;
/**
* @brief Instantiate a Formatter for the given plugin and load its configuration.
......@@ -111,11 +96,6 @@ public:
*/
SourceFormatter* createFormatterForPlugin(KDevelop::ISourceFormatter* ifmt) const;
/**
* @brief Find the first formatter which supports a given mime type.
*/
ISourceFormatter* findFirstFormatterForMimeType(const QMimeType& mime) const;
KDevelop::ContextMenuExtension contextMenuExtension(KDevelop::Context* context, QWidget* parent);
KDevelop::SourceFormatterStyle styleForUrl(const QUrl& url, const QMimeType& mime) override;
......
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