Feature request: "black" compatible python line-splitting
lukelbd opened this issue · 3 comments
It would be really nice to have an option for line splitting compatible with the popular "black" format for python code. Currently with let g:splitjoin_python_brackets_on_separate_lines = 1
and the following code snippet:
args = (variable_name_1, variable_name_2, really_long_variable_name_3)
pressing gS
formats the code with a non-standard indentation determined by the bracket position:
args = (
variable_name_1,
variable_name_2,
really_long_variable_name_3,
)
However, the "black" format uses a standard indentation amount and puts the closing bracket on the starting indentation level (note this style is also pep8-compatible):
args = (
variable_name_1,
variable_name_2,
really_long_variable_name_3,
)
Actually I see indentation is not based on the starting line -- it's just that the lines are overindented by one. For example this:
args = foobarbaz(1, 2, 3, 4)
gets formatted as follows:
args = foobarbaz(
1,
2,
3,
4,
)
This is incompatible with pep8 (error message E126: continuation line over-indented for hanging indent
).
...maybe this is not expected behavior / something wrong with my configuration?
I try to avoid applying any manual indentation when splitting, other than in specific circumstances. Looking at the python support, I don't think I apply anything special in these cases either, other than using Vim's built-in indentation.
For the "black" brackets, could you try installing this plugin and see if it takes care of the indent? https://github.com/jeetsukumaran/vim-python-indent-black
This is incompatible with pep8 (error message E126: continuation line over-indented for hanging indent).
I would assume this is another issue of the built-in indentation. I don't know if there's any alternatives out there for this kind of thing.
I can add some code to apply the right indent in such cases, controlled by a setting. But reindenting will undo it -- in the second example, even if I shift the 1, 2, 3, 4 to the left, if I then run ==
on the 1,
line, it gets shifted to its previous position. This might be fine, since I think python is kind of tricky to auto-indent anyway, so maybe splitjoin could do the pep8 thing and then just... you don't reindent it 😅. But before I work on that, could you investigate options for changing the indent expression?
The built-in is support is here: https://github.com/vim/vim/blob/master/runtime/indent/python.vim. I see a few settings (look for "pyindent" in the file), although I'm not sure if any of them will change this stuff. Since the file is currently maintained by Bram himself, it might be a good idea, regardless of what we do here, to open an issue on the Vim repo if there isn't one already (I didn't see one with a quick search, at least).
Appreciate the quick response! The plugin you linked worked perfectly -- the above example gets formatted as:
args = foobarbaz(
1,
2,
3,
4,
)
I also found this pep8-style indent plugin that could be useful for people who don't use black. Don't think you need to do anything else -- this solution is simple enough, and working around indentexpr
quirks for every single language seems like it's beyond any reasonable scope for this project :). Thanks for the help and the cool plugin, will use it a lot more now.
And agree re: starting a thread in the vim repo, would love to see python's default auto-indentation improved (pep8 is probably more reasonable than black, since black is a strict subset of pep8 adopted by only a subset of the community while pep8 seems to be nearly universally accepted).