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

1

6

I would like to align all C++ class member names ( do not confuse with member types ) in one column.

Lets look at the example of what we have at entrance:

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;
};

So the longest member type declaration defines the column to which class member names will be aligned. How can i perform such transformation in emacs ?

Nuno answered 13/5, 2010 at 11:20 Comment(6)
You could use align-regexp with some funky stuff to sort of achieve the effect but I agree with Neil.Calvincalvina
What happens when Jimmy on your team adds a new method that returns a const Thingummyjiggle*? Should he edit every other line in your header file?Syl
Neil: It is hard to discuss about one's preferences :) I personally think that this form of class memeber formatting is nice and elegant. It also helps to you to read member methods faster without straining your eye muscles too much.Nuno
Noufal: Thx for the tip. I will check that out.Nuno
Johnsyweb: Basicly yes, whole file or at least part of it ( public section, group of similar methods ) should be parsed to meet the formatting standard. What I want is to be able to select some part of the class and format it according to above description.Nuno
Surprisingly unhelpful comment, Neil.Kiernan
W
6

Select the region with the method declarations

M-x align-regexp

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

Edited to add the ; in the matching, which aligns the member variable as well.

Whittling answered 13/5, 2010 at 13:25 Comment(2)
I added the semicolon which aligns the member variable as well.Dwaindwaine
and @Tray, your merged solution solves the posted problem. Thank you both :)Nuno

© 2022 - 2024 — McMap. All rights reserved.