python-lsp/python-lsp-ruff

Surface Ruff auto-fix capabilities as Quick Fix actions

charliermarsh opened this issue ยท 14 comments

Surface Ruff auto-fix capabilities as Quick Fix actions

I don't think pylsp supports quick fix actions but it supports documentFormatting. On that side note, we need to add --no-fix to the ruff call for linting because otherwise ruff will return the new file instead of json dict with diagnostics.

Actually, I just went through the log of pylsp and found this:

2022-12-11 16:48:17,187 CET - INFO - pylsp.python_lsp - Server capabilities: {'codeActionProvider': True,  ... 

It at least says that codeactions are available, but I need to look into that.

I think it does, they are called code actions. For example, they are used by pylsp-rope.

I think it does, they are called code actions. For example, they are used by pylsp-rope.

Can we add it as a feature to https://github.com/python-lsp/python-lsp-server#lsp-server-features?

Yeah sorry, I believe Quick Fix is a โ€œkindโ€ of Code Action.

Can we add it as a feature to https://github.com/python-lsp/python-lsp-server#lsp-server-features?

Sure, please open a pull request for that and I'll merge it.

On that side note, we need to add --no-fix to the ruff call for linting because otherwise ruff will return the new file instead of json dict with diagnostics.

Yeah we should pass --no-fix, that sounds right.

If I understand correctly, then in the future it is planned for the plugin to provided fixes from ruff as lsp actions, right?

That'd be great! since, if I'm correct, python-lsp-server and most of other plugins don't provide functionality for that (ex. simple stuff like removing trailing whitespaces, etc), and it's very tiresome to fix all that stuff by hand when it could easily be done by the lsp.

I have been letting it slide for a while since there seemed to be not much of interest, but I can start implementing it now. Should this be done when applying textDocument/formatting or should it be textDocument/codeAction only?

I think it should be implemented as code actions, not auto-formatting, so that users can opt in into the suggested fixes.

Furthermore, I think most people understand auto-formatting as providing purely stylistic rewriting to make the code look better, not actual changes without their consent.

I agree ๐Ÿ‘ I wonder though, are there any methods in the lsp reference to apply all code actions?

Don't know about that, sorry.

I agree ๐Ÿ‘ I wonder though, are there any methods in the lsp reference to apply all code actions?

I don't know much about lsp's but if it is of any help, this is an example of what hls (the haskell lsp) does in that regard :)

Screenshot 2023-02-10 at 7 35 16 p m

fyi: i started implementing this in #22