forked from qt-creator/qt-creator
Editor: move ownership of assist interface to processor
This way the base class can manage the lifetime of the interface object and it doesn't need to be done in each implementation of perform. Change-Id: Ie1ce742e31b688a337533ee6c57d376146e25ace Reviewed-by: <github-actions-qt-creator@cristianadam.eu> Reviewed-by: Christian Stenger <christian.stenger@qt.io> Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
@@ -293,8 +293,7 @@ LanguageClientCompletionAssistProcessor::~LanguageClientCompletionAssistProcesso
|
||||
|
||||
QTextDocument *LanguageClientCompletionAssistProcessor::document() const
|
||||
{
|
||||
QTC_ASSERT(m_assistInterface, return nullptr);
|
||||
return m_assistInterface->textDocument();
|
||||
return interface()->textDocument();
|
||||
}
|
||||
|
||||
QList<AssistProposalItemInterface *> LanguageClientCompletionAssistProcessor::generateCompletionItems(
|
||||
@@ -314,26 +313,25 @@ static QString assistReasonString(AssistReason reason)
|
||||
return QString("unknown reason");
|
||||
}
|
||||
|
||||
IAssistProposal *LanguageClientCompletionAssistProcessor::perform(AssistInterface *interface)
|
||||
IAssistProposal *LanguageClientCompletionAssistProcessor::perform()
|
||||
{
|
||||
m_assistInterface.reset(interface);
|
||||
QTC_ASSERT(m_client, return nullptr);
|
||||
m_pos = interface->position();
|
||||
m_pos = interface()->position();
|
||||
m_basePos = m_pos;
|
||||
auto isIdentifierChar = [](const QChar &c) { return c.isLetterOrNumber() || c == '_'; };
|
||||
while (m_basePos > 0 && isIdentifierChar(interface->characterAt(m_basePos - 1)))
|
||||
while (m_basePos > 0 && isIdentifierChar(interface()->characterAt(m_basePos - 1)))
|
||||
--m_basePos;
|
||||
if (interface->reason() == IdleEditor) {
|
||||
if (interface()->reason() == IdleEditor) {
|
||||
// Trigger an automatic completion request only when we are on a word with at least n "identifier" characters
|
||||
if (m_pos - m_basePos < TextEditorSettings::completionSettings().m_characterThreshold)
|
||||
return nullptr;
|
||||
if (m_client->documentUpdatePostponed(interface->filePath())) {
|
||||
if (m_client->documentUpdatePostponed(interface()->filePath())) {
|
||||
m_postponedUpdateConnection
|
||||
= QObject::connect(m_client,
|
||||
&Client::documentUpdated,
|
||||
[this, interface](TextEditor::TextDocument *document) {
|
||||
if (document->filePath() == interface->filePath())
|
||||
perform(interface);
|
||||
[this](TextEditor::TextDocument *document) {
|
||||
if (document->filePath() == interface()->filePath())
|
||||
perform();
|
||||
});
|
||||
return nullptr;
|
||||
}
|
||||
@@ -341,9 +339,9 @@ IAssistProposal *LanguageClientCompletionAssistProcessor::perform(AssistInterfac
|
||||
if (m_postponedUpdateConnection)
|
||||
QObject::disconnect(m_postponedUpdateConnection);
|
||||
CompletionParams::CompletionContext context;
|
||||
if (interface->reason() == ActivationCharacter) {
|
||||
if (interface()->reason() == ActivationCharacter) {
|
||||
context.setTriggerKind(CompletionParams::TriggerCharacter);
|
||||
QChar triggerCharacter = interface->characterAt(interface->position() - 1);
|
||||
QChar triggerCharacter = interface()->characterAt(interface()->position() - 1);
|
||||
if (!triggerCharacter.isNull())
|
||||
context.setTriggerCharacter(triggerCharacter);
|
||||
} else {
|
||||
@@ -352,13 +350,14 @@ IAssistProposal *LanguageClientCompletionAssistProcessor::perform(AssistInterfac
|
||||
CompletionParams params;
|
||||
int line;
|
||||
int column;
|
||||
if (!Utils::Text::convertPosition(interface->textDocument(), m_pos, &line, &column))
|
||||
if (!Utils::Text::convertPosition(interface()->textDocument(), m_pos, &line, &column))
|
||||
return nullptr;
|
||||
--line; // line is 0 based in the protocol
|
||||
--column; // column is 0 based in the protocol
|
||||
params.setPosition({line, column});
|
||||
params.setContext(context);
|
||||
params.setTextDocument(TextDocumentIdentifier(DocumentUri::fromFilePath(interface->filePath())));
|
||||
params.setTextDocument(
|
||||
TextDocumentIdentifier(DocumentUri::fromFilePath(interface()->filePath())));
|
||||
if (const int limit = m_client->completionResultsLimit(); limit >= 0)
|
||||
params.setLimit(limit);
|
||||
CompletionRequest completionRequest(params);
|
||||
@@ -368,10 +367,10 @@ IAssistProposal *LanguageClientCompletionAssistProcessor::perform(AssistInterfac
|
||||
m_client->sendMessage(completionRequest);
|
||||
m_client->addAssistProcessor(this);
|
||||
m_currentRequest = completionRequest.id();
|
||||
m_filePath = interface->filePath();
|
||||
m_filePath = interface()->filePath();
|
||||
qCDebug(LOGLSPCOMPLETION) << QTime::currentTime()
|
||||
<< " : request completions at " << m_pos
|
||||
<< " by " << assistReasonString(interface->reason());
|
||||
<< " by " << assistReasonString(interface()->reason());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
@@ -59,7 +59,7 @@ class LANGUAGECLIENT_EXPORT LanguageClientCompletionAssistProcessor
|
||||
public:
|
||||
LanguageClientCompletionAssistProcessor(Client *client, const QString &snippetsGroup);
|
||||
~LanguageClientCompletionAssistProcessor() override;
|
||||
TextEditor::IAssistProposal *perform(TextEditor::AssistInterface *interface) override;
|
||||
TextEditor::IAssistProposal *perform() override;
|
||||
bool running() override;
|
||||
bool needsRestart() const override { return true; }
|
||||
void cancel() override;
|
||||
@@ -74,7 +74,6 @@ protected:
|
||||
private:
|
||||
void handleCompletionResponse(const LanguageServerProtocol::CompletionRequest::Response &response);
|
||||
|
||||
QScopedPointer<const TextEditor::AssistInterface> m_assistInterface;
|
||||
Utils::FilePath m_filePath;
|
||||
QPointer<Client> m_client;
|
||||
std::optional<LanguageServerProtocol::MessageId> m_currentRequest;
|
||||
|
||||
@@ -66,14 +66,13 @@ FunctionHintProcessor::FunctionHintProcessor(Client *client)
|
||||
: m_client(client)
|
||||
{}
|
||||
|
||||
IAssistProposal *FunctionHintProcessor::perform(AssistInterface *interface)
|
||||
IAssistProposal *FunctionHintProcessor::perform()
|
||||
{
|
||||
const QScopedPointer<const AssistInterface> deleter(interface);
|
||||
QTC_ASSERT(m_client, return nullptr);
|
||||
m_pos = interface->position();
|
||||
QTextCursor cursor(interface->textDocument());
|
||||
m_pos = interface()->position();
|
||||
QTextCursor cursor(interface()->textDocument());
|
||||
cursor.setPosition(m_pos);
|
||||
auto uri = DocumentUri::fromFilePath(interface->filePath());
|
||||
auto uri = DocumentUri::fromFilePath(interface()->filePath());
|
||||
SignatureHelpRequest request((TextDocumentPositionParams(TextDocumentIdentifier(uri), Position(cursor))));
|
||||
request.setResponseCallback([this](auto response) { this->handleSignatureResponse(response); });
|
||||
m_client->addAssistProcessor(this);
|
||||
@@ -118,7 +117,7 @@ FunctionHintAssistProvider::FunctionHintAssistProvider(Client *client)
|
||||
, m_client(client)
|
||||
{}
|
||||
|
||||
TextEditor::IAssistProcessor *FunctionHintAssistProvider::createProcessor(
|
||||
IAssistProcessor *FunctionHintAssistProvider::createProcessor(
|
||||
const AssistInterface *) const
|
||||
{
|
||||
return new FunctionHintProcessor(m_client);
|
||||
|
||||
@@ -44,7 +44,7 @@ class LANGUAGECLIENT_EXPORT FunctionHintProcessor : public TextEditor::IAssistPr
|
||||
{
|
||||
public:
|
||||
explicit FunctionHintProcessor(Client *client);
|
||||
TextEditor::IAssistProposal *perform(TextEditor::AssistInterface *interface) override;
|
||||
TextEditor::IAssistProposal *perform() override;
|
||||
bool running() override { return m_currentRequest.has_value(); }
|
||||
bool needsRestart() const override { return true; }
|
||||
void cancel() override;
|
||||
|
||||
@@ -45,13 +45,11 @@ void CommandQuickFixOperation::perform()
|
||||
m_client->executeCommand(m_command);
|
||||
}
|
||||
|
||||
IAssistProposal *LanguageClientQuickFixAssistProcessor::perform(AssistInterface *interface)
|
||||
IAssistProposal *LanguageClientQuickFixAssistProcessor::perform()
|
||||
{
|
||||
m_assistInterface = QSharedPointer<const AssistInterface>(interface);
|
||||
|
||||
CodeActionParams params;
|
||||
params.setContext({});
|
||||
QTextCursor cursor = interface->cursor();
|
||||
QTextCursor cursor = interface()->cursor();
|
||||
if (!cursor.hasSelection()) {
|
||||
if (cursor.atBlockEnd() || cursor.atBlockStart())
|
||||
cursor.select(QTextCursor::LineUnderCursor);
|
||||
@@ -62,7 +60,7 @@ IAssistProposal *LanguageClientQuickFixAssistProcessor::perform(AssistInterface
|
||||
cursor.select(QTextCursor::LineUnderCursor);
|
||||
Range range(cursor);
|
||||
params.setRange(range);
|
||||
auto uri = DocumentUri::fromFilePath(interface->filePath());
|
||||
auto uri = DocumentUri::fromFilePath(interface()->filePath());
|
||||
params.setTextDocument(TextDocumentIdentifier(uri));
|
||||
CodeActionParams::CodeActionContext context;
|
||||
context.setDiagnostics(m_client->diagnosticsAt(uri, cursor));
|
||||
@@ -110,7 +108,7 @@ GenericProposal *LanguageClientQuickFixAssistProcessor::handleCodeActionResult(c
|
||||
else if (auto command = std::get_if<Command>(&item))
|
||||
ops << new CommandQuickFixOperation(*command, m_client);
|
||||
}
|
||||
return GenericProposal::createProposal(m_assistInterface.data(), ops);
|
||||
return GenericProposal::createProposal(interface(), ops);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ class LANGUAGECLIENT_EXPORT LanguageClientQuickFixAssistProcessor
|
||||
public:
|
||||
explicit LanguageClientQuickFixAssistProcessor(Client *client) : m_client(client) {}
|
||||
bool running() override { return m_currentRequest.has_value(); }
|
||||
TextEditor::IAssistProposal *perform(TextEditor::AssistInterface *interface) override;
|
||||
TextEditor::IAssistProposal *perform() override;
|
||||
void cancel() override;
|
||||
|
||||
protected:
|
||||
@@ -76,7 +76,6 @@ private:
|
||||
virtual TextEditor::GenericProposal *handleCodeActionResult(
|
||||
const LanguageServerProtocol::CodeActionResult &result);
|
||||
|
||||
QSharedPointer<const TextEditor::AssistInterface> m_assistInterface;
|
||||
Client *m_client = nullptr; // not owned
|
||||
std::optional<LanguageServerProtocol::MessageId> m_currentRequest;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user