What is this feature/option called? (enum formatting) v 2.3.2

image

This is annoying and I'd like to change it to :
   

image

I can't find anything in preferences to do this.

I doubt that you can change it. It's the definition of the enum so I think that you need to change (reformat) the definition.

As far as I know it can be disabled but no idea how.

Hi @dougp. Since you didn't provide much information, I'll have to guess that you are referring to Auto Format?

Arduino IDE simply passes your code to a standard external C++ code formatter tool (for Arduino IDE 2.x, this tool is the industry standard ClangFormat). So you can't configure the behavior of the formatter tool via the Arduino IDE preferences. You must instead use the formatter tool's native configuration interface.

You can learn how to do that from this tutorial:

https://docs.arduino.cc/software/ide-v2/tutorials/ide-v2-customize-auto-formatter/

It looks like the relevant ClangFormat configuration option should be AllowShortEnumsOnASingleLine:

https://releases.llvm.org/14.0.0/tools/clang/docs/ClangFormatStyleOptions.html#:~:text=AllowShortEnumsOnASingleLine

However, that option is already set to true in the default auto format configuration:

I see there is a bug report about this in the issue tracker for ClangFormat:

The workaround for the bug would be to change the value of the ColumnLimit option from 0 to some arbitrary large number. For example:

ColumnLimit: 999

You might be tempted to add only that line to your custom .clang-format file, but if you do that you will find that there are many random changes to the behavior of the IDE's auto format feature. The reason for this is that your custom file overrides Arduino IDE's default formatter configuration completely, meaning that the ClangFormat default setting (which is often different from the Arduino IDE defaults) will be used for any option you don't define in your file. So you should copy the entire default .clang-format file from the tutorial I linked above, then use that as a base for your modifications.

And I thought it was the hover functionality :frowning:

So @ptillisch gave an option; alternative is to disable the formatter for a piece of code.

int formatted_code;
// clang-format off
    void    unformatted_code  ;
// clang-format on
void formatted_code_again;

See https://clang.llvm.org/docs/ClangFormatStyleOptions.html#disabling-formatting-on-a-piece-of-code

Thank you. For that reason I prefer @sterretje's option.

More typing but easier to control, IMO.

I don't follow your logic. The problem you referenced is overcome by spending 30 seconds to copy and paste the default configuration into your file. You only have to do that work once.

The solution provided by @sterretje is perfectly valid, but it simply doesn't make sense to consider it superior to the one I provided for the specific reason you gave.

Maybe I didn't explain the process clearly in my previous post. I'll provide more detailed instructions:

  1. Click the following link to open Arduino's default ClangFormat configuration file in your web browser:
    https://raw.githubusercontent.com/arduino/tooling-project-assets/refs/heads/main/other/clang-format-configuration/.clang-format

  2. Press the Ctrl+A keyboard shortcut (Command+A for macOS users).
    This will select all the text in the page.

  3. Press the Ctrl+C keyboard shortcut (Command+C for macOS users).
    This will copy the selected text to the clipboard.

  4. Open a new text file in any text editor.

  5. Press the Ctrl+V keyboard shortcut (Command+V for macOS users).
    This will paste the copied text into the editor.

  6. Change line 62 from this:

    ColumnLimit: 0
    

    to this

    ColumnLimit: 999
    
  7. Save the file to the following path:

    • If you are using Linux:
      /home/<username>/.arduinoIDE/.clang-format
      
      (where <username> is your Linux username)
      :exclamation: The .arduinoIDE folder may be hidden by default in your file manager and terminal.
    • If you are using macOS:
      /Users/<username>/.arduinoIDE/.clang-format
      
      (where <username> is your macOS username)
      :exclamation: The .arduinoIDE folder is hidden by default. You can make it visible by pressing the
      Command+Shift+. keyboard shortcut.
    • If you are using Windows:
      C:\Users\<username>\.arduinoIDE\.clang-format
      
      (where <username> is your Windows username)

    :exclamation: Make sure to name the file with the exact filename .clang-format, not something like .clang-format.txt.

