plugin_katebuild.h 5.37 KB
Newer Older
Kåre Särs's avatar
Kåre Särs committed
1 2 3 4
#ifndef PLUGIN_KATEBUILD_H
#define PLUGIN_KATEBUILD_H
/* plugin_katebuild.h                    Kate Plugin
**
5
** Copyright (C) 2008-2015 by Kåre Särs <kare.sars@iki.fi>
Kåre Särs's avatar
Kåre Särs committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
**
** This code is almost a total rewrite of the GPL'ed Make plugin
** by Adriaan de Groot.
*/

/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program 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 General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program in a file called COPYING; if not, write to
** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
** MA 02110-1301, USA.
*/

28 29 30
#include <KProcess>
#include <QHash>
#include <QPointer>
Bernhard Beschow's avatar
Bernhard Beschow committed
31
#include <QRegularExpression>
Kåre Särs's avatar
Kåre Särs committed
32
#include <QStack>
33
#include <QString>
Kåre Särs's avatar
Kåre Särs committed
34

35
#include <KTextEditor/Document>
36 37 38
#include <KTextEditor/MainWindow>
#include <KTextEditor/MarkInterface>
#include <KTextEditor/Message>
39 40
#include <KTextEditor/Plugin>
#include <KTextEditor/SessionConfigInterface>
41
#include <KTextEditor/View>
42

43
#include <KConfigGroup>
44
#include <KXMLGUIClient>
Kåre Särs's avatar
Kåre Särs committed
45

46
#include "targets.h"
47
#include "ui_build.h"
Kåre Särs's avatar
Kåre Särs committed
48 49

/******************************************************************/
50
class KateBuildView : public QObject, public KXMLGUIClient, public KTextEditor::SessionConfigInterface
Kåre Särs's avatar
Kåre Särs committed
51 52
{
    Q_OBJECT
53
    Q_INTERFACES(KTextEditor::SessionConfigInterface)
54
    Q_PROPERTY(QUrl docUrl READ docUrl)
Kåre Särs's avatar
Kåre Särs committed
55

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
public:
    enum ResultDetails { FullOutput, ParsedOutput, ErrorsAndWarnings, OnlyErrors };

    enum TreeWidgetRoles { ErrorRole = Qt::UserRole + 1, DataRole };

    enum ErrorCategory { CategoryInfo, CategoryWarning, CategoryError };

    KateBuildView(KTextEditor::Plugin *plugin, KTextEditor::MainWindow *mw);
    ~KateBuildView() override;

    // reimplemented: read and write session config
    void readSessionConfig(const KConfigGroup &config) override;
    void writeSessionConfig(KConfigGroup &config) override;

    bool buildCurrentTarget();

    QUrl docUrl();

private Q_SLOTS:

    // Building
    void slotSelectTarget();
    void slotBuildActiveTarget();
    void slotBuildPreviousTarget();
    void slotBuildDefaultTarget();
    bool slotStop();

    // Parse output
    void slotProcExited(int exitCode, QProcess::ExitStatus exitStatus);
    void slotReadReadyStdErr();
    void slotReadReadyStdOut();

    // Selecting warnings/errors
    void slotNext();
    void slotPrev();
    void slotErrorSelected(QTreeWidgetItem *item);

    // Settings
    void targetSetNew();
    void targetOrSetCopy();
    void targetDelete();

    void slotAddTargetClicked();

    void slotDisplayMode(int mode);

    void handleEsc(QEvent *e);

    void slotViewChanged();
    void slotDisplayOption();
    void slotMarkClicked(KTextEditor::Document *doc, KTextEditor::Mark mark, bool &handled);
    void slotInvalidateMoving(KTextEditor::Document *doc);
    /**
     * keep track if the project plugin is alive and if the project map did change
     */
    void slotPluginViewCreated(const QString &name, QObject *pluginView);
    void slotPluginViewDeleted(const QString &name, QObject *pluginView);
    void slotProjectMapChanged();
    void slotAddProjectTarget();

protected:
    bool eventFilter(QObject *obj, QEvent *ev) override;

private:
    void processLine(const QString &);
    void addError(const QString &filename, const QString &line, const QString &column, const QString &message);
    bool startProcess(const QString &dir, const QString &command);
    bool checkLocal(const QUrl &dir);
    void clearBuildResults();

    void displayBuildResult(const QString &message, KTextEditor::Message::MessageType level);

    void clearMarks();
    void addMarks(KTextEditor::Document *doc, bool mark);

    KTextEditor::MainWindow *m_win;
    QWidget *m_toolView;
133
    Ui::build m_buildUi{};
134 135 136 137 138 139 140 141 142 143 144 145 146 147
    QWidget *m_buildWidget;
    int m_outputWidgetWidth;
    TargetsUi *m_targetsUi;
    KProcess m_proc;
    QString m_stdOut;
    QString m_stdErr;
    QString m_currentlyBuildingTarget;
    bool m_buildCancelled;
    int m_displayModeBeforeBuild;
    QString m_make_dir;
    QStack<QString> m_make_dir_stack;
    QRegularExpression m_filenameDetector;
    QRegularExpression m_filenameDetectorIcpc;
    bool m_filenameDetectorGccWorked;
148
    bool m_ninjaBuildDetected = false;
149
    QRegularExpression m_newDirDetector;
150 151
    unsigned int m_numErrors = 0;
    unsigned int m_numWarnings = 0;
152 153 154 155 156 157 158 159 160 161
    QString m_prevItemContent;
    QModelIndex m_previousIndex;
    QPointer<KTextEditor::Message> m_infoMessage;
    QPointer<QAction> m_showMarks;
    QHash<KTextEditor::Document *, QPointer<KTextEditor::Document>> m_markedDocs;

    /**
     * current project plugin view, if any
     */
    QObject *m_projectPluginView = nullptr;
Kåre Särs's avatar
Kåre Särs committed
162 163
};

164 165
typedef QList<QVariant> VariantList;

Kåre Särs's avatar
Kåre Särs committed
166
/******************************************************************/
167
class KateBuildPlugin : public KTextEditor::Plugin
Kåre Särs's avatar
Kåre Särs committed
168 169 170
{
    Q_OBJECT

171 172 173 174 175
public:
    explicit KateBuildPlugin(QObject *parent = nullptr, const VariantList & = VariantList());
    ~KateBuildPlugin() override
    {
    }
Kåre Särs's avatar
Kåre Särs committed
176

177 178
    QObject *createView(KTextEditor::MainWindow *mainWindow) override;
};
Kåre Särs's avatar
Kåre Särs committed
179 180

#endif