diff options
author | Eike Ziller <eike.ziller@qt.io> | 2020-03-02 16:30:18 +0100 |
---|---|---|
committer | Eike Ziller <eike.ziller@qt.io> | 2020-03-04 07:28:35 +0000 |
commit | edf6d1f89d2c78832253a60d48d7f0cf05bec89c (patch) | |
tree | be36b44396e9a60f251d0d65d8d39bca4ee64a3f | |
parent | 8326e16c4ef1ae6bfa60b6495b6a1f5a8ec290f0 (diff) |
Add button for running GHCi to editor tool bar
Loading the file automatically in GHCi, so it is directly available for
investigation.
Change-Id: I221d02ef2e17de465244df3a8b6674d84ba52c6e
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
-rw-r--r-- | plugins/haskell/haskellconstants.h | 1 | ||||
-rw-r--r-- | plugins/haskell/haskelleditorfactory.cpp | 15 | ||||
-rw-r--r-- | plugins/haskell/haskellmanager.cpp | 23 | ||||
-rw-r--r-- | plugins/haskell/haskellmanager.h | 1 | ||||
-rw-r--r-- | plugins/haskell/haskellplugin.cpp | 16 |
5 files changed, 56 insertions, 0 deletions
diff --git a/plugins/haskell/haskellconstants.h b/plugins/haskell/haskellconstants.h index 8428ec1..8b63761 100644 --- a/plugins/haskell/haskellconstants.h +++ b/plugins/haskell/haskellconstants.h @@ -35,6 +35,7 @@ const char C_HASKELL_PROJECT_ID[] = "Haskell.Project"; const char C_HASKELL_RUNCONFIG_ID[] = "Haskell.RunConfiguration"; const char C_STACK_BUILD_STEP_ID[] = "Haskell.Stack.Build"; const char OPTIONS_GENERAL[] = "Haskell.A.General"; +const char A_RUN_GHCI[] = "Haskell.RunGHCi"; } // namespace Haskell } // namespace Constants diff --git a/plugins/haskell/haskelleditorfactory.cpp b/plugins/haskell/haskelleditorfactory.cpp index 6570d22..be450d9 100644 --- a/plugins/haskell/haskelleditorfactory.cpp +++ b/plugins/haskell/haskelleditorfactory.cpp @@ -27,7 +27,9 @@ #include "haskellconstants.h" #include "haskellhighlighter.h" +#include "haskellmanager.h" +#include <coreplugin/actionmanager/commandbutton.h> #include <texteditor/textdocument.h> #include <texteditor/texteditoractionhandler.h> @@ -36,6 +38,18 @@ namespace Haskell { namespace Internal { +static QWidget *createEditorWidget() +{ + auto widget = new TextEditor::TextEditorWidget; + auto ghciButton = new Core::CommandButton(Constants::A_RUN_GHCI, widget); + ghciButton->setText(HaskellManager::tr("GHCi")); + QObject::connect(ghciButton, &QToolButton::clicked, HaskellManager::instance(), [widget] { + HaskellManager::openGhci(widget->textDocument()->filePath()); + }); + widget->insertExtraToolBarWidget(TextEditor::TextEditorWidget::Left, ghciButton); + return widget; +} + HaskellEditorFactory::HaskellEditorFactory() { setId(Constants::C_HASKELLEDITOR_ID); @@ -44,6 +58,7 @@ HaskellEditorFactory::HaskellEditorFactory() setEditorActionHandlers(TextEditor::TextEditorActionHandler::UnCommentSelection | TextEditor::TextEditorActionHandler::FollowSymbolUnderCursor); setDocumentCreator([] { return new TextEditor::TextDocument(Constants::C_HASKELLEDITOR_ID); }); + setEditorWidgetCreator(createEditorWidget); setCommentDefinition(Utils::CommentDefinition("--", "{-", "-}")); setParenthesesMatchingEnabled(true); setMarksVisible(true); diff --git a/plugins/haskell/haskellmanager.cpp b/plugins/haskell/haskellmanager.cpp index 9e18b51..5c9ab1d 100644 --- a/plugins/haskell/haskellmanager.cpp +++ b/plugins/haskell/haskellmanager.cpp @@ -25,7 +25,11 @@ #include "haskellmanager.h" +#include <coreplugin/messagemanager.h> +#include <utils/algorithm.h> +#include <utils/consoleprocess.h> #include <utils/hostosinfo.h> +#include <utils/mimetypes/mimedatabase.h> #include <QCoreApplication> #include <QDir> @@ -93,6 +97,25 @@ void HaskellManager::setStackExecutable(const FilePath &filePath) emit m_instance->stackExecutableChanged(m_d->stackExecutable); } +void HaskellManager::openGhci(const FilePath &haskellFile) +{ + const QList<MimeType> mimeTypes = mimeTypesForFileName(haskellFile.toString()); + const bool isHaskell = Utils::anyOf(mimeTypes, [](const MimeType &mt) { + return mt.inherits("text/x-haskell") || mt.inherits("text/x-literate-haskell"); + }); + const auto args = QStringList{"ghci"} + + (isHaskell ? QStringList{haskellFile.fileName()} : QStringList()); + auto p = new ConsoleProcess; + p->setCommand({stackExecutable(), args}); + p->setWorkingDirectory(haskellFile.toFileInfo().path()); + connect(p, &ConsoleProcess::processError, p, [p](const QString &errorString) { + Core::MessageManager::write(tr("Failed to run GHCi: \"%1\".").arg(errorString)); + p->deleteLater(); + }); + connect(p, &ConsoleProcess::stubStopped, p, &QObject::deleteLater); + p->start(); +} + void HaskellManager::readSettings(QSettings *settings) { m_d->stackExecutable = FilePath::fromString( diff --git a/plugins/haskell/haskellmanager.h b/plugins/haskell/haskellmanager.h index 63013c9..361b8f8 100644 --- a/plugins/haskell/haskellmanager.h +++ b/plugins/haskell/haskellmanager.h @@ -46,6 +46,7 @@ public: static Utils::FilePath findProjectDirectory(const Utils::FilePath &filePath); static Utils::FilePath stackExecutable(); static void setStackExecutable(const Utils::FilePath &filePath); + static void openGhci(const Utils::FilePath &haskellFile); static void readSettings(QSettings *settings); static void writeSettings(QSettings *settings); diff --git a/plugins/haskell/haskellplugin.cpp b/plugins/haskell/haskellplugin.cpp index 0aa187f..843a524 100644 --- a/plugins/haskell/haskellplugin.cpp +++ b/plugins/haskell/haskellplugin.cpp @@ -34,10 +34,14 @@ #include "optionspage.h" #include "stackbuildstep.h" +#include <coreplugin/actionmanager/actionmanager.h> +#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/icore.h> #include <projectexplorer/projectmanager.h> #include <texteditor/snippets/snippetprovider.h> +#include <QAction> + namespace Haskell { namespace Internal { @@ -56,6 +60,16 @@ HaskellPlugin::~HaskellPlugin() delete d; } +static void registerGhciAction() +{ + QAction *action = new QAction(HaskellManager::tr("Run GHCi"), HaskellManager::instance()); + Core::ActionManager::registerAction(action, Constants::A_RUN_GHCI); + QObject::connect(action, &QAction::triggered, HaskellManager::instance(), [] { + if (Core::IDocument *doc = Core::EditorManager::currentDocument()) + HaskellManager::openGhci(doc->filePath()); + }); +} + bool HaskellPlugin::initialize(const QStringList &arguments, QString *errorString) { Q_UNUSED(arguments) @@ -72,6 +86,8 @@ bool HaskellPlugin::initialize(const QStringList &arguments, QString *errorStrin HaskellManager::writeSettings(Core::ICore::settings()); }); + registerGhciAction(); + HaskellManager::readSettings(Core::ICore::settings()); return true; } |