aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/beautifier/beautifiertool.h
blob: b41197b49e57a73e08b5f61b7d49881f9b601f79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
// Copyright (C) 2016 Lorenz Haas
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#pragma once

#include <coreplugin/dialogs/ioptionspage.h>

#include <texteditor/command.h>

#include <utils/aspects.h>

#include <QHash>
#include <QMap>
#include <QObject>
#include <QRegularExpression>
#include <QSet>
#include <QStringList>
#include <QVersionNumber>

#include <memory>

namespace Core {
class IDocument;
class IEditor;
}

namespace Beautifier::Internal  {

class BeautifierTool : public QObject
{
public:
    BeautifierTool();

    static const QList<BeautifierTool *> &allTools();

    virtual QString id() const = 0;
    virtual void updateActions(Core::IEditor *editor) = 0;

    /**
     * Returns the tool's command to format an entire file.
     *
     * @note    The received command may be invalid.
     */
    virtual TextEditor::Command textCommand() const = 0;

    virtual bool isApplicable(const Core::IDocument *document) const = 0;

    static QString msgCannotGetConfigurationFile(const QString &command);
    static QString msgFormatCurrentFile();
    static QString msgFormatSelectedText();
    static QString msgFormatAtCursor();
    static QString msgFormatLines();
    static QString msgDisableFormattingSelectedText();
    static QString msgCommandPromptDialogTitle(const QString &command);
    static void showError(const QString &error);
};

class AbstractSettings : public Utils::AspectContainer
{
public:
    explicit AbstractSettings(const QString &name, const QString &ending);
    ~AbstractSettings() override;

    void read();
    void save();

    virtual void createDocumentationFile() const;
    virtual QStringList completerWords();

    QStringList styles() const;
    QString style(const QString &key) const;
    bool styleExists(const QString &key) const;
    bool styleIsReadOnly(const QString &key);
    void setStyle(const QString &key, const QString &value);
    void removeStyle(const QString &key);
    void replaceStyle(const QString &oldKey, const QString &newKey, const QString &value);
    virtual Utils::FilePath styleFileName(const QString &key) const;

    Utils::FilePathAspect command{this};
    Utils::StringAspect supportedMimeTypes{this};

    Utils::FilePath documentationFilePath;

    QVersionNumber version() const;

    bool isApplicable(const Core::IDocument *document) const;

    QStringList options();
    QString documentation(const QString &option) const;

protected:
    void setVersionRegExp(const QRegularExpression &versionRegExp);

    QMap<QString, QString> m_styles;
    QString m_ending;
    Utils::FilePath m_styleDir;

    void readDocumentation();
    virtual void readStyles();

private:
    QStringList m_stylesToRemove;
    QSet<QString> m_changedStyles;
    QHash<QString, int> m_options;
    QStringList m_docu;
    QStringList m_supportedMimeTypes;
    mutable QVersionNumber m_version;
    QRegularExpression m_versionRegExp;
};

} // Beautifier::Internal