Given an input of a string representing a function definition, output the string with newlines and spaces inserted so that the function's arguments are newline-separated and aligned.
The input string will follow the following pattern:
First, it will start with a prefix, which is always at least one character long and does not contain any of the characters
,()
.An open parenthesis (
(
) will then mark the beginning of the argument list.A list of zero or more arguments will then follow. These are separated by the string
", "
(a comma and then a space). None of the arguments will contain any of the characters,()
.A close parenthesis (
)
) will mark the end of the argument list.Lastly, a postfix may be found, which is zero or more characters long and may contain the characters
,()
.
The input string will consist solely of printable ASCII (which means it will never contain a newline).
The output must be:
The prefix, copied down verbatim, and the open parenthesis.
The argument list, this time separated not by
", "
but by a comma, newline, and as many spaces as is needed to vertically align the first character of each argument.The close paren and postfix (if it exists) verbatim.
Since this is code-golf, the shortest code in bytes will win.
Test cases (format: single-line input followed by output followed by double newline):
def foo(bar, baz, quux):
def foo(bar,
baz,
quux):
int main() {
int main() {
fn f(a: i32, b: f64, c: String) -> (String, Vec<i32>) {
fn f(a: i32,
b: f64,
c: String) -> (String, Vec<i32>) {
function g(h) {
function g(h) {
def abc(def, ghi, jkl, mno)
def abc(def,
ghi,
jkl,
mno)
x y z(x, y, z) x, y, z)
x y z(x,
y,
z) x, y, z)