Bug 234583 - [formatter] Code formatter should adapt edits instead of regions
Summary: [formatter] Code formatter should adapt edits instead of regions
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.4   Edit
Hardware: PC Windows XP
: P2 normal (vote)
Target Milestone: 3.5 M2   Edit
Assignee: Frederic Fusier CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 237592 239447 241470 (view as bug list)
Depends on:
Blocks:
 
Reported: 2008-05-29 07:19 EDT by Frederic Fusier CLA
Modified: 2008-09-15 09:14 EDT (History)
6 users (show)

See Also:


Attachments
First proposed patch (9.30 KB, patch)
2008-07-10 13:31 EDT, Frederic Fusier CLA
no flags Details | Diff
New proposed patch (63.90 KB, patch)
2008-08-29 07:49 EDT, Frederic Fusier CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Frederic Fusier CLA 2008-05-29 07:19:28 EDT
Since bug 208451 has been fixed, regions are tentatively adapted to include edits before the region beginning and after the region's end to have a better output while formatting.

Unfortunately, the applied fix has several potential issues:
1) more than the selected part of the code can be formatted
2) not all possible cases are addressed
3) some regions might be not considered


The symptoms for the problems above are:

1) Format the initial bug 208541 comment 0 test case as described. See that the selection at the end of the format includes the line separator before the selected line.
Changing this test case a little (spaces are replaced by points to see the difference):
public class Test {.....
........................int i= 1;.....................
}
Then it will be formatted as:
public class Test {
	int i= 1;
}
Note that trailing spaces after the opening brace are removed although they were outside the selection.

2) The following test case is not formatted properly while selecting the entire line of field declaration:
public class X01 {      
	
	
	
                        int i= 1;               
     



     }
Although removing one space or tab anywhere between the opening brace and the selection beginning will make the correct formatting happening again (and shows the problem 1) as the 3 empty lines before the selected line are replace by only one).

3) In the adaptRegions() method of Scribe version v_869, if the condition at line 243 is false:
	if (offset != upperBound || regionEnd != lowerBound) { // ...
then, the adaptedRegions[i] will not be set
Comment 1 Frederic Fusier CLA 2008-05-29 07:20:13 EDT
I think this must be fixed for 3.4.1
Comment 2 Frederic Fusier CLA 2008-06-19 11:33:55 EDT
This adaptation was needed by the fact that the whole snippet is formatted and only edits belonging to the given regions should be applied. The problem was for edits which overlap the regions...

The solution for this problem is in fact quite simple: the Scribe should apply only the part of the edit included in the region. The initial implementation of this solution was to adapt the region to include the edits completely... The new solution will be to split the concerned edits into 2 or 3 parts depending on how the overlapping occurs.

+ case 1
<------ edit ------>
         <------- region -------->

then the edit needs to be split in 2 parts:
<-edit1-><- edit2 ->
         <------- region -------->

+ case 2
            <----- edit ----->
<----- region ------>

then the edit needs to be split in 2 parts:
            <-edit1-><-edit2->
<----- region ------>

+ case 3
<--------------- edit ---------------->
         <----- region ------>

then the edit needs to be split in 2 parts:
<-edit1-><-------edit2-------><-edit3->
         <----- region ------>

+ all other cases
<--- edit --->
                    <--- edit --->
                                          <--- edit --->
                 <----- region ------>

then there's no need to split the edit

Note that the split of edits is not something obvious after having formatted the whole source (i.e while building the root edit). The difficulty is to retrieve the exact position of the region offset inside the edit as the original source is not stored in it, only its offset and length.

To solve this, we can either split the edit at the time we create it or, if we want to keep the split at the end of the scribe formatting, accept to split only on line ends (as we have the lineEnds table in hand)...
Comment 3 Frederic Fusier CLA 2008-07-08 11:44:30 EDT
*** Bug 239447 has been marked as a duplicate of this bug. ***
Comment 4 Frederic Fusier CLA 2008-07-08 11:45:59 EDT
*** Bug 237592 has been marked as a duplicate of this bug. ***
Comment 5 Frederic Fusier CLA 2008-07-08 12:40:30 EDT
*** Bug 237453 has been marked as a duplicate of this bug. ***
Comment 6 Frederic Fusier CLA 2008-07-10 13:31:50 EDT
Created attachment 107111 [details]
First proposed patch

Not finalized yet but the approach used in this patch looks promising :-)
First it removes all previous regions adapting except for block and javadoc comments which unfortunately modify not only whitespaces.

For all other cases, we would have an adaption of edits when they overlap either the beginning or the end of a region.

When the overlap occurs at the beginning of the region, the rule would be to restart the edit at the beginning of the line which contains the region start and apply the end of the replacement string (after the last new line).

When the overlap occurs at the end of the region, the rule would be to apply the number of new lines between the region end and the edit end, but no more than the number contained in the replacement string.

This algorithm fixes this bug and all the duplicate ones, but introduces 37 failures while running all formatter tests. After a quick look, it looks like the output of the failing tests were not valid, but I lack of time to verify them one by one.

I'll restart on this when I come back from vacations...
Comment 7 Frederic Fusier CLA 2008-08-27 11:56:06 EDT
Defer to 3.4.2 as code freeze for 3.4.1 is tomorrow...
Comment 8 Olivier Thomann CLA 2008-08-28 12:23:33 EDT
*** Bug 237592 has been marked as a duplicate of this bug. ***
Comment 9 Frederic Fusier CLA 2008-08-29 07:49:56 EDT
Created attachment 111293 [details]
New proposed patch

New patch fixing the problem and (real) duplicate without any failures on JDT tests (Core, UI and Text).

The edits overlapping described in comment 2 is still valid, but the edit modification has been changed since the first proposed patch (comment 6).

In this patch, it is done as follow:
 1) start the edit from the region start if it overlaps the region's start
 2) end the edit at the region end if it overlaps the region's end
 3) remove from the replacement string the number of lines which are outside
 the region (before when overlapping region's start and after when overlapping
 region's end). The trailing indentation of the replacement string is not kept 
 when the region's end is overlapped because it's always considered as outside 
 the region.
Comment 10 Frederic Fusier CLA 2008-08-29 07:50:57 EDT
Change the target of this bug the patch is too big, hence too risky, to go into a maintenance stream.
Comment 11 Frederic Fusier CLA 2008-08-29 09:21:35 EDT
Released for 3.5M2
Comment 12 David Audel CLA 2008-09-03 09:07:04 EDT
*** Bug 241470 has been marked as a duplicate of this bug. ***
Comment 13 David Audel CLA 2008-09-15 09:14:21 EDT
Verified for 3.5M2 using I20080914-2000