LanguageClient: guard client access in formatter

Change-Id: I76bd41b3a786b1893bc159e0363f90494fd14b9f
Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
David Schulz
2022-09-27 13:12:20 +02:00
parent 99589a7572
commit 2710e68610
2 changed files with 7 additions and 4 deletions

View File

@@ -49,6 +49,7 @@ static const FormattingOptions formattingOptions(const TextEditor::TabSettings &
QFutureWatcher<ChangeSet> *LanguageClientFormatter::format(
const QTextCursor &cursor, const TextEditor::TabSettings &tabSettings)
{
QTC_ASSERT(m_client, return nullptr);
cancelCurrentRequest();
m_progress = QFutureInterface<ChangeSet>();
@@ -101,7 +102,7 @@ QFutureWatcher<ChangeSet> *LanguageClientFormatter::format(
void LanguageClientFormatter::cancelCurrentRequest()
{
if (m_currentRequest.has_value()) {
if (QTC_GUARD(m_client) && m_currentRequest.has_value()) {
m_progress.reportCanceled();
m_progress.reportFinished();
m_client->cancelRequest(*m_currentRequest);
@@ -113,8 +114,8 @@ void LanguageClientFormatter::cancelCurrentRequest()
void LanguageClientFormatter::handleResponse(const DocumentRangeFormattingRequest::Response &response)
{
m_currentRequest = std::nullopt;
if (const std::optional<DocumentRangeFormattingRequest::Response::Error> &error = response
.error())
const std::optional<DocumentRangeFormattingRequest::Response::Error> &error = response.error();
if (QTC_GUARD(m_client) && error)
m_client->log(*error);
ChangeSet changeSet;
if (std::optional<LanguageClientArray<TextEdit>> result = response.result()) {

View File

@@ -7,6 +7,8 @@
#include <texteditor/formatter.h>
#include <QPointer>
namespace TextEditor { class TextDocument; }
namespace LanguageClient {
@@ -26,7 +28,7 @@ private:
void handleResponse(
const LanguageServerProtocol::DocumentRangeFormattingRequest::Response &response);
Client *m_client = nullptr; // not owned
QPointer<Client> m_client = nullptr; // not owned
QMetaObject::Connection m_cancelConnection;
TextEditor::TextDocument *m_document; // not owned
bool m_ignoreCancel = false;