0

背景:我使用 PHPStylist 来缩进 PHP 代码,通常效果很好。但是,当它找到带有许多参数的函数调用时,它会将所有这些参数放在一行中。因此,例如,一个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 或其他常用工具中的解决方案也将非常感激。)

4

2 回答 2

2

作为第一个建议,像这样的东西?

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
于 2013-03-21T11:21:13.033 回答
1

此 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);
}
于 2013-03-21T22:59:27.170 回答