-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use new lines
option in Black 23.11 to format range
#52
Conversation
@@ -64,29 +64,33 @@ def pylsp_settings(): | |||
|
|||
|
|||
def format_document(client_config, document, range=None): | |||
text = document.source | |||
config = load_config(document.path, client_config) | |||
lines = [(range["start"]["line"] + 1, range["end"]["line"])] if range else () |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Black says "indices are 1-based and inclusive on both ends" (https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#line-ranges), range
uses a 0-based index.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also black says lines
is a list of tuple but uses a tuple as default argument in https://github.com/psf/black/pull/4020/files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Black says "indices are 1-based and inclusive on both ends" (https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#line-ranges),
range
uses a 0-based index.
Can you add this as a comment in the code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just added.
@@ -1,2 +1,3 @@ | |||
a = 'hello' | |||
b = 42 | |||
b = ["a", "very", "very", "very", "very", "very", "very", "very", "very", "long", "line"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not that long but long enough for black :) I added this line in the middle of the script to make sure we can format a selection in the middle of the text, that results in more lines in the final text.
This should also fix #42. |
@remisalmon, please merge with master or rebase on top of it to get the changes in PR #53, which are necessary to increase our required Black version to 23.11.0 |
@remisalmon, could you fix the small linting errors reported by Flake8 so we can merge this one? Thanks! |
So this one is due to the long line formatting test and cannot be reformatted by black (this is very meta). I disabled the flake8 E501 line too long error on those 2 lines, let me know if that works. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @remisalmon for this great improvement!
lines
option in Black 23.11 to format range
That's awesome, thank you so much! |
Possible fix for spyder-ide/spyder#21460 where formatting a text selection with black does not work if the code is intended due to black requiring the entire text as input.
This is using the new
lines
option added in black 23.11.0 that allows formatting only a range of lines in the text: https://github.com/psf/black/releases/tag/23.11.0Doc:
Fixes #42