psf/black

vim plugin: provide black-compatible `indentexpr`

Open

#3.151 geöffnet am 1. Juli 2022

Auf GitHub ansehen
 (3 Kommentare) (2 Reaktionen) (0 zugewiesene Personen)Python (2.693 Forks)batch import
C: vimS: acceptedT: enhancementhelp wanted

Repository-Metriken

Stars
 (41.258 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 8T 13h) (33 gemergte PRs in 30 T)

Beschreibung

The way that vim automatically indents python code1 differs from the black format. I'm aware of two differences: (i) lines within parentheses/braces are indented twice rather than once and (ii) closing parentheses/braces are not dedented. These differences can be seen clearly by looking at a function argument list that spans multiple lines. Here is such a list as it is automatically formatted by vim (i.e. typed without hitting tab or backspace):

# $ vim -u /etc/vimrc +'filetype indent on' +'set ft=python'
def foo(
        a,
        b,
        c,
        ):
    pass

Here is the same argument list as it would be formatted by black (ignoring that it would be condensed onto a single line):

def foo(
    a,
    b,
    c,
):
    pass

Multiline list/dict/set comprehensions have the same behavior.

I personally find it annoying that the code I type does not immediately conform to the black style. As such, I think it'd be nice if the vim plugin provided by this repository included an indentexpr that implemented the black indentation rules.

Is there any interest in having something like this? I have some experience writing vim plugins, so I might be able to contribute something, but probably not in the near future.

Alternatively, is there an easy way to get vim to do what I want without having to write a bunch of vimscript code? A brief search didn't turn up any existing plugins or relevant vimrc settings, but this feels like it should be a common desire and I'm half-convinced that I'm missing something simple.

Footnotes

  1. As specified by /usr/share/vim/vim82/indent/python.vim.

Contributor Guide