From eb8b38cf9ff6f8fc57603283fe9daf7be6b2c977 Mon Sep 17 00:00:00 2001 From: Nicolas Arnaud-Cormos Date: Thu, 25 Sep 2025 11:54:49 +0200 Subject: [PATCH] fix: Fix crash without LSP enabled Crash was happening during edition or hover. --- src/core/codedocument.cpp | 3 +++ src/core/settings.h | 4 ++-- src/gui/textview.cpp | 43 +++++++++++++++++++++------------------ 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/core/codedocument.cpp b/src/core/codedocument.cpp index f8e10f57..b0a6fd6b 100644 --- a/src/core/codedocument.cpp +++ b/src/core/codedocument.cpp @@ -16,6 +16,7 @@ #include "project.h" #include "querymatch.h" #include "rangemark.h" +#include "settings.h" #include "symbol.h" #include "treesitter/predicates.h" #include "utils/log.h" @@ -755,6 +756,8 @@ int CodeDocument::revision() const bool CodeDocument::checkClient() const { Q_ASSERT(textEdit()); + if (!Settings::instance()->hasLsp()) + return false; if (!client()) { spdlog::error("{}: CodeDocument {} has no LSP client - API not available", FUNCTION_NAME, fileName()); return false; diff --git a/src/core/settings.h b/src/core/settings.h index 085de80d..9ddbb7fd 100644 --- a/src/core/settings.h +++ b/src/core/settings.h @@ -1,4 +1,4 @@ -/* +/* This file is part of Knut. SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company @@ -139,7 +139,7 @@ public slots: void saveSettings(); void saveIfApplicable(); bool isUser() const; - void triggerLog(const Utils::LoadJsonStatus &loadJsonStatus, const QString &fileName, const QString &caller); + void triggerLog(const ::Utils::LoadJsonStatus &loadJsonStatus, const QString &fileName, const QString &caller); inline static Settings *m_instance = nullptr; diff --git a/src/gui/textview.cpp b/src/gui/textview.cpp index 06d5968d..2cbf2502 100644 --- a/src/gui/textview.cpp +++ b/src/gui/textview.cpp @@ -1,4 +1,4 @@ -/* +/* This file is part of Knut. SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company @@ -24,6 +24,7 @@ #include #include #include +#include namespace Gui { @@ -143,25 +144,27 @@ bool TextView::eventFilter(QObject *obj, QEvent *event) if (event->type() == QEvent::Paint) updateMarkRect(); if (event->type() == QEvent::ToolTip) { - if (const auto *codedocument = qobject_cast(m_document)) { - if (const auto *helpEvent = dynamic_cast(event)) { - auto cursor = codedocument->textEdit()->cursorForPosition(helpEvent->pos()); - - // Make the textEdit a guarded pointer, as it might have been destroyed once the hover - // callback returns. - QPointer textEdit(codedocument->textEdit()); - QPoint position(helpEvent->globalPos()); - - // Hover spams the log if it doesn't find anything. - // In our case, that's not a problem, so just disable the log. - Core::LoggerDisabler ld; - - codedocument->hover(cursor.position(), [textEdit, position](const auto &hoverText) { - if (!textEdit.isNull() && textEdit->isVisible()) { - QToolTip::showText(position, hoverText, textEdit); - } - }); - return true; + if (Core::Settings::instance()->hasLsp()) { + if (const auto *codedocument = qobject_cast(m_document)) { + if (const auto *helpEvent = dynamic_cast(event)) { + auto cursor = codedocument->textEdit()->cursorForPosition(helpEvent->pos()); + + // Make the textEdit a guarded pointer, as it might have been destroyed once the hover + // callback returns. + QPointer textEdit(codedocument->textEdit()); + QPoint position(helpEvent->globalPos()); + + // Hover spams the log if it doesn't find anything. + // In our case, that's not a problem, so just disable the log. + Core::LoggerDisabler ld; + + codedocument->hover(cursor.position(), [textEdit, position](const auto &hoverText) { + if (!textEdit.isNull() && textEdit->isVisible()) { + QToolTip::showText(position, hoverText, textEdit); + } + }); + return true; + } } } }