openlawlibrary / pygls

A pythonic generic language server
https://pygls.readthedocs.io/en/latest/
Apache License 2.0
546 stars 101 forks source link

How to register a command and run it in one step? #476

Open noklam opened 3 weeks ago

noklam commented 3 weeks ago

I found this section in the docs: https://pygls.readthedocs.io/en/v0.10.2/pages/advanced_usage.html#commands The entrypoint is registered in the playground: https://github.com/openlawlibrary/pygls/blob/a40a3c3ad18943503e29cbd61586545974d4dbc8/.vscode/extensions/pygls-playground/package.json#L29-L32

While this works fine, it involves two steps to execute a command:

  1. Execute pygls command
  2. Select the corresponding one

Is there a way to combine these into 1 step? The alternative is using the VSCode API directly https://code.visualstudio.com/api/extension-guides/command, but it has some downside:

  1. I have to write TS, I am much more familiar with Python
  2. It cannot access information on the server side.

It seems like this is possible by modifying https://github.com/openlawlibrary/pygls/blob/a40a3c3ad18943503e29cbd61586545974d4dbc8/.vscode/extensions/pygls-playground/src/extension.ts#L244-L248, is there any tradeoff implementing this on the server side instead of client?

alcarney commented 3 weeks ago

The 2-step process is just a quirk from the fact the playground tries to make it possible to call a command from any server, without knowing up front what those commands are called.

Assuming that your command does not require any arguments, you should be able to replace the value of command in https://github.com/openlawlibrary/pygls/blob/a40a3c3ad18943503e29cbd61586545974d4dbc8/.vscode/extensions/pygls-playground/package.json#L29-L32

with the id of your custom command (the string you pass to @server.command()). It should then be possible to call your command directly from the VSCode command palette.

However, if your command does take arguments or you need to process the return value in any way then I don't think you can avoid writing some TS! 😅