4.3.2. Customizing line wrapping

The following configuration snippets customize how GNATformat wraps code. They override only the relevant parts of the default unparsing configuration.

Save a snippet to a JSON file and pass it to --override-layout:

gnatformat -P prj.gpr --override-layout fill_string_concatenations.json

To use it for all formatting of a project, set the Override_Layout attribute in its Format package:

package Format is
   for Override_Layout ("Ada") use ("fill_string_concatenations.json");
end Format;

Overrides are applied in the order they are given. If several snippets configure the same construct, the last one wins.

4.3.2.1. Filling lines in string concatenations

By default, when a concatenation does not fit on one line, GNATformat breaks before every & operator. This snippet keeps as many operands as fit on each line and breaks before & when the next operand would exceed the configured line width. Short concatenations still stay on one line.

The ConcatOp template uses a fill document, and the ConcatOperandList separator makes each break before & optional. Apply this snippet after the tall layout to replace its mandatory breaks between operands as well. The operand formatting, continuation indentation and other operators keep their existing layouts. The snippet applies to all uses of &, including array concatenations, as the layout does not depend on operand types.

{
  "node_configs": {
    "ConcatOp": {
      "node": {
        "kind": "fill",
        "document": [
          {
            "kind": "recurse_field",
            "field": "f_first_operand"
          },
          "line",
          {
            "kind": "recurse_field",
            "field": "f_other_operands"
          }
        ]
      }
    },
    "ConcatOperandList": {
      "sep": [
        "line",
        "recurse"
      ]
    }
  }
}

Example

Input code:

package Messages is
   Short : constant String := "Hello, " & "world!";
   Long : constant String := "The configuration keeps " & "several operands " & "on each line, "
   & "and wraps before an ampersand " & "when the next operand " & "would exceed the line width.";
end Messages;

GNATformat output with the snippet above and a line width of 79:

package Messages is
   Short : constant String := "Hello, " & "world!";
   Long  : constant String :=
     "The configuration keeps " & "several operands " & "on each line, "
     & "and wraps before an ampersand " & "when the next operand "
     & "would exceed the line width.";
end Messages;

Comments can force additional line breaks. String literals are not split, so an individual operand that is too long can still exceed the line width.