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
Context
When working on code, I often end up on code-bases that I didn't write. I'd like to be able to reformat only some specific sections of those files.
Possible solutions
The problem being stated above, I see two solutions that could solve it:
Provide an optional range of lines (where formatting should be applied) as it might be done in yapf
Accept partial code from stdin
I won't be commenting on option 1: it has been done elsewhere, and even though it does solve the initial statement of my problem, it doesn't solve it in a really "handy way": you still need to provide the entire file, and figuring out the ranges I'm interested in is not often easy.
Option 2 is more suited to my actual use and would make black more consistent in its handling of stdin (as option 1 doesn't really apply to any stdin input).
In-depth example
Let's take this example, slightly modified from this page of the documentation:
error: cannot format -: cannot use --safe with this file; failed to parse source file. AST error message: unexpected indent (<unknown>, line 1)
Solving it the stdin way
However, black would actually be able to process it if we just handle those spaces and the expected line length (80 -> 72):
$ echo "$line" | sed 's/^ //' | black -q -l 72 - | sed 's/^/ /'
ImportantClass.important_method(
exc,
limit,
lookup_lines,
capture_locals,
extra_argument,
other_argument,
)
Please note that the formatting changed, as we have less line length available.
Assuming that the first line represents the base indentation level (which seems a reasonable assumption), this easy hack shows that black can actually do it with a little help.
A more elaborate script to detect indentation level and use some kind of sed magic could do it (and if you have one I'd be happy to use it for now) but it feels a bit weird for something so simple.
Additional information
For those interested, this problem originates from using black as a filter inside vim, giving a sample of code as input and replacing that sample with black's output.
Obviously, if anyone has interesting arguments as to why this behavior does not make sense, do not hesitate!
The text was updated successfully, but these errors were encountered:
Context
When working on code, I often end up on code-bases that I didn't write. I'd like to be able to reformat only some specific sections of those files.
Possible solutions
The problem being stated above, I see two solutions that could solve it:
I won't be commenting on option 1: it has been done elsewhere, and even though it does solve the initial statement of my problem, it doesn't solve it in a really "handy way": you still need to provide the entire file, and figuring out the ranges I'm interested in is not often easy.
Option 2 is more suited to my actual use and would make black more consistent in its handling of stdin (as option 1 doesn't really apply to any stdin input).
In-depth example
Let's take this example, slightly modified from this page of the documentation:
Black formats this line just fine from stdin (assuming
$code
contains the code):Now assume that this line of code is deep inside a complex file of code, and is nested inside a class, and a method.
Our line of code is still the same, but is now prefixed with 8 spaces (or two tabs if you prefer).
Now black is having trouble to process it:
Solving it the stdin way
However, black would actually be able to process it if we just handle those spaces and the expected line length (80 -> 72):
Please note that the formatting changed, as we have less line length available.
Assuming that the first line represents the base indentation level (which seems a reasonable assumption), this easy hack shows that black can actually do it with a little help.
A more elaborate script to detect indentation level and use some kind of sed magic could do it (and if you have one I'd be happy to use it for now) but it feels a bit weird for something so simple.
Additional information
For those interested, this problem originates from using black as a filter inside vim, giving a sample of code as input and replacing that sample with black's output.
Obviously, if anyone has interesting arguments as to why this behavior does not make sense, do not hesitate!
The text was updated successfully, but these errors were encountered: