2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2019 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
2018-11-08 11:39:48 +01:00
|
|
|
|
|
|
|
|
#include "highlightersettingspage.h"
|
2022-07-27 12:57:49 +02:00
|
|
|
|
2018-11-08 11:39:48 +01:00
|
|
|
#include "highlightersettings.h"
|
|
|
|
|
#include "highlighter.h"
|
|
|
|
|
|
|
|
|
|
#include <coreplugin/icore.h>
|
2022-07-27 12:57:49 +02:00
|
|
|
|
2022-05-24 00:40:44 +02:00
|
|
|
#include <utils/hostosinfo.h>
|
2022-07-27 12:57:49 +02:00
|
|
|
#include <utils/layoutbuilder.h>
|
|
|
|
|
#include <utils/pathchooser.h>
|
2018-11-08 11:39:48 +01:00
|
|
|
|
2022-07-27 12:57:49 +02:00
|
|
|
#include <QApplication>
|
2018-11-08 11:39:48 +01:00
|
|
|
#include <QDir>
|
2022-07-27 12:57:49 +02:00
|
|
|
#include <QLabel>
|
|
|
|
|
#include <QLineEdit>
|
2018-11-08 11:39:48 +01:00
|
|
|
#include <QMessageBox>
|
|
|
|
|
#include <QPointer>
|
2022-07-27 12:57:49 +02:00
|
|
|
#include <QPushButton>
|
2018-11-08 11:39:48 +01:00
|
|
|
|
2021-09-27 19:05:08 +02:00
|
|
|
using namespace Utils;
|
2018-11-08 11:39:48 +01:00
|
|
|
|
2021-09-27 19:05:08 +02:00
|
|
|
namespace TextEditor {
|
2022-07-27 12:57:49 +02:00
|
|
|
namespace Internal {
|
2021-09-27 19:05:08 +02:00
|
|
|
|
2022-07-27 12:57:49 +02:00
|
|
|
class HighlighterSettingsPageWidget : public QWidget
|
|
|
|
|
{
|
2022-10-14 14:52:54 +02:00
|
|
|
Q_DECLARE_TR_FUNCTIONS(TextEditor::Internal::HighlighterSettingsPage)
|
2022-07-27 12:57:49 +02:00
|
|
|
public:
|
|
|
|
|
QLabel *definitionsInfolabel;
|
|
|
|
|
QPushButton *downloadDefinitions;
|
|
|
|
|
QLabel *updateStatus;
|
|
|
|
|
PathChooser *definitionFilesPath;
|
|
|
|
|
QPushButton *reloadDefinitions;
|
|
|
|
|
QPushButton *resetCache;
|
|
|
|
|
QLineEdit *ignoreEdit;
|
|
|
|
|
|
|
|
|
|
HighlighterSettingsPageWidget()
|
|
|
|
|
{
|
|
|
|
|
resize(521, 332);
|
|
|
|
|
|
|
|
|
|
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("<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>"));
|
|
|
|
|
|
|
|
|
|
downloadDefinitions = new QPushButton(tr("Download Definitions"));
|
|
|
|
|
downloadDefinitions->setToolTip(tr("Download missing and update existing syntax definition files."));
|
|
|
|
|
|
|
|
|
|
updateStatus = new QLabel;
|
2022-10-17 08:56:10 +02:00
|
|
|
updateStatus->setObjectName("updateStatus");
|
2022-07-27 12:57:49 +02:00
|
|
|
|
|
|
|
|
definitionFilesPath = new PathChooser;
|
|
|
|
|
definitionFilesPath->setExpectedKind(PathChooser::ExistingDirectory);
|
|
|
|
|
definitionFilesPath->setHistoryCompleter("TextEditor.Highlighter.History");
|
|
|
|
|
|
|
|
|
|
reloadDefinitions = new QPushButton(tr("Reload Definitions"));
|
|
|
|
|
reloadDefinitions->setToolTip(tr("Reload externally modified definition files."));
|
|
|
|
|
|
|
|
|
|
resetCache = new QPushButton(tr("Reset Remembered Definitions"));
|
|
|
|
|
resetCache->setToolTip(tr("Reset definitions remembered for files that can be "
|
|
|
|
|
"associated with more than one highlighter definition."));
|
|
|
|
|
|
|
|
|
|
ignoreEdit = new QLineEdit;
|
|
|
|
|
|
|
|
|
|
using namespace Layouting;
|
|
|
|
|
Column {
|
|
|
|
|
definitionsInfolabel,
|
|
|
|
|
Space(3),
|
|
|
|
|
Group {
|
|
|
|
|
title(tr("Syntax Highlight Definition Files")),
|
|
|
|
|
Column {
|
|
|
|
|
Row { downloadDefinitions, updateStatus, st },
|
|
|
|
|
Row { tr("User Highlight Definition Files"),
|
|
|
|
|
definitionFilesPath, reloadDefinitions },
|
|
|
|
|
Row { st, resetCache }
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
Row { tr("Ignored file patterns:"), ignoreEdit },
|
|
|
|
|
st
|
|
|
|
|
}.attachTo(this);
|
|
|
|
|
|
|
|
|
|
connect(downloadDefinitions, &QPushButton::pressed,
|
|
|
|
|
[label = QPointer<QLabel>(updateStatus)]() {
|
|
|
|
|
Highlighter::downloadDefinitions([label] {
|
|
|
|
|
if (label)
|
|
|
|
|
label->setText(tr("Download finished"));
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
connect(reloadDefinitions, &QPushButton::pressed, this, [] {
|
|
|
|
|
Highlighter::reload();
|
|
|
|
|
});
|
|
|
|
|
connect(resetCache, &QPushButton::clicked, this, [] {
|
|
|
|
|
Highlighter::clearDefinitionForDocumentCache();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // Internal
|
|
|
|
|
|
2022-11-23 18:25:55 +01:00
|
|
|
using namespace Internal;
|
|
|
|
|
|
2022-07-27 12:57:49 +02:00
|
|
|
class HighlighterSettingsPagePrivate
|
2018-11-08 11:39:48 +01:00
|
|
|
{
|
2020-01-20 12:47:00 +01:00
|
|
|
Q_DECLARE_TR_FUNCTIONS(TextEditor::Internal::HighlighterSettingsPage)
|
|
|
|
|
|
|
|
|
|
public:
|
2021-09-27 19:05:08 +02:00
|
|
|
HighlighterSettingsPagePrivate() = default;
|
|
|
|
|
|
2018-11-08 11:39:48 +01:00
|
|
|
void ensureInitialized();
|
|
|
|
|
void migrateGenericHighlighterFiles();
|
|
|
|
|
|
2022-07-27 12:57:49 +02:00
|
|
|
void settingsFromUI();
|
|
|
|
|
void settingsToUI();
|
|
|
|
|
bool settingsChanged();
|
|
|
|
|
|
2018-11-08 11:39:48 +01:00
|
|
|
bool m_initialized = false;
|
2021-09-27 19:05:08 +02:00
|
|
|
const QString m_settingsPrefix{"Text"};
|
2018-11-08 11:39:48 +01:00
|
|
|
|
|
|
|
|
HighlighterSettings m_settings;
|
|
|
|
|
|
2022-07-27 12:57:49 +02:00
|
|
|
QPointer<HighlighterSettingsPageWidget> m_widget;
|
2018-11-08 11:39:48 +01:00
|
|
|
};
|
|
|
|
|
|
2022-07-27 12:57:49 +02:00
|
|
|
void HighlighterSettingsPagePrivate::migrateGenericHighlighterFiles()
|
2018-11-08 11:39:48 +01:00
|
|
|
{
|
2021-09-27 19:05:08 +02:00
|
|
|
QDir userDefinitionPath(m_settings.definitionFilesPath().toString());
|
2018-11-08 11:39:48 +01:00
|
|
|
if (userDefinitionPath.mkdir("syntax")) {
|
|
|
|
|
const auto link = Utils::HostOsInfo::isAnyUnixHost()
|
2020-06-22 09:16:14 +02:00
|
|
|
? static_cast<bool(*)(const QString &, const QString &)>(&QFile::link)
|
|
|
|
|
: static_cast<bool(*)(const QString &, const QString &)>(&QFile::copy);
|
2018-11-08 11:39:48 +01:00
|
|
|
|
|
|
|
|
for (const QFileInfo &file : userDefinitionPath.entryInfoList({"*.xml"}, QDir::Files))
|
|
|
|
|
link(file.filePath(), file.absolutePath() + "/syntax/" + file.fileName());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-27 12:57:49 +02:00
|
|
|
void HighlighterSettingsPagePrivate::ensureInitialized()
|
2018-11-08 11:39:48 +01:00
|
|
|
{
|
|
|
|
|
if (m_initialized)
|
|
|
|
|
return;
|
|
|
|
|
m_initialized = true;
|
|
|
|
|
m_settings.fromSettings(m_settingsPrefix, Core::ICore::settings());
|
|
|
|
|
migrateGenericHighlighterFiles();
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-20 12:47:00 +01:00
|
|
|
HighlighterSettingsPage::HighlighterSettingsPage()
|
2021-09-27 19:05:08 +02:00
|
|
|
: d(new HighlighterSettingsPagePrivate)
|
2018-11-08 11:39:48 +01:00
|
|
|
{
|
2020-01-20 12:47:00 +01:00
|
|
|
setId(Constants::TEXT_EDITOR_HIGHLIGHTER_SETTINGS);
|
|
|
|
|
setDisplayName(HighlighterSettingsPagePrivate::tr("Generic Highlighter"));
|
2020-01-15 15:00:57 +01:00
|
|
|
setCategory(TextEditor::Constants::TEXT_EDITOR_SETTINGS_CATEGORY);
|
|
|
|
|
setDisplayCategory(QCoreApplication::translate("TextEditor", "Text Editor"));
|
|
|
|
|
setCategoryIconPath(TextEditor::Constants::TEXT_EDITOR_SETTINGS_CATEGORY_ICON_PATH);
|
2018-11-08 11:39:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HighlighterSettingsPage::~HighlighterSettingsPage()
|
|
|
|
|
{
|
2021-09-27 19:05:08 +02:00
|
|
|
delete d;
|
2018-11-08 11:39:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QWidget *HighlighterSettingsPage::widget()
|
|
|
|
|
{
|
2021-09-27 19:05:08 +02:00
|
|
|
if (!d->m_widget) {
|
2022-07-27 12:57:49 +02:00
|
|
|
d->m_widget = new HighlighterSettingsPageWidget;
|
|
|
|
|
d->settingsToUI();
|
2018-11-08 11:39:48 +01:00
|
|
|
}
|
2021-09-27 19:05:08 +02:00
|
|
|
return d->m_widget;
|
2018-11-08 11:39:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HighlighterSettingsPage::apply()
|
|
|
|
|
{
|
2022-07-27 12:57:49 +02:00
|
|
|
if (!d->m_widget) // page was not shown
|
2018-11-08 11:39:48 +01:00
|
|
|
return;
|
2022-07-27 12:57:49 +02:00
|
|
|
if (d->settingsChanged())
|
|
|
|
|
d->settingsFromUI();
|
2018-11-08 11:39:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HighlighterSettingsPage::finish()
|
|
|
|
|
{
|
2021-09-27 19:05:08 +02:00
|
|
|
delete d->m_widget;
|
2022-07-27 12:57:49 +02:00
|
|
|
d->m_widget = nullptr;
|
2018-11-08 11:39:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const HighlighterSettings &HighlighterSettingsPage::highlighterSettings() const
|
|
|
|
|
{
|
2021-09-27 19:05:08 +02:00
|
|
|
d->ensureInitialized();
|
|
|
|
|
return d->m_settings;
|
2018-11-08 11:39:48 +01:00
|
|
|
}
|
|
|
|
|
|
2022-07-27 12:57:49 +02:00
|
|
|
void HighlighterSettingsPagePrivate::settingsFromUI()
|
2018-11-08 11:39:48 +01:00
|
|
|
{
|
2022-07-27 12:57:49 +02:00
|
|
|
ensureInitialized();
|
|
|
|
|
m_settings.setDefinitionFilesPath(m_widget->definitionFilesPath->filePath());
|
|
|
|
|
m_settings.setIgnoredFilesPatterns(m_widget->ignoreEdit->text());
|
|
|
|
|
m_settings.toSettings(m_settingsPrefix, Core::ICore::settings());
|
2018-11-08 11:39:48 +01:00
|
|
|
}
|
|
|
|
|
|
2022-07-27 12:57:49 +02:00
|
|
|
void HighlighterSettingsPagePrivate::settingsToUI()
|
2018-11-08 11:39:48 +01:00
|
|
|
{
|
2022-07-27 12:57:49 +02:00
|
|
|
ensureInitialized();
|
|
|
|
|
m_widget->definitionFilesPath->setFilePath(m_settings.definitionFilesPath());
|
|
|
|
|
m_widget->ignoreEdit->setText(m_settings.ignoredFilesPatterns());
|
2018-11-08 11:39:48 +01:00
|
|
|
}
|
|
|
|
|
|
2022-07-27 12:57:49 +02:00
|
|
|
bool HighlighterSettingsPagePrivate::settingsChanged()
|
2018-11-08 11:39:48 +01:00
|
|
|
{
|
2022-07-27 12:57:49 +02:00
|
|
|
ensureInitialized();
|
|
|
|
|
return m_settings.definitionFilesPath() != m_widget->definitionFilesPath->filePath()
|
|
|
|
|
|| m_settings.ignoredFilesPatterns() != m_widget->ignoreEdit->text();
|
2018-11-08 11:39:48 +01:00
|
|
|
}
|
2021-09-27 19:05:08 +02:00
|
|
|
|
|
|
|
|
} // TextEditor
|