How to "unbreak" C code with "Artistic Style"
Asked Answered
I

1

10

With Artistic Style code formatter, how do I achieve the opposite of --break-after-logical / -xL such that if I have...

if (thisVariable1 == thatVariable1
        || thisVariable2 == thatVariable2
        || thisVariable3 == thatVariable3)
    ...

...I get...

if (thisVariable1 == thatVariable1 || thisVariable2 == thatVariable2 || thisVariable3 == thatVariable3)
    ...
Irrecusable answered 16/12, 2013 at 20:2 Comment(8)
@CloseVoter: If a code beautifier doesn't fall into the category of "tools used primarily for programming", then I don't know what possibly could.Skiagraph
From reading the documentation: possibly --unpad-paren / -U?Sooksoon
@Sooksoon just tried it and it did not work (nice idea though).Irrecusable
I browsed the doc for commands containing merge or join and it seems this beautifier doesn't merge existing lines at all. Is that correct?Sooksoon
@Sooksoon I guess multiline processing is beyond this beautifiers capabilitiesIrrecusable
Is your code editor capable of a decent GREP? In TextWrangler, this should work: \s+(?=\|\|), replace with a single space. (Assuming your \s picks up newlines as well, I believe it does in TW.)Sooksoon
@Sooksoon yes I know I could do that --and thank you btw-- but I don't know whether I am ready to go down that road (ie. go through the entire C/C++ language specification)Irrecusable
Personally I'm in favor of multi-line stuff like this that improves readability. However, in the spirit of answering the original question: you could remove all line breaks and re-format the code. If you only want to change specific things: Clang (and probably gcc) can generate an abstract syntax tree for your code; you should be able to parse it to look for multi-line if statements. Assuming Artistic Style supports it, just feed it the line range you want reformatted.Edva
T
5

Artistic style doesn't appear to make this possible.

That's quite sensible, since it actually obfuscates the code :

  • Vertical alignment improve legibility by emphasizing identical pattern, and distinct patterns as well (a single != would be harder to spot in a one-liner).
  • It also eases diff tracking.

I would actually write :

if  (  thisVariable1   == thatVariable1
    || thisVariable2   == thatVariable2
    || longerVariable3 == thatVariable3 )
    ...
Thermosiphon answered 18/12, 2013 at 0:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.