Skip to main content
inotec
Associate
July 15, 2022
Solved

I want to change source formatting of if statements from the top example to the bottom example shown. I want the brackets to align with the if, i.e. same column..

if (SW1Pressed_F && (!SW1ActionTaken_F))

{

   DesiredSpeed = 10;

    DesiredDirection = LEFT;

   }

if (SW1Pressed_F && (!SW1ActionTaken_F))

{

   DesiredSpeed = 10;

    DesiredDirection = LEFT;

   }

    This topic has been closed for replies.
    Peter BENSCH
    Peter BENSCHBest answer
    ST Technical Moderator
    July 19, 2022

    Formatting the source code can either be done by the editor, or many GUIs already contain it. The STM32CubeIDE, for example, is able to apply such formatting as an Eclipse variant with so-called profiles. Some profiles are already predefined, but you can also modify them and save or export them under your preferred name.

    By the way, exporting a profile is very useful because you can import the exported profile into new installations with just a few clicks.

    How to reach the Formatter:

    • Windows > Preferences > C/C++ > Code Style > Formatter

    or a little shorter:

    • Windows > Preferences > [type filter text] --> formatt

    Modify an existing profile:

    • within the Formatter via New > [new profile name] + select the profile to be used as a template
    • Adjusting the 65 million parameters (after that it is clear why Export is extremely useful)
    • Export
    • save with OK

    Export:

    • within the Formatter via Edit > Export

    Perhaps my Kernighan&Richie-derived Quick'n'Dirty profile (attached) will be of interest to you and could serve as a template.

    Regards

    /Peter

    In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

    10 replies

    inotec
    inotecAuthor
    Associate
    July 15, 2022

    Note that the forum messed up my columns in the example, but my statement is correct.

    inotec
    inotecAuthor
    Associate
    July 15, 2022

    Trying again...

    if (SW1Pressed_F && (!SW1ActionTaken_F))

    {

    DesiredSpeed = 10;

    DesiredDirection = LEFT;

    }

    if (SW1Pressed_F && (!SW1ActionTaken_F))

    {

    DesiredSpeed = 10;

    DesiredDirection = LEFT;

    }

    inotec
    inotecAuthor
    Associate
    July 15, 2022

    Nope, screwed it up again

    Tesla DeLorean
    Guru
    July 15, 2022

    You can a) Edit your posts see V icon in upper right corner of posting, b) use the code posting tool </> icon, or c) post a picture or bitmap mountain/sub icon

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    ST Employee
    July 18, 2022

    Hello,

    Something like this?

    if (SW1Pressed_F && (!SW1ActionTaken_F))
     
    {
     
    	DesiredSpeed = 10;
     
    	DesiredDirection = LEFT;
     
    }

    inotec
    inotecAuthor
    Associate
    July 18, 2022
    Yes, but without the lines before the brackets:
    if (SW1Pressed_F && (!SW1ActionTaken_F)) { DesiredSpeed = 10; DesiredDirection = LEFT; }
    1. if (SW1Pressed_F && (!SW1ActionTaken_F))
    2. {
    3.
    4. DesiredSpeed = 10;
    5.
    6. DesiredDirection = LEFT;
    7. }
    Tesla DeLorean
    Guru
    July 19, 2022

    Aren't there tools like Uncrustify that can apply any kind of formatting template you want?​

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Peter BENSCH
    Peter BENSCHBest answer
    ST Technical Moderator
    July 19, 2022

    Formatting the source code can either be done by the editor, or many GUIs already contain it. The STM32CubeIDE, for example, is able to apply such formatting as an Eclipse variant with so-called profiles. Some profiles are already predefined, but you can also modify them and save or export them under your preferred name.

    By the way, exporting a profile is very useful because you can import the exported profile into new installations with just a few clicks.

    How to reach the Formatter:

    • Windows > Preferences > C/C++ > Code Style > Formatter

    or a little shorter:

    • Windows > Preferences > [type filter text] --> formatt

    Modify an existing profile:

    • within the Formatter via New > [new profile name] + select the profile to be used as a template
    • Adjusting the 65 million parameters (after that it is clear why Export is extremely useful)
    • Export
    • save with OK

    Export:

    • within the Formatter via Edit > Export

    Perhaps my Kernighan&Richie-derived Quick'n'Dirty profile (attached) will be of interest to you and could serve as a template.

    Regards

    /Peter

    In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
    inotec
    inotecAuthor
    Associate
    July 20, 2022
    Thank you for the response. Very helpful.
    Peter BENSCH
    ST Technical Moderator
    July 21, 2022

    Great!

    If the problem is resolved, please mark this thread as answered by selecting Select as best under your preferred answer. This will help other users find that answer faster.

    /Peter

    In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.