Php 打断长源行,但仅限于逗号
背景:我使用PHPStylist缩进PHP代码,这通常很好。但是,当它找到具有许多参数的函数调用时,它将所有这些参数放在一行上。例如,一个Php 打断长源行,但仅限于逗号,php,coding-style,sed,awk,indentation,Php,Coding Style,Sed,Awk,Indentation,背景:我使用PHPStylist缩进PHP代码,这通常很好。但是,当它找到具有许多参数的函数调用时,它将所有这些参数放在一行上。例如,一个bind_param()调用可以很容易地包含300个字符宽 这不符合我们的编码风格指南,它规定了180个字符的最大行长 我们的缩进脚本已经有一个sed命令来清除PHPStylist留下的尾随空格,所以我在想,sed是否也可以打断太长的行,但只能在逗号上 输入示例: function xyz() { somecall($somevariable1, $s
bind_param()
调用可以很容易地包含300个字符宽
这不符合我们的编码风格指南,它规定了180个字符的最大行长
我们的缩进脚本已经有一个sed命令来清除PHPStylist留下的尾随空格,所以我在想,sed是否也可以打断太长的行,但只能在逗号上
输入示例:
function xyz()
{
somecall($somevariable1, $somevariable1, $somevariable1, $somevariable1, $somevariable1, $somevariable1, $somevariable1, $somevariable1, $somevariable1);
}
示例输出:
function xyz()
{
somecall($somevariable1, $somevariable1, $somevariable1, $somevariable1,
$somevariable1, $somevariable1, $somevariable1, $somevariable1, $somevariable1);
}
(如果脚本也能很好地缩进下一行,那就有好处了,但我认为在sed中这是很难做到的。如果能用awk、perl、python或其他常用工具解决问题,我也会非常感激。)作为第一个建议,类似这样的东西
sed 's/.\{70\}[^,]*,/&\
/g' file
---编辑---
对于缩进,您可以尝试:
sed -e 's/\(.\{70\}[^,]*,\)[[:blank:]]*/\1\
/g; tend' -e n -e :end -e 's/\(^[[:blank:]]*\)\(.*\n\)/\1\2\1/ ' file
作为第一个建议,像这样的事情
sed 's/.\{70\}[^,]*,/&\
/g' file
---编辑---
对于缩进,您可以尝试:
sed -e 's/\(.\{70\}[^,]*,\)[[:blank:]]*/\1\
/g; tend' -e n -e :end -e 's/\(^[[:blank:]]*\)\(.*\n\)/\1\2\1/ ' file
此awk脚本将在最大行长度之前的最后一个逗号处分割行,如果愿意,可以将其设置为运行时参数。它将分割线缩进比原始线多4个空格:
$ 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);
}
此awk脚本将在最大行长度之前的最后一个逗号处分割行,如果愿意,可以将其设置为运行时参数。它将分割线缩进比原始线多4个空格:
$ 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);
}
如果一行需要分为多行,则下一行不能正确缩进,但无论如何,答案太棒了!我想我会用最上面的。再次感谢!如果一行需要分为多行,则下一行不能正确缩进,但无论如何,答案太棒了!我想我会用最上面的。再次感谢!谢谢,这正是我需要的。谢谢,这正是我需要的。