How to align C ++ class member names in one column in emacs?

I would like to align all members of a C ++ class (not to be confused with member types) in one column.

Let's look at an example of what we have at the input:

class Foo { public: void method1( ); int method2( ); const Bar * method3( ) const; protected: float m_member; }; 

and this is what we would like to have at the end:

 class Foo { public: void method1( ); int method2( ); const Bar * method3( ) const; protected: float m_member; }; 

Thus, declaring the longest type of an element defines a column to which class member names will be bound. How can I do this conversion in emacs?

+6
c ++ code-formatting indentation emacs
source share
1 answer

Select a region using method declarations

 Mx align-regexp 

Type the string [^ ]+\((\|;\) and press Enter

Edited to add ; to match, which also aligns the member variable.

+6
source share

All Articles