Toby
Toby

Reputation: 10144

Formatting of new line indentation of if conditions in Eclipse CDT

In Eclipse (4.4) CDT (8.5) on windows (7) the indentation of new lines within if and else if conditions does not make sense to me. Where in eclipse is this set?

I have tried looking in Window > Preferences > C/C++ > Code Style > Formatter but could not see any relevant setting.

An example where I hit the return key as I type the conditions or select the lines and press Ctrl+I ("Fix Indentation"). Note that the first four lines all have the same indentation despite differing levels of nesting. The following fifth line indents two more levels than the preceding line, as does the seventh line both with no apparent relation to the level of nesting.

else if (((wp == SMB_Protocol_Write_Byte) && (qcn >= 1)
        && (rp != SMB_Protocol_Process_Call) 
        && (rp != SMB_Protocol_Block_WR)) 
        || ((wp == SMB_Protocol_Write_Word) && (qcn >=2) 
                && (rp != SMB_Protocol_Block_WR)) 
                || ((wp == SMB_Protocol_Block_Write) 
                        && (qcn >= (CMD_Number_Bytes_max + 1)))) {
    e = EXIT_FAILURE;
}

I would prefer something along the lines of the following:

else if (((wp == SMB_Protocol_Write_Byte) && (qcn >= 1)
                && (rp != SMB_Protocol_Process_Call) 
                && (rp != SMB_Protocol_Block_WR)) 
            || ((wp == SMB_Protocol_Write_Word) && (qcn >=2) 
                && (rp != SMB_Protocol_Block_WR)) 
            || ((wp == SMB_Protocol_Block_Write) 
                && (qcn >= (CMD_Number_Bytes_max + 1)))) {
    e = EXIT_FAILURE;
}

Is it possible to change this? Also if someone could comment with an explanation of the current indentation/nesting formatting it would be appreciated!

EDIT: Realised this may be similar to Eclipse JDT Code Formatter: Indent parameter in IF-Statement

Upvotes: 2

Views: 3029

Answers (1)

Jonah Graham
Jonah Graham

Reputation: 7970

Is it possible to change this?

Yes. With the default indentation strategy (K&R built-in) I get the same as you with Ctrl+I, but if I do Ctrl+Shift+F (format code) I get the following.

} else if (((wp == SMB_Protocol_Write_Byte) && (qcn >= 1)
        && (rp != SMB_Protocol_Process_Call)
        && (rp != SMB_Protocol_Block_WR))
        || ((wp == SMB_Protocol_Write_Word) && (qcn >= 2)
                && (rp != SMB_Protocol_Block_WR))
        || ((wp == SMB_Protocol_Block_Write)
                && (qcn >= (CMD_Number_Bytes_max + 1)))) {

If you want to get column alignment, rather than simple indentation on continued lines, create your own formatting profile and change the following:

  • Indentation tab -> Tab policy to either Spaces only or Mixed
    • With the default of Tabs only the indents will be to the next tab stop
  • Line Wrapping tab -> Expressions/Binary Expressions set Indentation policy to Indent on column

This is what you end up with when you format (but not when you simply correct indentation):

} else if (((wp == SMB_Protocol_Write_Byte) && (qcn >= 1)
            && (rp != SMB_Protocol_Process_Call)
            && (rp != SMB_Protocol_Block_WR))
           || ((wp == SMB_Protocol_Write_Word) && (qcn >= 2)
               && (rp != SMB_Protocol_Block_WR))
           || ((wp == SMB_Protocol_Block_Write) && (qcn
                   >= (CMD_Number_Bytes_max + 1)))) {

Here are screenshots showing the settings:

spaces instead of tabs

and

indentation policy

Upvotes: 4

Related Questions