Federico Bonelli
Federico Bonelli

Reputation: 849

Python client library for Language Server Protocol (LSP)

I need to interact with a language server (Eclipse JDT LS) implementing the Language Server Protocol (LSP), and I need it for a Python project that will be used to analyze different programming languages, starting from Java.

I've been looking around for a client library in Python that would fit my purpose, any suggestions?

If it was very easy I wouldn't mind writing one, but I feel the world doesn't need yet another library if a good one exists already.

Upvotes: 5

Views: 3399

Answers (2)

LakshyAAAgrawal
LakshyAAAgrawal

Reputation: 85

I am the author of multilspy, which is a LSP client in Python, with a library interface and is intended to be used to build applications around language servers. It handles the configuration and initialization of different language servers, and offers a simple interface. It currently supports running Eclipse JDT.LS for Java, rust-analyzer for Rust, OmniSharp for C# and jedi-language-server for Python. You can install it using pip by running:

pip install multilspy

Example usage of multilspy:

from multilspy import SyncLanguageServer
from multilspy.multilspy_config import MultilspyConfig
from multilspy.multilspy_logger import MultilspyLogger
...
config = MultilspyConfig.from_dict({"code_language": "java"}) # Also supports "python", "rust", "csharp"
logger = MultilspyLogger()
lsp = SyncLanguageServer.create(config, logger, "/abs/path/to/project/root/")
with lsp.start_server():
    result = lsp.request_definition(
        "relative/path/to/code_file.java", # Filename of location where request is being made
        163, # line number of symbol for which request is being made
        4 # column number of symbol for which request is being made
    )
    result2 = lsp.request_completions(
        ...
    )
    result3 = lsp.request_references(
        ...
    )
    ...

Upvotes: 6

Federico Bonelli
Federico Bonelli

Reputation: 849

I've discovered that, as of July 2023, there is a library named pygls (https://github.com/openlawlibrary/pygls) that is currently developing a usable client for the Language Server Protocol (LSP).

While we wait for the first official release, I've included it in my setup.py file under "install_requires". This ensures it will be automatically installed during the setup of my project. Here's how I modified the file:

install_requires=[
    'pygls @ git+https://github.com/openlawlibrary/pygls.git'
]

With this line of code, pygls will be directly fetched from the GitHub repository and installed into your environment.

Upvotes: 2

Related Questions