Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Align your code

0.00/5 (No votes)
29 Jul 2000 1  
Align multiple lines of assignments after the equal sign, for example
  • Download source files - 1 Kb
  • AlignNext.gif (12586 bytes)

    Introduction

    I always thought that aligning code to make it look "pretty" was a big waste of time.  Later I realized that aligning code will sometimes help you find bugs simply by inspection.  You can more easily see that one line doesn't fit the pattern of the others when the code is aligned. 

    Another thing that bothered me about aligning code was that you often had to go back and re-align them later on when a variable name was changed, for instance.  What's worse is that I found myself sometimes not changing a variable name to something more appropriate because it would mean re-aligning everything!

    This macros makes aligning you code a lot easier.  Put you cursor where you want the other line to be aligned (by adding or removing spaces).  One macro goes downwards (AlignNextLine) and the other macro goes backwards (AlignPrevLine) through the file.

    If a line has too many spaces the extra spaces will be removed to make it align correctly.  Also, if a line is already aligned it does not changed it, the cursor just moves up or down.

    AlignNext2.gif (5995 bytes)

    The cursor should be next to a punctuation character. A comma or an equal sign is an example of a punctuation character as far as the macro is concerned. The spaces will be added either to the left or the right of the punctuation charater depending on the cursor position.  The function should be fairly harmless, only adding and deleting spaces.  The undo buffer will have two events - 1) delete all blanks around cursor and 2) insert the appropriate number of spaces.

    Sometimes the macro gets confused when there are lots of punctuation characters around.   My work around for this is to align a smaller amount first, then go over the lines again aligning further out the second time.  Not ideal, but then I don't want to spend more time on these macros then it would take to align all the code that I'll ever write!

    This macro is not specific to C++ it should work on any text file.

    Some details of the code:

    The macro subroutines AlignNextLine and AlignPrevLine are one line routines which call the function AlignFunction with 1 or -1 for down and up, respectively. This function in turn uses FindClosest and the IsPunt function to do some of the work.

    As a side note, I map AlignNextLine and AlignPrevLine to Ctrl+N and Ctrl+Shift+N, respectively ("N" for Next).  This will override Ctrl+N's default: create a new file, which I hardy ever use.

    Also, for these and other macros and code you can visit my site at http://www.forusers.com/fordevelopers

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here