Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Clang formatting for {} and access modifers

In VsCode I would like to change my Clang formatting to :

class Test {
    public:
        Test() : tmp(0)
        {
            if (0 == 0) {
                std::cout << "hello" << std::endl;
            }
        }
        ~Test();

    protected:
    private:
        int tmp;
};

But currently I got :

class Test {
   public:
    Test() : tmp(0) {
        if (0 == 0) {
            std::cout << "hello" << std::endl;
        }
    }
    ~Test();

   protected:
   private:
    int tmp;
};

My CLang settings :

{ BasedOnStyle: Google, IndentWidth: 4 }

like image 750
Victor Avatar asked Jan 24 '26 02:01

Victor


1 Answers

About the brace breaking style

The brace you want to break is after a function, so you would want to add this to Clang setting:

BreakBeforeBraces: Custom
BraceWrapping: 
  AfterFunction: true

You can find other BraceWrapping options in the Clang-Format Style Options.

About the modifiers indentation

At present, Clang provides only an option to adjust modifiers indentation - AccessModifierOffset. This option will offset in or out from the IndentWidth you set. It is not currently possible to make modifier indentation standard. Fortunately, it is currently being discussed and reviewed here.

You can set this to make it less annoying:

AccessModifierOffset: 0

Here is what you get if you set all above options:

class Test {
    public:
    Test() : tmp(0)
    {
        if (0 == 0) {
            std::cout << "hello" << std::endl;
        }
    }
    ~Test();

    protected:
    private:
    int tmp;
};
like image 51
thanhph111 Avatar answered Jan 26 '26 18:01

thanhph111