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

Bug 499648

Summary: "Never join already wrapped lines" counterintuitive for ternary operators
Product: [Eclipse Project] JDT Reporter: Atsushi Nakagawa <atnak>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: keinohrschlumpf2, mateusz.matela
Version: 4.6   
Target Milestone: ---   
Hardware: PC   
OS: Windows 10   
Whiteboard: stalebug

Description Atsushi Nakagawa CLA 2016-08-14 21:54:59 EDT
Regarding to ternary operators  (`a ? b : c`), the Formatter option "Never join already wrapped lines" *does not appear to do anything*.

My Line wrapping is configured like so:
[x] Never join already wrapped lines
[x] Prefer wrapping outer expressions

Expressions > Conditionals:
Line wrapping policy: [Wrap all elements, every element on new line]
[ ] Force split, even if line shorter than maximum line width
[x] Wrap before operators


This results in:

```
// 1. This line shorter than maximum line width:
return argument ? 100000 : 20000;
// Is formatted to: (as expected)
return argument ? 100000 : 20000;

```

```
// 2. This line shorter than maximum line width:
return argument ?
    100000 : 20000;
// Is formatted to: (Not expected!)
return argument ? 100000 : 20000;

```

```
// 3. This line shorter than maximum line width:
return argument
    ? 100000 : 20000;
// Is formatted to:
return argument
    ? 100000
    : 20000;
```

The result for 2 is unexpected because this is what "Never join already wrapped lines" was thought to counter.
Comment 1 Mateusz Matela CLA 2016-08-16 14:23:55 EDT
In example 2 the formatter did not respect the existing wrap, because it was on the other side of the operator. Do you want the formatter to keep inconsistent wraps?

"Wrap all elements, every element on new line" means the formatter should either keep everything in one line if possible, or wrap all elements. So "never join..." doesn't make a lot of sense with this assumption. Current interpretation of "never join.." is that if there's at least one wrap, then all elements are wrapped (as you see with example 3).
It seems the behavior you want is achieved with policy "Wrap where necessary", isn't it?
Comment 2 Atsushi Nakagawa CLA 2016-08-19 20:00:55 EDT
> Do you want the formatter to keep inconsistent wraps?

Yes is ultimately the answer to this.  What it comes down to is that I was after the ability to keep two styles:

Style 1:
```
int some_variable = something_really_long_here ?
        a_call_foo_method() : a_call_bar_method();
```

Style 2:
```
int some_variable = something_really_long_here
        ? (a_call__foo_method() ? 1000 : 2000)
        : a_call_bar_method() + 30000;
```

However, I did some more testing and found that the formatter will always proactively join lines that are wrapped inconsistently to the "Wrap before operators" option.   Style 1 is joined if "Wrap before operators" is checked and Style 2 if not.

This does not appear to be affected by the choice for "Line wrapping policy" nor "Never join already wrapped lines" and was/can be the cause of initial confusion.
Comment 3 Heribert Samweis CLA 2017-06-19 11:17:51 EDT
In my current team, we normally use this style throughout:

```
String radius = System.currentTimeMillis() < 0 ?
    "large".toUpperCase(Locale.ENGLISH) :
    "small".toUpperCase(Locale.ENGLISH);

```

Currently there is no way to stop eclipse from collapsing this into the following:

```
String radius = System.currentTimeMillis() < 0 ? "large".toUpperCase(Locale.ENGLISH) : "small"
    .toUpperCase(Locale.ENGLISH);

```

"Never join existing lines" and "Prefer wrapping outer exception" have no effect, and I can't find a "Wrap where necessary" option that corresponds to ternary operators. Is it missing? Also I can't find a "Wrap before operators" options, maybe it was added very recently? (using Neon.3 Release (4.6.3) Build id: 20170314-1500)
Comment 4 Mateusz Matela CLA 2017-06-19 16:45:50 EDT
(In reply to Heribert Samweis from comment #3)
> I can't find a "Wrap where necessary" option that corresponds to
> ternary operators. Is it missing?

In the Line Wrapping tab, select Exressions -> Conditionals.
From your descriptions it seems you have it set to "Do not wrap".
Comment 5 Eclipse Genie CLA 2020-03-29 05:40:44 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. As such, we're closing this bug.

If you have further information on the current state of the bug, please add it and reopen this bug. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.