pygls

pygls (pronounced like “pie glass”) is a generic implementation of the Language Server Protocol written in the Python programming language. It allows you to write your own language server in just a few lines of code

from pygls.lsp.server import LanguageServer
from lsprotocol import types

server = LanguageServer("example-server", "v0.1")


@server.feature(
    types.TEXT_DOCUMENT_COMPLETION,
    types.CompletionOptions(trigger_characters=["."]),
)
def completions(params: types.CompletionParams):
    document = server.workspace.get_document(params.text_document.uri)
    current_line = document.lines[params.position.line].strip()

    if not current_line.endswith("hello."):
        return []

    return [
        types.CompletionItem(label="world"),
        types.CompletionItem(label="friend"),
    ]


if __name__ == "__main__":
    server.start_io()

pygls supports

  • Python 3.9+ on Windows, MacOS and Linux

  • Experimental support for Pyodide

  • STDIO, TCP/IP and WEBSOCKET communication

  • Both sync and async styles of programming

  • Running code in background threads

  • Automatic text and notebook document syncronisation