CodeAssistant: add cancel to asynchronous processors

Fixes crash when the processor reports a result after the code assistant
was destroyed.

Change-Id: I8588d3d6acad69f1ec6302e8ba09d642ebbb77f1
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
David Schulz
2020-02-12 14:06:45 +01:00
parent 86fae567fa
commit 552ccd6a61
6 changed files with 52 additions and 16 deletions

View File

@@ -65,14 +65,15 @@ class FunctionHintProcessor : public IAssistProcessor
public:
explicit FunctionHintProcessor(Client *client) : m_client(client) {}
IAssistProposal *perform(const AssistInterface *interface) override;
bool running() override { return m_running; }
bool running() override { return m_currentRequest.isValid(); }
bool needsRestart() const override { return true; }
void cancel() override;
private:
void handleSignatureResponse(const SignatureHelpRequest::Response &response);
QPointer<Client> m_client;
bool m_running = false;
MessageId m_currentRequest;
int m_pos = -1;
};
@@ -87,13 +88,21 @@ IAssistProposal *FunctionHintProcessor::perform(const AssistInterface *interface
request.setParams(TextDocumentPositionParams(TextDocumentIdentifier(uri), Position(cursor)));
request.setResponseCallback([this](auto response) { this->handleSignatureResponse(response); });
m_client->sendContent(request);
m_running = true;
m_currentRequest = request.id();
return nullptr;
}
void FunctionHintProcessor::cancel()
{
if (running()) {
m_client->cancelRequest(m_currentRequest);
m_currentRequest = MessageId();
}
}
void FunctionHintProcessor::handleSignatureResponse(const SignatureHelpRequest::Response &response)
{
m_running = false;
m_currentRequest = MessageId();
if (auto error = response.error())
m_client->log(error.value());
FunctionHintProposalModelPtr model(