File indexing completed on 2024-04-28 05:49:11

0001 /* This file is part of the KDE project
0002    SPDX-FileCopyrightText: 2014 Martin Sandsmark <martin.sandsmark@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "replicodeview.h"
0008 
0009 #include "hostprocess.h"
0010 #include "replicodeconfig.h"
0011 #include "replicodesettings.h"
0012 
0013 #include <QPushButton>
0014 #include <QStandardPaths>
0015 
0016 #include <KLocalizedString>
0017 #include <KXMLGUIFactory>
0018 
0019 #include <QAction>
0020 #include <QDebug>
0021 #include <QFileInfo>
0022 #include <QFormLayout>
0023 #include <QListWidget>
0024 #include <QMessageBox>
0025 
0026 #include <KActionCollection>
0027 #include <KConfigGroup>
0028 #include <KSharedConfig>
0029 #include <KTextEditor/Document>
0030 #include <KTextEditor/MainWindow>
0031 #include <KTextEditor/View>
0032 
0033 ReplicodeView::ReplicodeView(KTextEditor::Plugin *plugin, KTextEditor::MainWindow *mainWindow)
0034     : QObject(mainWindow)
0035     , m_mainWindow(mainWindow)
0036     , m_executor(nullptr)
0037 {
0038     m_runAction = new QAction(QIcon(QStringLiteral("code-block")), i18n("Run replicode"), this);
0039     connect(m_runAction, &QAction::triggered, this, &ReplicodeView::runReplicode);
0040     actionCollection()->addAction(QStringLiteral("katereplicode_run"), m_runAction);
0041 
0042     m_stopAction = new QAction(QIcon(QStringLiteral("process-stop")), i18n("Stop replicode"), this);
0043     connect(m_stopAction, &QAction::triggered, this, &ReplicodeView::stopReplicode);
0044     actionCollection()->addAction(QStringLiteral("katereplicode_stop"), m_stopAction);
0045     m_stopAction->setEnabled(false);
0046 
0047     m_toolview.reset(m_mainWindow->createToolView(plugin,
0048                                                   QStringLiteral("kate_private_plugin_katereplicodeplugin_run"),
0049                                                   KTextEditor::MainWindow::Bottom,
0050                                                   QIcon::fromTheme(QStringLiteral("code-block")),
0051                                                   i18n("Replicode")));
0052     m_replicodeOutput = new QListWidget(m_toolview.get());
0053     m_replicodeOutput->setSelectionMode(QAbstractItemView::ContiguousSelection);
0054     connect(m_replicodeOutput, &QListWidget::itemActivated, this, &ReplicodeView::outputClicked);
0055     m_mainWindow->hideToolView(m_toolview.get());
0056 
0057     m_configSidebar.reset(m_mainWindow->createToolView(plugin,
0058                                                        QStringLiteral("kate_private_plugin_katereplicodeplugin_config"),
0059                                                        KTextEditor::MainWindow::Right,
0060                                                        QIcon::fromTheme(QStringLiteral("code-block")),
0061                                                        i18n("Replicode Config")));
0062     m_configView = new ReplicodeConfig(m_configSidebar.get());
0063 
0064     m_runButton = new QPushButton(i18nc("shortcut for action", "Run (%1)", m_runAction->shortcut().toString()));
0065     m_stopButton = new QPushButton(i18nc("shortcut for action", "Stop (%1)", m_stopAction->shortcut().toString()));
0066     m_stopButton->setEnabled(false);
0067 
0068     QFormLayout *l = qobject_cast<QFormLayout *>(m_configView->widget(0)->layout());
0069     Q_ASSERT(l);
0070     l->addRow(m_runButton, m_stopButton);
0071     connect(m_runButton, &QPushButton::clicked, m_runAction, &QAction::trigger);
0072     connect(m_stopButton, &QPushButton::clicked, m_stopAction, &QAction::trigger);
0073 
0074     m_mainWindow->guiFactory()->addClient(this);
0075     connect(m_mainWindow, &KTextEditor::MainWindow::viewChanged, this, &ReplicodeView::viewChanged);
0076 }
0077 
0078 ReplicodeView::~ReplicodeView()
0079 {
0080     delete m_executor;
0081     m_mainWindow->guiFactory()->removeClient(this);
0082 }
0083 
0084 void ReplicodeView::viewChanged()
0085 {
0086     if (m_mainWindow->activeView() && m_mainWindow->activeView()->document()
0087         && m_mainWindow->activeView()->document()->url().fileName().endsWith(QLatin1String(".replicode"))) {
0088         m_mainWindow->showToolView(m_configSidebar.get());
0089     } else {
0090         m_mainWindow->hideToolView(m_configSidebar.get());
0091         m_mainWindow->hideToolView(m_toolview.get());
0092     }
0093 }
0094 
0095 void ReplicodeView::runReplicode()
0096 {
0097     m_mainWindow->showToolView(m_toolview.get());
0098     KTextEditor::View *editor = m_mainWindow->activeView();
0099     if (!editor || !editor->document()) {
0100         QMessageBox::warning(m_mainWindow->window(), i18nc("@title:window", "Active Document Not Found"), i18n("Could not find an active document to run."));
0101         return;
0102     }
0103 
0104     if (editor->document()->isEmpty()) {
0105         QMessageBox::warning(m_mainWindow->window(), i18nc("@title:window", "Empty Document"), i18n("Cannot execute an empty document."));
0106         return;
0107     }
0108 
0109     QFileInfo sourceFile = QFileInfo(editor->document()->url().toLocalFile());
0110 
0111     if (!sourceFile.isReadable()) {
0112         QMessageBox::warning(m_mainWindow->window(), i18nc("@title:window", "File Not Found"), i18n("Unable to open source file for reading."));
0113         return;
0114     }
0115 
0116     KConfigGroup config(KSharedConfig::openConfig(), QStringLiteral("Replicode"));
0117 
0118     QString executorPath = config.readEntry<QString>("replicodePath", QString());
0119 
0120     // ensure we only call replicode from PATH if not given as absolute path already
0121     if (!executorPath.isEmpty() && !QFileInfo(executorPath).isAbsolute()) {
0122         executorPath = safeExecutableName(executorPath);
0123     }
0124 
0125     if (executorPath.isEmpty()) {
0126         QMessageBox::warning(m_mainWindow->window(),
0127                              i18nc("@title:window", "Replicode Executable Not Found"),
0128                              i18n("Unable to find replicode executor.\n"
0129                                   "Please go to settings and set the path to the Replicode executable."));
0130         return;
0131     }
0132 
0133     if (m_configView->settingsObject()->userOperatorPath.isEmpty()) {
0134         QMessageBox::warning(m_mainWindow->window(),
0135                              i18nc("@title:window", "User Operator Library Not Found"),
0136                              i18n("Unable to find user operator library.\n"
0137                                   "Please go to settings and set the path to the library."));
0138     }
0139 
0140     m_configView->settingsObject()->sourcePath = editor->document()->url().toLocalFile();
0141     m_configView->load();
0142     m_configView->settingsObject()->save();
0143 
0144     m_replicodeOutput->clear();
0145 
0146     if (m_executor) {
0147         delete m_executor;
0148     }
0149     m_executor = new QProcess(this);
0150     m_executor->setWorkingDirectory(sourceFile.canonicalPath());
0151     connect(m_executor, &QProcess::readyReadStandardError, this, &ReplicodeView::gotStderr);
0152     connect(m_executor, &QProcess::readyReadStandardOutput, this, &ReplicodeView::gotStdout);
0153     connect(m_executor, static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), this, &ReplicodeView::replicodeFinished);
0154     connect(m_executor, static_cast<void (QProcess::*)(QProcess::ProcessError)>(&QProcess::errorOccurred), this, &ReplicodeView::runErrored);
0155     qDebug() << executorPath << sourceFile.canonicalPath();
0156     m_completed = false;
0157     startHostProcess(*m_executor, executorPath, {}, QProcess::ReadOnly);
0158 
0159     m_runAction->setEnabled(false);
0160     m_runButton->setEnabled(false);
0161     m_stopAction->setEnabled(true);
0162     m_stopButton->setEnabled(true);
0163 }
0164 
0165 void ReplicodeView::stopReplicode()
0166 {
0167     if (m_executor) {
0168         m_executor->kill();
0169     }
0170 }
0171 
0172 void ReplicodeView::outputClicked(QListWidgetItem *item)
0173 {
0174     QString output = item->text();
0175     QStringList pieces = output.split(QLatin1Char(':'));
0176 
0177     if (pieces.length() < 2) {
0178         return;
0179     }
0180 
0181     QFileInfo file(pieces[0]);
0182     if (!file.isReadable()) {
0183         return;
0184     }
0185 
0186     bool ok = false;
0187     int lineNumber = pieces[1].toInt(&ok);
0188     qDebug() << lineNumber;
0189     if (!ok) {
0190         return;
0191     }
0192 
0193     KTextEditor::View *doc = m_mainWindow->openUrl(QUrl::fromLocalFile(pieces[0]));
0194     doc->setCursorPosition(KTextEditor::Cursor(lineNumber, 0));
0195     qDebug() << doc->cursorPosition().line();
0196 }
0197 
0198 void ReplicodeView::runErrored(QProcess::ProcessError error)
0199 {
0200     Q_UNUSED(error);
0201     QListWidgetItem *item = new QListWidgetItem(i18n("Replicode execution failed: %1", m_executor->errorString()));
0202     item->setForeground(Qt::red);
0203     m_replicodeOutput->addItem(item);
0204     m_replicodeOutput->scrollToBottom();
0205     m_completed = true;
0206 }
0207 
0208 void ReplicodeView::replicodeFinished()
0209 {
0210     if (!m_completed) {
0211         QListWidgetItem *item = new QListWidgetItem(i18n("Replicode execution finished."));
0212         item->setForeground(Qt::blue);
0213         m_replicodeOutput->addItem(item);
0214         m_replicodeOutput->scrollToBottom();
0215     }
0216 
0217     m_runAction->setEnabled(true);
0218     m_runButton->setEnabled(true);
0219     m_stopAction->setEnabled(false);
0220     m_stopButton->setEnabled(false);
0221     //    delete m_executor;
0222     //    delete m_settingsFile;
0223     //    m_executor = 0;
0224     //    m_settingsFile = 0;
0225 }
0226 
0227 void ReplicodeView::gotStderr()
0228 {
0229     const QByteArray output = m_executor->readAllStandardError();
0230     const auto lines = output.split('\n');
0231     for (QByteArray line : lines) {
0232         line = line.simplified();
0233         if (line.isEmpty()) {
0234             continue;
0235         }
0236         QListWidgetItem *item = new QListWidgetItem(QString::fromLocal8Bit(line));
0237         item->setForeground(Qt::red);
0238         m_replicodeOutput->addItem(item);
0239     }
0240     m_replicodeOutput->scrollToBottom();
0241 }
0242 
0243 void ReplicodeView::gotStdout()
0244 {
0245     const QByteArray output = m_executor->readAllStandardOutput();
0246     const auto lines = output.split('\n');
0247     for (QByteArray line : lines) {
0248         line = line.simplified();
0249         if (line.isEmpty()) {
0250             continue;
0251         }
0252         QListWidgetItem *item = new QListWidgetItem(QString::fromLocal8Bit(' ' + line));
0253         if (line[0] == '>') {
0254             item->setForeground(Qt::gray);
0255         }
0256         m_replicodeOutput->addItem(item);
0257     }
0258     m_replicodeOutput->scrollToBottom();
0259 }
0260 
0261 #include "moc_replicodeview.cpp"