Auto Format settings

Can the Auto Format actions be customised as was possible in earlier versions ?

Not currently, but there is work in progress to allow this:

I have also created a formatter configuration that matches the classic Arduino IDE's auto format as closely as possible and that configuration will be used by Arduino IDE 2.x as soon as support is added for custom configurations. Unfortunately, due to changing to a new formatter tool, it is not possible to get a perfect match.

The new formatter tool is clang-format:
https://clang.llvm.org/docs/ClangFormat.html
This is far more configurable than the "Artistic Style" formatter used by the classic Arduino IDE. clang-format is part of the extremely popular LLVM project. The Artistic Style project is no longer actively maintained, so it was probably good to move on to something with a more certain future.

Unfortunately, due to changing to a new formatter tool, it is not possible to get a perfect match.

I don't want a match, but I do like to be able to move { and } onto their own lines, to remove blank lines in functions and to add spaces between function parameters and other odds and ends using Auto Format as I have done with the current IDE which is the first thing that I do when copying code copied from the forum unless I want to retain line numbering pertinent to error messages

Thank you for the links, which I will take a look at

I think you'll find that clang-format gives you all the options you could possibly want. There are somewhere around 150 different configuration options!

Even though it is very configurable, the problem I ran into is that it is very prescriptive by its very nature. With most of the formatting settings, you have a good deal of choice in how you configure it, but the one choice you don't have is to disable formatting altogether. You brought up the subject of attached vs broken braces. Artistic Style allows you to set a brace style, but it also allows you to leave the braces alone. In the classic Arduino IDE with the default formatter settings, people can use attached or broken, or more often some random mishmash of both without any pattern at all. But with clang-format you have to pick a style and all your code is formatted to that style.

The reason is because clang-format works from an abstract syntax tree (AST) of the code, rewriting it according to the formatting rules. So it doesn't know anything about how the braces looked when the program was lexed, and thus doesn't have any way to put them back as they were when it writes the program. In theory, this is a much more robust approach than the filter approach used by Artistic Style because it means that clang-format actually understands the structure of the code it's tinkering with, and thus has less likelihood of screwing it up. On the other hand, Arduino has been using Artistic Style for 7 years and I only remember a few reports of problems with it (related to raw string literals, which has actually been fixed but not released), so it works very well!

with clang-format you have to pick a style and all your code is formatted to that style.

In the case of moving braces to their own line, that is no problem. I would want them all on their own line as I have Auto Format configured now, nor do I see any of the other formatting rules that I currently have set up being a problem if applied throughout the code to which they are applied

The problem is for the people using the default configuration. Right now, if you use broken braces in the stock installation of the classic Arduino IDE and do an auto format, they are left alone. But if you do the same in Arduino IDE 2.x, all your precious broken braces become attached. I think this could be annoying to some users.

But they will have the option of configuring the formatter to their preferences. Cristian is even adding the capability for advanced users to add a formatting configuration file to the sketch folder, which will allow maintaining a consistent formatting configuration for that sketch when collaborating on it with multiple people.

And when it comes to probably the majority of Arduino users who end up with atrociously messy code because they are focusing all their attention on learning, a more prescriptive formatter will have a beneficial effect. I was actually a bit disappointed to have to disable some of the useful formatting options when I was trying to create a configuration similar to the classic IDE.

For example, I set "KeepEmptyLinesAtTheStartOfBlocks" to true because the classic IDE allows it, but I can't imagine any valid reason to have empty lines at the start of a block, and we certainly see a lot of skethes here riddled with random pointless blank lines. Oddly, there is no companion "KeepEmptyLinesAtTheEndOfBlocks". It always removes these lines, which is a bit of a problem since the "BareMinimum" sketch that you get from File > New has such lines. There is a related one you might like: "MaxEmptyLinesToKeep". I would be happy with that set to 2 because I don't think it's ever necessary to use more than 2.

if you do the same in Arduino IDE 2.x, all your precious broken braces become attached. I think this could be annoying to some users.

It certainly annoys me, hence me investigating the option to change the formatting rules

I like IDE 2.0 a lot; thank you!
But I dislike "attached braces" so much that I may quit using IDE 2.x until broken can be selected.

Hi @catacluj. Work is in progress to allow for customizing the auto format configuration any way you like. You can get an idea of how that will work here:

The auto formatter tool used by Arduino IDE 2.x is ClangFormat. The documentation for its configuration is here:
https://clang.llvm.org/docs/ClangFormatStyleOptions.html

You can see that it gives the option for pretty much any code style you could possibly want.

Great; I wonder when.
Cat

@pert Is there any news on user formatting options. As it is the lack of it has stopped me using 2.0

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.

@pert

Auto Formatting is broken in beta 12

Open a new sketch and you get the usual BareMinimum sketch like this

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

Press Ctrl + T and you get this

void setup() {
  // put your setup code here, to run once:
}

void loop() {
  // put your main code here, to run repeatedly:
}

So far so good, although it is not actually in the format that I prefer

Now insert 3 blank lines after the comment in setup() then press ctrl+T again and the loop() function is deleted along with one of the blank lines in setup()

void setup() {
  // put your setup code here, to run once:


}

There is more fun to be had if you add the 3 extra blank lines in setup() before using ctrl+T for the first time as nothing happens, but press ctrl+T again and you are left with

void setup() {
  // put your setup code here, to run once:



}
v

}

After this I stopped looking for problems with Auto formatting

1 Like

Thanks @UKHeliBob! I gave you a mention on GitHub, but I don't know whether you have notifications enabled over there, so I'll comment here as well. I am able to reproduce this. I also found there to be a correlation with the "Auto Save" preference setting. I submitted a formal bug report to the IDE developers here:
https://github.com/arduino/arduino-ide/issues/543

Thanks for the feedback

I am not sure of my GitHub settings but will take a look

Maybe more "olds" than "news" after so many months delay in the response, but there is some good news:

The work on the Arduino Language Server linked to previously in this thread was completed and Arduino IDE 2.x is using the version of Arduino Language Server with that capability now. That brought the support for per-sketch custom formatter configuration by putting a .clang-format file in the root of the sketch folder. As you noted, the Auto Formatter has problems, but it does correctly apply the configuration defined in this file in the occasions when it works at all.

So the piece of the custom formatter configuration project that is missing is the ability to set a global custom configuration, similar to the Arduino IDE's Arduino15/formatter.conf approach. All the work has already been done in Arduino Language Server to support that capability, so now it only remains for Arduino IDE 2.x to make use of it. That requirement is tracked here:
https://github.com/arduino/arduino-ide/issues/566