aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/texteditor/highlightersettingspage.cpp
blob: 81eacbe7e062bf802bc3a19d7793ef02799ed1f9 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "highlightersettingspage.h"

#include "highlighter.h"
#include "highlighterhelper.h"
#include "highlightersettings.h"
#include "texteditortr.h"

#include <coreplugin/icore.h>

#include <utils/hostosinfo.h>
#include <utils/layoutbuilder.h>
#include <utils/pathchooser.h>

#include <QDir>
#include <QLabel>
#include <QLineEdit>
#include <QPointer>
#include <QPushButton>

using namespace Utils;

namespace TextEditor {

class HighlighterSettingsPageWidget;

class HighlighterSettingsPagePrivate
{
public:
    void ensureInitialized()
    {
        if (m_initialized)
            return;
        m_initialized = true;
        m_settings.fromSettings(m_settingsPrefix, Core::ICore::settings());
        migrateGenericHighlighterFiles();
    }

    void migrateGenericHighlighterFiles()
    {
        QDir userDefinitionPath(m_settings.definitionFilesPath().toString());
        if (userDefinitionPath.mkdir("syntax")) {
            const auto link = Utils::HostOsInfo::isAnyUnixHost()
                                  ? static_cast<bool(*)(const QString &, const QString &)>(&QFile::link)
                                  : static_cast<bool(*)(const QString &, const QString &)>(&QFile::copy);

            for (const QFileInfo &file : userDefinitionPath.entryInfoList({"*.xml"}, QDir::Files))
                link(file.filePath(), file.absolutePath() + "/syntax/" + file.fileName());
        }
    }

    bool m_initialized = false;
    const Key m_settingsPrefix{"Text"};

    HighlighterSettings m_settings;

    QPointer<HighlighterSettingsPageWidget> m_widget;
};

class HighlighterSettingsPageWidget : public Core::IOptionsPageWidget
{
public:
    HighlighterSettingsPageWidget(HighlighterSettingsPagePrivate *d) : d(d)
    {
        d->ensureInitialized();

        auto definitionsInfolabel = new QLabel(this);
        definitionsInfolabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
        definitionsInfolabel->setTextFormat(Qt::RichText);
        definitionsInfolabel->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter);
        definitionsInfolabel->setWordWrap(true);
        definitionsInfolabel->setOpenExternalLinks(true);
        definitionsInfolabel->setText(Tr::tr("<html><head/><body><p>Highlight definitions are provided by the "
                                             "<a href=\"https://api.kde.org/frameworks/syntax-highlighting/html/index.html\">"
                                             "KSyntaxHighlighting</a> engine.</p></body></html>"));

        auto downloadDefinitions = new QPushButton(Tr::tr("Download Definitions"));
        downloadDefinitions->setToolTip(Tr::tr("Download missing and update existing syntax definition files."));

        auto updateStatus = new QLabel;
        updateStatus->setObjectName("updateStatus");

        m_definitionFilesPath = new PathChooser;
        m_definitionFilesPath->setFilePath(d->m_settings.definitionFilesPath());
        m_definitionFilesPath->setExpectedKind(PathChooser::ExistingDirectory);
        m_definitionFilesPath->setHistoryCompleter("TextEditor.Highlighter.History");

        auto reloadDefinitions = new QPushButton(Tr::tr("Reload Definitions"));
        reloadDefinitions->setToolTip(Tr::tr("Reload externally modified definition files."));

        auto resetCache = new QPushButton(Tr::tr("Reset Remembered Definitions"));
        resetCache->setToolTip(Tr::tr("Reset definitions remembered for files that can be "
                                      "associated with more than one highlighter definition."));

        m_ignoreEdit = new QLineEdit(d->m_settings.ignoredFilesPatterns());

        using namespace Layouting;
        Column {
            definitionsInfolabel,
            Space(3),
            Group {
                title(Tr::tr("Syntax Highlight Definition Files")),
                Column {
                    Row { downloadDefinitions, updateStatus, st },
                    Row { Tr::tr("User Highlight Definition Files"),
                                m_definitionFilesPath, reloadDefinitions },
                    Row { st, resetCache }
                }
            },
            Row { Tr::tr("Ignored file patterns:"), m_ignoreEdit },
            st
        }.attachTo(this);

        connect(downloadDefinitions, &QPushButton::pressed,
                [label = QPointer<QLabel>(updateStatus)]() {
                    HighlighterHelper::downloadDefinitions([label] {
                        if (label)
                            label->setText(Tr::tr("Download finished"));
                    });
                });

        connect(reloadDefinitions, &QPushButton::pressed, this, [] {
            HighlighterHelper::reload();
        });
        connect(resetCache, &QPushButton::clicked, this, [] {
            HighlighterHelper::clearDefinitionForDocumentCache();
        });
    }

    void apply() final
    {
        bool changed = d->m_settings.definitionFilesPath() != m_definitionFilesPath->filePath()
                    || d->m_settings.ignoredFilesPatterns() != m_ignoreEdit->text();

        if (changed) {
            d->m_settings.setDefinitionFilesPath(m_definitionFilesPath->filePath());
            d->m_settings.setIgnoredFilesPatterns(m_ignoreEdit->text());
            d->m_settings.toSettings(d->m_settingsPrefix, Core::ICore::settings());
        }
    }

    PathChooser *m_definitionFilesPath;
    QLineEdit *m_ignoreEdit;
    HighlighterSettingsPagePrivate *d;
};

// HighlighterSettingsPage

HighlighterSettingsPage::HighlighterSettingsPage()
    : d(new HighlighterSettingsPagePrivate)
{
    setId(Constants::TEXT_EDITOR_HIGHLIGHTER_SETTINGS);
    setDisplayName(Tr::tr("Generic Highlighter"));
    setCategory(TextEditor::Constants::TEXT_EDITOR_SETTINGS_CATEGORY);
    setDisplayCategory(Tr::tr("Text Editor"));
    setCategoryIconPath(TextEditor::Constants::TEXT_EDITOR_SETTINGS_CATEGORY_ICON_PATH);
    setWidgetCreator([this] { return new HighlighterSettingsPageWidget(d); });
}

HighlighterSettingsPage::~HighlighterSettingsPage()
{
    delete d;
}

const HighlighterSettings &HighlighterSettingsPage::highlighterSettings() const
{
    d->ensureInitialized();
    return d->m_settings;
}

} // TextEditor