Automatic styling of Ada code?

My experimental Ada program now consists of almost 30.000 lines and is becoming less of a personal playgroud. Before release I should transform it according to a well known/common style guide. Any hint about how to do this in a quick way - something more than the simplest code formatters and beautifiers?

reinert

I use gnatformat --no-subprojects --charset=utf8 (or alr exec -- gnatformat --no-subprojects --charset=utf8 with alire).

2 Likes

In addition, you can also add a package inside your GPR to configure gnatformat, ex:

project Amazing_Thing is
  -- you config

   package Format is
      for Width ("Ada") use "119"; -- Max char per line
      for Charset ("Ada") use "utf-8"; -- default encoding
   end Format;
end Amazing_Thing;

You can refer to the official documentation for more details.

Furthermore, you can define a pre-commit hook to format the code automatically before committing. In VS-Code you can also configure Format-On-Save. For GNAT Studio, I don’t know.

3 Likes

Is there a way to default to --no-subprojects? I didn’t find how to do that in the documentation.

That default choice is quite weird as it tries to format all withed libraries living their own life outside the project. I don’t care if VSS (or others) don’t follow gnatformat rules, as it’s not my code…

Good question, I don’t know.

On my side, I use a pre-commit script that does this: alr exec -- gnatformat -P My_Project.gpr
It does not format Alire dependencies.

Unfortunately, there is no 1:1 flag configuration…

Thanks, adding the project as argument works. I don’t know why I expected the same behaviour without the argument. Maybe it seemed logical within an alire crate to only format the current crate and not the dependencies…

I set format file as keyboard and mouse button shortcuts in preferences.

J.P. Rosen is with a company called AdaLog, and they provide a utility for checking Ada rules (very customizable), and I think that there’s a formatter, or at least formatting checker: AdaControl.

2 Likes

J.P. Rosen was my N+1, and he is my company’s PhD supervisor. I work for Novasys Ingénierie, where AdaLog is a subsidiary.

Yes, in AdaControl, we have a few “formatting” rules (called Style): AdaControl User Guide > Style
It is possible to apply auto-fix, which acts as gnatformat. BUT, AdaControl is firstly a coding rule verification tool, not a formatter. So it is not as convenient as gnatformat for formatting.

Furthermore, due to ASIS-4-GNAT end-of-life, AdaControl only supports Ada up to Ada 2012.

I don’t care if VSS (or others) don’t follow gnatformat rules, as it’s not my code…

We understand this argument (it was actually how I first implemented), however, ultimately we decided to follow the default libgpr2 behaviour (for consistency with other tools in AdaCore), which means that withed projects that are not externally built, are formatted. I’m not super familiar with Alire, but I suspect that’s why you don’t see Alire dependencies being formatted.

The behaviour could be specified in the gpr file within the Format package (like Charset). Something like Subprojects := False or something like that.