Search code examples
c++lambdauncrustify

Uncrustify + Lambda


I'm having trouble getting uncrustify to format C++ lambdas

this is what it turns my lambda into:

auto print = [](auto const &i, qi::unused_type, qi::unused_type)
             {
                 qDebug() << i;
             };

This is what I would like:

auto print = [](auto const &i, qi::unused_type, qi::unused_type)
{
    qDebug() << i;
};

Doe anyone now what setting is responsible for the positioning of the braces?


Solution

  • Looking at your config file, it seems there are a few rogue settings:

    1. At line 223

      # Align continued statements at the '='. Default=True
      # If FALSE or the '=' is followed by a newline, the next line is indent one tab.
      indent_align_assign                       = true     # false/true
      

      As you can see, you are assigning a lambda to a symbol. Additional newlines are configured to align themselves with the = sign of the assignment.

    2. At lines 830 and 833

      # The span for aligning on '=' in assignments (0=don't align)
      align_assign_span                         = 1        # number
      
      # The threshold for aligning on '=' in assignments (0=no limit)
      align_assign_thresh                       = 0        # number
      

      If you don't feel like changing the config at line 223, maybe you can try playing around with this pair of settings.

    If changing any of those settings is not really plausible for you, you might have to make some serious code style reforms. For one, I see that the example lambda is suitable as one line statement. Maybe keep them in single lines. (They will stay that way as Line 964 in your config file ensures that).