Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 354626 - [formatter] Never join wrapped lines allows braces on next line
Summary: [formatter] Never join wrapped lines allows braces on next line
Status: CLOSED DUPLICATE of bug 303519
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.8   Edit
Hardware: PC Windows 7
: P3 trivial with 7 votes (vote)
Target Milestone: 4.5   Edit
Assignee: JDT Core Triaged CLA
QA Contact: Ayushman Jain CLA
URL:
Whiteboard:
Keywords:
: 470433 (view as bug list)
Depends on:
Blocks:
 
Reported: 2011-08-12 10:17 EDT by Brad Cupit CLA
Modified: 2015-09-08 14:50 EDT (History)
8 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Brad Cupit CLA 2011-08-12 10:17:32 EDT
Build Identifier: 20110615-0604

With "Never join already wrapped lines" and Braces set to "Same line", users can circumvent the Brace style manually.

This code:

public static void main(String[] args)
{
    System.out.println("hi");
}

should instead look like this code since the Brace style is "Same line":

public static void main(String[] args) {
    System.out.println("hi");
}

Brace style should override "Never join already wrapped lines"

I understand this is subjective (which style _should_ override the other?) but I want the brace style enforced and still prevent lines from auto-joining.

Reproducible: Always

Steps to Reproduce:
1. make a copy of the Eclipse [built in] formatter
2. Edit it and in "Line Wrapping" check "Never join already wrapped lines"
3. Write code with a brace on next line:
   public static void main(String[] args)
   {
       System.out.println("hi");
   }
4. Run the formatter
5. Notice the brace is still on its own line

EXPECTED: the formatter should move the brace so it's on the same line, after "args)"
Comment 1 Ayushman Jain CLA 2011-08-12 10:38:49 EDT
I agree that it may be a bit of an inconvenience. Will investigate, time permitting.
Comment 2 Matthew CLA 2011-08-26 04:36:07 EDT
Not unexpected, but this is not a Windows specific bug, can confirm also affecting Ubuntu. 

It is also affecting our team considerably due to us having company coding conventions and pre-commit hooks for subversion that enforce the coding standard.

If you could point us in the right direction, we may be able to patch this.
Comment 3 Ayushman Jain CLA 2011-08-27 07:27:26 EDT
You can try debugging /org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java and /org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/Scribe.java#printNewLine() and see where the formatter is inserting a new line before opening brace.
Comment 4 Nick Johnson CLA 2011-10-18 22:45:51 EDT
FWIW, I think the way it worked in Helios was the desired behaviour; I use that combination of settings, and Eclipse does the "right" thing (where right is, of course, subjective).

A potential workaround is to put the // @formatter:off and // @formatter:on comments around blocks you don't want formatted, and then uncheck "never join", though if you have a lot of those, it's going to be cumbersome.

My personal preference would be for line wrapping to have the lowest precedence after all the other line-wrap-affecting settings, like the braces settings, blank lines, new lines, control statements, and comments, etc.
Comment 5 Nick Johnson CLA 2011-10-18 22:47:37 EDT
(Also we're seeing this behavior on Mac and Windows, so as someone else also noted with Ubuntu, it's almost certainly not platform-dependent.)
Comment 6 Travis Mising name CLA 2015-02-20 13:40:10 EST
(In reply to Nick Johnson from comment #4)
> FWIW, I think the way it worked in Helios was the desired behaviour; I use
> that combination of settings, and Eclipse does the "right" thing (where
> right is, of course, subjective).
> 
> A potential workaround is to put the // @formatter:off and // @formatter:on
> comments around blocks you don't want formatted, and then uncheck "never
> join", though if you have a lot of those, it's going to be cumbersome.
> 
> My personal preference would be for line wrapping to have the lowest
> precedence after all the other line-wrap-affecting settings, like the braces
> settings, blank lines, new lines, control statements, and comments, etc.

I agree that the old behavior is (subjectively) "right."  I am surprised that this is listed as trivial - to ensure consistent code style across a team, the formatter should be able to fix simple things like brace location automatically, while respecting the 'never join lines' option to avoid the formatter making sweeping changes that potentially reduce code readability.
Comment 7 Mateusz Matela CLA 2015-06-28 16:51:47 EDT
*** Bug 470433 has been marked as a duplicate of this bug. ***
Comment 8 Mateusz Matela CLA 2015-09-08 14:50:38 EDT
This problem no longer occurs after the formatter redesign.

*** This bug has been marked as a duplicate of bug 303519 ***