Protocol

class pygls.protocol.LanguageServerProtocol(server, converter)

A class that represents language server protocol.

It contains implementations for generic LSP features.

workspace

In memory workspace

Type:

Workspace

apply_edit(edit, label=None)

Sends apply edit request to the client.

Parameters:
Return type:

WorkspaceApplyEditResponse

apply_edit_async(edit, label=None)

Sends apply edit request to the client. Should be called with await

Parameters:
Return type:

WorkspaceApplyEditResponse

get_configuration(params, callback=None)

Sends configuration request to the client.

Parameters:
  • params (WorkspaceConfigurationParams) – WorkspaceConfigurationParams from lsp specs

  • callback (callable) – Callabe which will be called after response from the client is received

Returns:

concurrent.futures.Future object that will be resolved once a response has been received

Return type:

Future

get_configuration_async(params)

Calls get_configuration method but designed to use with coroutines

Parameters:

params (WorkspaceConfigurationParams) – WorkspaceConfigurationParams from lsp specs

Returns:

asyncio.Future that can be awaited

Return type:

Future

get_message_type(method)

Return LSP type definitions, as provided by lsprotocol

Parameters:

method (str) –

Return type:

Optional[Type]

get_result_type(method)

Return the type definition of the result associated with the given method.

Parameters:

method (str) –

Return type:

Optional[Type]

log_trace(message, verbose=None)

Sends trace notification to the client.

Parameters:
Return type:

None

lsp_exit(*args)

Stops the server process.

Return type:

None

lsp_initialize(params)

Method that initializes language server. It will compute and return server capabilities based on registered features.

Parameters:

params (InitializeParams) –

Return type:

InitializeResult

lsp_initialized(*args)

Notification received when client and server are connected.

Return type:

None

lsp_notebook_document__did_change(params)

Update a notebook’s contents

Parameters:

params (DidChangeNotebookDocumentParams) –

Return type:

None

lsp_notebook_document__did_close(params)

Remove a notebook document from the workspace.

Parameters:

params (DidCloseNotebookDocumentParams) –

Return type:

None

lsp_notebook_document__did_open(params)

Put a notebook document into the workspace

Parameters:

params (DidOpenNotebookDocumentParams) –

Return type:

None

lsp_set_trace(params)

Changes server trace value.

Parameters:

params (SetTraceParams) –

Return type:

None

lsp_shutdown(*args)

Request from client which asks server to shutdown.

Return type:

None

lsp_text_document__did_change(params)

Updates document’s content. (Incremental(from server capabilities); not configurable for now)

Parameters:

params (DidChangeTextDocumentParams) –

Return type:

None

lsp_text_document__did_close(params)

Removes document from workspace.

Parameters:

params (DidCloseTextDocumentParams) –

Return type:

None

lsp_text_document__did_open(params)

Puts document to the workspace.

Parameters:

params (DidOpenTextDocumentParams) –

Return type:

None

lsp_work_done_progress_cancel(params)

Received a progress cancellation from client.

Parameters:

params (WorkDoneProgressCancelParams) –

Return type:

None

lsp_workspace__did_change_workspace_folders(params)

Adds/Removes folders from the workspace.

Parameters:

params (DidChangeWorkspaceFoldersParams) –

Return type:

None

lsp_workspace__execute_command(params, msg_id)

Executes commands with passed arguments and returns a value.

Parameters:
Return type:

None

publish_diagnostics(params_or_uri, diagnostics=None, version=None, **kwargs)

Sends diagnostic notification to the client.

Deprecated since version 1.0.1: Passing (uri, diagnostics, version) as arguments is deprecated. Pass an instance of PublishDiagnosticParams instead.

Parameters:
register_capability(params, callback=None)

Register a new capability on the client.

Parameters:
  • params (RegistrationParams) – RegistrationParams from lsp specs

  • callback (callable) – Callabe which will be called after response from the client is received

Returns:

concurrent.futures.Future object that will be resolved once a response has been received

Return type:

Future

register_capability_async(params)

Register a new capability on the client.

Parameters:

params (RegistrationParams) – RegistrationParams from lsp specs

Returns:

asyncio.Future object that will be resolved once a response has been received

Return type:

Future

semantic_tokens_refresh(callback=None)

Requesting a refresh of all semantic tokens.

Parameters:

callback (callable) – Callabe which will be called after response from the client is received

Returns:

concurrent.futures.Future object that will be resolved once a response has been received

Return type:

Future

semantic_tokens_refresh_async()

Requesting a refresh of all semantic tokens.

Returns:

asyncio.Future object that will be resolved once a response has been received

Return type:

Future

show_document(params, callback=None)

Display a particular document in the user interface.

Parameters:
  • params (ShowDocumentParams) – ShowDocumentParams from lsp specs

  • callback (callable) – Callabe which will be called after response from the client is received

Returns:

concurrent.futures.Future object that will be resolved once a response has been received

Return type:

Future

show_document_async(params)

Display a particular document in the user interface.

Parameters:

params (ShowDocumentParams) – ShowDocumentParams from lsp specs

Returns:

asyncio.Future object that will be resolved once a response has been received

Return type:

Future

show_message(message, msg_type=MessageType.Info)

Sends message to the client to display message.

show_message_log(message, msg_type=MessageType.Log)

Sends message to the client’s output channel.

unregister_capability(params, callback=None)

Unregister a new capability on the client.

Parameters:
  • params (UnregistrationParams) – UnregistrationParams from lsp specs

  • callback (callable) – Callabe which will be called after response from the client is received

Returns:

concurrent.futures.Future object that will be resolved once a response has been received

Return type:

Future

unregister_capability_async(params)

Unregister a new capability on the client.

Parameters:
  • params (UnregistrationParams) – UnregistrationParams from lsp specs

  • callback (callable) – Callabe which will be called after response from the client is received

Returns:

asyncio.Future object that will be resolved once a response has been received

Return type:

Future

class pygls.protocol.JsonRPCProtocol(server, converter)

Json RPC protocol implementation using on top of asyncio.Protocol .

Specification of the protocol can be found here: https://www.jsonrpc.org/specification

This class provides bidirectional communication which is needed for LSP.

Parameters:

server (LanguageServer) –

connection_lost(exc)

Method from base class, called when connection is lost, in which case we want to shutdown the server’s process as well.

connection_made(transport)

Method from base class, called when connection is established

Parameters:

transport (Transport) –

data_received(data)

Called when some data is received.

The argument is a bytes object.

Parameters:

data (bytes) –

get_message_type(method)

Return the type definition of the message associated with the given method.

Parameters:

method (str) –

Return type:

Optional[Type]

get_result_type(method)

Return the type definition of the result associated with the given method.

Parameters:

method (str) –

Return type:

Optional[Type]

notify(method, params=None)

Sends a JSON RPC notification to the client.

Parameters:

method (str) –

send_request(method, params=None, callback=None, msg_id=None)

Sends a JSON RPC request to the client.

Parameters:
  • method (str) – The method name of the message to send

  • params (any) – The payload of the message

Returns:

Future that will be resolved once a response has been received

send_request_async(method, params=None, msg_id=None)

Calls send_request and wraps concurrent.futures.Future with asyncio.Future so it can be used with await keyword.

Parameters:
  • method (str) – The method name of the message to send

  • params (any) – The payload of the message

  • msg_id (str|int) – Optional, message id

Returns:

asyncio.Future that can be awaited

thread()

Decorator that mark function to execute it in a thread.

pygls.protocol.default_converter()

Default converter factory function.