SAP / abap-cleaner

ABAP cleaner applies 95+ cleanup rules to ABAP code at a single keystroke
Apache License 2.0
439 stars 48 forks source link

Feature Request: Introduce ABAP-Cleaner Pragmas (e.g. skip certain code sections from applying rules) #353

Closed berndeplo closed 1 month ago

berndeplo commented 1 month ago

We were thinking of a new option to skip the ABAP Cleaner from cleaning certain areas in our classes. Sometimes we would like to just skip a few lines - as the readability would be better when self-formatted.

e.g. when we look at this code:

    add_field_to_table( field_name = 'firstname' field_value = 'John' ).   "#AC NO_FORMATTING START
    add_field_to_table( field_name = 'lastname'  field_value = 'Doe' ).
    add_field_to_table( field_name = 'age'       field_value = 40 ).       "#AC NO_FORMATTING END

It looks good - but the ABAP Cleaner formatting rules is changing it to this format:

    add_field_to_table( field_name  = 'firstname'
                        field_value = 'John' ).
    add_field_to_table( field_name  = 'lastname'
                        field_value = 'Doe' ).
    add_field_to_table( field_name  = 'age'
                        field_value = 40 ).

The Pseudo Comment "#AC NO_FORMATTING START should prevent the ABAP Cleaner from changing the formatting in between START and END.

For sure you'll be able to find a better syntax for this ABAP-Cleaner Pragmas / Pseudo comments and also maybe other pragmas that prevent from applying other parts of the ruleset.

Thanks in advance :-)

raymx42 commented 1 month ago

I like the idea in general, but your specific problem can be solved via the ABAP Cleaner Configuration.

In rule "Align parameters and components" you can change the behavior "Keep other one-liners" to "always" or to "if maximum line length A is observed".

image

jmgrassau commented 1 month ago

Hi Benny,

thanks for opening this, and thanks, raymx42 for pointing out this configuration for the specific example!

So far, we've tried to avoid introducing another set of pseudo-comments (there are already ##pragmas, "#EC… pseudo-comments for extended check, "#EC CI_… code inspector, …): If someone wanted to deactivate just a specific cleanup rule, we'd need a fixed name for each rule (̀"#AC NO_FORMATTING[ALIGN_PARAMETERS, REMOVE_NEEDLESS_SPACES] START), which can then never be changed again once they are out there in the code. Also, the need for class-/method-/section-/command-scope pseudo-comments might come up etc.

So, I would rather prefer to find solutions for the specific issues – either finding ways to automatically align them better, or at least to automatically determine the "keep as is" cases (and create configuration accordingly, if needed). From my experience, once you start to consider "why would I intuitively want this case to be aligned differently than that other case?", you can often find rules to determine those cases (depending on existing alignment, number of involved elements, required line length etc.)

Kind regards, Jörg-Michael

P.S.: By the way, it has been on my wish list for a long time to enable ABAP cleaner to automatically align and even create this "tabular style" as in your example. Esp. rows in VALUE statements (often in test code) can become nicely readable with it, so I completely agree with your perception that

    add_field_to_table( field_name = 'firstname' field_value = 'John' ).
    add_field_to_table( field_name = 'lastname'  field_value = 'Doe' ).
    add_field_to_table( field_name = 'age'       field_value = 40 ).

is better readable (up to a certain line length, of course).

berndeplo commented 1 month ago

Hi raymx42,

Thanks for pointing that out. Works well for my example.

Hi jmgrassau Jörg-Michael,

Thanks for your detailed response and the explanations why adding pragmas is not the way to go. I agree on finding individual solutions. Also I wouldn't like to add something to the code just for the formatting.

Have a good day Benjamin