microsoft / vscode-black-formatter

Formatting support for Python using the Black formatter
https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter
MIT License
150 stars 35 forks source link

Support "Format Selection" now that Black 23.11.0 adds a `--line-ranges=` CLI option. #374

Closed yilei closed 9 months ago

yilei commented 11 months ago

https://github.com/psf/black/pull/4020 added a --line-ranges= option to Black and is now released in 23.11.0. See https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#line-ranges

VS Code should now be able to properly support the "Format Selection" command.

Previously related: https://github.com/microsoft/vscode-black-formatter/issues/176

karthiknadig commented 11 months ago

@yilei If you are interested in providing a PR we are open for this. This code here needs to be updated to pass in the line range. https://github.com/microsoft/vscode-black-formatter/blob/b4c47350772ca2f423a66a8c749b374f9c071a10/bundled/tool/lsp_server.py#L105-L111

There is also proposed support for multiple line ranges via format ranges. That support is still not in pygls, that will allow us to add the multi-range support as well.

yilei commented 11 months ago

Thanks for the pointer. Any pointers how to build my local version of the extension and have VS Code use it? The Contributing Guide doesn't mention this.

karthiknadig commented 11 months ago

You can run this to build it locally:

> npm run package
karthiknadig commented 11 months ago

I also update the contributing guide. The requirements were a bit out dated.

yilei commented 11 months ago

@karthiknadig Thanks for updating. But I think the correct command to build the vsix package is npm run vsce-package, took me a while to poke around the source since I'm not familiar with npm.

karthiknadig commented 10 months ago

@yilei Were you able to add this support?

yilei commented 10 months ago

@karthiknadig I'm able to make it work, but I had a hard time writing tests and make the tests run. Should I still send the PR without tests?

karthiknadig commented 10 months ago

Yes. I can help with tests.