2019-01-29 13:20:58 +01:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2018 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 "languageclientquickfix.h"
|
|
|
|
|
|
|
|
|
|
#include "client.h"
|
|
|
|
|
#include "languageclientutils.h"
|
|
|
|
|
|
|
|
|
|
#include <texteditor/codeassist/assistinterface.h>
|
|
|
|
|
#include <texteditor/codeassist/genericproposal.h>
|
|
|
|
|
#include <texteditor/quickfix.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using namespace LanguageServerProtocol;
|
|
|
|
|
using namespace TextEditor;
|
|
|
|
|
|
|
|
|
|
namespace LanguageClient {
|
|
|
|
|
|
2021-06-22 14:47:30 +02:00
|
|
|
CodeActionQuickFixOperation::CodeActionQuickFixOperation(const CodeAction &action, Client *client)
|
|
|
|
|
: m_action(action)
|
|
|
|
|
, m_client(client)
|
2019-01-29 13:20:58 +01:00
|
|
|
{
|
2021-06-22 14:47:30 +02:00
|
|
|
setDescription(action.title());
|
|
|
|
|
}
|
2019-01-29 13:20:58 +01:00
|
|
|
|
2021-06-22 14:47:30 +02:00
|
|
|
void CodeActionQuickFixOperation::perform()
|
|
|
|
|
{
|
|
|
|
|
if (!m_client)
|
|
|
|
|
return;
|
|
|
|
|
if (Utils::optional<WorkspaceEdit> edit = m_action.edit())
|
|
|
|
|
applyWorkspaceEdit(m_client, *edit);
|
|
|
|
|
else if (Utils::optional<Command> command = m_action.command())
|
|
|
|
|
m_client->executeCommand(*command);
|
|
|
|
|
}
|
2019-01-29 13:20:58 +01:00
|
|
|
|
2022-05-13 14:57:08 +02:00
|
|
|
CommandQuickFixOperation::CommandQuickFixOperation(const Command &command, Client *client)
|
|
|
|
|
: m_command(command)
|
|
|
|
|
, m_client(client)
|
|
|
|
|
{ setDescription(command.title()); }
|
2019-01-29 13:20:58 +01:00
|
|
|
|
2022-05-13 14:57:08 +02:00
|
|
|
|
|
|
|
|
void CommandQuickFixOperation::perform()
|
|
|
|
|
{
|
|
|
|
|
if (m_client)
|
|
|
|
|
m_client->executeCommand(m_command);
|
|
|
|
|
}
|
2019-01-29 13:20:58 +01:00
|
|
|
|
|
|
|
|
IAssistProposal *LanguageClientQuickFixAssistProcessor::perform(const AssistInterface *interface)
|
|
|
|
|
{
|
|
|
|
|
m_assistInterface = QSharedPointer<const AssistInterface>(interface);
|
|
|
|
|
|
|
|
|
|
CodeActionParams params;
|
|
|
|
|
params.setContext({});
|
|
|
|
|
QTextCursor cursor(interface->textDocument());
|
|
|
|
|
cursor.setPosition(interface->position());
|
|
|
|
|
if (cursor.atBlockEnd() || cursor.atBlockStart())
|
|
|
|
|
cursor.select(QTextCursor::LineUnderCursor);
|
|
|
|
|
else
|
|
|
|
|
cursor.select(QTextCursor::WordUnderCursor);
|
|
|
|
|
if (!cursor.hasSelection())
|
|
|
|
|
cursor.select(QTextCursor::LineUnderCursor);
|
|
|
|
|
Range range(cursor);
|
|
|
|
|
params.setRange(range);
|
2020-09-02 12:29:23 +02:00
|
|
|
auto uri = DocumentUri::fromFilePath(interface->filePath());
|
2020-07-14 14:31:20 +02:00
|
|
|
params.setTextDocument(TextDocumentIdentifier(uri));
|
2019-01-29 13:20:58 +01:00
|
|
|
CodeActionParams::CodeActionContext context;
|
2021-02-01 14:16:34 +01:00
|
|
|
context.setDiagnostics(m_client->diagnosticsAt(uri, cursor));
|
2019-01-29 13:20:58 +01:00
|
|
|
params.setContext(context);
|
|
|
|
|
|
|
|
|
|
CodeActionRequest request(params);
|
|
|
|
|
request.setResponseCallback([this](const CodeActionRequest::Response &response){
|
|
|
|
|
handleCodeActionResponse(response);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
m_client->requestCodeActions(request);
|
2020-02-12 14:06:45 +01:00
|
|
|
m_currentRequest = request.id();
|
2019-01-29 13:20:58 +01:00
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-12 14:06:45 +01:00
|
|
|
void LanguageClientQuickFixAssistProcessor::cancel()
|
|
|
|
|
{
|
|
|
|
|
if (running()) {
|
2022-02-24 09:38:59 +01:00
|
|
|
m_client->cancelRequest(*m_currentRequest);
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-31 13:10:28 +01:00
|
|
|
void LanguageClientQuickFixAssistProcessor::handleCodeActionResponse(const CodeActionRequest::Response &response)
|
2019-01-29 13:20:58 +01:00
|
|
|
{
|
2020-05-14 08:15:39 +02:00
|
|
|
m_currentRequest.reset();
|
2019-01-29 13:20:58 +01:00
|
|
|
if (const Utils::optional<CodeActionRequest::Response::Error> &error = response.error())
|
|
|
|
|
m_client->log(*error);
|
2020-03-26 09:21:57 +01:00
|
|
|
m_client->removeAssistProcessor(this);
|
2022-05-13 14:57:08 +02:00
|
|
|
GenericProposal *proposal = nullptr;
|
|
|
|
|
if (const Utils::optional<CodeActionResult> &result = response.result())
|
|
|
|
|
proposal = handleCodeActionResult(*result);
|
|
|
|
|
setAsyncProposalAvailable(proposal);
|
2022-01-31 13:10:28 +01:00
|
|
|
}
|
|
|
|
|
|
2022-05-13 14:57:08 +02:00
|
|
|
GenericProposal *LanguageClientQuickFixAssistProcessor::handleCodeActionResult(const CodeActionResult &result)
|
2022-01-31 13:10:28 +01:00
|
|
|
{
|
2022-05-13 14:57:08 +02:00
|
|
|
if (auto list = Utils::get_if<QList<Utils::variant<Command, CodeAction>>>(&result)) {
|
|
|
|
|
QuickFixOperations ops;
|
|
|
|
|
for (const Utils::variant<Command, CodeAction> &item : *list) {
|
|
|
|
|
if (auto action = Utils::get_if<CodeAction>(&item))
|
|
|
|
|
ops << new CodeActionQuickFixOperation(*action, m_client);
|
|
|
|
|
else if (auto command = Utils::get_if<Command>(&item))
|
|
|
|
|
ops << new CommandQuickFixOperation(*command, m_client);
|
|
|
|
|
}
|
|
|
|
|
return GenericProposal::createProposal(m_assistInterface.data(), ops);
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
2019-01-29 13:20:58 +01:00
|
|
|
}
|
|
|
|
|
|
2019-09-10 08:03:36 +02:00
|
|
|
LanguageClientQuickFixProvider::LanguageClientQuickFixProvider(Client *client)
|
|
|
|
|
: IAssistProvider(client)
|
|
|
|
|
, m_client(client)
|
2019-01-29 13:20:58 +01:00
|
|
|
{
|
|
|
|
|
QTC_CHECK(client);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IAssistProvider::RunType LanguageClientQuickFixProvider::runType() const
|
|
|
|
|
{
|
|
|
|
|
return Asynchronous;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-15 07:04:12 +02:00
|
|
|
IAssistProcessor *LanguageClientQuickFixProvider::createProcessor(const AssistInterface *) const
|
2019-01-29 13:20:58 +01:00
|
|
|
{
|
|
|
|
|
return new LanguageClientQuickFixAssistProcessor(m_client);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace LanguageClient
|