Skip to content

Commit

Permalink
feat: Disable LSP when running a script via command line
Browse files Browse the repository at this point in the history
Misc: Added a Gui option to enable LSP (Default = true)
  • Loading branch information
mgiroday authored Jun 19, 2024
1 parent 424b420 commit 52b4c34
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 101 deletions.
17 changes: 10 additions & 7 deletions src/core/core/settings.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{
"lsp": [
{
"type": "cpp_type",
"program": "clangd",
"arguments": []
}
],
"lsp": {
"enabled": true,
"servers": [
{
"type": "cpp_type",
"program": "clangd",
"arguments": []
}
]
},
"rc": {
"dialog_flags": [
"UpdateGeometry",
Expand Down
20 changes: 13 additions & 7 deletions src/core/knutcore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "knutcore.h"
#include "project.h"
#include "scriptmanager.h"
#include "settings.h"
#include "textdocument.h"

#include <QAbstractItemModel>
Expand All @@ -32,7 +31,7 @@ KnutCore::KnutCore(QObject *parent)
{
// If KnutCore is created directly, it means that we are in a test
// Just initialize all singletons here
initialize(true);
initialize(Settings::Mode::Test);
}

KnutCore::KnutCore(InternalTag, QObject *parent)
Expand All @@ -54,7 +53,7 @@ void KnutCore::process(const QStringList &arguments)

const bool list = parser.isSet("json-list");
if (list) {
initialize(false);
initialize(Settings::Mode::Cli);
auto model = Core::ScriptManager::model();
if (model->rowCount() == 0) {
std::cout << "[]\n";
Expand All @@ -72,8 +71,15 @@ void KnutCore::process(const QStringList &arguments)
exit(0);
}

const bool isTesting = parser.isSet("test");
initialize(isTesting);
Settings::Mode mode;
if (parser.isSet("test"))
mode = Settings::Mode::Test;
else if (parser.isSet("run"))
mode = Settings::Mode::Cli;
else
mode = Settings::Mode::Gui;

initialize(mode);

const QStringList positionalArguments = parser.positionalArguments();
// Set the root directory
Expand Down Expand Up @@ -149,13 +155,13 @@ void KnutCore::doParse(const QCommandLineParser &parser) const
Q_UNUSED(parser)
}

void KnutCore::initialize(bool isTesting)
void KnutCore::initialize(Settings::Mode mode)
{
// Make sure we initialize only once, double initialization could happen in tests
// If creating a KnutCore and then processing command line arguments
if (m_initialized)
return;
new Settings(isTesting, this);
new Settings(mode, this);
new Project(this);
new ScriptManager(this);
if (Core::Settings::instance()->value<bool>(Core::Settings::SaveLogsToFile))
Expand Down
4 changes: 3 additions & 1 deletion src/core/knutcore.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#pragma once

#include "settings.h"

#include <QCommandLineParser>
#include <QObject>

Expand Down Expand Up @@ -37,7 +39,7 @@ class KnutCore : public QObject
virtual void doParse(const QCommandLineParser &parser) const;

private:
void initialize(bool isTesting);
void initialize(Settings::Mode mode);
void initializeMultiSinkLogger();

bool m_initialized = false;
Expand Down
4 changes: 4 additions & 0 deletions src/core/project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ static Document *createDocument(const QString &suffix)

Lsp::Client *Project::getClient(Document::Type type)
{
// Check if we use LSP
if (!Settings::instance()->hasLsp())
return nullptr;

static auto lspServers = Settings::instance()->value<std::vector<LspServer>>(Settings::LspServers);

auto cit = m_lspClients.find(type);
Expand Down
15 changes: 10 additions & 5 deletions src/core/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ static constexpr char SettingsName[] = "knut.json";
* Returns true if Knut is currently in a test, and false otherwise
*/

Settings::Settings(bool isTesting, QObject *parent)
Settings::Settings(Mode mode, QObject *parent)
: QObject(parent)
, m_saveTimer(new QTimer(this))
, m_isTesting(isTesting)
, m_mode(mode)
{
Q_ASSERT(m_instance == nullptr);
m_instance = this;

loadKnutSettings();
if (!m_isTesting) // Only load if not testing
if (!isTesting()) // Only load if not testing
loadUserSettings();

m_saveTimer->callOnTimeout(this, &Settings::saveSettings);
Expand Down Expand Up @@ -252,7 +252,12 @@ QString Settings::logFilePath() const

bool Settings::isTesting() const
{
return m_isTesting;
return (m_mode == Mode::Test);
}

bool Settings::hasLsp() const
{
return m_mode == Mode::Test || (m_mode == Mode::Gui && DEFAULT_VALUE(bool, EnableLSP));
}

void Settings::loadKnutSettings()
Expand All @@ -265,7 +270,7 @@ void Settings::loadKnutSettings()
void Settings::saveSettings()
{
// Don't save settings if testing
if (m_isTesting)
if (isTesting())
return;

const auto &settings = isUser() ? m_userSettings : m_projectSettings;
Expand Down
14 changes: 11 additions & 3 deletions src/core/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ class Settings : public QObject
Q_PROPERTY(bool isTesting READ isTesting CONSTANT)

public:
static inline constexpr char EnableLSP[] = "/lsp/enabled";
static inline constexpr char MimeTypes[] = "/mime_types";
static inline constexpr char LspServers[] = "/lsp";
static inline constexpr char LspServers[] = "/lsp/servers";
static inline constexpr char RcDialogFlags[] = "/rc/dialog_flags";
static inline constexpr char RcDialogScaleX[] = "/rc/dialog_scalex";
static inline constexpr char RcDialogScaleY[] = "/rc/dialog_scaley";
Expand Down Expand Up @@ -104,6 +105,7 @@ class Settings : public QObject
QString logFilePath() const;

bool isTesting() const;
bool hasLsp() const;

public slots:
bool setValue(QString path, const QVariant &value);
Expand All @@ -114,7 +116,13 @@ public slots:
void settingsSaved();

protected:
Settings(bool isTesting, QObject *parent = nullptr);
enum class Mode {
Test,
Cli,
Gui,
};

Settings(Mode mode, QObject *parent = nullptr);

private:
friend class KnutCore;
Expand All @@ -132,7 +140,7 @@ public slots:
nlohmann::json m_projectSettings;
QString m_projectPath;
QTimer *m_saveTimer = nullptr;
bool m_isTesting = true;
Mode m_mode = Mode::Test;
};

} // namespace Core
Expand Down
14 changes: 13 additions & 1 deletion src/gui/optionsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#include "optionsdialog.h"
#include "core/cppdocument_p.h"
#include "core/project.h"
#include "core/rcdocument.h"
#include "core/scriptmanager.h"
#include "core/settings.h"
Expand Down Expand Up @@ -45,6 +44,7 @@ OptionsDialog::OptionsDialog(QWidget *parent)
initializeScriptBehaviorSettings();
initializeRcSettings();
initializeSaveToLogFileSetting();
initializeEnableLSPSetting();

updateScriptPaths();
}
Expand All @@ -64,6 +64,13 @@ void OptionsDialog::initializeSaveToLogFileSetting()
connect(ui->saveLogsToFile, &QCheckBox::toggled, this, &OptionsDialog::changeSaveLogsToFileSetting);
}

void OptionsDialog::initializeEnableLSPSetting()
{
// Enable LSP when running in Gui mode
ui->enableLSP->setChecked(Core::Settings::instance()->value<bool>(Core::Settings::EnableLSP));
connect(ui->enableLSP, &QCheckBox::toggled, this, &OptionsDialog::changeEnableLSPSetting);
}

void OptionsDialog::initializeScriptPathSettings()
{
// User and project paths settings
Expand Down Expand Up @@ -254,6 +261,11 @@ void OptionsDialog::changeSaveLogsToFileSetting()
SET_DEFAULT_VALUE(SaveLogsToFile, ui->saveLogsToFile->checkState() == Qt::Checked);
}

void OptionsDialog::changeEnableLSPSetting()
{
SET_DEFAULT_VALUE(EnableLSP, ui->enableLSP->checkState() == Qt::Checked);
}

void OptionsDialog::changeToggleSectionSetting()
{
auto sectionSettings = DEFAULT_VALUE(Core::ToggleSectionSettings, ToggleSection);
Expand Down
2 changes: 2 additions & 0 deletions src/gui/optionsdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class OptionsDialog : public QDialog
void addSettings(QWidget *widget);

private:
void initializeEnableLSPSetting();
void initializeSaveToLogFileSetting();
void initializeScriptPathSettings();
void initializeScriptBehaviorSettings();
Expand All @@ -48,6 +49,7 @@ class OptionsDialog : public QDialog
void changeAssetColorsSetting();
void changeDialogFlagsSetting();
void changeLanguageMap();
void changeEnableLSPSetting();
void changeSaveLogsToFileSetting();

void changePage();
Expand Down
Loading

0 comments on commit 52b4c34

Please sign in to comment.