Clang-format - need help with some breaks

Hi guys,

I hope this is the correct forum to ask. After reading clang-format style options documentation, I mean nearly there with my desired style. I just have problems with two break:
First:

  struct test demo[] = {
    {56,    23, "hello"},
    {-1, 93463, "world"}
  };

Is it possible to break before first brace? To have this:

  struct test demo[] = 
  {
    {56,    23, "hello"},
    {-1, 93463, "world"}
  };

And the second case:

  switch (myarray[0][0])
  {
    case 1: {
      return 0;
      break;
    }
    case 2: {
      return 3;
      break;
    }
    default:
      break;
  }

I would like to break after case, like this:

  switch (myarray[0][0])
  {
    case 1: 
    {
      return 0;
      break;
    }
    case 2: 
    {
      return 3;
      break;
    }
    default:
      break;
  }

Thanks for any help!

Hi @Wodzu,

You should definitely check the following options at Clang-Format Style Options — Clang 13 documentation (llvm.org):

For your 2nd use case, BraceWrapping/AfterCaseLabel: true and BraceWrapping/AfterControlStatement: Always (or Multiline). You need BreakBeforeBraces: Custom for both of these options. You might check what BreakBeforeBraces: Allman gives you.

A config like:

BasedOnStyle: LLVM
BreakBeforeBraces: Custom
BraceWrapping:
  AfterCaseLabel: true
  AfterControlStatement: Always

For the first case, I doubt there’s a knob you could use. You can create a PR or add write a feature request!
Otherwise, you might also manually :frowning: add a comment after = and before { but that will indent the brace.

Hope this helps,


Marek