diff --git a/shell/sourceformattercontroller.h b/shell/sourceformattercontroller.h --- a/shell/sourceformattercontroller.h +++ b/shell/sourceformattercontroller.h @@ -133,7 +133,7 @@ bool sourceFormattingEnabled() override; private Q_SLOTS: - void activeDocumentChanged(KDevelop::IDocument *doc); + void updateFormatTextAction(); void beautifySource(); void beautifyLine(); void formatFiles(); diff --git a/shell/sourceformattercontroller.cpp b/shell/sourceformattercontroller.cpp --- a/shell/sourceformattercontroller.cpp +++ b/shell/sourceformattercontroller.cpp @@ -124,16 +124,17 @@ m_formatFilesAction->setWhatsThis(i18n("Formatting functionality using astyle library.")); connect(m_formatFilesAction, &QAction::triggered, this, static_cast(&SourceFormatterController::formatFiles)); - m_formatTextAction->setEnabled(false); - m_formatFilesAction->setEnabled(true); - + // connect to both documentActivated & documentClosed, + // otherwise we miss when the last document was closed connect(Core::self()->documentController(), &IDocumentController::documentActivated, - this, &SourceFormatterController::activeDocumentChanged); + this, &SourceFormatterController::updateFormatTextAction); + connect(Core::self()->documentController(), &IDocumentController::documentClosed, + this, &SourceFormatterController::updateFormatTextAction); // Use a queued connection, because otherwise the view is not yet fully set up connect(Core::self()->documentController(), &IDocumentController::documentLoaded, this, &SourceFormatterController::documentLoaded, Qt::QueuedConnection); - activeDocumentChanged(Core::self()->documentController()->activeDocument()); + updateFormatTextAction(); } void SourceFormatterController::documentLoaded( IDocument* doc ) @@ -339,23 +340,26 @@ { } - -void SourceFormatterController::activeDocumentChanged(IDocument* doc) +void SourceFormatterController::updateFormatTextAction() { bool enabled = false; + IDocument* doc = KDevelop::ICore::self()->documentController()->activeDocument(); if (doc) { QMimeType mime = QMimeDatabase().mimeTypeForUrl(doc->url()); if (isMimeTypeSupported(mime)) enabled = true; } + m_formatLine->setEnabled(enabled); m_formatTextAction->setEnabled(enabled); } void SourceFormatterController::beautifySource() { IDocument* idoc = KDevelop::ICore::self()->documentController()->activeDocument(); + if (!idoc) + return; KTextEditor::View* view = idoc->activeTextView(); if (!view) return;