beautifierplugin.h 3.49 KB
Newer Older
1
/****************************************************************************
Lorenz Haas's avatar
Lorenz Haas committed
2
**
3 4
** Copyright (C) 2016 Lorenz Haas
** Contact: https://www.qt.io/licensing/
Lorenz Haas's avatar
Lorenz Haas committed
5 6 7 8 9 10 11
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
12 13 14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
Lorenz Haas's avatar
Lorenz Haas committed
15
**
16 17 18 19 20 21 22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
Lorenz Haas's avatar
Lorenz Haas committed
23 24 25
**
****************************************************************************/

hjk's avatar
hjk committed
26
#pragma once
Lorenz Haas's avatar
Lorenz Haas committed
27

28 29
#include "command.h"

Lorenz Haas's avatar
Lorenz Haas committed
30 31
#include <extensionsystem/iplugin.h>

32
#include <QPlainTextEdit>
33
#include <QPointer>
34
#include <QSharedPointer>
35

36 37 38 39 40
namespace Core {
class IDocument;
class IEditor;
}
namespace TextEditor {class TextEditorWidget;}
Lorenz Haas's avatar
Lorenz Haas committed
41 42 43 44 45

namespace Beautifier {
namespace Internal {

class BeautifierAbstractTool;
46
class GeneralSettings;
47 48 49 50

struct FormatTask
{
    FormatTask(QPlainTextEdit *_editor, const QString &_filePath, const QString &_sourceData,
51
               const Command &_command, int _startPos = -1, int _endPos = 0) :
52 53 54 55
        editor(_editor),
        filePath(_filePath),
        sourceData(_sourceData),
        command(_command),
56
        startPos(_startPos),
57
        endPos(_endPos) {}
58 59 60 61 62

    QPointer<QPlainTextEdit> editor;
    QString filePath;
    QString sourceData;
    Command command;
63 64
    int startPos = -1;
    int endPos = 0;
65
    QString formattedData;
66
    QString error;
67
};
Lorenz Haas's avatar
Lorenz Haas committed
68 69 70 71 72 73 74

class BeautifierPlugin : public ExtensionSystem::IPlugin
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "Beautifier.json")

public:
75 76
    bool initialize(const QStringList &arguments, QString *errorString) override;
    void extensionsInitialized() override;
Lorenz Haas's avatar
Lorenz Haas committed
77

78
    void formatCurrentFile(const Command &command, int startPos = -1, int endPos = 0);
Lorenz Haas's avatar
Lorenz Haas committed
79

80 81 82
    static QString msgCannotGetConfigurationFile(const QString &command);
    static QString msgFormatCurrentFile();
    static QString msgFormatSelectedText();
83
    static QString msgDisableFormattingSelectedText();
84
    static QString msgCommandPromptDialogTitle(const QString &command);
85 86
    static void showError(const QString &error);

Lorenz Haas's avatar
Lorenz Haas committed
87
private:
88
    void updateActions(Core::IEditor *editor = nullptr);
Lorenz Haas's avatar
Lorenz Haas committed
89
    QList<BeautifierAbstractTool *> m_tools;
90
    QSharedPointer<GeneralSettings> m_generalSettings;
91
    QHash<QObject*, QMetaObject::Connection> m_autoFormatConnections;
92 93 94 95 96 97
    void formatEditor(TextEditor::TextEditorWidget *editor, const Command &command,
                      int startPos = -1, int endPos = 0);
    void formatEditorAsync(TextEditor::TextEditorWidget *editor, const Command &command,
                           int startPos = -1, int endPos = 0);
    void checkAndApplyTask(const FormatTask &task);
    void updateEditorText(QPlainTextEdit *editor, const QString &text);
98 99

    void autoFormatOnSave(Core::IDocument *document);
Lorenz Haas's avatar
Lorenz Haas committed
100 101 102 103
};

} // namespace Internal
} // namespace Beautifier