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: https://github.com/arduino/arduino-language-server/pull/69

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