I'm looking for a minimal way to convert code that looks like this:
my_function(param1, param2=value2, param4=a_function_call())
into something like:
my_function(
param1,
param2=value2,
param4=a_function_call(),
)
I'm sure there's a plugin that could work, but that seems pretty overkill for what can probably just be done with a function and a mapping, or possibly even just a clever regex. Also this often comes up in situations where I don't have a LSP/linter/formatter set up (like typing markdown snippets or in languages I don't use very often) so "just use formatting tool X" doesn't really cover the situations I care about. What if I've got some LaTeX citations in a similar format \cite{key1, key2, key3}
and I want to get each key on a newline? I'd like something that doesn't care about the underlying language.
Some details:
- Trailing whitespace should be removed,
- Trailing commas should be added,
- The solution should also work for function definitions:
def my_function(param1, param2=value2, param3=value3):
should become:
def my_function(
param1,
param2=value2,
param3=value3,
):
- Indentation should be consistent regardless of the starting indentation:
my_function(param1, param2=value2, param4=a_function_call())
should become:
my_function(
param1,
param2=value2,
param4=a_function_call(),
)
Edit: To clarify a comment, I'm not interested in a solution that works for language X or language Y. I want something that solves the generic formatting problem of converting a list of elements all on one line (separated by a delimiter and surrounded by some sort of brackets) and converts it into a list of elements where each element is on a different line. This happens most frequently in code, but also in LaTeX, markdown, little bash scripts, etc, so I'm not interested in something that only works in one particular language.
:s/, /,\r\t/g |:s/)/\r&/
and fix the remaining stuff manually, as this may not be hundert percent correct for all edge cases