forked from qt-creator/qt-creator
Beautifier for CMake files
Fixes: QTCREATORBUG-25773 Change-Id: I30df110512553b28894427e4d473814400153923 Reviewed-by: <github-actions-qt-creator@cristianadam.eu> Reviewed-by: Cristian Adam <cristian.adam@qt.io>
This commit is contained in:
@@ -7,6 +7,8 @@
|
||||
#include "cmakebuildstep.h"
|
||||
#include "cmakebuildsystem.h"
|
||||
#include "cmakeeditor.h"
|
||||
#include "cmakeformatter.h"
|
||||
#include "cmakeformattersettings.h"
|
||||
#include "cmakekitinformation.h"
|
||||
#include "cmakelocatorfilter.h"
|
||||
#include "cmakeproject.h"
|
||||
@@ -26,12 +28,14 @@
|
||||
#include <projectexplorer/projectmanager.h>
|
||||
#include <projectexplorer/projecttree.h>
|
||||
|
||||
#include <texteditor/formattexteditor.h>
|
||||
#include <texteditor/snippets/snippetprovider.h>
|
||||
|
||||
#include <utils/fsengine/fileiconprovider.h>
|
||||
#include <utils/parameteraction.h>
|
||||
|
||||
#include <QTimer>
|
||||
#include <QMenu>
|
||||
|
||||
using namespace Core;
|
||||
using namespace ProjectExplorer;
|
||||
@@ -39,9 +43,28 @@ using namespace Utils;
|
||||
|
||||
namespace CMakeProjectManager::Internal {
|
||||
|
||||
class CMakeProjectPluginPrivate
|
||||
bool isAutoFormatApplicable(const Core::IDocument *document,
|
||||
const QList<Utils::MimeType> &allowedMimeTypes)
|
||||
{
|
||||
if (!document)
|
||||
return false;
|
||||
|
||||
if (allowedMimeTypes.isEmpty())
|
||||
return true;
|
||||
|
||||
const Utils::MimeType documentMimeType = Utils::mimeTypeForName(document->mimeType());
|
||||
return Utils::anyOf(allowedMimeTypes, [&documentMimeType](const Utils::MimeType &mime) {
|
||||
return documentMimeType.inherits(mime.name());
|
||||
});
|
||||
}
|
||||
|
||||
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{
|
||||
@@ -63,8 +86,57 @@ public:
|
||||
CMakeKitAspect cmakeKitAspect;
|
||||
CMakeGeneratorKitAspect cmakeGeneratorKitAspect;
|
||||
CMakeConfigurationKitAspect cmakeConfigurationKitAspect;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
CMakeSpecificSettings *CMakeProjectPlugin::projectTypeSpecificSettings()
|
||||
{
|
||||
static CMakeSpecificSettings theSettings;
|
||||
@@ -115,6 +187,14 @@ bool CMakeProjectPlugin::initialize(const QStringList & /*arguments*/, QString *
|
||||
}
|
||||
});
|
||||
|
||||
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();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user