Have code indentation follow preprocessor directives indentation
Asked Answered
F

0

6

I'm in a situation where I have code that looks like this :

#ifdef NOISE
#ifdef NOISE_2D
DECLARE_NOISE2D();
#else // NOISE_3D
DECLARE_NOISE3D();
#endif
#else
DECLARE_NO_NOISE();
#endif

Is there a way to have Clang-format look at this piece of code and format it as such :

#ifdef NOISE
    #ifdef NOISE_2D
        DECLARE_NOISE2D();
    #else // NOISE_3D
        DECLARE_NOISE3D();
    #endif
#else
    DECLARE_NO_NOISE();
#endif

The best I've been able to do is with IndentPPDirectives: BeforeHash, which gives this as a result :

#ifdef NOISE
    #ifdef NOISE_2D
DECLARE_NOISE2D();
    #else // NOISE_3D
DECLARE_NOISE3D();
    #endif
#else
DECLARE_NO_NOISE();
#endif

I also tried to pass preprocessor directives as macro block starts and ends, but it seems like they don't get picked up (worth a try).

It looks like preprocessor directives and C++ code have their own indentation tree that they don't seem to share. Is there anyway to have Clang-format give the desired output, or alternatively, is there any other automatic formatter that would be able to provide this functionality ?

Fly answered 27/10, 2020 at 16:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.