Commit 46a105b4 authored by Christoph Roick's avatar Christoph Roick

Adapt indentation mode after a new project was opened

parent 4a77ecc3
...@@ -85,8 +85,27 @@ public: ...@@ -85,8 +85,27 @@ public:
QList<KDevelop::ProjectBaseItem*> prjItems; QList<KDevelop::ProjectBaseItem*> prjItems;
QList<QUrl> urls; QList<QUrl> urls;
bool enabled = true; bool enabled = true;
ISourceFormatter* formatterForConfigEntry(const QString& entry, const QString& mimename);
}; };
ISourceFormatter* SourceFormatterControllerPrivate::formatterForConfigEntry(const QString& entry, const QString& mimename)
{
QStringList formatterinfo = entry.split( QStringLiteral("||"), QString::SkipEmptyParts );
if( formatterinfo.size() != 2 ) {
qCDebug(SHELL) << "Broken formatting entry for mime:" << mimename << "current value:" << entry;
}
foreach (ISourceFormatter* iformatter, sourceFormatters) {
if (iformatter->name() == formatterinfo.first()) {
return iformatter;
}
}
return nullptr;
}
QString SourceFormatterController::kateModeLineConfigKey() QString SourceFormatterController::kateModeLineConfigKey()
{ {
return QStringLiteral("ModelinesEnabled"); return QStringLiteral("ModelinesEnabled");
...@@ -171,6 +190,7 @@ SourceFormatterController::SourceFormatterController(QObject *parent) ...@@ -171,6 +190,7 @@ SourceFormatterController::SourceFormatterController(QObject *parent)
const auto textDoc = QPointer<TextDocument>(dynamic_cast<TextDocument*>(doc)); const auto textDoc = QPointer<TextDocument>(dynamic_cast<TextDocument*>(doc));
QMetaObject::invokeMethod(this, "documentLoaded", Qt::QueuedConnection, Q_ARG(QPointer<KDevelop::TextDocument>, textDoc)); QMetaObject::invokeMethod(this, "documentLoaded", Qt::QueuedConnection, Q_ARG(QPointer<KDevelop::TextDocument>, textDoc));
}); });
connect(Core::self()->projectController(), &IProjectController::projectOpened, this, &SourceFormatterController::projectOpened);
updateFormatTextAction(); updateFormatTextAction();
} }
...@@ -189,6 +209,32 @@ void SourceFormatterController::documentLoaded(const QPointer<TextDocument>& doc ...@@ -189,6 +209,32 @@ void SourceFormatterController::documentLoaded(const QPointer<TextDocument>& doc
adaptEditorIndentationMode(doc->textDocument(), formatterForUrl(url, mime), url); adaptEditorIndentationMode(doc->textDocument(), formatterForUrl(url, mime), url);
} }
void SourceFormatterController::projectOpened(const IProject* project)
{
// Adapt the indentation mode if a project was just opened. Otherwise if a document
// is loaded before its project, it might not have the correct indentation mode set.
auto config = project->projectConfiguration()->group(Strings::SourceFormatter());
if (!config.isValid() || config.readEntry(Strings::UseDefault(), true)) {
return;
}
QHash<QString, ISourceFormatter*> formatters;
foreach (const KDevelop::IDocument* doc, ICore::self()->documentController()->openDocuments()) {
if (project->inProject(IndexedString(doc->url()))) {
const QString mimename = QMimeDatabase().mimeTypeForUrl(doc->url()).name();
auto it = formatters.find(mimename);
if (it == formatters.end()) {
const auto entry = config.readEntry(mimename, QString());
it = formatters.insert(mimename, entry.isEmpty() ? nullptr : d->formatterForConfigEntry(entry, mimename));
}
if (it.value()) {
adaptEditorIndentationMode(doc->textDocument(), it.value(), doc->url());
}
}
}
}
void SourceFormatterController::pluginLoaded(IPlugin* plugin) void SourceFormatterController::pluginLoaded(IPlugin* plugin)
{ {
auto* sourceFormatter = plugin->extension<ISourceFormatter>(); auto* sourceFormatter = plugin->extension<ISourceFormatter>();
...@@ -328,20 +374,7 @@ ISourceFormatter* SourceFormatterController::formatterForUrl(const QUrl& url, co ...@@ -328,20 +374,7 @@ ISourceFormatter* SourceFormatterController::formatterForUrl(const QUrl& url, co
return findFirstFormatterForMimeType( mime ); return findFirstFormatterForMimeType( mime );
} }
QStringList formatterinfo = formatter.split( QStringLiteral("||"), QString::SkipEmptyParts ); return d->formatterForConfigEntry(formatter, mime.name());
if( formatterinfo.size() != 2 ) {
qCDebug(SHELL) << "Broken formatting entry for mime:" << mime.name() << "current value:" << formatter;
return nullptr;
}
foreach (ISourceFormatter* iformatter, d->sourceFormatters) {
if (iformatter->name() == formatterinfo.first()) {
return iformatter;
}
}
return nullptr;
} }
bool SourceFormatterController::isMimeTypeSupported(const QMimeType& mime) bool SourceFormatterController::isMimeTypeSupported(const QMimeType& mime)
......
...@@ -47,6 +47,7 @@ class Context; ...@@ -47,6 +47,7 @@ class Context;
class ContextMenuExtension; class ContextMenuExtension;
class IDocument; class IDocument;
class IProject;
class TextDocument; class TextDocument;
class ISourceFormatter; class ISourceFormatter;
class IPlugin; class IPlugin;
...@@ -147,6 +148,7 @@ private Q_SLOTS: ...@@ -147,6 +148,7 @@ private Q_SLOTS:
void beautifyLine(); void beautifyLine();
void formatFiles(); void formatFiles();
void documentLoaded(const QPointer<KDevelop::TextDocument>& doc); void documentLoaded(const QPointer<KDevelop::TextDocument>& doc);
void projectOpened(const KDevelop::IProject* project);
void pluginLoaded(KDevelop::IPlugin* plugin); void pluginLoaded(KDevelop::IPlugin* plugin);
void unloadingPlugin(KDevelop::IPlugin* plugin); void unloadingPlugin(KDevelop::IPlugin* plugin);
......
Markdown is supported
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