【问题标题】:Break long source lines, but only on comma打破长的源代码行,但仅限于逗号
【发布时间】:2013-03-21 09:00:31
【问题描述】:

背景:我使用 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 或其他常用工具中的解决方案也将非常感激。)

【问题讨论】:

    标签: php coding-style sed awk indentation


    【解决方案1】:

    第一个建议是这样的吗?

    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
    

    【讨论】:

    • 如果行需要分成多个行,则较低的缩进不正确,但是,很棒的答案!我想我会用最上面的。再次感谢!
    【解决方案2】:

    此 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);
    }
    

    【讨论】:

    • 谢谢,这正是我所需要的。
    猜你喜欢
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-18
    • 2013-10-09
    • 1970-01-01
    相关资源
    最近更新 更多