You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, as far as I can see, the new ruff formatter comparable to black is not yet supported by this plugin. If I understand it correctly, the term "format" in the code of this repo relates to fixing errors. Would it be possible to add formatting (in the sense of black) as a new code action?
Reacted by Fidel Ramos, Matthias Weigand and Ivan Dubrovin
I'm the maintainer for python-lsp-black, feel free to look and borrow its implementation to implement the formatting capabilities.
If nobody else is planning on working on it I will try to find time to implement it, I have been moving into a ruff-exclusive workflow and black was the last piece.
Reacted by Cameron Nemo, Mithrandir2k18 and Ivan Dubrovin
@jhossbach I started work on this, but hit some issues and I'm not sure about the UX we should have.
The plugin already has a format document action to sort the imports, and the format setting is a list of strings (instead of a boolean to enable/disable formatting capabilities). I would repurpose the action to also run ruff format. I think this should be the default behavior, as I expect it to be the common case for ruff users. We can add another setting to allow disabling formatting (if someone wants to keep using black, for example). What should the setting be named if we cannot change format setting to keep backwards compatibility? formatEnabled defaulting to True? formatDisabled defaulting to False?
Another thing I discovered is that ruff format is not sorting imports, and it's done on purpose, see astral-sh/ruff#8087. This means that we will need to call ruff twice: once to fix imports, then again to invoke ruff format. Good news is that the ruff team is working on a combined command to format and sort imports, but it's still a WIP. I might wait until the new command is done to implement it.
One last thing: I think the plugin should disable pylsp_black, yapf and autopep8. python-lsp-black currently disables yapf and autopep8.
I would repurpose the action to also run ruff format
Yes, that sounds like a good idea.
Another thing I discovered is that ruff format is not sorting imports
I am not entirely sure what change in the semantics can happen but I suppose they know what they are doing. If they come up with a solution that formats the code just like running black and isort would do (in addition to the code fixes I suppose?) I am happy.
I think the plugin should disable pylsp_black, yapf and autopep
You're right, also good idea, except it is called black and autopep8 in the configuration I think.
Change is straightforward, just update run_ruff_format to include an extra run_ruff that calls ruff format ...
Maybe not quite so straightforward as run_ruff doesn't appear to support subcommands AFAICT. But hopefully someone can point out where I've missed something?
Change is straightforward, just update run_ruff_format to include an extra run_ruff that calls ruff format ...
Maybe not quite so straightforward as run_ruff doesn't appear to support subcommands AFAICT. But hopefully someone can point out where I've missed something?
You are right, some extra refactor work would be needed on run_ruff. All in all shouldn't be too hard, the existing test suite should help.
You could modify this function and add an argument subcommand that defaults to "check" for ruff check ... unless ruff format is desired. Please check whether the other arguments generated work with it (e.g. ruff format --format=json etc.)
Sliding in here because I think my issue might be related to this discussion. I'm just trying to get ruff to format my files using the black ruleset they reimplemented. Is there a way to set this up yet? All I can get to work is isort.
@Mithrandir2k18 Yes this is your problem. If you're in the mood to test you can try installing python-lsp-ruff from git+https://github.com/mmcshane/python-lsp-ruff.git@mpm/ruff-format which is the PR above.
@Mithrandir2k18 Yes this is your problem. If you're in the mood to test you can try installing python-lsp-ruff from git+https://github.com/mmcshane/python-lsp-ruff.git@mpm/ruff-format which is the PR above.
Activity
[-]Support ´ruff format´[/-][+]Support `ruff format`[/+]jhossbach commentedon Oct 20, 2023
Hi, are you talking about this? https://github.com/astral-sh/ruff/blob/main/crates/ruff_python_formatter/README.md
I was not aware of this feature, but since it is in the
Alphastate I would avoid adding this feature for nowJaRoSchm commentedon Oct 20, 2023
Yes, that's exactly what I mean. As this will probably come up again sooner or later, one could consider adding it behind an environment variable like ruff-lsp does it: https://github.com/astral-sh/ruff-lsp/blob/0bb19b4f56e2c99c7651ac391a964a1c8e59ffa5/ruff_lsp/server.py#L732
JaRoSchm commentedon Oct 24, 2023
Just a short note: The formatter was moved to beta and essentially production-ready today: https://astral.sh/blog/the-ruff-formatter
haplo commentedon Oct 25, 2023
I'm the maintainer for python-lsp-black, feel free to look and borrow its implementation to implement the formatting capabilities.
If nobody else is planning on working on it I will try to find time to implement it, I have been moving into a ruff-exclusive workflow and black was the last piece.
jhossbach commentedon Oct 25, 2023
@haplo I would greatly appreciate you working on it! I am not opposed to the idea but can currently not invest a lot of time into it
haplo commentedon Oct 30, 2023
@jhossbach I started work on this, but hit some issues and I'm not sure about the UX we should have.
The plugin already has a format document action to sort the imports, and the
formatsetting is a list of strings (instead of a boolean to enable/disable formatting capabilities). I would repurpose the action to also runruff format. I think this should be the default behavior, as I expect it to be the common case for ruff users. We can add another setting to allow disabling formatting (if someone wants to keep using black, for example). What should the setting be named if we cannot changeformatsetting to keep backwards compatibility?formatEnableddefaulting toTrue?formatDisableddefaulting toFalse?Another thing I discovered is that
ruff formatis not sorting imports, and it's done on purpose, see astral-sh/ruff#8087. This means that we will need to call ruff twice: once to fix imports, then again to invokeruff format. Good news is that the ruff team is working on a combined command to format and sort imports, but it's still a WIP. I might wait until the new command is done to implement it.One last thing: I think the plugin should disable
pylsp_black,yapfandautopep8. python-lsp-black currently disables yapf and autopep8.jhossbach commentedon Oct 30, 2023
Yes, that sounds like a good idea.
I am not entirely sure what change in the semantics can happen but I suppose they know what they are doing. If they come up with a solution that formats the code just like running
blackandisortwould do (in addition to the code fixes I suppose?) I am happy.You're right, also good idea, except it is called
blackandautopep8in the configuration I think.betaboon commentedon Nov 6, 2023
@haplo is there anything i could help you with, working on this?
haplo commentedon Nov 6, 2023
I put it on pause waiting for the new ruff command that will do formatting and linting, so feel free to work on this.
Change is straightforward, just update
run_ruff_formatto include an extrarun_ruffthat callsruff formatafter the imports have been sorted:python-lsp-ruff/pylsp_ruff/plugin.py
Lines 416 to 434 in be27747
Do add/update tests and documentation too.
@jhossbach I think as part of this work we should update the ruff dependency on python-lsp-ruff to the first ruff version with format support (0.1.2).
You can tag me in the pull request, would be happy to give it a try and review.
mmcshane commentedon Nov 6, 2023
Maybe not quite so straightforward as
run_ruffdoesn't appear to support subcommands AFAICT. But hopefully someone can point out where I've missed something?haplo commentedon Nov 6, 2023
You are right, some extra refactor work would be needed on
run_ruff. All in all shouldn't be too hard, the existing test suite should help.jhossbach commentedon Nov 7, 2023
You could modify this function and add an argument
subcommandthat defaults to "check" forruff check ...unlessruff formatis desired. Please check whether the other arguments generated work with it (e.g.ruff format --format=jsonetc.)Mithrandir2k18 commentedon Nov 9, 2023
Sliding in here because I think my issue might be related to this discussion. I'm just trying to get ruff to format my files using the black ruleset they reimplemented. Is there a way to set this up yet? All I can get to work is isort.
mmcshane commentedon Nov 9, 2023
@Mithrandir2k18 Yes this is your problem. If you're in the mood to test you can try installing python-lsp-ruff from
git+https://github.com/mmcshane/python-lsp-ruff.git@mpm/ruff-formatwhich is the PR above.Mithrandir2k18 commentedon Nov 10, 2023
works like a charm :) here's my pylsp config:
Weirdly enough it doesn't fix missing trailing commas, which it should do, right?
haplo commentedon Nov 13, 2023
I believe it should, yes, same as Black does. Do check out the magic trailing comma docs in case that explains your situation.