wbolster / emacs-python-black

Emacs package to reformat Python using black-macchiato
BSD 3-Clause "New" or "Revised" License
95 stars 11 forks source link

How can I set python-black to format only extra newlines? #9

Closed avatar-lavventura closed 2 years ago

avatar-lavventura commented 2 years ago

I was wondering would it be possible to set python-black only to remove the extra new-lines?

Following link was dead I was not able to open it: https://black.readthedocs.io/en/stable/pyproject_toml.html

wbolster commented 2 years ago

i do not understand your question/problem. please give more details and examples

i've fixed the broken link in the readme in 01f1f4269136cfb36938567854383628730faaab

avatar-lavventura commented 2 years ago

I want to use python-black inside emacs only to fix extra new lines and nothing else, if possible.

Example:

def mkdir(path) -> None:
    if not os.path.isdir(path):
        os.makedirs(path)

def mkdirs(paths) -> None:
    for path in paths:
        mkdir(path)

converted into:

def mkdir(path) -> None:
    if not os.path.isdir(path):
        os.makedirs(path)

def mkdirs(paths) -> None:
    for path in paths:
        mkdir(path)

I just want to use this behavior of python-black inside emacs, is it possible?

wbolster commented 2 years ago

i still don't understand what this means:

and nothing else

this package (or actually, black itself) happily converts your example input into your example output. so what exactly doesn't work which you expect/wish to work?

avatar-lavventura commented 2 years ago

Actually main problem was after each save when python-black reformats a change the cursor location also changes to the location where the format occurs, which makes my job very difficult during coding. For that reason. I just want to apply convert formant only for removing extra new-lines.

# in:

j = [1,
     2,
     3
]

# out:

j = [1, 2, 3]
wbolster commented 2 years ago

your example input is not black compliant so if you run black it will obviously change it. if you don't want that to happen on save, don't use the -on-save mode, e.g. use a shortcut instead to do it manually.

i am not sure what ‘removing extra newlines’ really means, but in any case black is an all-or-nothing approach (but see readme notes on black-macchiato for partial formatting) so doing only a subset of formatting is impossible.