Skip to content

Add a more low-level LanguageServer.send example to the documentation #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Andrej730 opened this issue Mar 23, 2025 · 1 comment
Open

Comments

@Andrej730
Copy link
Contributor

If I understand it correctly, SyncLanguageServer currently supports only a few API methods directly: request_definition, request_references, request_completions, request_document_symbols, request_hover.

For everyhing else user should use SyncLanguageServer.language_server.server.send.
Would be great to attach some example to the documentation on how to do so. Perhaps in a form of test and just refer to it.

I'll attach a simple example below.

import asyncio
from multilspy import SyncLanguageServer
from multilspy.multilspy_config import MultilspyConfig
from multilspy.multilspy_logger import MultilspyLogger
from multilspy.lsp_protocol_handler.lsp_constants import LSPConstants
from pathlib import Path, PurePath


config = MultilspyConfig.from_dict({"code_language": "python"})
logger = MultilspyLogger()
repository_root_path = r"L:\pomoika\test_files"
lsp = SyncLanguageServer.create(config, logger, repository_root_path)

# Start the language server
with lsp.start_server():
    file_path = "test.py"
    line_number, column_number = 1, 0
    server = lsp.language_server

    async def req_definition():
        with server.open_file(file_path):
            response = await server.server.send.declaration(
                {
                    LSPConstants.TEXT_DOCUMENT: {
                        LSPConstants.URI: Path(str(PurePath(server.repository_root_path, file_path))).as_uri()
                    },
                    LSPConstants.POSITION: {
                        LSPConstants.LINE: line_number,
                        LSPConstants.CHARACTER: column_number,
                    },
                }
            )
            return response

    async def req_declaration():
        with server.open_file(file_path):
            response = await server.server.send.declaration(
                {
                    LSPConstants.TEXT_DOCUMENT: {
                        LSPConstants.URI: Path(str(PurePath(server.repository_root_path, file_path))).as_uri()
                    },
                    LSPConstants.POSITION: {
                        LSPConstants.LINE: line_number,
                        LSPConstants.CHARACTER: column_number,
                    },
                }
            )
            return response

    result = asyncio.run_coroutine_threadsafe(req_definition(), lsp.loop).result(timeout=lsp.timeout)
    # [{'uri': 'file:///l:/pomoika/test_files/test.py', 'range': {'start': {'line': 1, 'character': 0}, 'end': {'line': 1, 'character': 1}}}]
    print(result)
    result = asyncio.run_coroutine_threadsafe(req_declaration(), lsp.loop).result(timeout=lsp.timeout)
    # [{'uri': 'file:///l:/pomoika/test_files/test.py', 'range': {'start': {'line': 1, 'character': 0}, 'end': {'line': 1, 'character': 1}}}]
    print(result)
@Andrej730 Andrej730 changed the title Add more low-level LanguageServer.send example to the documentation Add a more low-level LanguageServer.send example to the documentation Mar 23, 2025
@LakshyAAAgrawal
Copy link
Collaborator

This is a great example. You can create a PR for the same, and I would be happy to add it. It would actually be ideal if we could get all lsp method implemented, but it will take time, and in the meanwhile, this provides a good middle-ground for those still interested in using the multilspy configurations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants