Haskell: Use ActionBuilder to create action to run ghci

Change-Id: I7bb4064a06e91b580c1d5f72dc4f0ff2ab8ef0cd
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
This commit is contained in:
hjk
2023-11-20 13:49:00 +01:00
parent 8c3a1c7e5c
commit e53308eb7d
5 changed files with 25 additions and 34 deletions

View File

@@ -3,8 +3,7 @@
#pragma once #pragma once
namespace Haskell { namespace Haskell::Constants {
namespace Constants {
const char C_HASKELLEDITOR_ID[] = "Haskell.HaskellEditor"; const char C_HASKELLEDITOR_ID[] = "Haskell.HaskellEditor";
const char C_HASKELLSNIPPETSGROUP_ID[] = "Haskell"; const char C_HASKELLSNIPPETSGROUP_ID[] = "Haskell";
@@ -15,5 +14,4 @@ const char C_STACK_BUILD_STEP_ID[] = "Haskell.Stack.Build";
const char OPTIONS_GENERAL[] = "Haskell.A.General"; const char OPTIONS_GENERAL[] = "Haskell.A.General";
const char A_RUN_GHCI[] = "Haskell.RunGHCi"; const char A_RUN_GHCI[] = "Haskell.RunGHCi";
} // namespace Haskell } // Haskell::Constants
} // namespace Constants

View File

@@ -26,7 +26,7 @@ static QWidget *createEditorWidget()
auto ghciButton = new Core::CommandButton(Constants::A_RUN_GHCI, widget); auto ghciButton = new Core::CommandButton(Constants::A_RUN_GHCI, widget);
ghciButton->setText(Tr::tr("GHCi")); ghciButton->setText(Tr::tr("GHCi"));
QObject::connect(ghciButton, &QToolButton::clicked, widget, [widget] { QObject::connect(ghciButton, &QToolButton::clicked, widget, [widget] {
HaskellManager::openGhci(widget->textDocument()->filePath()); openGhci(widget->textDocument()->filePath());
}); });
widget->insertExtraToolBarWidget(TextEditorWidget::Left, ghciButton); widget->insertExtraToolBarWidget(TextEditorWidget::Left, ghciButton);
return widget; return widget;

View File

@@ -3,24 +3,27 @@
#include "haskellmanager.h" #include "haskellmanager.h"
#include "haskellconstants.h"
#include "haskellsettings.h" #include "haskellsettings.h"
#include "haskelltr.h" #include "haskelltr.h"
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/editormanager/editormanager.h>
#include <utils/algorithm.h> #include <utils/algorithm.h>
#include <utils/commandline.h> #include <utils/commandline.h>
#include <utils/hostosinfo.h>
#include <utils/mimeutils.h> #include <utils/mimeutils.h>
#include <utils/process.h> #include <utils/process.h>
#include <utils/processenums.h>
#include <QDir> #include <QDir>
#include <QFileInfo> #include <QFileInfo>
using namespace Core;
using namespace Utils; using namespace Utils;
namespace Haskell::Internal { namespace Haskell::Internal {
FilePath HaskellManager::findProjectDirectory(const FilePath &filePath) FilePath findProjectDirectory(const FilePath &filePath)
{ {
if (filePath.isEmpty()) if (filePath.isEmpty())
return {}; return {};
@@ -36,7 +39,7 @@ FilePath HaskellManager::findProjectDirectory(const FilePath &filePath)
return {}; return {};
} }
void HaskellManager::openGhci(const FilePath &haskellFile) void openGhci(const FilePath &haskellFile)
{ {
const QList<MimeType> mimeTypes = mimeTypesForFileName(haskellFile.toString()); const QList<MimeType> mimeTypes = mimeTypesForFileName(haskellFile.toString());
const bool isHaskell = Utils::anyOf(mimeTypes, [](const MimeType &mt) { const bool isHaskell = Utils::anyOf(mimeTypes, [](const MimeType &mt) {
@@ -51,4 +54,14 @@ void HaskellManager::openGhci(const FilePath &haskellFile)
p.start(); p.start();
} }
void setupHaskellActions(QObject *guard)
{
ActionBuilder runGhci(guard, Haskell::Constants::A_RUN_GHCI);
runGhci.setText(Tr::tr("Run GHCi"));
runGhci.setOnTriggered(guard, [] {
if (IDocument *doc = EditorManager::currentDocument())
openGhci(doc->filePath());
});
}
} // Haskell::Internal } // Haskell::Internal

View File

@@ -5,17 +5,13 @@
#include <QObject> #include <QObject>
#include <utils/fileutils.h> namespace Utils { class FilePath; }
namespace Haskell::Internal { namespace Haskell::Internal {
class HaskellManager Utils::FilePath findProjectDirectory(const Utils::FilePath &filePath);
{ void openGhci(const Utils::FilePath &haskellFile);
public:
static HaskellManager *instance();
static Utils::FilePath findProjectDirectory(const Utils::FilePath &filePath); void setupHaskellActions(QObject *guard);
static void openGhci(const Utils::FilePath &haskellFile);
};
} // Haskell::Internal } // Haskell::Internal

View File

@@ -10,30 +10,14 @@
#include "haskelltr.h" #include "haskelltr.h"
#include "stackbuildstep.h" #include "stackbuildstep.h"
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/icore.h>
#include <extensionsystem/iplugin.h> #include <extensionsystem/iplugin.h>
#include <projectexplorer/jsonwizard/jsonwizardfactory.h> #include <projectexplorer/jsonwizard/jsonwizardfactory.h>
#include <texteditor/snippets/snippetprovider.h> #include <texteditor/snippets/snippetprovider.h>
#include <QAction>
namespace Haskell::Internal { namespace Haskell::Internal {
static void registerGhciAction(QObject *guard)
{
QAction *action = new QAction(Tr::tr("Run GHCi"), guard);
Core::ActionManager::registerAction(action, Constants::A_RUN_GHCI);
QObject::connect(action, &QAction::triggered, guard, [] {
if (Core::IDocument *doc = Core::EditorManager::currentDocument())
HaskellManager::openGhci(doc->filePath());
});
}
class HaskellPlugin final : public ExtensionSystem::IPlugin class HaskellPlugin final : public ExtensionSystem::IPlugin
{ {
Q_OBJECT Q_OBJECT
@@ -54,7 +38,7 @@ public:
TextEditor::SnippetProvider::registerGroup(Constants::C_HASKELLSNIPPETSGROUP_ID, TextEditor::SnippetProvider::registerGroup(Constants::C_HASKELLSNIPPETSGROUP_ID,
Tr::tr("Haskell", "SnippetProvider")); Tr::tr("Haskell", "SnippetProvider"));
registerGhciAction(this); setupHaskellActions(this);
ProjectExplorer::JsonWizardFactory::addWizardPath(":/haskell/share/wizards/"); ProjectExplorer::JsonWizardFactory::addWizardPath(":/haskell/share/wizards/");
} }