php - Break long source lines, but only on comma -


background: use phpstylist indent php code, working out fine. but, when find function calls many arguments, puts these arguments on 1 single line. instance bind_param() call can 300 characters wide.

this not play our coding style guide, dictates maximum line length of 180 characters.

our indent script has sed command clean off trailing whitespace left phpstylist, thinking, can sed break long lines, on comma?

example input:

function xyz() {     somecall($somevariable1, $somevariable1, $somevariable1, $somevariable1, $somevariable1, $somevariable1, $somevariable1, $somevariable1, $somevariable1); } 

example output:

function xyz() {     somecall($somevariable1, $somevariable1, $somevariable1, $somevariable1, $somevariable1, $somevariable1, $somevariable1, $somevariable1, $somevariable1); } 

(bonus points if script indent next line prettily, think hard in sed. solution in awk, perl, python or other common tool appreciated too.)

this awk script split lines @ last comma before max line length, can set run-time argument if like. indents split-off lines 4 spaces more original lines:

$ cat file function xyz() {     somecall($somevariable1, $somevariable2, $somevariable3, $somevariable4, $somevariable5, $somevariable6, $somevariable7, $somevariable8, $somevariable9);          somecall($somevariable1, $somevariable2, $somevariable3, $somevariable4, $somevariable5, $somevariable6, $somevariable7, $somevariable8, $somevariable9); }  $ awk -f tst.awk file function xyz() {     somecall($somevariable1, $somevariable2, $somevariable3,         $somevariable4, $somevariable5, $somevariable6,         $somevariable7, $somevariable8,         $somevariable9);          somecall($somevariable1, $somevariable2, $somevariable3,                 $somevariable4, $somevariable5, $somevariable6,                 $somevariable7, $somevariable8,                 $somevariable9); } $ $ cat tst.awk begin{ maxlength = (maxlength ? maxlength : 66) }  (length($0) > maxlength) && /,/ {     indent = $0     sub(/[^[:space:]].*/,"",indent)      tail = $0     while (tail ~ /[^[:space:]]/) {         head = substr(tail,1,maxlength)         sub(/,[^,]+$/,",",head)         tail = substr(tail,length(head)+1)         sub(/^[[:space:]]*/,indent"    ",tail)         print head     }     next } 1  $ awk -v maxlength=100 -f tst.awk file function xyz() {     somecall($somevariable1, $somevariable2, $somevariable3, $somevariable4, $somevariable5,         $somevariable6, $somevariable7, $somevariable8,         $somevariable9);          somecall($somevariable1, $somevariable2, $somevariable3, $somevariable4, $somevariable5,                 $somevariable6, $somevariable7, $somevariable8,                 $somevariable9); }  $ awk -v maxlength=30 -f tst.awk file function xyz() {     somecall($somevariable1,         $somevariable2,         $somevariable3,         $somevariable4,         $somevariable5,         $somevariable6,         $somevariable7,         $somevariable8,         $somevariable9);          somecall($somevariable1,                 $somevariable2,                 $somevariable3,                 $somevariable4,                 $somevariable5,                 $somevariable6,                 $somevariable7,                 $somevariable8,                 $somevariable9); } 

Comments