2019-05-17 14:37:06 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2019 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
|
**
|
|
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "languageclientfunctionhint.h"
|
|
|
|
|
#include "client.h"
|
|
|
|
|
|
|
|
|
|
#include <languageserverprotocol/languagefeatures.h>
|
|
|
|
|
#include <texteditor/codeassist/assistinterface.h>
|
|
|
|
|
#include <texteditor/codeassist/functionhintproposal.h>
|
|
|
|
|
#include <texteditor/codeassist/iassistprocessor.h>
|
|
|
|
|
#include <texteditor/codeassist/ifunctionhintproposalmodel.h>
|
|
|
|
|
|
|
|
|
|
using namespace TextEditor;
|
|
|
|
|
using namespace LanguageServerProtocol;
|
|
|
|
|
|
|
|
|
|
namespace LanguageClient {
|
|
|
|
|
|
|
|
|
|
class FunctionHintProposalModel : public IFunctionHintProposalModel
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit FunctionHintProposalModel(SignatureHelp signature)
|
|
|
|
|
: m_sigis(signature)
|
|
|
|
|
{}
|
|
|
|
|
void reset() override {}
|
|
|
|
|
int size() const override
|
|
|
|
|
{ return m_sigis.signatures().size(); }
|
|
|
|
|
QString text(int index) const override;
|
|
|
|
|
|
|
|
|
|
int activeArgument(const QString &/*prefix*/) const override
|
|
|
|
|
{ return m_sigis.activeParameter().value_or(0); }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
LanguageServerProtocol::SignatureHelp m_sigis;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
QString FunctionHintProposalModel::text(int index) const
|
|
|
|
|
{
|
|
|
|
|
return m_sigis.signatures().size() > index ? m_sigis.signatures().at(index).label() : QString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class FunctionHintProcessor : public IAssistProcessor
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit FunctionHintProcessor(Client *client) : m_client(client) {}
|
|
|
|
|
IAssistProposal *perform(const AssistInterface *interface) override;
|
2020-02-12 14:06:45 +01:00
|
|
|
bool running() override { return m_currentRequest.isValid(); }
|
2019-05-17 14:37:06 +02:00
|
|
|
bool needsRestart() const override { return true; }
|
2020-02-12 14:06:45 +01:00
|
|
|
void cancel() override;
|
2019-05-17 14:37:06 +02:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void handleSignatureResponse(const SignatureHelpRequest::Response &response);
|
|
|
|
|
|
|
|
|
|
QPointer<Client> m_client;
|
2020-02-12 14:06:45 +01:00
|
|
|
MessageId m_currentRequest;
|
2019-05-17 14:37:06 +02:00
|
|
|
int m_pos = -1;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
IAssistProposal *FunctionHintProcessor::perform(const AssistInterface *interface)
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(m_client, return nullptr);
|
|
|
|
|
m_pos = interface->position();
|
|
|
|
|
QTextCursor cursor(interface->textDocument());
|
|
|
|
|
cursor.setPosition(m_pos);
|
2019-09-11 14:34:20 +02:00
|
|
|
auto uri = DocumentUri::fromFilePath(Utils::FilePath::fromString(interface->fileName()));
|
2019-05-17 14:37:06 +02:00
|
|
|
SignatureHelpRequest request;
|
|
|
|
|
request.setParams(TextDocumentPositionParams(TextDocumentIdentifier(uri), Position(cursor)));
|
|
|
|
|
request.setResponseCallback([this](auto response) { this->handleSignatureResponse(response); });
|
|
|
|
|
m_client->sendContent(request);
|
2020-02-12 14:06:45 +01:00
|
|
|
m_currentRequest = request.id();
|
2019-05-17 14:37:06 +02:00
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-12 14:06:45 +01:00
|
|
|
void FunctionHintProcessor::cancel()
|
|
|
|
|
{
|
|
|
|
|
if (running()) {
|
|
|
|
|
m_client->cancelRequest(m_currentRequest);
|
2020-03-26 09:21:57 +01:00
|
|
|
m_client->removeAssistProcessor(this);
|
2020-02-12 14:06:45 +01:00
|
|
|
m_currentRequest = MessageId();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 14:37:06 +02:00
|
|
|
void FunctionHintProcessor::handleSignatureResponse(const SignatureHelpRequest::Response &response)
|
|
|
|
|
{
|
2020-02-12 14:06:45 +01:00
|
|
|
m_currentRequest = MessageId();
|
2019-05-17 14:37:06 +02:00
|
|
|
if (auto error = response.error())
|
|
|
|
|
m_client->log(error.value());
|
|
|
|
|
FunctionHintProposalModelPtr model(
|
|
|
|
|
new FunctionHintProposalModel(response.result().value().value()));
|
2020-03-26 09:21:57 +01:00
|
|
|
m_client->removeAssistProcessor(this);
|
2019-05-17 14:37:06 +02:00
|
|
|
setAsyncProposalAvailable(new FunctionHintProposal(m_pos, model));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FunctionHintAssistProvider::FunctionHintAssistProvider(Client *client)
|
2019-09-10 08:03:36 +02:00
|
|
|
: CompletionAssistProvider(client)
|
|
|
|
|
, m_client(client)
|
2019-05-17 14:37:06 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
TextEditor::IAssistProcessor *FunctionHintAssistProvider::createProcessor() const
|
|
|
|
|
{
|
|
|
|
|
return new FunctionHintProcessor(m_client);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IAssistProvider::RunType FunctionHintAssistProvider::runType() const
|
|
|
|
|
{
|
|
|
|
|
return Asynchronous;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int FunctionHintAssistProvider::activationCharSequenceLength() const
|
|
|
|
|
{
|
|
|
|
|
return m_activationCharSequenceLength;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FunctionHintAssistProvider::isActivationCharSequence(
|
|
|
|
|
const QString &sequence) const
|
|
|
|
|
{
|
|
|
|
|
return Utils::anyOf(m_triggerChars,
|
|
|
|
|
[sequence](const QString &trigger) { return trigger.endsWith(sequence); });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FunctionHintAssistProvider::isContinuationChar(const QChar &/*c*/) const
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FunctionHintAssistProvider::setTriggerCharacters(QList<QString> triggerChars)
|
|
|
|
|
{
|
|
|
|
|
m_triggerChars = triggerChars;
|
|
|
|
|
for (const QString &trigger : triggerChars) {
|
|
|
|
|
if (trigger.length() > m_activationCharSequenceLength)
|
|
|
|
|
m_activationCharSequenceLength = trigger.length();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace LanguageClient
|