File indexing completed on 2025-01-05 05:20:15

0001 /*  This file is part of the Kate project.
0002  *
0003  *  SPDX-FileCopyrightText: 2017 Héctor Mesa Jiménez <hector@lcc.uma.es>
0004  *
0005  *  SPDX-License-Identifier: LGPL-2.0-or-later
0006  */
0007 
0008 #include "flake8.h"
0009 
0010 #include "kateproject.h"
0011 
0012 #include <KLocalizedString>
0013 #include <QRegularExpression>
0014 
0015 KateProjectCodeAnalysisToolFlake8::KateProjectCodeAnalysisToolFlake8(QObject *parent)
0016     : KateProjectCodeAnalysisTool(parent)
0017 {
0018 }
0019 
0020 KateProjectCodeAnalysisToolFlake8::~KateProjectCodeAnalysisToolFlake8()
0021 {
0022 }
0023 
0024 QString KateProjectCodeAnalysisToolFlake8::name() const
0025 {
0026     return i18n("Flake8 (Python)");
0027 }
0028 
0029 QString KateProjectCodeAnalysisToolFlake8::description() const
0030 {
0031     return i18n("Flake8: Your Tool For Style Guide Enforcement for Python");
0032 }
0033 
0034 QString KateProjectCodeAnalysisToolFlake8::fileExtensions() const
0035 {
0036     return QStringLiteral("py");
0037 }
0038 
0039 QStringList KateProjectCodeAnalysisToolFlake8::filter(const QStringList &files) const
0040 {
0041     // for now we expect files with extension
0042     return files.filter(QRegularExpression(QStringLiteral("\\.(") + fileExtensions() + QStringLiteral(")$")));
0043 }
0044 
0045 QString KateProjectCodeAnalysisToolFlake8::path() const
0046 {
0047     /*
0048      * for now, only the executable in the path can be called,
0049      * but it would be great to be able to specify a version
0050      * installed in a virtual environment
0051      */
0052     return QStringLiteral("flake8");
0053 }
0054 
0055 QStringList KateProjectCodeAnalysisToolFlake8::arguments()
0056 {
0057     QStringList _args;
0058 
0059     _args << QStringLiteral("--exit-zero")
0060           /*
0061            * translating a flake8 code to a severity level is subjective,
0062            * so the code is provided as a severity level.
0063            */
0064           << QStringLiteral("--format=%(path)s////%(row)d////%(code)s////%(text)s");
0065 
0066     if (m_project) {
0067         auto &&fileList = filter(m_project->files());
0068         setActualFilesCount(fileList.size());
0069         _args.append(fileList);
0070     }
0071 
0072     return _args;
0073 }
0074 
0075 QString KateProjectCodeAnalysisToolFlake8::notInstalledMessage() const
0076 {
0077     return i18n("Please install 'flake8'.");
0078 }
0079 
0080 FileDiagnostics KateProjectCodeAnalysisToolFlake8::parseLine(const QString &line) const
0081 {
0082     const QStringList elements = line.split(QLatin1String("////"), Qt::SkipEmptyParts);
0083     const auto url = QUrl::fromLocalFile(elements[0]);
0084     Diagnostic d;
0085     d.message = elements[3];
0086     d.severity = DiagnosticSeverity::Warning;
0087     int ln = elements[1].toInt() - 1;
0088     d.range = KTextEditor::Range(ln, 0, ln, -1);
0089     return {url, {d}};
0090 }
0091 
0092 QString KateProjectCodeAnalysisToolFlake8::stdinMessages()
0093 {
0094     return QString();
0095 }