Commit 5201edc2 authored by Igor Kushnir's avatar Igor Kushnir
Browse files

Make disabling source formatting more reliable

The only effect of disabling source formatting is that
SourceFormatterController::formatterForUrl() returns nullptr. But some
of SourceFormatterController's member functions are unaffected by the
disablement: hasFormatters() may still return true; formatters() may
return pointers to mostly inactive ISourceFormatter objects;
SourceFormatterController::projectOpened() bypasses formatterForUrl()
and may "adapt the mode of the editor regarding indentation-style".

So current SourceFormatterController's behavior in the disabled state is
inconsistent and possibly incorrect. The inconsistency hinders major
refactoring of formatterForUrl().

Only one unit test disables source formatting and never reenables it.
Therefore it must be safe to "unload" all registered formatters in
disableSourceFormatting() like unloadingPlugin() does; afterwards return
early from pluginLoaded() and unloadingPlugin() if source formatting is
disabled.
parent 630be51e
......@@ -49,8 +49,11 @@ public:
virtual KDevelop::SourceFormatterStyle styleForUrl(const QUrl& url, const QMimeType& mime) = 0;
///Set whether or not source formatting is disabled with \arg disable
virtual void disableSourceFormatting(bool disable) = 0;
/**
* Disable source formatting
* Once disabled, source formatting cannot be reenabled. Call this from within tests.
*/
virtual void disableSourceFormatting() = 0;
///\return Whether or not source formatting is enabled
virtual bool sourceFormattingEnabled() = 0;
......
......@@ -234,7 +234,7 @@ void SourceFormatterController::pluginLoaded(IPlugin* plugin)
auto* sourceFormatter = plugin->extension<ISourceFormatter>();
if (!sourceFormatter) {
if (!sourceFormatter || !d->enabled) {
return;
}
......@@ -255,7 +255,7 @@ void SourceFormatterController::unloadingPlugin(IPlugin* plugin)
auto* sourceFormatter = plugin->extension<ISourceFormatter>();
if (!sourceFormatter) {
if (!sourceFormatter || !d->enabled) {
return;
}
......@@ -798,11 +798,28 @@ SourceFormatterStyle SourceFormatterController::styleForUrl(const QUrl& url, con
return s;
}
void SourceFormatterController::disableSourceFormatting(bool disable)
void SourceFormatterController::disableSourceFormatting()
{
Q_D(SourceFormatterController);
d->enabled = !disable;
d->enabled = false;
if (d->sourceFormatters.empty()) {
return;
}
decltype(d->sourceFormatters) loadedFormatters{};
d->sourceFormatters.swap(loadedFormatters);
resetUi();
for (auto* formatter : std::as_const(loadedFormatters)) {
emit formatterUnloading(formatter);
}
Q_ASSERT(!loadedFormatters.empty());
Q_ASSERT(d->sourceFormatters.empty());
emit hasFormattersChanged(false);
}
bool SourceFormatterController::sourceFormattingEnabled()
......
......@@ -106,7 +106,7 @@ public:
void settingsChanged();
void disableSourceFormatting(bool disable) override;
void disableSourceFormatting() override;
bool sourceFormattingEnabled() override;
QVector<KDevelop::ISourceFormatter*> formatters() const;
......
......@@ -69,7 +69,7 @@ void TestAssistants::initTestCase()
TestCore::initialize();
DUChain::self()->disablePersistentStorage();
Core::self()->languageController()->backgroundParser()->setDelay(0);
Core::self()->sourceFormatterController()->disableSourceFormatting(true);
Core::self()->sourceFormatterController()->disableSourceFormatting();
CodeRepresentation::setDiskChangesForbidden(true);
globalTestLock = new ForegroundLock;
......
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