From 0de61a51aeec0a739dd816bd4b07c7bbcf4d0d4e Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Mon, 2 May 2022 10:37:53 +0100 Subject: [PATCH] Analyze the Quick Commands code with shellcheck --- .../QuickCommands/quickcommandswidget.cpp | 41 +++++++++++++++++++ .../QuickCommands/quickcommandswidget.h | 2 + 2 files changed, 43 insertions(+) diff --git a/src/plugins/QuickCommands/quickcommandswidget.cpp b/src/plugins/QuickCommands/quickcommandswidget.cpp index a378c353..d09112f8 100644 --- a/src/plugins/QuickCommands/quickcommandswidget.cpp +++ b/src/plugins/QuickCommands/quickcommandswidget.cpp @@ -10,14 +10,19 @@ #include #include "ui_qcwidget.h" +#include #include #include +#include +#include +#include struct QuickCommandsWidget::Private { QuickCommandsModel *model = nullptr; FilterModel *filterModel = nullptr; Konsole::SessionController *controller = nullptr; bool hasShellCheck = false; + QTimer shellCheckTimer; }; QuickCommandsWidget::QuickCommandsWidget(QWidget *parent) @@ -31,6 +36,7 @@ QuickCommandsWidget::QuickCommandsWidget(QWidget *parent) if (!priv->hasShellCheck) { ui->warning->setPlainText(QStringLiteral("Missing executable shellcheck")); } + priv->shellCheckTimer.setSingleShot(true); priv->filterModel = new FilterModel(this); connect(ui->btnAdd, &QPushButton::clicked, this, &QuickCommandsWidget::addMode); @@ -52,6 +58,12 @@ QuickCommandsWidget::QuickCommandsWidget(QWidget *parent) connect(ui->commandsTreeView, &QTreeView::clicked, this, &QuickCommandsWidget::indexSelected); connect(ui->commandsTreeView, &QTreeView::customContextMenuRequested, this, &QuickCommandsWidget::createMenu); + + connect(&priv->shellCheckTimer, &QTimer::timeout, this, &QuickCommandsWidget::runShellCheck); + connect(ui->command, &QPlainTextEdit::textChanged, this, [this] { + priv->shellCheckTimer.start(250); + }); + viewMode(); } @@ -245,3 +257,32 @@ void QuickCommandsWidget::createMenu(const QPoint &pos) connect(actionDelete, &QAction::triggered, this, &QuickCommandsWidget::triggerDelete); menu->popup(ui->commandsTreeView->viewport()->mapToGlobal(pos)); } + +void QuickCommandsWidget::runShellCheck() +{ + if (!priv->hasShellCheck) { + return; + } + + QTemporaryFile file; + file.open(); + + QTextStream ts(&file); + ts << "#!/bin/bash\n"; + ts << ui->command->toPlainText(); + file.close(); + + QString fName = file.fileName(); + QProcess process; + process.start(QStringLiteral("shellcheck"), {fName}); + process.waitForFinished(); + + const QString errorString = QString::fromLocal8Bit(process.readAllStandardOutput()); + ui->warning->setPlainText(errorString); + + if (errorString.isEmpty()) { + ui->tabWidget->setTabText(1, QStringLiteral("Warnings")); + } else { + ui->tabWidget->setTabText(1, QStringLiteral("Warnings (*)")); + } +} diff --git a/src/plugins/QuickCommands/quickcommandswidget.h b/src/plugins/QuickCommands/quickcommandswidget.h index bc97be5d..e0f3a4a9 100644 --- a/src/plugins/QuickCommands/quickcommandswidget.h +++ b/src/plugins/QuickCommands/quickcommandswidget.h @@ -49,6 +49,8 @@ public: Q_SLOT void createMenu(const QPoint &pos); + Q_SLOT void runShellCheck(); + void setModel(QuickCommandsModel *model); void setCurrentController(Konsole::SessionController *controller);