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
|
|
|
|
|
{
|
2021-07-09 11:20:27 +02:00
|
|
|
using Parameters = QList<ParameterInformation>;
|
2021-06-21 10:17:21 +02:00
|
|
|
if (index < 0 || m_sigis.signatures().size() <= index)
|
2020-05-14 08:15:39 +02:00
|
|
|
return {};
|
2021-07-09 11:20:27 +02:00
|
|
|
const SignatureInformation signature = m_sigis.signatures().at(index);
|
2021-09-30 07:23:23 +02:00
|
|
|
int parametersIndex = signature.activeParameter().value_or(-1);
|
|
|
|
|
if (parametersIndex < 0) {
|
|
|
|
|
if (index == m_sigis.activeSignature().value_or(-1))
|
|
|
|
|
parametersIndex = m_sigis.activeParameter().value_or(-1);
|
|
|
|
|
}
|
2021-07-09 11:20:27 +02:00
|
|
|
QString label = signature.label();
|
|
|
|
|
if (parametersIndex < 0)
|
|
|
|
|
return label;
|
|
|
|
|
|
|
|
|
|
const QList<QString> parameters = Utils::transform(signature.parameters().value_or(Parameters()),
|
|
|
|
|
&ParameterInformation::label);
|
|
|
|
|
if (parameters.size() <= parametersIndex)
|
|
|
|
|
return label;
|
|
|
|
|
|
|
|
|
|
const QString ¶meterText = parameters.at(parametersIndex);
|
|
|
|
|
const int start = label.indexOf(parameterText);
|
|
|
|
|
const int end = start + parameterText.length();
|
|
|
|
|
return label.mid(0, start).toHtmlEscaped() + "<b>" + parameterText.toHtmlEscaped() + "</b>"
|
|
|
|
|
+ label.mid(end).toHtmlEscaped();
|
2019-05-17 14:37:06 +02:00
|
|
|
}
|
|
|
|
|
|
2021-09-15 11:35:19 +02:00
|
|
|
FunctionHintProcessor::FunctionHintProcessor(Client *client)
|
2021-09-17 11:52:32 +02:00
|
|
|
: m_client(client)
|
|
|
|
|
{}
|
2019-05-17 14:37:06 +02:00
|
|
|
|
|
|
|
|
IAssistProposal *FunctionHintProcessor::perform(const AssistInterface *interface)
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(m_client, return nullptr);
|
|
|
|
|
m_pos = interface->position();
|
|
|
|
|
QTextCursor cursor(interface->textDocument());
|
|
|
|
|
cursor.setPosition(m_pos);
|
2020-09-02 12:29:23 +02:00
|
|
|
auto uri = DocumentUri::fromFilePath(interface->filePath());
|
2020-07-14 14:31:20 +02:00
|
|
|
SignatureHelpRequest request((TextDocumentPositionParams(TextDocumentIdentifier(uri), Position(cursor))));
|
2019-05-17 14:37:06 +02:00
|
|
|
request.setResponseCallback([this](auto response) { this->handleSignatureResponse(response); });
|
2022-05-04 12:56:46 +02:00
|
|
|
m_client->addAssistProcessor(this);
|
2019-05-17 14:37:06 +02:00
|
|
|
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()) {
|
2020-05-14 08:15:39 +02:00
|
|
|
m_client->cancelRequest(m_currentRequest.value());
|
2020-03-26 09:21:57 +01:00
|
|
|
m_client->removeAssistProcessor(this);
|
2020-05-14 08:15:39 +02:00
|
|
|
m_currentRequest.reset();
|
2020-02-12 14:06:45 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 14:37:06 +02:00
|
|
|
void FunctionHintProcessor::handleSignatureResponse(const SignatureHelpRequest::Response &response)
|
|
|
|
|
{
|
2020-05-14 08:15:39 +02:00
|
|
|
m_currentRequest.reset();
|
2019-05-17 14:37:06 +02:00
|
|
|
if (auto error = response.error())
|
|
|
|
|
m_client->log(error.value());
|
2020-03-26 09:21:57 +01:00
|
|
|
m_client->removeAssistProcessor(this);
|
2021-04-21 11:28:39 +02:00
|
|
|
auto result = response.result().value_or(LanguageClientValue<SignatureHelp>());
|
|
|
|
|
if (result.isNull()) {
|
2021-09-15 11:35:19 +02:00
|
|
|
setAsyncProposalAvailable(nullptr);
|
2021-04-21 11:28:39 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const SignatureHelp &signatureHelp = result.value();
|
2020-05-13 15:18:38 +02:00
|
|
|
if (signatureHelp.signatures().isEmpty()) {
|
2021-09-15 11:35:19 +02:00
|
|
|
setAsyncProposalAvailable(nullptr);
|
2020-05-13 15:18:38 +02:00
|
|
|
} else {
|
|
|
|
|
FunctionHintProposalModelPtr model(new FunctionHintProposalModel(signatureHelp));
|
2021-09-15 11:35:19 +02:00
|
|
|
setAsyncProposalAvailable(new FunctionHintProposal(m_pos, model));
|
2020-05-13 15:18:38 +02:00
|
|
|
}
|
2019-05-17 14:37:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
{}
|
|
|
|
|
|
2021-09-15 07:04:12 +02:00
|
|
|
TextEditor::IAssistProcessor *FunctionHintAssistProvider::createProcessor(
|
|
|
|
|
const AssistInterface *) const
|
2019-05-17 14:37:06 +02:00
|
|
|
{
|
2021-09-15 11:35:19 +02:00
|
|
|
return new FunctionHintProcessor(m_client);
|
2019-05-17 14:37:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-22 09:53:50 +01:00
|
|
|
void FunctionHintAssistProvider::setTriggerCharacters(
|
|
|
|
|
const Utils::optional<QList<QString>> &triggerChars)
|
2019-05-17 14:37:06 +02:00
|
|
|
{
|
2021-01-22 09:53:50 +01:00
|
|
|
m_triggerChars = triggerChars.value_or(QList<QString>());
|
|
|
|
|
for (const QString &trigger : qAsConst(m_triggerChars)) {
|
2019-05-17 14:37:06 +02:00
|
|
|
if (trigger.length() > m_activationCharSequenceLength)
|
|
|
|
|
m_activationCharSequenceLength = trigger.length();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace LanguageClient
|