Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 327499

Summary: Correct Indentation after ternary expr ( <stmt> ? <stmt> : <stmt;) incorrect
Product: [Tools] CDT Reporter: Cameron Miller <cameron.miller>
Component: cdt-editorAssignee: Project Inbox <cdt-editor-inbox>
Status: RESOLVED WORKSFORME QA Contact: Anton Leherbauer <aleherb+eclipse>
Severity: minor    
Priority: P3 CC: cdtdoug, marotig, zeratul976
Version: 6.0.2   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
Test cases for indentation issues of the ternary operator none

Description Cameron Miller CLA 2010-10-11 23:34:22 EDT
Build Identifier: 20100218-1602

If you enter a new line at various points in the middle or after the ternary statement, the indentation of the newline is wrong.

It may only happen if done as part of a method argument (or possibly even if it is a vararg??)

In the following example, the second line "true)'" should not be indented so far: 
   printf("%s %d", true ? "TRUE" : "FALSE",
                        true);

Also if you enter a new line after the colon ':' the indentation seems to be wrong too:
   printf("%s %d", true ? "TRUE" 
                          : "FALSE", true);

Lastly if you enter a new line after the colon ':':
   printf("%s %d", true ? "TRUE" :
                        "FALSE", true);

Reproducible: Sometimes

Steps to Reproduce:
1. use a ternary operator as a method argument
2. after the comma, press the enter key to create a new line
3. the new line is indented incorrectly
Comment 1 Anton Leherbauer CLA 2010-10-15 02:59:16 EDT
(In reply to comment #0)
> Build Identifier: 20100218-1602

Please clarify the CDT version you are using.
Comment 2 Cameron Miller CLA 2010-10-15 03:51:14 EDT
Build Identifier was for Eclipse. The CDT details are:

Eclipse C/C++ Development Tools

Version: 6.0.2.201002161416
Build id: 201002161416
Comment 3 Anton Leherbauer CLA 2010-10-15 04:30:59 EDT
This seems to work for me.
The new line is indented 2 levels deeper (the default line continuation indent) regardless where I press enter.

If you are using a custom code style, please attach it here (as exported XML file).
Try also a newer version of CDT, you won't get a fix in the 6.0.x stream anyway.
Comment 4 Cameron Miller CLA 2010-10-15 05:26:22 EDT
Ok, I'll try the newer version. 
Note that I didn't even realise there was a newer version - I've been using Eclipse's "Check for updates" and it says there are none. I guess it doesn't compare completely different releases of eclipse or major version changes to plugins... Instead I've subscribed to the eclipse downloads RSS feed to keep up to date.
Comment 5 Gabor CLA 2014-10-23 12:07:51 EDT
Issue:
    incorrect indentation after ternary operator

Version of Eclipse:
    - Luna 4.4.1, Build id: 20140925-1800, Windows 7
    - workspace with default settings

Example (showing the left margin)
    Expected outcome:
        |String res = condition
        |        ? bar() : bar();
        |proof_of_pudding();
    
    Actual outcome:
        |String res = condition
        |        ? bar() : bar();
        |    proof_of_pudding();  // <--- incorrect

Remarks
    - The problem appears to be triggered by two conditions on the `then' branch
      of the ternary expression:
      (i)  the `then' branch is to a new line (no matter whether the break is
           before or after the question mark);
      (ii) the `then' branch ends with a closing parenthesis.
    - Attached please find a file with a number of test cases.

Thank you in advance for looking at this issue.

Regards,
G.
Comment 6 Gabor CLA 2014-10-23 12:09:05 EDT
Created attachment 248137 [details]
Test cases for indentation issues of the ternary operator
Comment 7 Nathan Ridge CLA 2014-10-24 02:51:27 EDT
(In reply to Gabor from comment #6)
> Created attachment 248137 [details]
> Test cases for indentation issues of the ternary operator

This attachment is a Java file.

This bug concerns the C++ formatter (as it's in the CDT product).

Is it possible you meant to comment in (or file) a bug in the JDT product instead?
Comment 8 Gabor CLA 2014-10-24 04:04:59 EDT
> Is it possible you meant to comment in (or file) a bug in the JDT product
> instead?

It absolutely *is*.  Feel free to ignore/remove my report, I'll re-post it for JDT.

Regards, and sorry for the pollution,
G.
Comment 9 Nathan Ridge CLA 2017-01-07 18:45:51 EST
Closing per comment 3. Feel free to reopen if you're still seeing this.