Files
qt-creator/src/libs/languageserverprotocol/servercapabilities.h
David Schulz d17277b546 LSP: reduce error handling complexity
Instead of checking recursively every possible object just check the
required keys for an object and validate it on construction or
assignment from json.

This will reduce the implementation effort for protocol extensions and
also reduce the false positives we might get if the protocol gets
updated.

Change-Id: I3df24e62430d2c7575d26c1581e6a9606e7da4c1
Reviewed-by: hjk <hjk@qt.io>
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
2021-03-02 12:51:47 +00:00

427 lines
20 KiB
C++

/****************************************************************************
**
** 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 "lsptypes.h"
namespace LanguageServerProtocol {
class LANGUAGESERVERPROTOCOL_EXPORT WorkDoneProgressOptions : public JsonObject
{
public:
using JsonObject::JsonObject;
Utils::optional<bool> workDoneProgress() const { return optionalValue<bool>(workDoneProgressKey); }
void setWorkDoneProgress(bool workDoneProgress) { insert(workDoneProgressKey, workDoneProgress); }
void clearWorkDoneProgress() { remove(workDoneProgressKey); }
};
class LANGUAGESERVERPROTOCOL_EXPORT ResolveProviderOption : public JsonObject
{
public:
using JsonObject::JsonObject;
Utils::optional<bool> resolveProvider() const { return optionalValue<bool>(resolveProviderKey); }
void setResolveProvider(bool resolveProvider) { insert(resolveProviderKey, resolveProvider); }
void clearResolveProvider() { remove(resolveProviderKey); }
};
class LANGUAGESERVERPROTOCOL_EXPORT TextDocumentRegistrationOptions : public JsonObject
{
public:
using JsonObject::JsonObject;
LanguageClientArray<DocumentFilter> documentSelector() const
{ return clientArray<DocumentFilter>(documentSelectorKey); }
void setDocumentSelector(const LanguageClientArray<DocumentFilter> &documentSelector)
{ insert(documentSelectorKey, documentSelector.toJson()); }
bool filterApplies(const Utils::FilePath &fileName,
const Utils::MimeType &mimeType = Utils::MimeType()) const;
bool isValid() const override { return contains(documentSelectorKey); }
};
class LANGUAGESERVERPROTOCOL_EXPORT SaveOptions : public JsonObject
{
public:
using JsonObject::JsonObject;
// The client is supposed to include the content on save.
Utils::optional<bool> includeText() const { return optionalValue<bool>(includeTextKey); }
void setIncludeText(bool includeText) { insert(includeTextKey, includeText); }
void clearIncludeText() { remove(includeTextKey); }
};
class LANGUAGESERVERPROTOCOL_EXPORT TextDocumentSyncOptions : public JsonObject
{
public:
using JsonObject::JsonObject;
// Open and close notifications are sent to the server.
Utils::optional<bool> openClose() const { return optionalValue<bool>(openCloseKey); }
void setOpenClose(bool openClose) { insert(openCloseKey, openClose); }
void clearOpenClose() { remove(openCloseKey); }
// Change notifications are sent to the server. See TextDocumentSyncKind.None,
// TextDocumentSyncKind.Full and TextDocumentSyncKind.Incremental.
Utils::optional<int> change() const { return optionalValue<int>(changeKey); }
void setChange(int change) { insert(changeKey, change); }
void clearChange() { remove(changeKey); }
// Will save notifications are sent to the server.
Utils::optional<bool> willSave() const { return optionalValue<bool>(willSaveKey); }
void setWillSave(bool willSave) { insert(willSaveKey, willSave); }
void clearWillSave() { remove(willSaveKey); }
// Will save wait until requests are sent to the server.
Utils::optional<bool> willSaveWaitUntil() const
{ return optionalValue<bool>(willSaveWaitUntilKey); }
void setWillSaveWaitUntil(bool willSaveWaitUntil)
{ insert(willSaveWaitUntilKey, willSaveWaitUntil); }
void clearWillSaveWaitUntil() { remove(willSaveWaitUntilKey); }
// Save notifications are sent to the server.
Utils::optional<SaveOptions> save() const { return optionalValue<SaveOptions>(saveKey); }
void setSave(const SaveOptions &save) { insert(saveKey, save); }
void clearSave() { remove(saveKey); }
};
enum class TextDocumentSyncKind
{
// Documents should not be synced at all.
None = 0,
// Documents are synced by always sending the full content of the document.
Full = 1,
// Documents are synced by sending the full content on open.
// After that only incremental updates to the document are send.
Incremental = 2
};
class LANGUAGESERVERPROTOCOL_EXPORT CodeActionOptions : public WorkDoneProgressOptions
{
public:
using WorkDoneProgressOptions::WorkDoneProgressOptions;
QList<QString> codeActionKinds() const { return array<QString>(codeActionKindsKey); }
void setCodeActionKinds(const QList<QString> &codeActionKinds)
{ insertArray(codeActionKindsKey, codeActionKinds); }
bool isValid() const override;
};
class LANGUAGESERVERPROTOCOL_EXPORT ServerCapabilities : public JsonObject
{
public:
using JsonObject::JsonObject;
// Defines how the host (editor) should sync document changes to the language server.
class LANGUAGESERVERPROTOCOL_EXPORT CompletionOptions : public WorkDoneProgressOptions
{
public:
using WorkDoneProgressOptions::WorkDoneProgressOptions;
// The characters that trigger completion automatically.
Utils::optional<QList<QString>> triggerCharacters() const
{ return optionalArray<QString>(triggerCharactersKey); }
void setTriggerCharacters(const QList<QString> &triggerCharacters)
{ insertArray(triggerCharactersKey, triggerCharacters); }
void clearTriggerCharacters() { remove(triggerCharactersKey); }
Utils::optional<bool> resolveProvider() const { return optionalValue<bool>(resolveProviderKey); }
void setResolveProvider(bool resolveProvider) { insert(resolveProviderKey, resolveProvider); }
void clearResolveProvider() { remove(resolveProviderKey); }
};
class LANGUAGESERVERPROTOCOL_EXPORT SignatureHelpOptions : public WorkDoneProgressOptions
{
public:
using WorkDoneProgressOptions::WorkDoneProgressOptions;
// The characters that trigger signature help automatically.
Utils::optional<QList<QString>> triggerCharacters() const
{ return optionalArray<QString>(triggerCharactersKey); }
void setTriggerCharacters(const QList<QString> &triggerCharacters)
{ insertArray(triggerCharactersKey, triggerCharacters); }
void clearTriggerCharacters() { remove(triggerCharactersKey); }
};
using CodeLensOptions = ResolveProviderOption;
class LANGUAGESERVERPROTOCOL_EXPORT DocumentOnTypeFormattingOptions : public JsonObject
{
public:
using JsonObject::JsonObject;
// A character on which formatting should be triggered, like `}`.
QString firstTriggerCharacter() const { return typedValue<QString>(firstTriggerCharacterKey); }
void setFirstTriggerCharacter(QString firstTriggerCharacter)
{ insert(firstTriggerCharacterKey, firstTriggerCharacter); }
// More trigger characters.
Utils::optional<QList<QString>> moreTriggerCharacter() const
{ return optionalArray<QString>(moreTriggerCharacterKey); }
void setMoreTriggerCharacter(const QList<QString> &moreTriggerCharacter)
{ insertArray(moreTriggerCharacterKey, moreTriggerCharacter); }
void clearMoreTriggerCharacter() { remove(moreTriggerCharacterKey); }
bool isValid() const override { return contains(firstTriggerCharacterKey); }
};
using DocumentLinkOptions = ResolveProviderOption;
class LANGUAGESERVERPROTOCOL_EXPORT ExecuteCommandOptions : public WorkDoneProgressOptions
{
public:
using WorkDoneProgressOptions::WorkDoneProgressOptions;
QList<QString> commands() const { return array<QString>(commandsKey); }
void setCommands(const QList<QString> &commands) { insertArray(commandsKey, commands); }
bool isValid() const override;
};
using ColorProviderOptions = JsonObject;
class LANGUAGESERVERPROTOCOL_EXPORT StaticRegistrationOptions : public JsonObject
{
public:
using JsonObject::JsonObject;
// The id used to register the request. The id can be used to deregister
// the request again. See also Registration#id.
Utils::optional<QString> id() const { return optionalValue<QString>(idKey); }
void setId(const QString &id) { insert(idKey, id); }
void clearId() { remove(idKey); }
};
class LANGUAGESERVERPROTOCOL_EXPORT SemanticHighlightingServerCapabilities : public JsonObject
{
public:
using JsonObject::JsonObject;
Utils::optional<QList<QList<QString>>> scopes() const;
void setScopes(const QList<QList<QString>> &scopes);
bool isValid() const override;
};
// Defines how text documents are synced. Is either a detailed structure defining each
// notification or for backwards compatibility the TextDocumentSyncKind number.
using TextDocumentSync = Utils::variant<TextDocumentSyncOptions, int>;
Utils::optional<TextDocumentSync> textDocumentSync() const;
void setTextDocumentSync(const TextDocumentSync &textDocumentSync);
void clearTextDocumentSync() { remove(textDocumentSyncKey); }
TextDocumentSyncKind textDocumentSyncKindHelper();
// The server provides hover support.
Utils::optional<Utils::variant<bool, WorkDoneProgressOptions>> hoverProvider() const;
void setHoverProvider(const Utils::variant<bool, WorkDoneProgressOptions> &hoverProvider);
void clearHoverProvider() { remove(hoverProviderKey); }
// The server provides completion support.
Utils::optional<CompletionOptions> completionProvider() const
{ return optionalValue<CompletionOptions>(completionProviderKey); }
void setCompletionProvider(const CompletionOptions &completionProvider)
{ insert(completionProviderKey, completionProvider); }
void clearCompletionProvider() { remove(completionProviderKey); }
// The server provides signature help support.
Utils::optional<SignatureHelpOptions> signatureHelpProvider() const
{ return optionalValue<SignatureHelpOptions>(signatureHelpProviderKey); }
void setSignatureHelpProvider(const SignatureHelpOptions &signatureHelpProvider)
{ insert(signatureHelpProviderKey, signatureHelpProvider); }
void clearSignatureHelpProvider() { remove(signatureHelpProviderKey); }
// The server provides goto definition support.
Utils::optional<bool> definitionProvider() const
{ return optionalValue<bool>(definitionProviderKey); }
void setDefinitionProvider(bool definitionProvider)
{ insert(definitionProviderKey, definitionProvider); }
void clearDefinitionProvider() { remove(definitionProviderKey); }
class LANGUAGESERVERPROTOCOL_EXPORT RegistrationOptions : public JsonObject
{
public:
using JsonObject::JsonObject;
LanguageClientArray<DocumentFilter> documentSelector() const
{ return clientArray<DocumentFilter>(documentSelectorKey); }
void setDocumentSelector(const LanguageClientArray<DocumentFilter> &documentSelector)
{ insert(documentSelectorKey, documentSelector.toJson()); }
bool filterApplies(const Utils::FilePath &fileName,
const Utils::MimeType &mimeType = Utils::MimeType()) const;
// The id used to register the request. The id can be used to deregister
// the request again. See also Registration#id.
Utils::optional<QString> id() const { return optionalValue<QString>(idKey); }
void setId(const QString &id) { insert(idKey, id); }
void clearId() { remove(idKey); }
bool isValid() const override { return contains(documentSelectorKey); }
};
// The server provides Goto Type Definition support.
Utils::optional<Utils::variant<bool, RegistrationOptions>> typeDefinitionProvider() const;
void setTypeDefinitionProvider(const Utils::variant<bool, RegistrationOptions> &typeDefinitionProvider);
void clearTypeDefinitionProvider() { remove(typeDefinitionProviderKey); }
// The server provides Goto Implementation support.
Utils::optional<Utils::variant<bool, RegistrationOptions>> implementationProvider() const;
void setImplementationProvider(const Utils::variant<bool, RegistrationOptions> &implementationProvider);
void clearImplementationProvider() { remove(implementationProviderKey); }
// The server provides find references support.
Utils::optional<Utils::variant<bool, WorkDoneProgressOptions>> referencesProvider() const;
void setReferencesProvider(const Utils::variant<bool, WorkDoneProgressOptions> &referencesProvider);
void clearReferencesProvider() { remove(referencesProviderKey); }
// The server provides document highlight support.
Utils::optional<Utils::variant<bool, WorkDoneProgressOptions>> documentHighlightProvider() const;
void setDocumentHighlightProvider(
const Utils::variant<bool, WorkDoneProgressOptions> &documentHighlightProvider);
void clearDocumentHighlightProvider() { remove(documentHighlightProviderKey); }
// The server provides document symbol support.
Utils::optional<Utils::variant<bool, WorkDoneProgressOptions>> documentSymbolProvider() const;
void setDocumentSymbolProvider(Utils::variant<bool, WorkDoneProgressOptions> documentSymbolProvider);
void clearDocumentSymbolProvider() { remove(documentSymbolProviderKey); }
// The server provides workspace symbol support.
Utils::optional<Utils::variant<bool, WorkDoneProgressOptions>> workspaceSymbolProvider() const;
void setWorkspaceSymbolProvider(Utils::variant<bool, WorkDoneProgressOptions> workspaceSymbolProvider);
void clearWorkspaceSymbolProvider() { remove(workspaceSymbolProviderKey); }
// The server provides code actions.
Utils::optional<Utils::variant<bool, CodeActionOptions>> codeActionProvider() const;
void setCodeActionProvider(bool codeActionProvider)
{ insert(codeActionProviderKey, codeActionProvider); }
void setCodeActionProvider(CodeActionOptions options)
{ insert(codeActionProviderKey, options); }
void clearCodeActionProvider() { remove(codeActionProviderKey); }
// The server provides code lens.
Utils::optional<CodeLensOptions> codeLensProvider() const
{ return optionalValue<CodeLensOptions>(codeLensProviderKey); }
void setCodeLensProvider(CodeLensOptions codeLensProvider)
{ insert(codeLensProviderKey, codeLensProvider); }
void clearCodeLensProvider() { remove(codeLensProviderKey); }
// The server provides document formatting.
Utils::optional<Utils::variant<bool, WorkDoneProgressOptions>> documentFormattingProvider() const;
void setDocumentFormattingProvider(
const Utils::variant<bool, WorkDoneProgressOptions> &documentFormattingProvider);
void clearDocumentFormattingProvider() { remove(documentFormattingProviderKey); }
// The server provides document formatting on typing.
Utils::optional<Utils::variant<bool, WorkDoneProgressOptions>> documentRangeFormattingProvider() const;
void setDocumentRangeFormattingProvider(Utils::variant<bool, WorkDoneProgressOptions> documentRangeFormattingProvider);
void clearDocumentRangeFormattingProvider() { remove(documentRangeFormattingProviderKey); }
class LANGUAGESERVERPROTOCOL_EXPORT RenameOptions : public WorkDoneProgressOptions
{
public:
using WorkDoneProgressOptions::WorkDoneProgressOptions;
// Renames should be checked and tested before being executed.
Utils::optional<bool> prepareProvider() const { return optionalValue<bool>(prepareProviderKey); }
void setPrepareProvider(bool prepareProvider) { insert(prepareProviderKey, prepareProvider); }
void clearPrepareProvider() { remove(prepareProviderKey); }
};
// The server provides rename support.
Utils::optional<Utils::variant<RenameOptions, bool>> renameProvider() const;
void setRenameProvider(Utils::variant<RenameOptions,bool> renameProvider);
void clearRenameProvider() { remove(renameProviderKey); }
// The server provides document link support.
Utils::optional<DocumentLinkOptions> documentLinkProvider() const
{ return optionalValue<DocumentLinkOptions>(documentLinkProviderKey); }
void setDocumentLinkProvider(const DocumentLinkOptions &documentLinkProvider)
{ insert(documentLinkProviderKey, documentLinkProvider); }
void clearDocumentLinkProvider() { remove(documentLinkProviderKey); }
// The server provides color provider support.
Utils::optional<Utils::variant<bool, JsonObject>> colorProvider() const;
void setColorProvider(Utils::variant<bool, JsonObject> colorProvider);
void clearColorProvider() { remove(colorProviderKey); }
// The server provides execute command support.
Utils::optional<ExecuteCommandOptions> executeCommandProvider() const
{ return optionalValue<ExecuteCommandOptions>(executeCommandProviderKey); }
void setExecuteCommandProvider(ExecuteCommandOptions executeCommandProvider)
{ insert(executeCommandProviderKey, executeCommandProvider); }
void clearExecuteCommandProvider() { remove(executeCommandProviderKey); }
class LANGUAGESERVERPROTOCOL_EXPORT WorkspaceServerCapabilities : public JsonObject
{
public:
using JsonObject::JsonObject;
class LANGUAGESERVERPROTOCOL_EXPORT WorkspaceFoldersCapabilities : public JsonObject
{
public:
using JsonObject::JsonObject;
// The server has support for workspace folders
Utils::optional<bool> supported() const { return optionalValue<bool>(supportedKey); }
void setSupported(bool supported) { insert(supportedKey, supported); }
void clearSupported() { remove(supportedKey); }
Utils::optional<Utils::variant<QString, bool>> changeNotifications() const;
void setChangeNotifications(Utils::variant<QString, bool> changeNotifications);
void clearChangeNotifications() { remove(changeNotificationsKey); }
};
Utils::optional<WorkspaceFoldersCapabilities> workspaceFolders() const
{ return optionalValue<WorkspaceFoldersCapabilities>(workspaceFoldersKey); }
void setWorkspaceFolders(const WorkspaceFoldersCapabilities &workspaceFolders)
{ insert(workspaceFoldersKey, workspaceFolders); }
void clearWorkspaceFolders() { remove(workspaceFoldersKey); }
};
Utils::optional<WorkspaceServerCapabilities> workspace() const
{ return optionalValue<WorkspaceServerCapabilities>(workspaceKey); }
void setWorkspace(const WorkspaceServerCapabilities &workspace)
{ insert(workspaceKey, workspace); }
void clearWorkspace() { remove(workspaceKey); }
Utils::optional<JsonObject> experimental() const { return optionalValue<JsonObject>(experimentalKey); }
void setExperimental(const JsonObject &experimental) { insert(experimentalKey, experimental); }
void clearExperimental() { remove(experimentalKey); }
Utils::optional<SemanticHighlightingServerCapabilities> semanticHighlighting() const
{ return optionalValue<SemanticHighlightingServerCapabilities>(semanticHighlightingKey); }
void setSemanticHighlighting(const SemanticHighlightingServerCapabilities &semanticHighlighting)
{ insert(semanticHighlightingKey, semanticHighlighting); }
void clearSemanticHighlighting() { remove(semanticHighlightingKey); }
};
} // namespace LanguageClient