Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit 65293473 authored by Héctor Mesa Jiménez's avatar Héctor Mesa Jiménez Committed by Christoph Cullmann

flake8 as an additional code analysis tool for the Kate Editor Project addon

Contributed by Héctor Mesa Jiménez <hector@lcc.uma.es>

CHANGELOG: flake8 as an additional code analysis tool for the Kate Editor Project addon

Differential Revision: https://phabricator.kde.org/D8335
parent d2d67363
......@@ -23,6 +23,10 @@ set(kateprojectplugin_PART_SRCS
kateprojectinfoviewcodeanalysis.cpp
kateprojectinfoviewnotes.cpp
kateprojectconfigpage.cpp
kateprojectcodeanalysistool.cpp
tools/kateprojectcodeanalysistoolcppcheck.cpp
tools/kateprojectcodeanalysistoolflake8.cpp
tools/kateprojectcodeanalysisselector.cpp
)
# resource for ui file and stuff
......
/* This file is part of the Kate project.
*
* Copyright (C) 2017 Héctor Mesa Jiménez <hector@lcc.uma.es>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "kateprojectcodeanalysistool.h"
KateProjectCodeAnalysisTool::KateProjectCodeAnalysisTool(QObject *parent)
: QObject(parent)
, m_project(nullptr)
{
}
void KateProjectCodeAnalysisTool::setProject(KateProject *project)
{
m_project = project;
}
KateProjectCodeAnalysisTool::~KateProjectCodeAnalysisTool()
{
}
/* This file is part of the Kate project.
*
* Copyright (C) 2017 Héctor Mesa Jiménez <hector@lcc.uma.es>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef KATE_PROJECT_CODE_ANALYSIS_TOOL_H
#define KATE_PROJECT_CODE_ANALYSIS_TOOL_H
#include <QString>
#include <QStringList>
#include "kateproject.h"
/**
* Information provider for a code analysis tool
*/
class KateProjectCodeAnalysisTool: public QObject
{
Q_OBJECT
protected:
explicit KateProjectCodeAnalysisTool(QObject *parent = nullptr);
/**
* Current project
*/
KateProject *m_project;
public:
virtual ~KateProjectCodeAnalysisTool();
/**
* bind to this project
* @param project project this tool will analyse
*/
virtual void setProject(KateProject *project);
/**
* @return tool descriptive name
*/
virtual QString name() = 0;
/**
* filter relevant files
* @param files set of files in project
* @return relevant files that can be analysed
*/
virtual QStringList filter(const QStringList &files) = 0;
/**
* @return tool path
*/
virtual QString path() = 0;
/**
* @return arguments required for the tool
*/
virtual QStringList arguments() = 0;
/**
* @return warning message when the tool is not installed
*/
virtual QString notInstalledMessage() = 0;
/**
* parse output line
* @param line
* @return file, line, severity, message
*/
virtual QStringList parseLine(const QString &line) = 0;
/**
* @return messages passed to the tool through stdin
*/
virtual QString stdinMessages() = 0;
};
Q_DECLARE_METATYPE(KateProjectCodeAnalysisTool*)
#endif // KATE_PROJECT_CODE_ANALYSIS_TOOL_H
......@@ -20,6 +20,8 @@
#include "kateprojectinfoviewcodeanalysis.h"
#include "kateprojectpluginview.h"
#include "kateprojectcodeanalysistool.h"
#include "tools/kateprojectcodeanalysisselector.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
......@@ -37,6 +39,8 @@ KateProjectInfoViewCodeAnalysis::KateProjectInfoViewCodeAnalysis(KateProjectPlug
, m_treeView(new QTreeView())
, m_model(new QStandardItemModel(m_treeView))
, m_analyzer(nullptr)
, m_analysisTool(nullptr)
, m_toolSelector(new QComboBox())
{
/**
* default style
......@@ -58,6 +62,11 @@ KateProjectInfoViewCodeAnalysis::KateProjectInfoViewCodeAnalysis(KateProjectPlug
m_treeView->sortByColumn(1, Qt::AscendingOrder);
m_treeView->sortByColumn(2, Qt::AscendingOrder);
/**
* attach model to code analysis selector
*/
m_toolSelector->setModel(KateProjectCodeAnalysisSelector::model(this));
/**
* layout widget
*/
......@@ -68,6 +77,7 @@ KateProjectInfoViewCodeAnalysis::KateProjectInfoViewCodeAnalysis(KateProjectPlug
layout->addLayout(hlayout);
hlayout->setSpacing(0);
hlayout->addStretch();
hlayout->addWidget(m_toolSelector);
hlayout->addWidget(m_startStopAnalysis);
setLayout(layout);
......@@ -85,9 +95,10 @@ KateProjectInfoViewCodeAnalysis::~KateProjectInfoViewCodeAnalysis()
void KateProjectInfoViewCodeAnalysis::slotStartStopClicked()
{
/**
* get files for cppcheck
* get files for the external tool
*/
QStringList files = m_project->files().filter(QRegExp(QStringLiteral("\\.(cpp|cxx|cc|c\\+\\+|c|tpp|txx)$")));
m_analysisTool = m_toolSelector->currentData(Qt::UserRole + 1).value<KateProjectCodeAnalysisTool*>();
m_analysisTool->setProject(m_project);
/**
* clear existing entries
......@@ -104,9 +115,7 @@ void KateProjectInfoViewCodeAnalysis::slotStartStopClicked()
connect(m_analyzer, static_cast<void(QProcess::*)(int,QProcess::ExitStatus)>(&QProcess::finished),
this, &KateProjectInfoViewCodeAnalysis::finished);
QStringList args;
args << QStringLiteral("-q") << QStringLiteral("--inline-suppr") << QStringLiteral("--enable=all") << QStringLiteral("--template={file}////{line}////{severity}////{message}") << QStringLiteral("--file-list=-");
m_analyzer->start(QStringLiteral("cppcheck"), args);
m_analyzer->start(m_analysisTool->path(), m_analysisTool->arguments());
if (m_messageWidget) {
delete m_messageWidget;
......@@ -118,7 +127,7 @@ void KateProjectInfoViewCodeAnalysis::slotStartStopClicked()
m_messageWidget->setCloseButtonVisible(true);
m_messageWidget->setMessageType(KMessageWidget::Warning);
m_messageWidget->setWordWrap(false);
m_messageWidget->setText(i18n("Please install 'cppcheck'."));
m_messageWidget->setText(m_analysisTool->notInstalledMessage());
static_cast<QVBoxLayout *>(layout())->insertWidget(0, m_messageWidget);
m_messageWidget->animatedShow();
return;
......@@ -126,7 +135,10 @@ void KateProjectInfoViewCodeAnalysis::slotStartStopClicked()
/**
* write files list and close write channel
*/
m_analyzer->write(files.join(QStringLiteral("\n")).toLocal8Bit());
const QString stdinMessage = m_analysisTool->stdinMessages();
if (!stdinMessage.isEmpty()) {
m_analyzer->write(stdinMessage.toLocal8Bit());
}
m_analyzer->closeWriteChannel();
}
......@@ -140,7 +152,7 @@ void KateProjectInfoViewCodeAnalysis::slotReadyRead()
* get one line, split it, skip it, if too few elements
*/
QString line = QString::fromLocal8Bit(m_analyzer->readLine());
QStringList elements = line.split(QRegExp(QStringLiteral("////")), QString::SkipEmptyParts);
QStringList elements = m_analysisTool->parseLine(line);
if (elements.size() < 4) {
continue;
}
......
......@@ -26,8 +26,10 @@
#include <QPushButton>
#include <QProcess>
#include <QTreeView>
#include <QComboBox>
class KateProjectPluginView;
class KateProjectCodeAnalysisTool;
class KMessageWidget;
/**
......@@ -118,6 +120,11 @@ private:
* running analyzer process
*/
QProcess *m_analyzer;
KateProjectCodeAnalysisTool *m_analysisTool;
QComboBox *m_toolSelector;
};
#endif
......
/* This file is part of the Kate project.
*
* Copyright (C) 2017 Héctor Mesa Jiménez <hector@lcc.uma.es>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "kateprojectcodeanalysisselector.h"
#include "kateprojectcodeanalysistoolcppcheck.h"
#include "kateprojectcodeanalysistoolflake8.h"
QStandardItemModel *KateProjectCodeAnalysisSelector::model(QObject *parent)
{
auto model = new QStandardItemModel(parent);
/*
* available linters
*/
const QList<KateProjectCodeAnalysisTool*> tools = {
// cppcheck, for C++
new KateProjectCodeAnalysisToolCppcheck(model),
// flake8, for Python
new KateProjectCodeAnalysisToolFlake8(model)
};
QList<QStandardItem*> column;
for (auto tool : tools) {
auto item = new QStandardItem(tool->name());
item->setData(QVariant::fromValue(tool), Qt::UserRole + 1);
column << item;
}
model->appendColumn(column);
return model;
}
/* This file is part of the Kate project.
*
* Copyright (C) 2017 Héctor Mesa Jiménez <hector@lcc.uma.es>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef KATE_PROJECT_CODE_ANALYSIS_SELECTOR_H
#define KATE_PROJECT_CODE_ANALYSIS_SELECTOR_H
#include <QStandardItemModel>
class KateProjectCodeAnalysisSelector
{
public:
/**
* Model attachable to a code analysis tool selector.
*
* Each item contains a pointer to a KateProjectCodeAnalysisTool
* associated to the role Qt::UserRole + 1
*/
static QStandardItemModel *model(QObject* parent = nullptr);
};
#endif // KATE_PROJECT_CODE_ANALYSIS_SELECTOR_H
/* This file is part of the Kate project.
*
* Copyright (C) 2017 Héctor Mesa Jiménez <hector@lcc.uma.es>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "kateprojectcodeanalysistoolcppcheck.h"
#include <QRegularExpression>
#include <klocalizedstring.h>
KateProjectCodeAnalysisToolCppcheck::KateProjectCodeAnalysisToolCppcheck(QObject *parent)
: KateProjectCodeAnalysisTool(parent)
{
}
KateProjectCodeAnalysisToolCppcheck::~KateProjectCodeAnalysisToolCppcheck()
{
}
QString KateProjectCodeAnalysisToolCppcheck::name()
{
return i18n("cppcheck");
}
QStringList KateProjectCodeAnalysisToolCppcheck::filter(const QStringList &files)
{
// c++ files
return files.filter(QRegularExpression(QStringLiteral("\\.(cpp|cxx|cc|c\\+\\+|c|tpp|txx)$")));
}
QString KateProjectCodeAnalysisToolCppcheck::path()
{
return QStringLiteral("cppcheck");
}
QStringList KateProjectCodeAnalysisToolCppcheck::arguments()
{
QStringList _args;
_args << QStringLiteral("-q")
<< QStringLiteral("--inline-suppr")
<< QStringLiteral("--enable=all")
<< QStringLiteral("--template={file}////{line}////{severity}////{message}")
<< QStringLiteral("--file-list=-");
return _args;
}
QString KateProjectCodeAnalysisToolCppcheck::notInstalledMessage()
{
return i18n("Please install 'cppcheck'.");
}
QStringList KateProjectCodeAnalysisToolCppcheck::parseLine(const QString &line)
{
return line.split(QRegExp(QStringLiteral("////")), QString::SkipEmptyParts);
}
QString KateProjectCodeAnalysisToolCppcheck::stdinMessages()
{
// filenames are written to stdin (--file-list=-)
if (!m_project) {
return QString();
}
return filter(m_project->files()).join(QStringLiteral("\n"));
}
/* This file is part of the Kate project.
*
* Copyright (C) 2017 Héctor Mesa Jiménez <hector@lcc.uma.es>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef KATE_PROJECT_CODE_ANALYSIS_TOOL_CPPCHECK_H
#define KATE_PROJECT_CODE_ANALYSIS_TOOL_CPPCHECK_H
#include "../kateprojectcodeanalysistool.h"
/**
* Information provider for cppcheck
*/
class KateProjectCodeAnalysisToolCppcheck: public KateProjectCodeAnalysisTool
{
public:
explicit KateProjectCodeAnalysisToolCppcheck(QObject *parent = nullptr);
virtual ~KateProjectCodeAnalysisToolCppcheck() override;
virtual QString name() override;
virtual QStringList filter(const QStringList &files) override;
virtual QString path() override;
virtual QStringList arguments() override;
virtual QString notInstalledMessage() override;
virtual QStringList parseLine(const QString &line) override;
virtual QString stdinMessages() override;
};
#endif // KATE_PROJECT_CODE_ANALYSIS_TOOL_CPPCHECK_H
/* This file is part of the Kate project.
*
* Copyright (C) 2017 Héctor Mesa Jiménez <hector@lcc.uma.es>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "kateprojectcodeanalysistoolflake8.h"
#include <QRegularExpression>
#include <klocalizedstring.h>
KateProjectCodeAnalysisToolFlake8::KateProjectCodeAnalysisToolFlake8(QObject *parent)
: KateProjectCodeAnalysisTool(parent)
{
}
KateProjectCodeAnalysisToolFlake8::~KateProjectCodeAnalysisToolFlake8()
{
}
QString KateProjectCodeAnalysisToolFlake8::name()
{
return i18n("flake8");
}
QStringList KateProjectCodeAnalysisToolFlake8::filter(const QStringList &files)
{
// for now we expect files with extension
return files.filter(QRegularExpression(QStringLiteral("\\.py$")));
}
QString KateProjectCodeAnalysisToolFlake8::path()
{
/*
* for now, only the executable in the path can be called,
* but it would be great to be able to specify a version
* installed in a virtual environment
*/
return QStringLiteral("flake8");
}
QStringList KateProjectCodeAnalysisToolFlake8::arguments()
{
QStringList _args;
_args << QStringLiteral("--exit-zero")
/*
* translating a flake8 code to a severity level is subjective,
* so the code is provided as a severity level.
*/
<< QStringLiteral("--format=%(path)s////%(row)d////%(code)s////%(text)s");
if (m_project) {
_args.append(filter(m_project->files()));
}
return _args;
}
QString KateProjectCodeAnalysisToolFlake8::notInstalledMessage()
{
return i18n("Please install 'flake8'.");
}
QStringList KateProjectCodeAnalysisToolFlake8::parseLine(const QString &line)
{
return line.split(QRegExp(QStringLiteral("////")), QString::SkipEmptyParts);
}
QString KateProjectCodeAnalysisToolFlake8::stdinMessages()
{
return QString();
}
/* This file is part of the Kate project.
*
* Copyright (C) 2017 Héctor Mesa Jiménez <hector@lcc.uma.es>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef KATE_PROJECT_CODE_ANALYSIS_TOOL_FLAKE8_H
#define KATE_PROJECT_CODE_ANALYSIS_TOOL_FLAKE8_H
#include "../kateprojectcodeanalysistool.h"
/**
* Information provider for flake8
*/
class KateProjectCodeAnalysisToolFlake8: public KateProjectCodeAnalysisTool
{
public:
explicit KateProjectCodeAnalysisToolFlake8(QObject *parent = nullptr);
virtual ~KateProjectCodeAnalysisToolFlake8() override;
virtual QString name() override;
virtual QStringList filter(const QStringList &files) override;
virtual QString path() override;
virtual QStringList arguments() override;
virtual QString notInstalledMessage() override;
virtual QStringList parseLine(const QString &line) override;
virtual QString stdinMessages() override;
};
#endif // KATE_PROJECT_CODE_ANALYSIS_TOOL_FLAKE8_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment