Skip to content

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

Open

Description

@Andrej730

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)

Activity

  1. changed the title [-]Add more low-level LanguageServer.send example to the documentation[/-] [+]Add a more low-level LanguageServer.send example to the documentation[/+] on Mar 23, 2025
  2. LakshyAAAgrawal commented on Mar 31, 2025

    @LakshyAAAgrawal
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions