I have the official black plugin installed in vim. I have in my vimrc:
au FileType python autocmd BufWritePre <buffer> Black
This works fine, but I would like to be able to disable the autoformatting in some situations. I tried this:
au FileType python autocmd BufWritePre <buffer> if get(b:, 'black_enabled', 1) | Black | endif
That successfully disables formatting if I let b:black_enabled=0
, but when black_enabled=1
(or is unset), that results in:
Error detected while processing function black#Black:
line 4:
E684: list index out of range: 1
E684: list index out of range: 1
...and then Black appears to run anyway.
What's wrong with my syntax that it's causing errors?
Black
command defined? Does it have the-bar
attribute? If it doesn't it will see| endif
as its arguments. You can check whether the-bar
attribute was used by running:command Black
and checking if the first column contains a|
.|
was the vimscript command separator (like;
in the shell), so I wouldn't expect a function definition to influence how it behaves. Is that incorrect?|
as a separator; some see it as part of their arguments. Most notorious examples for the latter are:g
and:argdo
,:bufdo
and:windo
.Black()
function. What you need to check is the definition of the:Black
command here. It does not include the-bar
parameter, so:Black
will see|
as part of its arguments.:command! -bar -nargs=* -complete=customlist,BlackComplete Black :call black#Black(<f-args>)
before theau
-command fixes your problem. If yes you can ask the maintainer(s) to include this change.