Repository navigation
Open
Description
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)Metadata
Metadata
Assignees
Labels
No labels
Activity
[-]Add more low-level LanguageServer.send example to the documentation[/-][+]Add a more low-level LanguageServer.send example to the documentation[/+]LakshyAAAgrawal commentedon Mar 31, 2025
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.