Chivyham Humber
Chivyham Humber

Reputation: 51

How to let Clang-format allow custom new line?

I meet a problem while using clang-format.

Sometimes, I want to break a line in a custom way. For example, I wanna format following codes.

gParticleList = (ParticleType*) mmap(NULL, sizeof(ParticleType) * gGridNum, \
PROT_READ | PROT_WRITE, MAP_SHARED, 0, 0);

The expected result is like this:

gParticleList = (ParticleType*)mmap(NULL, sizeof(ParticleType) * gGridNum, \
                                    PROT_READ | PROT_WRITE, MAP_SHARED, 0, 0);

The \ is added by myself manually. I want clang accept line breaks defined by myself.

However, clang-format always formats delete my \, and give out code like this:

gParticleList =
        (ParticleType*)mmap(NULL, sizeof(ParticleType) * gGridNum, PROT_READ | PROT_WRITE, MAP_SHARED, 0, 0);

I know I can disable clang-format with some macros. But I want to learn some better method to meet my need.

Upvotes: 0

Views: 887

Answers (1)

Eric Backus
Eric Backus

Reputation: 1924

Here are a couple ideas:

  1. Disable formatting entirely around your code, by putting // clang-format off before the code and // clang-format on after your code. This is inconvenient, but will let you completely manually control the formatting of your code.

  2. Sometimes it is sufficient to force a line break where you want it. You can do this by putting an empty comment // at the place where you want a line break. Clang-format may change indentation and spacing, but won't move the comment to a different line and won't move code from one side of the comment to the other side. In your example above, you would replace your \ with the empty comment.

Upvotes: 1

Related Questions