Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 247928 - [Formatter] Provide/enhance newline policy for curly braces
Summary: [Formatter] Provide/enhance newline policy for curly braces
Status: VERIFIED DUPLICATE of bug 549249
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.5   Edit
Hardware: All All
: P5 enhancement with 1 vote (vote)
Target Milestone: 4.18 M3   Edit
Assignee: Mateusz Matela CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 373723 (view as bug list)
Depends on:
Blocks:
 
Reported: 2008-09-19 02:46 EDT by Eike Stepper CLA
Modified: 2020-11-18 04:26 EST (History)
7 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Eike Stepper CLA 2008-09-19 02:46:34 EDT
Hi,

Unfortunately I belong to those who are really picky about their code format ;-)
The Java formatter is really great!! 

Only one thing is missing for me and this becomes more important the more committers join my project (CDO):
I'm not able to express the following format policy:

    if (cond)
    {               <------ Remove all newlines after opening curly braces [1]
      if (cond)
      {
        {
          stmt();
        }
                    <------ Force 1 newline between closing and opening curly braces [2]
        {
          stmt();
        }           <------ Remove all newlines between two closing curly braces [3]
      }
                    <------ Force 1 newline after closing curly brace *if* no more closing curly braces follow [4]
      stmt();
                    <------ Force 1 newline before comments [5]
      // comment
      // comment
      stmt();
    }

[2] could be included in [4] already.

My intention is to better highlight "blocks" of code. 
Of course all this is subjective but I have evidence that it is appealing to certain people ;-)
- http://dev.eclipse.org/newslists/news.eclipse.tools.emf/msg32549.html
- http://dev.eclipse.org/newslists/news.eclipse.tools.emf/msg32548.html

Even if you're willing to consider this request I'm not sure about the best way to present
the functionality to the user. But I'd be glad to take part in a discussion!!

Thanks for your great work in general
/Eike
Comment 1 Frederic Fusier CLA 2008-09-19 03:29:16 EDT
I surely will not have enough time to address this request during the 3.5
development process, hence set its priority to P5.
Please provide a patch if you definitely need it in this version and I'll have
a look at it...
TIA
Comment 2 Eike Stepper CLA 2008-09-19 03:40:32 EDT
Sure, I'll try to come up with a patch. Can you give me some starting points in the formatter code?

I would focus on my needs and certainly offer a checkbox to turn it on/of, ok?
If you have ideas how to better parameterize this, I'd approciate your comments...
Comment 3 Eike Stepper CLA 2008-09-22 04:34:12 EDT
I looked into org.eclipse.jdt.internal.formatter.CodeFormatterVisitor but I fear I have no clue how to patch it ;-(
Comment 4 Frederic Fusier CLA 2008-09-22 05:46:53 EDT
(In reply to comment #3)
> I looked into org.eclipse.jdt.internal.formatter.CodeFormatterVisitor but I
> fear I have no clue how to patch it ;-(
> 
I'm not an expert of this class as I took the responsibility of this part of JDT/Core few months ago. However, here are some clues which may help you to start writing a patch...

This class is responsible to visit the ASTNode tree built after having parsed  the code to format. So, the first thing to do is to identify where you need to insert your changes during the visit of the tree.

In your case, the snippet you gave in comment 0 contains only 'if' statements. Looking at the grammar (file java.g in folder 'grammar' of o.e.jdt.core project), you can see that these kind of statements have the following syntax:
IfThenStatement ::=  'if' '(' Expression ')' Statement

In the same file, on the line below, you can see the method of the Parser called when this syntax is finally identified:
/.$putCase consumeStatementIfNoElse(); $break ./

Then, you go the org.eclipse.jdt.internal.compiler.parser.Parser class and have a look to this method. You can see that when this node has been parsed, the parser stores a 'IfStatement' ASTNode in its stack.

So, go back to the CodeFormatterVisitor and you can see that it overrides the ASTVisitor default implementation to visit this kind of node. Now, you first need to understand the code already written and then try to add your changes to implement your new functionality in this area...

While reading the code, you'll also need to understand what the Scribe do. This class is responsible to create the necessary edits to change the code. The best way to learn about it is to follow it step by step in a debug session while formatting a code snippet similar of what you want to do (typically braces in if statements in your case)...

Finally, you surely also need to add a constant to activate your new functionality through JDT/UI preferences. The JDT/Core part for this needs to be done in org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants. The way to learn about this is to look at how an existing constant is used in JDT/Core and JDT/UI lands and do similarly for the constant you want to add...

HTH
Comment 5 Ayushman Jain CLA 2012-03-09 02:14:38 EST
*** Bug 373723 has been marked as a duplicate of this bug. ***
Comment 6 Oleksandr Maksymchuk CLA 2012-03-09 08:57:30 EST
Please look at additional options and more comprehensive examples in #373723 when will be working on this.
Comment 7 Paweł Urban CLA 2014-01-21 11:12:01 EST
I would love to see this feature as well... It is really what I need.
Comment 8 Mateusz Matela CLA 2020-10-25 17:09:26 EDT
Most of the requested features are provided with bug 549249 and bug 550083.
If anything important is missing, please submit a new bug report.

*** This bug has been marked as a duplicate of bug 549249 ***
Comment 9 Manoj N Palat CLA 2020-11-18 04:26:52 EST
(In reply to Mateusz Matela from comment #8)
> Most of the requested features are provided with bug 549249 and bug 550083.
> If anything important is missing, please submit a new bug report.
> 
> *** This bug has been marked as a duplicate of bug 549249 ***

Agree with Mateusz in terms of raising individual bugs if any important ones missing

Verified for Eclipse 4.18 M3 Version: 2020-12 (4.18) with Build id: I20201117-0600