Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: How would you indent this? (emacs)

by LanX (Saint)
on Mar 18, 2021 at 12:12 UTC ( [id://11129904]=note: print w/replies, xml ) Need Help??


in reply to How would you indent this?

If I want compactness in emacs I usually break into the next line
my @headings = ( { key => 'k', name => 'Kanji', class => 'kanji'}, { key => 'skip', name => 'SKIP', class => 'skip-code'}, { key => 'co', name => 'Suggestion', class => 'skip-code'}, { key => 'disc', name => 'Discussion'}, );

I don't know where you got the break after the opening curly from

{ key => 'disc', name => 'Discussion'},

(Update: *I can't reproduce this!* I can now, see reply )

BUT cperl-mode offers 8 indent-styles (also available from the menu)

M-x cperl-set-style BSD C++ CPerl Current GNU K&R PerlStyle Whitesmith

... and plenty of customizations

Cperl Indentation Details group: Indentation. State : something in this group has been changed outside custom +ize. Show Value Cperl Brace Imaginary Offset Imagined indentation of a Perl open brace that actually follows a s +tatement. Hide An open brace following other text is treated as if it were this fa +r to the right of the start of its line. Show Value Cperl Brace Offset Extra indentation for braces, compared with other text in same cont +ext. Show Value Cperl Break One Line Blocks When Indent Non-nil means that one-line if/unless/while/until/for/foreach BLOCK +s Hide need to be reformatted into multiline ones when indenting a region. Show Value Cperl Close Paren Offset Extra indent for substatements that start with close-parenthesis. Hide Cperl Comment Column: 40 State : SAVED and set. Column to put comments in CPerl (use M-x cperl-indent to lineup wit +h code). Show Value Cperl Continued Brace Offset Extra indent for substatements that start with open-braces. Hide This is in addition to cperl-continued-statement-offset. Show Value Cperl Continued Statement Offset Extra indent for lines not starting new statements. Show Value Cperl Fix Hanging Brace When Indent Non-nil means that BLOCK-end `}' may be put on a separate line Hide when indenting a region. Braces followed by else/elsif/while/until are excepted. Show Value Cperl Indent Comment At Column 0 Non-nil means that comment started at column 0 should be indentable +. Show Value Cperl Indent Left Aligned Comments Non-nil means that the comment starting in leftmost column should i +ndent. Hide Cperl Indent Level: 2 State : CHANGED outside Customize. Indentation of CPerl statements with respect to containing block. Show Value Cperl Indent Parens As Block Non-nil means that non-block ()-, {}- and []-groups are indented as + blocks, Hide but for trailing "," inside the group, which won't increase indenta +tion. One should tune up `cperl-close-paren-offset' as well. Show Value Cperl Indent Region Fix Constructs Amount of space to insert between `}' and `else' or `elsif' Hide in `cperl-indent-region'. Set to nil to leave as is. Values other than 1 and nil will probably not work. Show Value Cperl Indent Wrt Brace Non-nil means indent statements in if/etc block relative brace, not + if/etc. Hide Versions 5.2 ... 5.20 behaved as if this were `nil'. Show Value Cperl Label Offset Offset of CPerl label lines relative to usual indentation. Show Value Cperl Lineup Step `cperl-lineup' will always lineup at multiple of this number. Hide If nil, the value of `cperl-indent-level' will be used. Show Value Cperl Merge Trailing Else Non-nil means that BLOCK-end `}' followed by else/elsif/continue Hi +de may be merged to be on the same line when indenting a region. Show Value Cperl Min Label Indent Minimal offset of CPerl label lines. Show Value Cperl Regexp Indent Step Indentation used when beautifying regexps. Hide If nil, the value of `cperl-indent-level' will be used. Show Value Cperl Tab Always Indent Non-nil means TAB in CPerl mode should always reindent the current +line, Hide regardless of where in the line point is when the TAB command is us +ed.

you might wanna read the micro-docs in the menu and experiment.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^2: How would you indent this? (cperl-indent-region)
by LanX (Saint) on Mar 18, 2021 at 16:19 UTC
    > (Update: I can't reproduce this! )

    OK, I only used TAB indentation for each line so far. (emacs default is that TAB indents a line)

    If I mark the region and apply an explicit M-x cperl-indent-region I'm getting your result. That's also available from the "Perl" menu as indent region .

    Tested in 24.3 and 27.1 on Windows.

    From my experience does cperl not attempt to format nested data structures, there are many styles and interfering with the styles of Data::Dumper , Data::Dump, Data::Printer or Perl::Tidy is cumbersome.

    Cperl-mode only does indenting!

    It is also NOT attempting to vertically align elements in a 2D structure. It's a requirement close to AI to DWIM nested data.

    My preference here is Data::Dump, but it's also resorting hash-keys alphabetically and I wouldn't want my editor to take away this decision from me.

    The best format depends on the POV.

    Anyway ... what you are experiencing looks to me like a misinterpretation of the left curly as a block start like in for (...) {

    Please note how replacing the hashes with arrays is fixing this:

    (NB: I'd personally line-break right of the assignment)

    my @headings = ( [key => 'k', name => 'Kanji', class => 'kanji' ], [key => 'skip', name => 'SKIP', class => 'skip-code'], [key => 'co', name => 'Suggestion', class => 'skip-cod +e'], [key => 'disc', name => 'Discussion'], );

    workarounds

    A) as quick workaround I'd suggest manually undoing nested data structures when indenting a large block of code.

    Since emacs has "regional undo" of marked regions ° this should be easy and you can use the formatter of your taste for nested datastructures.

    B) If you don't wanna use an external formatter, you can also appease yourself with this format by manually introducing line-breaks before right curlies } , b/c cperl-indent-region won't change this. ( I also manually did a vertical alignment)

    my @headings = ( { key => 'k', name => 'Kanji', class => 'kanji' }, { key => 'skip', name => 'SKIP', class => 'skip-code' }, { key => 'co', name => 'Suggestion', class => 'skip-code' }, { key => 'disc', name => 'Discussion' }, );

    Breaking the hash elements into individual lines works too.

    C) UPDATE Or don't use cperl-indent-*

    If you are working in a team, you'll need to use an RCS like GIT and chances are low everybody is using the same IDE.

    Resorting to a fixed Perl::Tidy setting hooked into your RCS is the only way here. Otherwise all reindented lines will show up as diffs.

    what's next

    I'll try to write a test for this problem in Perl to figure out the root cause.

    Could also be that the emacs maintainers changed some fundamentals which are sabotaging cperl-mode.

    HTH! :)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

    °) I haven't seen any other editor yet offering this.

      From my experience does cperl not attempt to format nested data structures

      It shouldn't, but it does. As you suspect, CPerl mode mistakes the nested data structure as a control structure, which it reformats intentionally (see example here). It just takes a wrong turn in the function cperl-fix-line-spacing. Therefore, it is rather obvious that the issue doesn't occur with arrays: square brackets can't be confused with control structures.

      If you want M-x cperl-indent-region and M-x cperl-indent-exp to do indentation and nothing else, set cperl-indent-region-fix-constructs to nil.

        > > From my experience does cperl not attempt to format nested data structures

        > It shouldn't, but it does.

        what I meant was that cperl doesn't attempt to mimic Data::Dump et al. (Many posters in this thread imply otherwise.)

        Which is a good thing, because the decision how to read the data must be left to the programmer.

        This particular example might have a "tabular interpretation" but others may not.

        > (see example here)

        oops, I was AFK and missed that

        > If you want M-x cperl-indent-region and M-x cperl-indent-exp to do indentation and nothing else, set cperl-indent-region-fix-constructs to nil.

        I can confirm this works.

        One quick solution could be to have another function cperl-format-region which activates the fix-construct flag internally.

        Tho having heuristics to distinguish between block and hash to avoid this problem would be nice.

        I'm also having another effect with 27.1 now

        {key    => 'k',...

        If I type a space between { and key , a TAB is introduced before the fat-comma => ?

        Can't be seen in 24.3 I'd say it's related to new global auto-indent settings in emacs.

        That's what I get after inserting a space after all {

        my @headings = ( { key => 'k', name => 'Kanji', class => 'kanji' }, { key => 'skip', name => 'SKIP', class => 'skip-code'}, { key => 'co', name => 'Suggestion', class => 'skip-code'}, { key => 'disc', name => 'Discussion'}, );

        UPDATE: Sorry, strike that. Seems like I had "silent" TABs in the code, which only jumped when a space was introduced.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11129904]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-24 23:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found