2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
2008-12-02 14:09:21 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "cmakeprojectplugin.h"
|
2014-08-20 00:07:43 +02:00
|
|
|
|
2020-04-17 15:30:05 +02:00
|
|
|
#include "cmakebuildconfiguration.h"
|
2016-01-07 12:33:52 +01:00
|
|
|
#include "cmakebuildstep.h"
|
2020-04-17 15:30:05 +02:00
|
|
|
#include "cmakebuildsystem.h"
|
|
|
|
|
#include "cmakeeditor.h"
|
2022-11-21 00:10:25 +01:00
|
|
|
#include "cmakeformatter.h"
|
|
|
|
|
#include "cmakeformattersettings.h"
|
2020-04-17 15:30:05 +02:00
|
|
|
#include "cmakekitinformation.h"
|
|
|
|
|
#include "cmakelocatorfilter.h"
|
2016-11-14 15:18:25 +01:00
|
|
|
#include "cmakeproject.h"
|
|
|
|
|
#include "cmakeprojectconstants.h"
|
2008-12-02 14:09:21 +01:00
|
|
|
#include "cmakeprojectmanager.h"
|
2022-09-29 15:26:31 +02:00
|
|
|
#include "cmakeprojectmanagertr.h"
|
2016-11-14 15:18:25 +01:00
|
|
|
#include "cmakeprojectnodes.h"
|
2014-09-12 13:02:40 +04:00
|
|
|
#include "cmakesettingspage.h"
|
2020-01-10 13:05:44 +01:00
|
|
|
#include "cmakespecificsettings.h"
|
2020-04-17 15:30:05 +02:00
|
|
|
#include "cmaketoolmanager.h"
|
2008-12-02 14:09:21 +01:00
|
|
|
|
2016-11-14 15:18:25 +01:00
|
|
|
#include <coreplugin/actionmanager/actioncontainer.h>
|
|
|
|
|
#include <coreplugin/actionmanager/actionmanager.h>
|
2018-03-10 18:45:06 +01:00
|
|
|
#include <coreplugin/icore.h>
|
2022-09-29 15:26:31 +02:00
|
|
|
|
2020-04-17 15:30:05 +02:00
|
|
|
#include <projectexplorer/projectexplorerconstants.h>
|
2017-03-03 18:16:34 +01:00
|
|
|
#include <projectexplorer/projectmanager.h>
|
2016-11-14 15:18:25 +01:00
|
|
|
#include <projectexplorer/projecttree.h>
|
2022-09-29 15:26:31 +02:00
|
|
|
|
2022-11-21 00:10:25 +01:00
|
|
|
#include <texteditor/formattexteditor.h>
|
2017-04-24 15:52:04 +02:00
|
|
|
#include <texteditor/snippets/snippetprovider.h>
|
|
|
|
|
|
2022-05-31 11:16:44 +02:00
|
|
|
#include <utils/fsengine/fileiconprovider.h>
|
2016-11-14 15:18:25 +01:00
|
|
|
#include <utils/parameteraction.h>
|
2016-11-09 14:25:30 +01:00
|
|
|
|
2022-09-29 10:11:28 +02:00
|
|
|
#include <QTimer>
|
2022-11-21 00:10:25 +01:00
|
|
|
#include <QMenu>
|
2022-09-29 10:11:28 +02:00
|
|
|
|
2016-11-14 15:18:25 +01:00
|
|
|
using namespace Core;
|
|
|
|
|
using namespace ProjectExplorer;
|
2019-10-25 09:55:32 +02:00
|
|
|
using namespace Utils;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2022-09-29 15:26:31 +02:00
|
|
|
namespace CMakeProjectManager::Internal {
|
2018-02-09 15:35:02 +01:00
|
|
|
|
2022-12-12 14:51:33 +01:00
|
|
|
bool isAutoFormatApplicable(const Core::IDocument *document, const QStringList &allowedMimeTypes)
|
2022-11-21 00:10:25 +01:00
|
|
|
{
|
|
|
|
|
if (!document)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (allowedMimeTypes.isEmpty())
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
const Utils::MimeType documentMimeType = Utils::mimeTypeForName(document->mimeType());
|
2022-12-12 14:51:33 +01:00
|
|
|
return Utils::anyOf(allowedMimeTypes, [&documentMimeType](const QString &mime) {
|
|
|
|
|
return documentMimeType.inherits(mime);
|
2022-11-21 00:10:25 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class CMakeProjectPluginPrivate : public QObject
|
2018-02-09 15:35:02 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2022-11-21 00:10:25 +01:00
|
|
|
CMakeProjectPluginPrivate();
|
|
|
|
|
void updateActions(Core::IEditor *editor = nullptr);
|
|
|
|
|
void autoFormatOnSave(Core::IDocument *document);
|
|
|
|
|
|
2019-03-19 15:36:07 +01:00
|
|
|
CMakeToolManager cmakeToolManager; // have that before the first CMakeKitAspect
|
|
|
|
|
|
2019-10-25 09:55:32 +02:00
|
|
|
ParameterAction buildTargetContextAction{
|
2022-09-29 15:26:31 +02:00
|
|
|
Tr::tr("Build"),
|
|
|
|
|
Tr::tr("Build \"%1\""),
|
2019-10-25 09:55:32 +02:00
|
|
|
ParameterAction::AlwaysEnabled/*handled manually*/
|
|
|
|
|
};
|
|
|
|
|
|
2018-02-09 15:35:02 +01:00
|
|
|
CMakeSettingsPage settingsPage;
|
2019-10-25 09:55:32 +02:00
|
|
|
CMakeSpecificSettingsPage specificSettings{CMakeProjectPlugin::projectTypeSpecificSettings()};
|
|
|
|
|
|
2018-02-09 15:35:02 +01:00
|
|
|
CMakeManager manager;
|
|
|
|
|
CMakeBuildStepFactory buildStepFactory;
|
|
|
|
|
CMakeBuildConfigurationFactory buildConfigFactory;
|
|
|
|
|
CMakeEditorFactory editorFactor;
|
2019-07-24 14:49:27 +02:00
|
|
|
BuildCMakeTargetLocatorFilter buildCMakeTargetLocatorFilter;
|
|
|
|
|
OpenCMakeTargetLocatorFilter openCMakeTargetLocationFilter;
|
2019-03-14 09:17:59 +01:00
|
|
|
|
|
|
|
|
CMakeKitAspect cmakeKitAspect;
|
|
|
|
|
CMakeGeneratorKitAspect cmakeGeneratorKitAspect;
|
|
|
|
|
CMakeConfigurationKitAspect cmakeConfigurationKitAspect;
|
2022-11-21 00:10:25 +01:00
|
|
|
|
|
|
|
|
CMakeFormatter cmakeFormatter;
|
2018-02-09 15:35:02 +01:00
|
|
|
};
|
|
|
|
|
|
2022-11-21 00:10:25 +01:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-10 18:45:06 +01:00
|
|
|
CMakeSpecificSettings *CMakeProjectPlugin::projectTypeSpecificSettings()
|
|
|
|
|
{
|
2019-10-25 09:55:32 +02:00
|
|
|
static CMakeSpecificSettings theSettings;
|
|
|
|
|
return &theSettings;
|
2018-03-10 18:45:06 +01:00
|
|
|
}
|
|
|
|
|
|
2018-02-09 15:35:02 +01:00
|
|
|
CMakeProjectPlugin::~CMakeProjectPlugin()
|
|
|
|
|
{
|
|
|
|
|
delete d;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:21:14 +01:00
|
|
|
bool CMakeProjectPlugin::initialize(const QStringList & /*arguments*/, QString *errorMessage)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2015-02-04 09:32:46 +01:00
|
|
|
Q_UNUSED(errorMessage)
|
2018-02-09 15:35:02 +01:00
|
|
|
|
|
|
|
|
d = new CMakeProjectPluginPrivate;
|
2021-03-23 15:58:16 +01:00
|
|
|
projectTypeSpecificSettings()->readSettings(ICore::settings());
|
2018-02-09 15:35:02 +01:00
|
|
|
|
2020-04-07 14:44:22 +02:00
|
|
|
const Context projectContext{CMakeProjectManager::Constants::CMAKE_PROJECT_ID};
|
2016-11-14 15:18:25 +01:00
|
|
|
|
2022-07-07 20:44:24 +02:00
|
|
|
FileIconProvider::registerIconOverlayForSuffix(Constants::Icons::FILE_OVERLAY, "cmake");
|
|
|
|
|
FileIconProvider::registerIconOverlayForFilename(Constants::Icons::FILE_OVERLAY,
|
2019-10-25 09:55:32 +02:00
|
|
|
"CMakeLists.txt");
|
2016-11-09 14:25:30 +01:00
|
|
|
|
2017-04-24 15:52:04 +02:00
|
|
|
TextEditor::SnippetProvider::registerGroup(Constants::CMAKE_SNIPPETS_GROUP_ID,
|
2022-09-29 15:26:31 +02:00
|
|
|
Tr::tr("CMake", "SnippetProvider"));
|
2020-04-07 14:44:22 +02:00
|
|
|
ProjectManager::registerProjectType<CMakeProject>(Constants::CMAKE_PROJECT_MIMETYPE);
|
2017-03-03 18:16:34 +01:00
|
|
|
|
2016-11-14 15:18:25 +01:00
|
|
|
//register actions
|
2019-10-25 09:55:32 +02:00
|
|
|
Command *command = ActionManager::registerAction(&d->buildTargetContextAction,
|
2020-04-07 14:44:22 +02:00
|
|
|
Constants::BUILD_TARGET_CONTEXT_MENU,
|
|
|
|
|
projectContext);
|
2016-11-14 15:18:25 +01:00
|
|
|
command->setAttribute(Command::CA_Hide);
|
|
|
|
|
command->setAttribute(Command::CA_UpdateText);
|
2019-10-25 09:55:32 +02:00
|
|
|
command->setDescription(d->buildTargetContextAction.text());
|
|
|
|
|
|
|
|
|
|
ActionManager::actionContainer(ProjectExplorer::Constants::M_SUBPROJECTCONTEXT)
|
|
|
|
|
->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
|
2016-11-14 15:18:25 +01:00
|
|
|
|
|
|
|
|
// Wire up context menu updates:
|
|
|
|
|
connect(ProjectTree::instance(), &ProjectTree::currentNodeChanged,
|
|
|
|
|
this, &CMakeProjectPlugin::updateContextActions);
|
2015-02-24 21:57:00 +01:00
|
|
|
|
2019-10-25 09:55:32 +02:00
|
|
|
connect(&d->buildTargetContextAction, &ParameterAction::triggered, this, [] {
|
|
|
|
|
if (auto bs = qobject_cast<CMakeBuildSystem *>(ProjectTree::currentBuildSystem())) {
|
|
|
|
|
auto targetNode = dynamic_cast<const CMakeTargetNode *>(ProjectTree::currentNode());
|
|
|
|
|
bs->buildCMakeTarget(targetNode ? targetNode->displayName() : QString());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2022-11-21 00:10:25 +01:00
|
|
|
Core::ActionContainer *menu = Core::ActionManager::createMenu(Constants::CMAKEFORMATTER_MENU_ID);
|
|
|
|
|
menu->menu()->setTitle(QCoreApplication::translate("CMakeFormatter", "CMakeFormatter"));
|
|
|
|
|
menu->setOnAllDisabledBehavior(Core::ActionContainer::Show);
|
|
|
|
|
Core::ActionManager::actionContainer(Core::Constants::M_TOOLS)->addMenu(menu);
|
|
|
|
|
|
|
|
|
|
d->cmakeFormatter.initialize();
|
|
|
|
|
d->updateActions();
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CMakeProjectPlugin::extensionsInitialized()
|
|
|
|
|
{
|
2022-09-29 10:11:28 +02:00
|
|
|
// Delay the restoration to allow the devices to load first.
|
|
|
|
|
QTimer::singleShot(0, this, [] { CMakeToolManager::restoreCMakeTools(); });
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2016-11-14 15:18:25 +01:00
|
|
|
|
2021-06-18 08:46:38 +02:00
|
|
|
void CMakeProjectPlugin::updateContextActions(Node *node)
|
2016-11-14 15:18:25 +01:00
|
|
|
{
|
2021-06-18 08:46:38 +02:00
|
|
|
auto targetNode = dynamic_cast<const CMakeTargetNode *>(node);
|
2016-12-08 22:31:43 +01:00
|
|
|
const QString targetDisplayName = targetNode ? targetNode->displayName() : QString();
|
2016-11-14 15:18:25 +01:00
|
|
|
|
|
|
|
|
// Build Target:
|
2019-10-25 09:55:32 +02:00
|
|
|
d->buildTargetContextAction.setParameter(targetDisplayName);
|
|
|
|
|
d->buildTargetContextAction.setEnabled(targetNode);
|
|
|
|
|
d->buildTargetContextAction.setVisible(targetNode);
|
2016-11-14 15:18:25 +01:00
|
|
|
}
|
2018-02-09 15:35:02 +01:00
|
|
|
|
2022-09-29 15:26:31 +02:00
|
|
|
} // CMakeProjectManager::Internal
|
2018-02-09 15:35:02 +01:00
|
|
|
|