Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perltidy autoformat hashref as parameter

I have the following code snippet:

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

My problem is, that perltidy tries to format it into something, like this:

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

I don't like the curly brace placement. Can I somehow configure perltidy to format it like the first example? (Skipping the formatting for the block is not an option. I want to format every longer hashref into that format, so it is more compact and readable)

My perltidyrc so far:

-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

If I remove the '{}' and pass the parameters as a list, it does the right thing btw. But i have to pass a hashref.

Or could you recommend a sane way of formatting such code?

like image 770
Davs Avatar asked Jan 04 '14 10:01

Davs


2 Answers

The following seems to solve the above problem and works for me:

# 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
like image 42
nicomen Avatar answered Oct 17 '22 08:10

nicomen


How about this option?

perltidy  -lp -vt=2 -vtc=1

which yields

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

which is here http://perltidy.sourceforge.net/perltidy.html#line_break_control

Closing tokens (except for block braces) are controlled by -vtc=n, or --vertical-tightness-closing=n, where

-vtc=0 always break a line before a closing token (default), -vtc=1 do not break before a closing token which is followed by a semicolon or another closing token, and is not in a list environment. -vtc=2 never break before a closing token.

EDIT I suspect you were missing the -lp (line up parameters) option which is also needed for vertical tightness (-vt and -vtc)

like image 112
KeepCalmAndCarryOn Avatar answered Oct 17 '22 08:10

KeepCalmAndCarryOn