Files
qt-creator/src/plugins/languageclient/baseclient.h

191 lines
6.9 KiB
C
Raw Normal View History

Introduce a basic client for the language server protocol The language server protocol is used to transport language specific information needed to efficiently edit source files. For example completion, go to operations and symbol information. These information are transferred via JSON-RPC. The complete definition can be found under https://microsoft.github.io/language-server-protocol/specification. This language server protocol support consists of two major parts, the C++ representation of the language server protocol, and the client part for the communication with an external language server. The TypeScript definitions of the protocol interfaces are transferred to C++ classes. Those classes have getter and setter for every interface value. Optional values from the protocol are represented by Utils::optional<ValueType>. The JSON objects that are used to transfer the data between client and server are hidden by a specialized JsonObject class derived from QJsonObject. Additionally this JsonObject provides a validity check that is capable of creating a detailed error message for malformed, or at least unexpected JSON representation of the protocol. The client is the interface between Qt Creator and language server functionality, like completion, diagnostics, document and workspace synchronization. The base client converts the data that is sent from/to the server between the raw byte array and the corresponding C++ objects. The transportat layer is defined in a specialized base client (this initial change will only support stdio language server). The running clients are handled inside the language client manager, which is also used to connect global and exclusive Qt Creator functionality to the clients. Task-number: QTCREATORBUG-20284 Change-Id: I8e123e20c3f14ff7055c505319696d5096fe1704 Reviewed-by: Eike Ziller <eike.ziller@qt.io>
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
#include "dynamiccapabilities.h"
#include "languageclientsettings.h"
#include <coreplugin/id.h>
#include <coreplugin/messagemanager.h>
#include <utils/link.h>
#include <languageserverprotocol/initializemessages.h>
#include <languageserverprotocol/shutdownmessages.h>
#include <languageserverprotocol/textsynchronization.h>
#include <languageserverprotocol/messages.h>
#include <languageserverprotocol/client.h>
#include <languageserverprotocol/languagefeatures.h>
#include <QBuffer>
#include <QHash>
#include <QProcess>
#include <QJsonDocument>
#include <QTextCursor>
namespace Core { class IDocument; }
namespace ProjectExplorer { class Project; }
namespace TextEditor
{
class TextDocument;
class TextEditorWidget;
}
namespace LanguageClient {
class BaseClient : public QObject
{
Q_OBJECT
public:
BaseClient();
~BaseClient() override;
BaseClient(const BaseClient &) = delete;
BaseClient(BaseClient &&) = delete;
BaseClient &operator=(const BaseClient &) = delete;
BaseClient &operator=(BaseClient &&) = delete;
enum State {
Uninitialized,
InitializeRequested,
Initialized,
ShutdownRequested,
Shutdown,
Error
};
void initialize();
void shutdown();
State state() const;
bool reachable() const { return m_state == Initialized; }
// document synchronization
void openDocument(Core::IDocument *document);
void closeDocument(const LanguageServerProtocol::DidCloseTextDocumentParams &params);
bool documentOpen(const LanguageServerProtocol::DocumentUri &uri) const;
void documentContentsSaved(Core::IDocument *document);
void documentWillSave(Core::IDocument *document);
void documentContentsChanged(Core::IDocument *document);
void registerCapabilities(const QList<LanguageServerProtocol::Registration> &registrations);
void unregisterCapabilities(const QList<LanguageServerProtocol::Unregistration> &unregistrations);
bool findLinkAt(LanguageServerProtocol::GotoDefinitionRequest &request);
void requestDocumentSymbols(TextEditor::TextDocument *document);
void cursorPositionChanged(TextEditor::TextEditorWidget *widget);
// workspace control
void projectOpened(ProjectExplorer::Project *project);
void projectClosed(ProjectExplorer::Project *project);
void sendContent(const LanguageServerProtocol::IContent &content);
void sendContent(const LanguageServerProtocol::DocumentUri &uri,
const LanguageServerProtocol::IContent &content);
void cancelRequest(const LanguageServerProtocol::MessageId &id);
void setSupportedLanguages(const QStringList &supportedLanguages);
bool isSupportedLanguage(const QString &language) const;
void setName(const QString &name) { m_displayName = name; }
QString name() const { return m_displayName; }
Core::Id id() const { return m_id; }
virtual bool start() { return true; }
virtual bool matches(const LanguageClientSettings &/*setting*/) { return false; }
virtual void reset();
void log(const QString &message,
Core::MessageManager::PrintToOutputPaneFlag flag = Core::MessageManager::NoModeSwitch);
void log(LanguageServerProtocol::LogMessageParams &message,
Core::MessageManager::PrintToOutputPaneFlag flag = Core::MessageManager::NoModeSwitch);
signals:
void initialized(LanguageServerProtocol::ServerCapabilities capabilities);
void finished();
protected:
void setError(const QString &message);
virtual void sendData(const QByteArray &data) = 0;
void parseData(const QByteArray &data);
private:
void handleResponse(const LanguageServerProtocol::MessageId &id, const QByteArray &content,
QTextCodec *codec);
void handleMethod(const QString &method, LanguageServerProtocol::MessageId id,
const LanguageServerProtocol::IContent *content);
void intializeCallback(const LanguageServerProtocol::InitializeResponse &initResponse);
void shutDownCallback(const LanguageServerProtocol::ShutdownResponse &shutdownResponse);
bool sendWorkspceFolderChanges() const;
using ContentHandler = std::function<void(const QByteArray &, QTextCodec *, QString &,
LanguageServerProtocol::ResponseHandlers,
LanguageServerProtocol::MethodHandler)>;
State m_state = Uninitialized;
QHash<LanguageServerProtocol::MessageId, LanguageServerProtocol::ResponseHandler> m_responseHandlers;
QHash<QByteArray, ContentHandler> m_contentHandler;
QBuffer m_buffer;
QString m_displayName;
QStringList m_supportedLanguageIds;
QList<Utils::FileName> m_openedDocument;
Core::Id m_id;
LanguageServerProtocol::ServerCapabilities m_serverCapabilities;
DynamicCapabilities m_dynamicCapabilities;
LanguageServerProtocol::BaseMessage m_currentMessage;
QHash<LanguageServerProtocol::DocumentUri, LanguageServerProtocol::MessageId> m_highlightRequests;
};
class StdIOClient : public BaseClient
{
Q_OBJECT
public:
StdIOClient(const QString &command, const QStringList &args = QStringList());
~StdIOClient() override;
StdIOClient() = delete;
StdIOClient(const StdIOClient &) = delete;
StdIOClient(StdIOClient &&) = delete;
StdIOClient &operator=(const StdIOClient &) = delete;
StdIOClient &operator=(StdIOClient &&) = delete;
bool start() override;
void setWorkingDirectory(const QString &workingDirectory);
bool matches(const LanguageClientSettings &setting) override;
protected:
void sendData(const QByteArray &data) final;
QProcess m_process;
private:
void readError();
void readOutput();
void onProcessFinished(int exitCode, QProcess::ExitStatus exitStatus);
};
} // namespace LanguageClient