No, I just got confused and shied away from the

phrase.

I have followed the instructions in post #6 but see no effect. I presume a restart of the IDE is required?

They work as expected for me. Please provide the following information and I'll investigate further:

  • The code you started with.
  • The action you performed.
  • The code as you expected it to look after you performed that action.

No, it is not necessary.

Starting code:

enum { ramSingle, zerkAlpha, indexRodAlpha, pusherVertical, shellPlate, caseSingle, rockerSingle, x10C };  //

After ctrl + t

enum { ramSingle,
       zerkAlpha,
       indexRodAlpha,
       pusherVertical,
       shellPlate,
       caseSingle,
       rockerSingle,
       x10C };  //

clang changed:

BreakInheritanceList: BeforeColon
BreakStringLiterals: false
ColumnLimit: 999
CommentPragmas: ''
CompactNamespaces: false

edit -
I have since done a complete reboot (windows update needed) and the behavior is unchanged. the 'column limit 999' setting did survive the reboot.

On my system, after following the instructions in post #6, auto format doesn't result in any changes to your starting code.

Before:

enum { ramSingle, zerkAlpha, indexRodAlpha, pusherVertical, shellPlate, caseSingle, rockerSingle, x10C };  //
void setup() {}
void loop() {}

After:

enum { ramSingle, zerkAlpha, indexRodAlpha, pusherVertical, shellPlate, caseSingle, rockerSingle, x10C };  //
void setup() {}
void loop() {}

(no change)


Please double check that the file is present at this path on your computer:

C:\Users\<username>\.arduinoIDE\.clang-format

(where <username> is your Windows username)


Please double check that the filename is exactly .clang-format.

Windows File Explorer has an annoying behavior where, with the default settings (the "Hide extensions for known file types" option enabled), it hides file extensions (so if the file is named .clang-format.txt, it will appear as .clang-format in File Explorer). If you have this configuration, the only indication of the file extension is the message shown in the "Type" column when you have the folder in the "Details" view. It must show "CLANG-FORMAT file" in that column:

Not something like "Text Source File" (which would indicate the file is actually named .clang-format.txt):

I renamed the original file so I'd have a fallback if needed

clang-format from explorer:

properties:

The file highlighted is the modified version (columns 999).

How to make it be CLANG_FORMAT file?

OK, great. Now we have identified the problem!

  1. Click the ●●● icon on the File Explorer toolbar.
    A menu will open.
  2. Select "Options" from the menu.
    The "Folder Options" dialog will open.
  3. Select the "View" tab in the dialog.
  4. Uncheck the box next to "Hide extensions for known file types".
  5. Click the "OK" button at the bottom of the dialog.
    The dialog will close.
  6. Right click on the file .clang-format.txt.
    A menu will open.
  7. Select "Show more options" from the menu.
    A menu with more options will open.
  8. Select "Rename" from the menu.
    The filename of .clang-format.txt will now become an editable field.
  9. Remove the .txt from the end of the .clang-format.txt filename.
  10. Press the Enter key.
    The file will now be renamed to .clang-format

Now try formatting your "Starting code" again in Arduino IDE. Hopefully this time it will leave the code as is.

Success! See post #12

Thanks, @ptillisch and @sterretje !

note: changed 'solution' to this post from

Thread bookmarked. :man_dancing:

You are welcome. I'm glad auto format is working according to your preferences now.

Hopefully the ClangFormat/LLVM developers will be able to fix that bug at some point in the future.

Since you seem to be fairly opinionated regarding code formatting, you might end up finding that the knowledge gained here regarding how to adjust the behavior of the formatter, and to disable it selectively in cases where it isn't possible to make it work according to your preferences (or when it isn't worth the effort to figure out how to configure it), will be useful for other applications in the future.

Regards, Per

I've subscribed to the thread(?) so I'll know when/if that happens.

1 Like