|
Lines 1-5
Link Here
|
| 1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2000, 2010 IBM Corporation and others. |
2 |
* Copyright (c) 2000, 2011 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
5 |
* which accompanies this distribution, and is available at |
|
Lines 58-63
Link Here
|
| 58 |
private boolean useContextToFilterImplicitImports; |
58 |
private boolean useContextToFilterImplicitImports; |
| 59 |
private boolean findAmbiguousImports; |
59 |
private boolean findAmbiguousImports; |
| 60 |
|
60 |
|
|
|
61 |
private IRegion[] preserveExistingCommentsRanges; |
| 62 |
|
| 61 |
private int flags= 0; |
63 |
private int flags= 0; |
| 62 |
|
64 |
|
| 63 |
private static final int F_NEEDS_LEADING_DELIM= 2; |
65 |
private static final int F_NEEDS_LEADING_DELIM= 2; |
|
Lines 89-94
Link Here
|
| 89 |
this.replaceRange= evaluateReplaceRange(root); |
91 |
this.replaceRange= evaluateReplaceRange(root); |
| 90 |
if (restoreExistingImports) { |
92 |
if (restoreExistingImports) { |
| 91 |
addExistingImports(root); |
93 |
addExistingImports(root); |
|
|
94 |
} else { |
| 95 |
// collect all existing comments inside imports and concatenate them |
| 96 |
this.preserveExistingCommentsRanges = retrieveExistingCommentsInImports(root); |
| 92 |
} |
97 |
} |
| 93 |
|
98 |
|
| 94 |
PackageEntry[] order= new PackageEntry[importOrder.length]; |
99 |
PackageEntry[] order= new PackageEntry[importOrder.length]; |
|
Lines 261-272
Link Here
|
| 261 |
int nextLength= next.getLength(); |
266 |
int nextLength= next.getLength(); |
| 262 |
int nextOffsetLine= root.getLineNumber(nextOffset); |
267 |
int nextOffsetLine= root.getLineNumber(nextOffset); |
| 263 |
|
268 |
|
|
|
269 |
int extendedStart = root.getExtendedStartPosition(next); |
| 270 |
int extendedLength = root.getExtendedLength(next); |
| 264 |
// if next import is on a different line, modify the end position to the next line begin offset |
271 |
// if next import is on a different line, modify the end position to the next line begin offset |
| 265 |
if (currEndLine < nextOffsetLine) { |
272 |
if (currEndLine < nextOffsetLine) { |
| 266 |
currEndLine++; |
273 |
currEndLine++; |
| 267 |
nextOffset= root.getPosition(currEndLine, 0); |
274 |
nextOffset= root.getPosition(currEndLine, 0); |
| 268 |
} |
275 |
} |
| 269 |
currPackage.add(new ImportDeclEntry(packName.length(), name, isStatic, new Region(currOffset, nextOffset - currOffset))); |
276 |
// retrieve preceding and trailing comments if any |
|
|
277 |
IRegion rangeBefore = null; |
| 278 |
IRegion rangeAfter = null; |
| 279 |
if (nextOffset != extendedStart) { |
| 280 |
rangeBefore = new Region(extendedStart, extendedStart - nextOffset + 1); |
| 281 |
} |
| 282 |
if (nextLength != extendedLength) { |
| 283 |
rangeAfter = new Region(nextOffset + nextLength, extendedLength - nextLength + 1); |
| 284 |
} |
| 285 |
currPackage.add( |
| 286 |
new ImportDeclEntry( |
| 287 |
packName.length(), |
| 288 |
name, |
| 289 |
isStatic, |
| 290 |
new Region(currOffset, nextOffset - currOffset), |
| 291 |
rangeBefore, |
| 292 |
rangeAfter)); |
| 270 |
currOffset= nextOffset; |
293 |
currOffset= nextOffset; |
| 271 |
curr= next; |
294 |
curr= next; |
| 272 |
|
295 |
|
|
Lines 294-299
Link Here
|
| 294 |
currPackage.add(new ImportDeclEntry(packName.length(), name, isStatic, new Region(curr.getStartPosition(), length))); |
317 |
currPackage.add(new ImportDeclEntry(packName.length(), name, isStatic, new Region(curr.getStartPosition(), length))); |
| 295 |
} |
318 |
} |
| 296 |
|
319 |
|
|
|
320 |
private IRegion[] retrieveExistingCommentsInImports(CompilationUnit root) { |
| 321 |
List/*ImportDeclaration*/ decls= root.imports(); |
| 322 |
if (decls.isEmpty()) { |
| 323 |
return null; |
| 324 |
} |
| 325 |
|
| 326 |
List regions = new ArrayList(); |
| 327 |
|
| 328 |
for (int i= 0; i < decls.size(); i++) { |
| 329 |
ImportDeclaration next= (ImportDeclaration) decls.get(i); |
| 330 |
int nextOffset= next.getStartPosition(); |
| 331 |
int nextLength= next.getLength(); |
| 332 |
|
| 333 |
int extendedStart = root.getExtendedStartPosition(next); |
| 334 |
int extendedLength = root.getExtendedLength(next); |
| 335 |
if (nextOffset != extendedStart) { |
| 336 |
// preceding comment |
| 337 |
int lengthOfPrecedingComment = nextOffset - extendedStart; |
| 338 |
regions.add(new Region(extendedStart, lengthOfPrecedingComment)); |
| 339 |
|
| 340 |
if (extendedLength != (nextLength + lengthOfPrecedingComment)) { |
| 341 |
// Preceding and trailing comments |
| 342 |
int regionLength = extendedLength - (nextLength + lengthOfPrecedingComment); |
| 343 |
regions.add(new Region(nextOffset + nextLength, regionLength)); |
| 344 |
} |
| 345 |
} else if (nextLength != extendedLength) { |
| 346 |
// no extended start - only trailing comment |
| 347 |
int regionLength = extendedLength - nextLength; |
| 348 |
regions.add(new Region(nextOffset + nextLength, regionLength)); |
| 349 |
} |
| 350 |
} |
| 351 |
return (IRegion[]) regions.toArray(new IRegion[regions.size()]); |
| 352 |
} |
| 297 |
/** |
353 |
/** |
| 298 |
* Specifies that implicit imports (for types in <code>java.lang</code>, types in the same package as the rewrite |
354 |
* Specifies that implicit imports (for types in <code>java.lang</code>, types in the same package as the rewrite |
| 299 |
* compilation unit and types in the compilation unit's main type) should not be created, except if necessary to |
355 |
* compilation unit and types in the compilation unit's main type) should not be created, except if necessary to |
|
Lines 550-555
Link Here
|
| 550 |
} |
606 |
} |
| 551 |
} |
607 |
} |
| 552 |
} |
608 |
} |
|
|
609 |
// concatenate existing comments inside the imports |
| 610 |
|
| 553 |
return new Region(startPos, endPos - startPos); |
611 |
return new Region(startPos, endPos - startPos); |
| 554 |
} else { |
612 |
} else { |
| 555 |
int start= getPackageStatementEndPos(root); |
613 |
int start= getPackageStatementEndPos(root); |
|
Lines 617-623
Link Here
|
| 617 |
|
675 |
|
| 618 |
boolean doStarImport= pack.hasStarImport(threshold, onDemandConflicts); |
676 |
boolean doStarImport= pack.hasStarImport(threshold, onDemandConflicts); |
| 619 |
if (doStarImport && (pack.find("*") == null)) { //$NON-NLS-1$ |
677 |
if (doStarImport && (pack.find("*") == null)) { //$NON-NLS-1$ |
| 620 |
String[] imports = getNewImportStrings(pack, isStatic, lineDelim); |
678 |
String[] imports = getNewImportStrings(buffer, pack, isStatic, lineDelim); |
| 621 |
for (int j = 0, max = imports.length; j < max; j++) { |
679 |
for (int j = 0, max = imports.length; j < max; j++) { |
| 622 |
stringsToInsert.add(imports[j]); |
680 |
stringsToInsert.add(imports[j]); |
| 623 |
} |
681 |
} |
|
Lines 648-654
Link Here
|
| 648 |
} else if (doStarImport && !currDecl.isOnDemand()) { |
706 |
} else if (doStarImport && !currDecl.isOnDemand()) { |
| 649 |
String simpleName = currDecl.getTypeQualifiedName(); |
707 |
String simpleName = currDecl.getTypeQualifiedName(); |
| 650 |
if (simpleName.indexOf('.') != -1) { |
708 |
if (simpleName.indexOf('.') != -1) { |
| 651 |
String str= getNewImportString(currDecl.getElementName(), isStatic, lineDelim); |
709 |
IRegion rangeBefore = currDecl.getPrecedingCommentRage(); |
|
|
710 |
if (rangeBefore != null) { |
| 711 |
stringsToInsert.add(buffer.getText(rangeBefore.getOffset(), rangeBefore.getLength())); |
| 712 |
} |
| 713 |
IRegion rangeAfter = currDecl.getTrailingCommentRange(); |
| 714 |
String trailingComment = null; |
| 715 |
if (rangeAfter != null) { |
| 716 |
trailingComment = buffer.getText(rangeAfter.getOffset(), rangeAfter.getLength()); |
| 717 |
} |
| 718 |
String str= getNewImportString(currDecl.getElementName(), isStatic, trailingComment, lineDelim); |
| 652 |
if (stringsToInsert.indexOf(str) == -1) { |
719 |
if (stringsToInsert.indexOf(str) == -1) { |
| 653 |
stringsToInsert.add(str); |
720 |
stringsToInsert.add(str); |
| 654 |
} |
721 |
} |
|
Lines 657-662
Link Here
|
| 657 |
} |
724 |
} |
| 658 |
} |
725 |
} |
| 659 |
|
726 |
|
|
|
727 |
// insert back all existing imports comments since existing imports were not preserved |
| 728 |
if (this.preserveExistingCommentsRanges != null) { |
| 729 |
for (int i = 0, max = this.preserveExistingCommentsRanges.length; i < max; i++) { |
| 730 |
IRegion region = this.preserveExistingCommentsRanges[i]; |
| 731 |
String text = buffer.getText(region.getOffset(), region.getLength()); |
| 732 |
if (!text.endsWith(lineDelim)) { |
| 733 |
text += lineDelim; |
| 734 |
} |
| 735 |
stringsToInsert.add(text); |
| 736 |
} |
| 737 |
} |
| 660 |
int end= importsStart + importsLen; |
738 |
int end= importsStart + importsLen; |
| 661 |
removeAndInsertNew(buffer, currPos, end, stringsToInsert, resEdit); |
739 |
removeAndInsertNew(buffer, currPos, end, stringsToInsert, resEdit); |
| 662 |
|
740 |
|
|
Lines 787-792
Link Here
|
| 787 |
} |
865 |
} |
| 788 |
|
866 |
|
| 789 |
private String getNewImportString(String importName, boolean isStatic, String lineDelim) { |
867 |
private String getNewImportString(String importName, boolean isStatic, String lineDelim) { |
|
|
868 |
return getNewImportString(importName, isStatic, null, lineDelim); |
| 869 |
} |
| 870 |
|
| 871 |
private String getNewImportString(String importName, boolean isStatic, String trailingComment, String lineDelim) { |
| 790 |
StringBuffer buf= new StringBuffer(); |
872 |
StringBuffer buf= new StringBuffer(); |
| 791 |
buf.append("import "); //$NON-NLS-1$ |
873 |
buf.append("import "); //$NON-NLS-1$ |
| 792 |
if (isStatic) { |
874 |
if (isStatic) { |
|
Lines 795-800
Link Here
|
| 795 |
buf.append(importName); |
877 |
buf.append(importName); |
| 796 |
if (insertSpaceBeforeSemicolon()) buf.append(' '); |
878 |
if (insertSpaceBeforeSemicolon()) buf.append(' '); |
| 797 |
buf.append(';'); |
879 |
buf.append(';'); |
|
|
880 |
if (trailingComment != null) { |
| 881 |
buf.append(trailingComment); |
| 882 |
} |
| 798 |
buf.append(lineDelim); |
883 |
buf.append(lineDelim); |
| 799 |
|
884 |
|
| 800 |
if (isStatic) { |
885 |
if (isStatic) { |
|
Lines 805-826
Link Here
|
| 805 |
return buf.toString(); |
890 |
return buf.toString(); |
| 806 |
} |
891 |
} |
| 807 |
|
892 |
|
| 808 |
private String[] getNewImportStrings(PackageEntry packageEntry, boolean isStatic, String lineDelim) { |
893 |
private String[] getNewImportStrings(IBuffer buffer, PackageEntry packageEntry, boolean isStatic, String lineDelim) { |
| 809 |
boolean isStarImportAdded = false; |
894 |
boolean isStarImportAdded = false; |
| 810 |
List allImports = new ArrayList(); |
895 |
List allImports = new ArrayList(); |
| 811 |
int nImports = packageEntry.getNumberOfImports(); |
896 |
int nImports = packageEntry.getNumberOfImports(); |
|
|
897 |
StringBuffer allComments = null; |
| 812 |
for (int i= 0; i < nImports; i++) { |
898 |
for (int i= 0; i < nImports; i++) { |
| 813 |
ImportDeclEntry curr= packageEntry.getImportAt(i); |
899 |
ImportDeclEntry curr= packageEntry.getImportAt(i); |
| 814 |
String simpleName = curr.getTypeQualifiedName(); |
900 |
String simpleName = curr.getTypeQualifiedName(); |
| 815 |
if (simpleName.indexOf('.') != -1) { |
901 |
if (simpleName.indexOf('.') != -1) { |
| 816 |
// member type imports - we preserve it |
902 |
// member type imports - we preserve it |
| 817 |
allImports.add(getNewImportString(curr.getElementName(), isStatic, lineDelim)); |
903 |
IRegion rangeBefore = curr.getPrecedingCommentRage(); |
|
|
904 |
if (rangeBefore != null) { |
| 905 |
allImports.add(buffer.getText(rangeBefore.getOffset(), rangeBefore.getLength())); |
| 906 |
} |
| 907 |
IRegion rangeAfter = curr.getTrailingCommentRange(); |
| 908 |
String trailingComment = null; |
| 909 |
if (rangeAfter != null) { |
| 910 |
trailingComment = buffer.getText(rangeAfter.getOffset(), rangeAfter.getLength()); |
| 911 |
} |
| 912 |
allImports.add(getNewImportString(curr.getElementName(), isStatic, trailingComment, lineDelim)); |
| 818 |
} else if (!isStarImportAdded) { |
913 |
} else if (!isStarImportAdded) { |
| 819 |
String starImportString= packageEntry.getName() + ".*"; //$NON-NLS-1$ |
914 |
String starImportString= packageEntry.getName() + ".*"; //$NON-NLS-1$ |
| 820 |
allImports.add(getNewImportString(starImportString, isStatic, lineDelim)); |
915 |
allImports.add(getNewImportString(starImportString, isStatic, lineDelim)); |
| 821 |
isStarImportAdded = true; |
916 |
isStarImportAdded = true; |
|
|
917 |
} else { |
| 918 |
// collect all comments |
| 919 |
IRegion rangeBefore = curr.getPrecedingCommentRage(); |
| 920 |
if (rangeBefore != null) { |
| 921 |
if (allComments == null) { |
| 922 |
allComments = new StringBuffer(); |
| 923 |
} |
| 924 |
allComments.append(buffer.getText(rangeBefore.getOffset(), rangeBefore.getLength())).append(lineDelim); |
| 925 |
} |
| 926 |
IRegion rangeAfter = curr.getTrailingCommentRange(); |
| 927 |
if (rangeAfter != null) { |
| 928 |
if (allComments == null) { |
| 929 |
allComments = new StringBuffer(); |
| 930 |
} |
| 931 |
allComments.append(buffer.getText(rangeAfter.getOffset(), rangeAfter.getLength())).append(lineDelim); |
| 932 |
} |
| 822 |
} |
933 |
} |
| 823 |
} |
934 |
} |
|
|
935 |
if (allComments != null) { |
| 936 |
allImports.add(0, String.valueOf(allComments)); |
| 937 |
} |
| 824 |
return (String[]) allImports.toArray(new String[allImports.size()]); |
938 |
return (String[]) allImports.toArray(new String[allImports.size()]); |
| 825 |
} |
939 |
} |
| 826 |
|
940 |
|
|
Lines 879-884
Link Here
|
| 879 |
private IRegion sourceRange; |
993 |
private IRegion sourceRange; |
| 880 |
private final boolean isStatic; |
994 |
private final boolean isStatic; |
| 881 |
private int containerNameLength; |
995 |
private int containerNameLength; |
|
|
996 |
private IRegion precedingCommentRange; |
| 997 |
private IRegion trailingCommentRange; |
| 998 |
|
| 999 |
public ImportDeclEntry( |
| 1000 |
int containerNameLength, |
| 1001 |
String elementName, |
| 1002 |
boolean isStatic, |
| 1003 |
IRegion sourceRange, |
| 1004 |
IRegion precedingCommentRange, |
| 1005 |
IRegion trailingCommentRange) { |
| 1006 |
this(containerNameLength, elementName, isStatic, sourceRange); |
| 1007 |
this.precedingCommentRange = precedingCommentRange; |
| 1008 |
this.trailingCommentRange = trailingCommentRange; |
| 1009 |
} |
| 882 |
|
1010 |
|
| 883 |
public ImportDeclEntry(int containerNameLength, String elementName, boolean isStatic, IRegion sourceRange) { |
1011 |
public ImportDeclEntry(int containerNameLength, String elementName, boolean isStatic, IRegion sourceRange) { |
| 884 |
this.elementName= elementName; |
1012 |
this.elementName= elementName; |
|
Lines 929-935
Link Here
|
| 929 |
public IRegion getSourceRange() { |
1057 |
public IRegion getSourceRange() { |
| 930 |
return this.sourceRange; |
1058 |
return this.sourceRange; |
| 931 |
} |
1059 |
} |
|
|
1060 |
|
| 1061 |
public IRegion getPrecedingCommentRage() { |
| 1062 |
return this.precedingCommentRange; |
| 1063 |
} |
| 932 |
|
1064 |
|
|
|
1065 |
public IRegion getTrailingCommentRange() { |
| 1066 |
return this.trailingCommentRange; |
| 1067 |
} |
| 933 |
} |
1068 |
} |
| 934 |
|
1069 |
|
| 935 |
/* |
1070 |
/* |