forked from qt-creator/qt-creator
CMakeProjectManager: Rework CMakeFormatter
Use PagedSettings, make setup more local, remove Q_OBJECT Change-Id: I9e91f9e63ed8ad15749323bd039d118562dba1a6 Reviewed-by: Cristian Adam <cristian.adam@qt.io>
This commit is contained in:
@@ -8,7 +8,6 @@
|
||||
#include "cmakebuildsystem.h"
|
||||
#include "cmakeeditor.h"
|
||||
#include "cmakeformatter.h"
|
||||
#include "cmakeformattersettings.h"
|
||||
#include "cmakeinstallstep.h"
|
||||
#include "cmakekitinformation.h"
|
||||
#include "cmakelocatorfilter.h"
|
||||
@@ -43,27 +42,9 @@ using namespace Utils;
|
||||
|
||||
namespace CMakeProjectManager::Internal {
|
||||
|
||||
bool isAutoFormatApplicable(const Core::IDocument *document, const QStringList &allowedMimeTypes)
|
||||
{
|
||||
if (!document)
|
||||
return false;
|
||||
|
||||
if (allowedMimeTypes.isEmpty())
|
||||
return true;
|
||||
|
||||
const Utils::MimeType documentMimeType = Utils::mimeTypeForName(document->mimeType());
|
||||
return Utils::anyOf(allowedMimeTypes, [&documentMimeType](const QString &mime) {
|
||||
return documentMimeType.inherits(mime);
|
||||
});
|
||||
}
|
||||
|
||||
class CMakeProjectPluginPrivate : public QObject
|
||||
{
|
||||
public:
|
||||
CMakeProjectPluginPrivate();
|
||||
void updateActions(Core::IEditor *editor = nullptr);
|
||||
void autoFormatOnSave(Core::IDocument *document);
|
||||
|
||||
CMakeToolManager cmakeToolManager; // have that before the first CMakeKitAspect
|
||||
|
||||
ParameterAction buildTargetContextAction{
|
||||
@@ -90,53 +71,6 @@ public:
|
||||
CMakeFormatter cmakeFormatter;
|
||||
};
|
||||
|
||||
CMakeProjectPluginPrivate::CMakeProjectPluginPrivate()
|
||||
{
|
||||
const Core::EditorManager *editorManager = Core::EditorManager::instance();
|
||||
QObject::connect(editorManager, &Core::EditorManager::currentEditorChanged,
|
||||
this, &CMakeProjectPluginPrivate::updateActions);
|
||||
QObject::connect(editorManager, &Core::EditorManager::aboutToSave,
|
||||
this, &CMakeProjectPluginPrivate::autoFormatOnSave);
|
||||
}
|
||||
|
||||
void CMakeProjectPluginPrivate::updateActions(Core::IEditor *editor)
|
||||
{
|
||||
cmakeFormatter.updateActions(editor);
|
||||
}
|
||||
|
||||
void CMakeProjectPluginPrivate::autoFormatOnSave(Core::IDocument *document)
|
||||
{
|
||||
if (!CMakeFormatterSettings::instance()->autoFormatOnSave())
|
||||
return;
|
||||
|
||||
if (!isAutoFormatApplicable(document, CMakeFormatterSettings::instance()->autoFormatMime()))
|
||||
return;
|
||||
|
||||
// Check if file is contained in the current project (if wished)
|
||||
if (CMakeFormatterSettings::instance()->autoFormatOnlyCurrentProject()) {
|
||||
const ProjectExplorer::Project *pro = ProjectExplorer::ProjectTree::currentProject();
|
||||
if (!pro || pro->files([document](const ProjectExplorer::Node *n) {
|
||||
return ProjectExplorer::Project::SourceFiles(n)
|
||||
&& n->filePath() == document->filePath();
|
||||
}).isEmpty()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!cmakeFormatter.isApplicable(document))
|
||||
return;
|
||||
const TextEditor::Command command = cmakeFormatter.command();
|
||||
if (!command.isValid())
|
||||
return;
|
||||
const QList<Core::IEditor *> editors = Core::DocumentModel::editorsForDocument(document);
|
||||
if (editors.isEmpty())
|
||||
return;
|
||||
IEditor *currentEditor = EditorManager::currentEditor();
|
||||
IEditor *editor = editors.contains(currentEditor) ? currentEditor : editors.first();
|
||||
if (auto widget = TextEditor::TextEditorWidget::fromEditor(editor))
|
||||
TextEditor::formatEditor(widget, command);
|
||||
}
|
||||
|
||||
CMakeProjectPlugin::~CMakeProjectPlugin()
|
||||
{
|
||||
delete d;
|
||||
@@ -177,14 +111,6 @@ void CMakeProjectPlugin::initialize()
|
||||
bs->buildCMakeTarget(targetNode ? targetNode->displayName() : QString());
|
||||
}
|
||||
});
|
||||
|
||||
Core::ActionContainer *menu = Core::ActionManager::createMenu(Constants::CMAKEFORMATTER_MENU_ID);
|
||||
menu->menu()->setTitle(Tr::tr("CMakeFormatter"));
|
||||
menu->setOnAllDisabledBehavior(Core::ActionContainer::Show);
|
||||
Core::ActionManager::actionContainer(Core::Constants::M_TOOLS)->addMenu(menu);
|
||||
|
||||
d->cmakeFormatter.initialize();
|
||||
d->updateActions();
|
||||
}
|
||||
|
||||
void CMakeProjectPlugin::extensionsInitialized()
|
||||
|
||||
Reference in New Issue
Block a user