2018-07-13 12:33:46 +02: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.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2021-09-02 08:20:34 +02:00
|
|
|
#include "languageclient_global.h"
|
|
|
|
|
|
2021-06-18 16:30:03 +02:00
|
|
|
#include <languageserverprotocol/completion.h>
|
2018-07-13 12:33:46 +02:00
|
|
|
#include <texteditor/codeassist/completionassistprovider.h>
|
2021-09-02 08:20:34 +02:00
|
|
|
#include <texteditor/codeassist/iassistprocessor.h>
|
2018-07-13 12:33:46 +02:00
|
|
|
|
2021-01-20 12:46:07 +01:00
|
|
|
#include <utils/optional.h>
|
|
|
|
|
|
2021-09-02 13:13:15 +02:00
|
|
|
#include <QPointer>
|
|
|
|
|
|
2021-06-18 16:30:03 +02:00
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
|
|
namespace TextEditor {
|
|
|
|
|
class IAssistProposal;
|
|
|
|
|
class TextDocumentManipulatorInterface;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-13 12:33:46 +02:00
|
|
|
namespace LanguageClient {
|
|
|
|
|
|
2019-01-31 12:15:43 +01:00
|
|
|
class Client;
|
2018-07-13 12:33:46 +02:00
|
|
|
|
2021-06-18 16:30:03 +02:00
|
|
|
using CompletionItemsTransformer = std::function<QList<LanguageServerProtocol::CompletionItem>(
|
|
|
|
|
const Utils::FilePath &, const QString &, int,
|
|
|
|
|
const QList<LanguageServerProtocol::CompletionItem> &)>;
|
|
|
|
|
using CompletionApplyHelper = std::function<void(
|
|
|
|
|
const LanguageServerProtocol::CompletionItem &,
|
|
|
|
|
TextEditor::TextDocumentManipulatorInterface &, QChar)>;
|
|
|
|
|
using ProposalHandler = std::function<void(TextEditor::IAssistProposal *)>;
|
|
|
|
|
|
2021-09-02 08:20:34 +02:00
|
|
|
class LANGUAGECLIENT_EXPORT LanguageClientCompletionAssistProvider
|
|
|
|
|
: public TextEditor::CompletionAssistProvider
|
2018-07-13 12:33:46 +02:00
|
|
|
{
|
2019-09-10 08:03:36 +02:00
|
|
|
Q_OBJECT
|
|
|
|
|
|
2018-07-13 12:33:46 +02:00
|
|
|
public:
|
2019-01-31 12:15:43 +01:00
|
|
|
LanguageClientCompletionAssistProvider(Client *client);
|
2018-07-13 12:33:46 +02:00
|
|
|
|
|
|
|
|
TextEditor::IAssistProcessor *createProcessor() const override;
|
|
|
|
|
RunType runType() const override;
|
|
|
|
|
|
|
|
|
|
int activationCharSequenceLength() const override;
|
|
|
|
|
bool isActivationCharSequence(const QString &sequence) const override;
|
|
|
|
|
bool isContinuationChar(const QChar &) const override { return true; }
|
|
|
|
|
|
2021-01-20 12:46:07 +01:00
|
|
|
void setTriggerCharacters(const Utils::optional<QList<QString>> triggerChars);
|
2018-07-13 12:33:46 +02:00
|
|
|
|
2021-06-18 16:30:03 +02:00
|
|
|
void setProposalHandler(const ProposalHandler &handler) { m_proposalHandler = handler; }
|
|
|
|
|
void setSnippetsGroup(const QString &group) { m_snippetsGroup = group; }
|
|
|
|
|
|
2021-09-13 16:09:04 +02:00
|
|
|
protected:
|
|
|
|
|
void setItemsTransformer(const CompletionItemsTransformer &transformer);
|
|
|
|
|
void setApplyHelper(const CompletionApplyHelper &applyHelper);
|
|
|
|
|
Client *client() const { return m_client; }
|
|
|
|
|
|
2018-07-13 12:33:46 +02:00
|
|
|
private:
|
|
|
|
|
QList<QString> m_triggerChars;
|
2021-06-18 16:30:03 +02:00
|
|
|
CompletionItemsTransformer m_itemsTransformer;
|
|
|
|
|
CompletionApplyHelper m_applyHelper;
|
|
|
|
|
ProposalHandler m_proposalHandler;
|
|
|
|
|
QString m_snippetsGroup;
|
2018-07-13 12:33:46 +02:00
|
|
|
int m_activationCharSequenceLength = 0;
|
2019-01-29 13:20:58 +01:00
|
|
|
Client *m_client = nullptr; // not owned
|
2018-07-13 12:33:46 +02:00
|
|
|
};
|
|
|
|
|
|
2021-09-02 08:20:34 +02:00
|
|
|
class LANGUAGECLIENT_EXPORT LanguageClientCompletionAssistProcessor
|
|
|
|
|
: public TextEditor::IAssistProcessor
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
LanguageClientCompletionAssistProcessor(Client *client,
|
|
|
|
|
const CompletionItemsTransformer &itemsTransformer,
|
|
|
|
|
const CompletionApplyHelper &applyHelper,
|
|
|
|
|
const ProposalHandler &proposalHandler,
|
|
|
|
|
const QString &snippetsGroup);
|
|
|
|
|
~LanguageClientCompletionAssistProcessor() override;
|
|
|
|
|
TextEditor::IAssistProposal *perform(const TextEditor::AssistInterface *interface) override;
|
|
|
|
|
bool running() override;
|
|
|
|
|
bool needsRestart() const override { return true; }
|
|
|
|
|
void cancel() override;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void handleCompletionResponse(const LanguageServerProtocol::CompletionRequest::Response &response);
|
|
|
|
|
|
|
|
|
|
QPointer<QTextDocument> m_document;
|
|
|
|
|
Utils::FilePath m_filePath;
|
|
|
|
|
QPointer<Client> m_client;
|
|
|
|
|
Utils::optional<LanguageServerProtocol::MessageId> m_currentRequest;
|
|
|
|
|
QMetaObject::Connection m_postponedUpdateConnection;
|
|
|
|
|
const CompletionItemsTransformer m_itemsTransformer;
|
|
|
|
|
const CompletionApplyHelper m_applyHelper;
|
|
|
|
|
const ProposalHandler m_proposalHandler;
|
|
|
|
|
const QString m_snippetsGroup;
|
|
|
|
|
int m_pos = -1;
|
|
|
|
|
int m_basePos = -1;
|
|
|
|
|
};
|
|
|
|
|
|
2018-07-13 12:33:46 +02:00
|
|
|
} // namespace LanguageClient
|