Closed gsabran closed 6 months ago
Your server code looks fine, the issue is probably with how the client is interacting with it.
Commands in LSP cannot be called directly, instead the client needs to send a workspace/executeCommand
request, specifying the name of the command it wishes to call.
For example
import asyncio
import sys
from lsprotocol import types
from pygls.lsp.client import BaseLanguageClient
async def main():
client = BaseLanguageClient("client", "v1")
await client.start_io(sys.executable, "test.py")
response = await client.initialize_async(
types.InitializeParams(capabilities=types.ClientCapabilities())
)
available_commands = response.capabilities.execute_command_provider.commands
print(f"Available commands: {available_commands}")
command_name = available_commands[0]
print(f"Calling command: {command_name!r}")
response = await client.workspace_execute_command_async(
types.ExecuteCommandParams(
command=command_name, arguments=[]
)
)
print(f"Result: {response!r}")
await client.shutdown_async(None)
client.exit(None)
await client.stop()
asyncio.run(main())
Saving your code to a file called test.py
I was able to use the code above to get the following result
$ python client.py
Available commands: ['saySomething']
Calling command: 'saySomething'
Result: 'hello'
Hope that helps! :)
You are right, thanks a lot!
I'm trying to register a custom command, following the documentation as well as I can. But when the server receives this command it fails. What should I do differently?
The error log is:
Thanks a lot for the support.