Bug 98037

Summary: [formatter] Templates with multiple lines are inlined when used
Product: [Eclipse Project] JDT Reporter: Eduardo Rocha <eduardorochabr>
Component: CoreAssignee: Olivier Thomann <Olivier_Thomann>
Status: CLOSED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: martinae, teicher-bugzilla
Version: 3.1   
Target Milestone: 3.1 RC2   
Hardware: PC   
OS: All   
Whiteboard:
Attachments:
Description Flags
Apply on HEAD
none
Apply on HEAD of org.eclipse.jdt.core.tests.model none

Description Eduardo Rocha CLA 2005-06-01 22:38:01 EDT
3.1 RC1

1. Go to Preferences > Java > Editor > Templates;
2. Use code formatter is checked;
3. New...
4. Name: obj, Context: java, Pattern:
Object obj = new Object();
Object obj2 = new Object();

When the above template is used, I get:
Object obj = new Object();Object obj2 = new Object();
Comment 1 Olivier Thomann CLA 2005-06-02 08:23:28 EDT
*** Bug 98040 has been marked as a duplicate of this bug. ***
Comment 2 Olivier Thomann CLA 2005-06-02 10:03:32 EDT
Created attachment 22228 [details]
Apply on HEAD

Proposed patch.
When probing, I check first for statements and then for body declarations. Your
template could be seen as two local variable declaration statements or as two
field declarations.
Comment 3 Olivier Thomann CLA 2005-06-02 10:04:30 EDT
Created attachment 22230 [details]
Apply on HEAD of org.eclipse.jdt.core.tests.model

Regression test
Comment 4 Philipe Mulet CLA 2005-06-02 10:05:23 EDT
+1 for RC2
Comment 5 Olivier Thomann CLA 2005-06-02 10:07:49 EDT
Fixed and released in HEAD.
Regression test added in
org.eclipse.jdt.core.tests.formatter.FormatterRegressionTests.test577
Comment 6 Olivier Thomann CLA 2005-06-02 14:42:45 EDT
For historical reason, need the old formatter API to continue to work, I revert
the code to format them first as body declarations and then if it failed, as
statements.

Martin, what do you think?
Should we format them first as statements and then as body declarations?

The side effect of body declarations is an empty line between the two lines.
Comment 7 Eduardo Rocha CLA 2005-06-02 15:40:46 EDT
I think it would be not very nice to an empty line between the two lines. Also,
the example above was suposed to be used inside a method, and not fields of a
class, when the empty line would make sense.
Comment 8 Olivier Thomann CLA 2005-06-02 15:44:30 EDT
The problem in this case is that there is no context given to the code formatter.
So from the code formatter stand point there is no way to make the distinction
between some statements or body declarations.
In some cases, they might want to be body declarations and not statements.
Comment 9 Martin Aeschlimann CLA 2005-06-03 03:42:59 EDT
I think the order is free to choose for jdt.core. If the templates prefer a
different order they can themselves first try to format as statements then as
unknown.
Comment 10 Tom Hofmann CLA 2005-06-03 04:31:08 EDT
(In reply to comment #9)
> I think the order is free to choose for jdt.core. If the templates prefer a
> different order they can themselves first try to format as statements then as
> unknown.

This is probably what we would do - templates are typically statements, but if
formatting as such fails, we could retry as unknown.

Comment 11 Olivier Thomann CLA 2005-06-03 06:20:04 EDT
Ok, thanks. In this case it is up to you to improve this case. I keep the actual
order (bodydeclarations and them statements).
Comment 12 Frederic Fusier CLA 2005-06-07 12:22:47 EDT
Verified for 3.1 RC2 using build N20050607-0010 + JDT/Core HEAD
Comment 13 Tom Hofmann CLA 2005-06-07 13:02:37 EDT
filed bug 98733 against jdt-text to track our side of this.
Comment 14 David Audel CLA 2005-06-10 09:51:39 EDT
Verified for 3.1 RC2 using build I20050610-0010