Perltidy autoformat hashref作为参数

时间:2014-01-04 10:56:24

标签: perl autoformatting perl-tidy

我有以下代码段:

my $obj = $class->new({ 
    schema    => $schema,
    reminder  => $reminder,
    action    => $action,
    dt        => $dt,     
});

我的问题是,perltidy会尝试将其格式化为某些内容,例如:

my $obj = $class->new(
    {   schema   => $schema,
        reminder => $reminder,
        action   => $action,
        dt       => $dt,
    }
);

我不喜欢花括号放置。我可以以某种方式配置perltidy来格式化它像第一个例子? (跳过块的格式不是一个选项。我想将每个更长的hashref格式化为该格式,因此它更紧凑和可读)

到目前为止我的perltidyrc:

-l=79  # Max line width is 78 cols
-i=4   # Indent level is 4 cols
-ci=4  # Continuation indent is 4 cols
-st    # Output to STDOUT
-se    # Errors to STDERR
-vt=2  # Maximal vertical tightness
-cti=0 # No extra indentation for closing brackets
-pt=1  # Medium parenthesis tightness
-bt=1  # Medium brace tightness
-sbt=1 # Medium square bracket tightness
-bbt=1 # Medium block brace tightness
-nsfs  # No space before semicolons
-nolq  # Don't outdent long quoted strings

如果我删除'{}'并将参数作为列表传递,它会做正确的事btw。但我必须通过hashref。

或者你能推荐一种理智的格式化代码吗?

2 个答案:

答案 0 :(得分:5)

这个选项怎么样?

perltidy  -lp -vt=2 -vtc=1

产生

my $obj = $class->new( { schema   => $schema,
                         reminder => $reminder,
                         action   => $action,
                         dt       => $dt,
                       } );

在这里http://perltidy.sourceforge.net/perltidy.html#line_break_control

  

关闭标记(块括号除外)由-vtc = n控制,或   --vertical-tightness-closing = n,其中

     

-vtc = 0总是在关闭令牌(默认)之前断开一行,-vtc = 1   不要在遵循的关闭令牌之前中断           用分号或其他结束标记,但不在           列表环境。 -vtc = 2永远不会在结束令牌之前中断。

EDIT 我怀疑你错过了-lp(排队参数)选项,它也是垂直松紧度所必需的(-vt和-vtc)

答案 1 :(得分:1)

以下似乎解决了上述问题,对我有用:

# perltidy configuration file created Thu Sep 24 15:54:07 2015
# using: -

# I/O control
--standard-error-output                 # -se
--nostandard-output                     # -nst

# Basic formatting options
--indent-columns=4                      # -i=4  [=default]
--maximum-line-length=140               # -l=140

# Code indentation control
--closing-brace-indentation=0           # -cbi=0  [=default]
--closing-paren-indentation=0           # -cpi=0  [=default]
--closing-square-bracket-indentation=0  # -csbi=0  [=default]
--continuation-indentation=4            # -ci=4
--nooutdent-labels                      # -nola
--nooutdent-long-quotes                 # -nolq

# Whitespace control
--block-brace-tightness=1               # -bbt=1
--brace-tightness=1                     # -bt=1  [=default]
--paren-tightness=2                     # -pt=2
--nospace-for-semicolon                 # -nsfs
--square-bracket-tightness=1            # -sbt=1  [=default]
--square-bracket-vertical-tightness=0   # -sbvt=0  [=default]

# Comment controls
--ignore-side-comment-lengths           # -iscl
--minimum-space-to-comment=2            # -msc=2
--static-side-comment-prefix="#"        # -sscp="#"
--static-side-comments                  # -ssc

# Linebreak controls
--brace-vertical-tightness=0            # -bvt=0  [=default]
--paren-vertical-tightness=0            # -pvt=0  [=default]
--stack-closing-hash-brace              # -schb
--stack-closing-paren                   # -scp
--stack-closing-square-bracket          # -scsb
--stack-opening-hash-brace              # -sohb
--stack-opening-paren                   # -sop
--stack-opening-square-bracket          # -sosb
--want-break-before="% + - * / x != == >= <= =~ < > | & **= += *= &= <<= &&= -= /= |= + >>= ||= .= %= ^= x="  # -wbb="% + - * / x != == >= <= =~ < > | & **= += *= &= <<= &&= -= /= |= + >>= ||= .= %= ^= x="

# Blank line control
--noblanks-before-comments              # -nbbc