Commit 68a2de71 authored by Christoph Cullmann's avatar Christoph Cullmann 🐮
Browse files

provide editor for user settings json file in configdialog

parent 4d09f481
......@@ -41,22 +41,25 @@ LSPClientConfigPage::LSPClientConfigPage(QWidget *parent, LSPClientPlugin *plugi
ui = new Ui::LspConfigWidget();
ui->setupUi(this);
// setup JSON highlighter for the default json stuff
auto highlighter = new KSyntaxHighlighting::SyntaxHighlighter(ui->defaultConfig->document());
highlighter->setDefinition(m_repository.definitionForFileName(QStringLiteral("settings.json")));
// we want mono-spaced font
ui->defaultConfig->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
// we want to have the proper theme for the current palette
const auto theme = (palette().color(QPalette::Base).lightness() < 128) ? m_repository.defaultTheme(KSyntaxHighlighting::Repository::DarkTheme) : m_repository.defaultTheme(KSyntaxHighlighting::Repository::LightTheme);
auto pal = qApp->palette();
if (theme.isValid()) {
pal.setColor(QPalette::Base, theme.editorColor(KSyntaxHighlighting::Theme::BackgroundColor));
pal.setColor(QPalette::Highlight, theme.editorColor(KSyntaxHighlighting::Theme::TextSelection));
// fix-up our two text edits to be proper JSON file editors
for (auto textEdit : {ui->userConfig, static_cast<QTextEdit *>(ui->defaultConfig)}) {
// setup JSON highlighter for the default json stuff
auto highlighter = new KSyntaxHighlighting::SyntaxHighlighter(textEdit->document());
highlighter->setDefinition(m_repository.definitionForFileName(QStringLiteral("settings.json")));
// we want mono-spaced font
textEdit->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
// we want to have the proper theme for the current palette
const auto theme = (palette().color(QPalette::Base).lightness() < 128) ? m_repository.defaultTheme(KSyntaxHighlighting::Repository::DarkTheme) : m_repository.defaultTheme(KSyntaxHighlighting::Repository::LightTheme);
auto pal = qApp->palette();
if (theme.isValid()) {
pal.setColor(QPalette::Base, theme.editorColor(KSyntaxHighlighting::Theme::BackgroundColor));
pal.setColor(QPalette::Highlight, theme.editorColor(KSyntaxHighlighting::Theme::TextSelection));
}
textEdit->setPalette(pal);
highlighter->setTheme(theme);
}
ui->defaultConfig->setPalette(pal);
highlighter->setTheme(theme);
// setup default json settings
QFile defaultConfigFile(QStringLiteral(":/lspclient/settings.json"));
......@@ -64,6 +67,9 @@ LSPClientConfigPage::LSPClientConfigPage(QWidget *parent, LSPClientPlugin *plugi
Q_ASSERT(defaultConfigFile.isOpen());
ui->defaultConfig->setPlainText(QString::fromUtf8(defaultConfigFile.readAll()));
// setup default config path as placeholder to show user where it is
ui->edtConfigPath->setPlaceholderText(m_plugin->m_defaultConfigPath.toLocalFile());
reset();
for (const auto &cb : {ui->chkSymbolDetails,
......@@ -79,8 +85,9 @@ LSPClientConfigPage::LSPClientConfigPage(QWidget *parent, LSPClientPlugin *plugi
ui->chkSemanticHighlighting,
ui->chkAutoHover})
connect(cb, &QCheckBox::toggled, this, &LSPClientConfigPage::changed);
connect(ui->edtConfigPath, &KUrlRequester::textChanged, this, &LSPClientConfigPage::changed);
connect(ui->edtConfigPath, &KUrlRequester::urlSelected, this, &LSPClientConfigPage::changed);
connect(ui->edtConfigPath, &KUrlRequester::textChanged, this, &LSPClientConfigPage::configUrlChanged);
connect(ui->edtConfigPath, &KUrlRequester::urlSelected, this, &LSPClientConfigPage::configUrlChanged);
connect(ui->userConfig, &QTextEdit::textChanged, this, &LSPClientConfigPage::changed);
// custom control logic
auto h = [this]() {
......@@ -132,6 +139,15 @@ void LSPClientConfigPage::apply()
m_plugin->m_configPath = ui->edtConfigPath->url();
// own scope to ensure file is flushed before we signal below in writeConfig!
{
QFile configFile(m_plugin->configPath().toLocalFile());
configFile.open(QIODevice::WriteOnly);
if (configFile.isOpen()) {
configFile.write(ui->userConfig->toPlainText().toUtf8());
}
}
m_plugin->writeConfig();
}
......@@ -155,9 +171,31 @@ void LSPClientConfigPage::reset()
ui->chkSemanticHighlighting->setChecked(m_plugin->m_semanticHighlighting);
ui->edtConfigPath->setUrl(m_plugin->m_configPath);
readUserConfig(m_plugin->configPath().toLocalFile());
}
void LSPClientConfigPage::defaults()
{
reset();
}
void LSPClientConfigPage::readUserConfig(const QString &fileName)
{
QFile configFile(fileName);
configFile.open(QIODevice::ReadOnly);
if (configFile.isOpen()) {
ui->userConfig->setPlainText(QString::fromUtf8(configFile.readAll()));
} else {
ui->userConfig->clear();
}
}
void LSPClientConfigPage::configUrlChanged()
{
// re-read config
readUserConfig(ui->edtConfigPath->url().isEmpty() ? m_plugin->m_defaultConfigPath.toLocalFile() : ui->edtConfigPath->url().toLocalFile());
// remember changed
changed();
}
......@@ -51,8 +51,11 @@ public Q_SLOTS:
void apply() override;
void defaults() override;
void reset() override;
void configUrlChanged();
private:
void readUserConfig(const QString &fileName);
Ui::LspConfigWidget *ui;
LSPClientPlugin *m_plugin;
KSyntaxHighlighting::Repository m_repository;
......
......@@ -57,6 +57,7 @@ K_PLUGIN_FACTORY_WITH_JSON(LSPClientPluginFactory, "lspclientplugin.json", regis
LSPClientPlugin::LSPClientPlugin(QObject *parent, const QList<QVariant> &)
: KTextEditor::Plugin(parent)
, m_settingsPath(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + QStringLiteral("/lspclient"))
, m_defaultConfigPath(QUrl::fromLocalFile(m_settingsPath + QStringLiteral("/settings.json")))
{
// ensure settings path exist, for e.g. local settings.json
QDir().mkpath(m_settingsPath);
......@@ -116,11 +117,6 @@ void LSPClientPlugin::readConfig()
m_configPath = config.readEntry(CONFIG_SERVER_CONFIG, QUrl());
m_semanticHighlighting = config.readEntry(CONFIG_SEMANTIC_HIGHLIGHTING, false);
// ensure we default to the default local config path for server config
if (m_configPath.isEmpty()) {
m_configPath = QUrl::fromLocalFile(m_settingsPath + QStringLiteral("/settings.json"));
}
emit update();
}
......
......@@ -50,6 +50,9 @@ public:
// path for local setting files, auto-created on load
const QString m_settingsPath;
// default config path
const QUrl m_defaultConfigPath;
// settings
bool m_symbolDetails;
bool m_symbolExpand;
......@@ -69,6 +72,12 @@ public:
// debug mode?
bool m_debugMode = false;
// get current config path
QUrl configPath() const
{
return m_configPath.isEmpty() ? m_defaultConfigPath : m_configPath;
}
private:
Q_SIGNALS:
// signal settings update
......
......@@ -600,7 +600,7 @@ private:
m_serverConfig = QJsonDocument::fromJson(defaultConfigFile.readAll()).object();
// consider specified configuration if existing
const auto configPath = m_plugin->m_configPath.toLocalFile();
const auto configPath = m_plugin->configPath().toLocalFile();
if (!configPath.isEmpty() && QFile::exists(configPath)) {
QFile f(configPath);
if (f.open(QIODevice::ReadOnly)) {
......
......@@ -30,7 +30,7 @@
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>Client</string>
<string>Client Settings</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_6">
<item>
......@@ -167,38 +167,52 @@
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>Server</string>
<string>User Server Settings</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_7">
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<widget class="QGroupBox" name="groupBox_3">
<property name="title">
<string>User Server Configuration</string>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Settings File:</string>
</property>
</widget>
</item>
<item>
<widget class="KUrlRequester" name="edtConfigPath">
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QTextEdit" name="userConfig">
<property name="lineWrapMode">
<enum>QTextEdit::NoWrap</enum>
</property>
<property name="acceptRichText">
<bool>false</bool>
</property>
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<widget class="KUrlRequester" name="edtConfigPath"/>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_3">
<attribute name="title">
<string>Default Server Settings</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_9">
<item>
<widget class="QGroupBox" name="groupBox_4">
<property name="title">
<string>Default Server Configuration</string>
<widget class="QTextBrowser" name="defaultConfig">
<property name="lineWrapMode">
<enum>QTextEdit::NoWrap</enum>
</property>
<property name="acceptRichText">
<bool>false</bool>
</property>
<layout class="QVBoxLayout" name="verticalLayout_8">
<item>
<widget class="QTextBrowser" name="defaultConfig">
<property name="lineWrapMode">
<enum>QTextEdit::NoWrap</enum>
</property>
<property name="acceptRichText">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment