View | Details | Raw Unified | Return to bug 208541 | Differences between
and this patch

Collapse All | Expand All

(-)formatter/org/eclipse/jdt/internal/formatter/Scribe.java (-27 / +53 lines)
Lines 71-76 Link Here
71
	public int tabLength;	
71
	public int tabLength;	
72
	public int indentationSize;
72
	public int indentationSize;
73
	private IRegion[] regions;
73
	private IRegion[] regions;
74
	private IRegion[] adaptedRegions;
74
	public int tabChar;
75
	public int tabChar;
75
	public int numberOfIndentations;
76
	public int numberOfIndentations;
76
	private boolean useTabsOnlyForLeadingIndents;
77
	private boolean useTabsOnlyForLeadingIndents;
Lines 116-122 Link Here
116
	 * If a region should be adapted (see isAdaptableRegion(IRegion))
117
	 * If a region should be adapted (see isAdaptableRegion(IRegion))
117
	 * retrieve correct upper and lower bounds and replace the region.
118
	 * retrieve correct upper and lower bounds and replace the region.
118
	 */
119
	 */
119
	private void adaptSelectedRegions() {
120
	private void adaptRegions() {
121
		this.adaptedRegions = new Region[this.regions.length];
120
		for (int i = 0, max = this.regions.length; i < max; i++) {
122
		for (int i = 0, max = this.regions.length; i < max; i++) {
121
			IRegion aRegion = this.regions[i];
123
			IRegion aRegion = this.regions[i];
122
			int offset = aRegion.getOffset();
124
			int offset = aRegion.getOffset();
Lines 131-147 Link Here
131
					for (int j = 0, max2 = this.editsIndex; j < max2; j++) {
133
					for (int j = 0, max2 = this.editsIndex; j < max2; j++) {
132
						// search for lower bound
134
						// search for lower bound
133
						int editOffset = this.edits[j].offset;
135
						int editOffset = this.edits[j].offset;
134
						if (upperFound) {
136
						if (upperFound && lowerBound == 0) {
135
							int editLength = this.edits[j].length;
137
							int editLength = this.edits[j].length;
136
							if (lowerBound == 0  && editOffset + editLength < regionEnd) {
138
							if (editOffset == regionEnd) { // matching edit found
139
								lowerBound = regionEnd;
140
								break;
141
							} else if (editOffset + editLength < regionEnd) {
137
								continue;
142
								continue;
138
							} else {
143
							} else {
139
								lowerBound = editOffset + editLength;
144
								lowerBound = editOffset + editLength; // upper and lower bounds found
140
								break; // found both bonds - leave the loop
145
								break;
141
							}
146
							}
142
						// search for upper bound
147
							// search for upper bound
143
						} else {
148
						} else {
144
							if (this.edits[j+1].offset < offset) {
149
							if (this.edits[j + 1].offset < offset) {
145
								continue;
150
								continue;
146
							} else {
151
							} else {
147
								upperBound = editOffset;
152
								upperBound = editOffset;
Lines 150-159 Link Here
150
						}
155
						}
151
					}
156
					}
152
					if (lowerBound != 0) {
157
					if (lowerBound != 0) {
153
						// store result if any
158
						if (offset != upperBound || regionEnd != lowerBound) { // ensure we found a different region
154
						this.regions[i] = new Region(upperBound , lowerBound - upperBound);
159
							this.adaptedRegions[i] = new Region(upperBound,
160
									lowerBound - upperBound);
161
						}
162
						// keep other unadaptable region
163
					} else {
164
						this.adaptedRegions[i] = this.regions[i];
155
					}
165
					}
166
				} else {
167
					this.adaptedRegions[i] = this.regions[i];
156
				}
168
				}
169
			} else {
170
				this.adaptedRegions[i] = this.regions[i];
157
			}
171
			}
158
		}
172
		}
159
	}
173
	}
Lines 606-624 Link Here
606
	
620
	
607
	public TextEdit getRootEdit() {
621
	public TextEdit getRootEdit() {
608
		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=208541
622
		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=208541
609
		adaptSelectedRegions();
623
		adaptRegions();
610
		
624
		
611
		MultiTextEdit edit = null;
625
		MultiTextEdit edit = null;
612
		int regionsLength = this.regions.length;
626
		int regionsLength = this.adaptedRegions.length;
613
		int textRegionStart;
627
		int textRegionStart;
614
		int textRegionEnd;
628
		int textRegionEnd;
615
		if (regionsLength == 1) {
629
		if (regionsLength == 1) {
616
			IRegion lastRegion = this.regions[0];
630
			IRegion lastRegion = this.adaptedRegions[0];
617
			textRegionStart = lastRegion.getOffset();
631
			textRegionStart = lastRegion.getOffset();
618
			textRegionEnd = textRegionStart + lastRegion.getLength();
632
			textRegionEnd = textRegionStart + lastRegion.getLength();
619
		} else {
633
		} else {
620
			textRegionStart = this.regions[0].getOffset();
634
			textRegionStart = this.adaptedRegions[0].getOffset();
621
			IRegion lastRegion = this.regions[regionsLength - 1];
635
			IRegion lastRegion = this.adaptedRegions[regionsLength - 1];
622
			textRegionEnd = lastRegion.getOffset() + lastRegion.getLength();
636
			textRegionEnd = lastRegion.getOffset() + lastRegion.getLength();
623
		}
637
		}
624
		
638
		
Lines 723-739 Link Here
723
	 * @return boolean true if line should be adapted, false otherwhise
737
	 * @return boolean true if line should be adapted, false otherwhise
724
	 */
738
	 */
725
	private boolean isAdaptableRegion(int offset, int length) {
739
	private boolean isAdaptableRegion(int offset, int length) {
726
		int span = offset + length;
740
		int regionEnd = offset + length + this.lineSeparator.length() - 1;
727
		// first check region width
741
		// first check region width
728
		if (span > this.pageWidth) {
742
		if (regionEnd > this.pageWidth) {
729
			return false;
743
			return false;
730
		}
744
		}
731
		// more than one line selected
745
		int numberOfLineEnds = this.lineEnds != null && this.lineEnds.length > 0 ? this.lineEnds.length : 0;
732
		if (span > this.getLineEnd(this.scanner.getLineNumber(offset) + 1)) {
746
		if (this.line > 1 && numberOfLineEnds > 0) { // CU has more than one line
733
			return false;
747
			int lineNumber = Util.getLineNumber(offset, this.lineEnds, 0, this.line);
734
		// region is on a single line and CU has more than one line
748
			int lineEnd = this.getLineEnd(lineNumber);
735
		} else if (this.lineEnds != null && this.lineEnds.length > 1) {
749
			if (regionEnd > lineEnd) {
736
			return true;
750
				// if more than one line selected, check whether selection is at line end
751
				for (int i = lineNumber + 1 ; i <=  numberOfLineEnds ; i++) {
752
					if (regionEnd == this.getLineEnd(i)) {
753
						return true;
754
					}
755
				}
756
				return false; // more than one line selected, no need to adapt region
757
			} else {
758
				if (this.scannerEndPosition - 1 == lineEnd) { // EOF reached?
759
					return false;
760
				}
761
				return true; // a single line was selected
762
			}
737
		}
763
		}
738
		return false;
764
		return false;
739
	}
765
	}
Lines 792-798 Link Here
792
818
793
		if (editOffset == this.scannerEndPosition) {
819
		if (editOffset == this.scannerEndPosition) {
794
			int index = Arrays.binarySearch(
820
			int index = Arrays.binarySearch(
795
				this.regions,
821
				this.adaptedRegions,
796
				new Region(editOffset, 0),
822
				new Region(editOffset, 0),
797
				new Comparator() {
823
				new Comparator() {
798
					public int compare(Object o1, Object o2) {
824
					public int compare(Object o1, Object o2) {
Lines 833-839 Link Here
833
			}
859
			}
834
		}
860
		}
835
861
836
		IRegion region = this.regions[index];
862
		IRegion region = this.adaptedRegions[index];
837
		if ((region.getOffset() <= offset) && (end <= region.getOffset() + region.getLength() - 1)) {
863
		if ((region.getOffset() <= offset) && (end <= region.getOffset() + region.getLength() - 1)) {
838
			return region;
864
			return region;
839
		}
865
		}
Lines 841-854 Link Here
841
	}
867
	}
842
	
868
	
843
	private int getIndexOfRegionAt(int offset) {
869
	private int getIndexOfRegionAt(int offset) {
844
		if (this.regions.length == 1) {
870
		if (this.adaptedRegions.length == 1) {
845
			int offset2 = this.regions[0].getOffset();
871
			int offset2 = this.adaptedRegions[0].getOffset();
846
			if (offset2 == offset) {
872
			if (offset2 == offset) {
847
				return 0;
873
				return 0;
848
			}
874
			}
849
			return offset2 < offset ? -2 : -1; 
875
			return offset2 < offset ? -2 : -1; 
850
		}
876
		}
851
		return Arrays.binarySearch(this.regions, new Region(offset, 0), new Comparator() {
877
		return Arrays.binarySearch(this.adaptedRegions, new Region(offset, 0), new Comparator() {
852
			public int compare(Object o1, Object o2) {
878
			public int compare(Object o1, Object o2) {
853
				int r1Offset = ((IRegion)o1).getOffset();
879
				int r1Offset = ((IRegion)o1).getOffset();
854
				int r2Offset = ((IRegion)o2).getOffset();
880
				int r2Offset = ((IRegion)o2).getOffset();
(-)src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java (+48 lines)
Lines 9627-9630 Link Here
9627
		};
9627
		};
9628
		runTest(codeFormatter, "test688", "A.java", CodeFormatter.K_UNKNOWN, 0, false, regions, "\n");//$NON-NLS-1$ //$NON-NLS-2$
9628
		runTest(codeFormatter, "test688", "A.java", CodeFormatter.K_UNKNOWN, 0, false, regions, "\n");//$NON-NLS-1$ //$NON-NLS-2$
9629
	}
9629
	}
9630
	
9631
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=208541
9632
	public void test689() {
9633
		final Map options = DefaultCodeFormatterConstants.getEclipseDefaultSettings();
9634
		DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(options);
9635
		preferences.line_separator = "\n";//$NON-NLS-1$
9636
		DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences);
9637
		IRegion[] regions = new IRegion[] {
9638
				new Region(31, 23)
9639
		};
9640
		runTest(codeFormatter, "test689", "A.java", CodeFormatter.K_UNKNOWN, 0, false, regions, "\n");//$NON-NLS-1$ //$NON-NLS-2$
9641
	}
9642
	
9643
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=208541
9644
	public void test690() {
9645
		final Map options = DefaultCodeFormatterConstants.getEclipseDefaultSettings();
9646
		DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(options);
9647
		preferences.line_separator = "\r";//$NON-NLS-1$
9648
		DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences);
9649
		IRegion[] regions = new IRegion[] {
9650
				new Region(31, 23)
9651
		};
9652
		runTest(codeFormatter, "test689", "A.java", CodeFormatter.K_UNKNOWN, 0, false, regions, "\r");//$NON-NLS-1$ //$NON-NLS-2$
9653
	}
9654
	
9655
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=208541
9656
	public void test691() {
9657
		final Map options = DefaultCodeFormatterConstants.getEclipseDefaultSettings();
9658
		DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(options);
9659
		preferences.line_separator = "\r\n";//$NON-NLS-1$
9660
		DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences);
9661
		IRegion[] regions = new IRegion[] {
9662
				new Region(31, 22)
9663
		};
9664
		runTest(codeFormatter, "test689", "A.java", CodeFormatter.K_UNKNOWN, 0, false, regions, "\r\n");//$NON-NLS-1$ //$NON-NLS-2$
9665
	}
9666
	
9667
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=208541
9668
	public void test692() {
9669
		final Map options = DefaultCodeFormatterConstants.getEclipseDefaultSettings();
9670
		DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(options);
9671
		preferences.page_width = 999;
9672
		DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences);
9673
		IRegion[] regions = new IRegion[] {
9674
				new Region(83, 41)
9675
		};
9676
		runTest(codeFormatter, "test692", "A.java", CodeFormatter.K_UNKNOWN, 0, false, regions, "\n");//$NON-NLS-1$ //$NON-NLS-2$
9677
	}
9630
}
9678
}
(-)workspace/Formatter/test688/A_out.java (-1 / +1 lines)
Lines 1-3 Link Here
1
public class A {
1
public class A {
2
                        int i = 1;
2
	int i = 1;
3
}
3
}
(-)workspace/Formatter/test689/A_in.java (+5 lines)
Added Link Here
1
package pkg1;
2
public class A {
3
        int i = 1;     
4
5
}
(-)workspace/Formatter/test689/A_out.java (+5 lines)
Added Link Here
1
package pkg1;
2
public class A {
3
	int i = 1;
4
5
}
(-)workspace/Formatter/test692/A_in.java (+10 lines)
Added Link Here
1
package test1;
2
3
import java.util.Vector;
4
5
public class A {
6
    public void foo() {
7
    Runnable runnable= new Runnable() {};
8
    runnable.toString();
9
    }
10
}
(-)workspace/Formatter/test692/A_out.java (+11 lines)
Added Link Here
1
package test1;
2
3
import java.util.Vector;
4
5
public class A {
6
    public void foo() {
7
		Runnable runnable = new Runnable() {
8
		};
9
    runnable.toString();
10
    }
11
}

Return to bug 208541