我有以下代码片段:
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 以像第一个示例一样格式化它吗?(跳过块的格式不是一个选项。我想将每个较长的哈希引用格式化为该格式,因此它更紧凑且可读性更强)
到目前为止,我的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
如果我删除"{}"并将参数作为列表传递,它就会做正确的事情。但我必须传递一个哈希参考。
或者你能推荐一种格式化此类代码的理智方法吗?
这个选项怎么样?
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 控制,或 --垂直-紧密性-关闭=n,其中
-vtc=0 始终在结束标记之前换行(默认),-vtc=1 不要在后面的关闭令牌之前中断 通过分号或其他结束标记,并且不在 列表环境。 -vtc=2 在关闭令牌之前永远不会中断。
编辑我怀疑您缺少 -lp(排队参数)选项,该选项也是垂直密封性(-vt 和 -vtc)所必需的
以下内容似乎解决了上述问题并对我有用:
# 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