in reply to prettyfy hashes

> so I can call it from vim.

What a pity! In Emacs, you can use align-regex , which I usually bind to C-x l . I can then just select the region and press C-x l => Enter and voilą!

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: prettyfy hashes (emacs)
by LanX (Saint) on Apr 03, 2016 at 03:39 UTC
    I normally move the cursor to the last => and type M-x align-current .

    One day I'll automatise it to happen with each auto indent. ..

    FWIW I seem to remember that perltidy can't do this.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      It aligns the assignment symbol = for me, too, which I don't want. Do you have any special align-rules-alist to prevent that?

      # here, instead of here # v V my %periods = (Mercury => { orbital => 0.24, rotation => +58.64 }, Venus => { orbital => 0.62, rotation => -243.02 }, Earth => { orbital => 1.00, rotation => 1.00 }, Mars => { orbital => 1.88, rotation => 1.03 }, Jupiter => { orbital => 11.86, rotation => 0.41 }, Saturn => { orbital => 29.46, rotation => 0.43 }, Uranus => { orbital => 84.01, rotation => -0.72 }, Neptune => { orbital => 164.8, rotation => 0.67 }, );

      Moving the structure from the assignment line doesn't help:

      my %periods = ( Mercury => { orbital => 0.24, rotation => 58.64 }, Venus => { orbital => 0.62, rotation => -243.02 }, # ...

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
        I have the same problem, which I normally solve manually.

        But this could be solved with separate rules for = , =~ and => and/or "sections separations".

        please note how this works

        my $h_periods = { Mercury => { orbital => 0.24, rotation => 58.64 }, Venus => { orbital => 0.62, rotation => -243.02 }, Earth => { orbital => 1.00, rotation => 1.00 }, Mars => { orbital => 1.88, rotation => 1.03 }, Jupiter => { orbital => 11.86, rotation => 0.41 }, Saturn => { orbital => 29.46, rotation => 0.43 }, Uranus => { orbital => 84.01, rotation => -0.72 }, Neptune => { orbital => 164.8, rotation => 0.67 }, };

        That's because { is a section separator while ( is not

        `separate' Each rule can define its own section separator, which describes how to identify the separation of "sections" within the region to be aligned. Setting the `separate' attribute overrides the value of `align-region-separate' (see the documentation of that variable for possible values), and any separation argument passed to `align'.

        using M-x customize-group RET align RET will facilitate customizing (it provides a textual UI for lisp data)

        the rule in question in align-rules-list is

        Alignment rule: Title: perl-assignment Required attributes: Regexp: (Regular expression to match) Choice: Value Menu Regexp: [^=!^&*-+<>/| ]\(\s-*\)=[~>]?\(\s-*\)\([^>= ]\|$\) Optional attributes: INS DEL Choice: Value Menu Paren group: (Parenthesis group to use) Choice: Value Menu Repeat: INS DEL Integer: 1 INS DEL Integer: 2 INS INS DEL Choice: Value Menu Modes: (Modes where this rule applies) Lisp expression: align-perl-modes INS DEL Choice: Value Menu To Tab Stop: (Should rule align to tab stops) Boolean: Toggle off (nil) INS

        this [~>]? part should be deleted or replaced.

        (BTW: not sure what the -* is intended to do...)

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!

        The default alignment rules in emacs are meant to handle = assignment not => fat comma.

        So task number one would be to separate these cases.

        Secondly we need to define region separators.

        as a demo: resetting the align-region-separate to handle parentheses helped

        (snippet from customize: Align Region Separate: Value Menu Regexp defines section boundaries: [][{}])

        to format

        $a = { bla => 1, trax => [ bla2 => { sauh1 => 1666, 3 => 2, }, ], # bla => 1, trallxa => 2, };

        to this, hence handling different levels accordingly

        $a = { bla => 1, trax => [ bla2 => { sauh1 => 1666, 3 => 2, }, ], # new section bla => 1, trallxa => 2, };

        but this doesn't help with this because the separators will be inside each line

        my $h_periods = { Mercury => { orbital => 0.24, rotation => 58.64 }, Venus => { orbal => 0.62, rotation => -243.02 }, Earth => { orbital => 1.00, rotation => 1.00 }, Mars => { orbital => 1.88, rotation => 1.03 }, Jupiter => { orbital => 11.86, rotation => 0.41 }, Saturn => { orbital => 29.46, rotation => 0.43 }, Uranus => { orbital => 84.01, rotation => -0.72 }, Neptune => { orbital => 164.8, rotation => 0.67 }, };

        The only way to solve this is to use indentation to distinguish groups.

        According to the docs it's possible to use a function call back instead of a regex, but documentation is sparse.

        I wanted to document my findings before getting lost in other projects ... ;-)

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re^2: prettyfy hashes
by LanX (Saint) on Apr 03, 2016 at 15:26 UTC
    > In Emacs, you can use align-regex , which I usually bind to C-x l . I can then just select the region and press C-x l => Enter and voilą!

    There is room for much improvement...

    1. selecting the region:

    a combination of backward-up-list and mark-sexp will select the surrounding structure (repeating will extend the selection till statement (ie. ';') boundary.

    (please note that cperl-mode injects here some semantic knowledge about the code's structure which can't be reflected in a simple regexp rule)

    2. the rule for align-current has the advantage to harmonize the surrounding whitespaces too. And align-regexp seems to allow the same extended logic.

    So combining both steps bound to a key should make alignment of fat-commas quite easy.

    I experimented with a kmacro and it seemed quite nice. :)

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!