Skip to content
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

Accept code sections in stdin #1372

Closed
Slyces opened this issue Apr 30, 2020 · 3 comments
Closed

Accept code sections in stdin #1372

Slyces opened this issue Apr 30, 2020 · 3 comments
Labels
T: enhancement New feature or request

Comments

@Slyces
Copy link

Slyces commented Apr 30, 2020

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:

  1. Provide an optional range of lines (where formatting should be applied) as it might be done in yapf
  2. 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:

ImportantClass.important_method(exc, limit, lookup_lines, capture_locals, extra_argument, other_argument)

Black formats this line just fine from stdin (assuming $code contains the code):

$ echo "$code" | black -l 80 -q -
ImportantClass.important_method(
    exc, limit, lookup_lines, capture_locals, extra_argument, other_argument
)

Now assume that this line of code is deep inside a complex file of code, and is nested inside a class, and a method.

class A:
    def method(self):
         ...
         ImportantClass.important_method(exc, limit, lookup_lines, capture_locals, extra_argument, other_argument)
        ...

Our line of code is still the same, but is now prefixed with 8 spaces (or two tabs if you prefer).

        ImportantClass.important_method(exc, limit, lookup_lines, capture_locals, extra_argument, other_argument)

Now black is having trouble to process it:

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!

@Slyces Slyces added the T: enhancement New feature or request label Apr 30, 2020
@ichard26
Copy link
Collaborator

Seems similar to #1352.

@Slyces
Copy link
Author

Slyces commented Apr 30, 2020

It totally is a duplicate. Thank you, I will close this issue and comment on the original one.

@Slyces Slyces closed this as completed Apr 30, 2020
@tartley
Copy link

tartley commented Jun 9, 2020

@Slyces see my latest comment on #1352

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants