A massive organize imports and formatting of the sources using default Eclipse code...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / folding / DefaultJavaFoldingStructureProvider.java
index c06a287..2b8c0ee 100644 (file)
@@ -65,10 +65,11 @@ import org.eclipse.ui.texteditor.ITextEditor;
 
 /**
  * Updates the projection model of a class file or compilation unit.
- *
+ * 
  * @since 3.0
  */
-public class DefaultJavaFoldingStructureProvider implements IProjectionListener, IJavaFoldingStructureProvider {
+public class DefaultJavaFoldingStructureProvider implements
+               IProjectionListener, IJavaFoldingStructureProvider {
 
        private static class JavaProjectionAnnotation extends ProjectionAnnotation {
 
@@ -76,7 +77,8 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
 
                private boolean fIsComment;
 
-               public JavaProjectionAnnotation(IJavaElement element, boolean isCollapsed, boolean isComment) {
+               public JavaProjectionAnnotation(IJavaElement element,
+                               boolean isCollapsed, boolean isComment) {
                        super(isCollapsed);
                        fJavaElement = element;
                        fIsComment = isComment;
@@ -131,7 +133,8 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
                                processDelta(delta);
                }
 
-               private IJavaElementDelta findElement(IJavaElement target, IJavaElementDelta delta) {
+               private IJavaElementDelta findElement(IJavaElement target,
+                               IJavaElementDelta delta) {
 
                        if (delta == null || target == null)
                                return null;
@@ -157,13 +160,14 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
        }
 
        /**
-        * Projection position that will return two foldable regions: one folding away
-        * the region from after the '/**' to the beginning of the content, the other
-        * from after the first content line until after the comment.
-        *
+        * Projection position that will return two foldable regions: one folding
+        * away the region from after the '/**' to the beginning of the content, the
+        * other from after the first content line until after the comment.
+        * 
         * @since 3.1
         */
-       private static final class CommentPosition extends Position implements IProjectionPosition {
+       private static final class CommentPosition extends Position implements
+                       IProjectionPosition {
                CommentPosition(int offset, int length) {
                        super(offset, length);
                }
@@ -171,8 +175,10 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
                /*
                 * @see org.eclipse.jface.text.source.projection.IProjectionPosition#computeFoldingRegions(org.eclipse.jface.text.IDocument)
                 */
-               public IRegion[] computeProjectionRegions(IDocument document) throws BadLocationException {
-                       DocumentCharacterIterator sequence = new DocumentCharacterIterator(document, offset, offset + length);
+               public IRegion[] computeProjectionRegions(IDocument document)
+                               throws BadLocationException {
+                       DocumentCharacterIterator sequence = new DocumentCharacterIterator(
+                                       document, offset, offset + length);
                        int prefixEnd = 0;
                        int contentStart = findFirstContent(sequence, prefixEnd);
 
@@ -180,14 +186,18 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
                        int captionLine = document.getLineOfOffset(offset + contentStart);
                        int lastLine = document.getLineOfOffset(offset + length);
 
-                       Assert.isTrue(firstLine <= captionLine, "first folded line is greater than the caption line"); //$NON-NLS-1$
-                       Assert.isTrue(captionLine <= lastLine, "caption line is greater than the last folded line"); //$NON-NLS-1$
+                       Assert.isTrue(firstLine <= captionLine,
+                                       "first folded line is greater than the caption line"); //$NON-NLS-1$
+                       Assert.isTrue(captionLine <= lastLine,
+                                       "caption line is greater than the last folded line"); //$NON-NLS-1$
 
                        IRegion preRegion;
                        if (firstLine < captionLine) {
-                               // preRegion= new Region(offset + prefixEnd, contentStart - prefixEnd);
+                               // preRegion= new Region(offset + prefixEnd, contentStart -
+                               // prefixEnd);
                                int preOffset = document.getLineOffset(firstLine);
-                               IRegion preEndLineInfo = document.getLineInformation(captionLine);
+                               IRegion preEndLineInfo = document
+                                               .getLineInformation(captionLine);
                                int preEnd = preEndLineInfo.getOffset();
                                preRegion = new Region(preOffset, preEnd - preOffset);
                        } else {
@@ -196,7 +206,8 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
 
                        if (captionLine < lastLine) {
                                int postOffset = document.getLineOffset(captionLine + 1);
-                               IRegion postRegion = new Region(postOffset, offset + length - postOffset);
+                               IRegion postRegion = new Region(postOffset, offset + length
+                                               - postOffset);
 
                                if (preRegion == null)
                                        return new IRegion[] { postRegion };
@@ -211,13 +222,13 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
                }
 
                /**
-                * Finds the offset of the first identifier part within <code>content</code>.
-                * Returns 0 if none is found.
-                *
+                * Finds the offset of the first identifier part within
+                * <code>content</code>. Returns 0 if none is found.
+                * 
                 * @param content
-                *          the content to search
-                * @return the first index of a unicode identifier part, or zero if none can
-                *         be found
+                *            the content to search
+                * @return the first index of a unicode identifier part, or zero if none
+                *         can be found
                 */
                private int findFirstContent(final CharSequence content, int prefixEnd) {
                        int lenght = content.length();
@@ -234,7 +245,8 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
                // * Returns 0 if none is found.
                // *
                // * @param content the content to search
-               // * @return the first index of a unicode identifier part, or zero if none
+               // * @return the first index of a unicode identifier part, or zero if
+               // none
                // can
                // * be found
                // */
@@ -244,7 +256,8 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
                // int i= 0;
                // while (i < len && isWhiteSpace(content.charAt(i)))
                // i++;
-               // if (len >= i + 2 && content.charAt(i) == '/' && content.charAt(i + 1) ==
+               // if (len >= i + 2 && content.charAt(i) == '/' && content.charAt(i + 1)
+               // ==
                // '*')
                // if (len >= i + 3 && content.charAt(i + 2) == '*')
                // return i + 3;
@@ -263,19 +276,21 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
                 */
                public int computeCaptionOffset(IDocument document) {
                        // return 0;
-                       DocumentCharacterIterator sequence = new DocumentCharacterIterator(document, offset, offset + length);
+                       DocumentCharacterIterator sequence = new DocumentCharacterIterator(
+                                       document, offset, offset + length);
                        return findFirstContent(sequence, 0);
                }
        }
 
        /**
-        * Projection position that will return two foldable regions: one folding away
-        * the lines before the one containing the simple name of the java element,
-        * one folding away any lines after the caption.
-        *
+        * Projection position that will return two foldable regions: one folding
+        * away the lines before the one containing the simple name of the java
+        * element, one folding away any lines after the caption.
+        * 
         * @since 3.1
         */
-       private static final class JavaElementPosition extends Position implements IProjectionPosition {
+       private static final class JavaElementPosition extends Position implements
+                       IProjectionPosition {
 
                private IMember fMember;
 
@@ -293,14 +308,16 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
                /*
                 * @see org.eclipse.jface.text.source.projection.IProjectionPosition#computeFoldingRegions(org.eclipse.jface.text.IDocument)
                 */
-               public IRegion[] computeProjectionRegions(IDocument document) throws BadLocationException {
+               public IRegion[] computeProjectionRegions(IDocument document)
+                               throws BadLocationException {
                        int nameStart = offset;
                        try {
                                /*
-                                * The member's name range may not be correct. However, reconciling
-                                * would trigger another element delta which would lead to reentrant
-                                * situations. Therefore, we optimistically assume that the name range
-                                * is correct, but double check the received lines below.
+                                * The member's name range may not be correct. However,
+                                * reconciling would trigger another element delta which would
+                                * lead to reentrant situations. Therefore, we optimistically
+                                * assume that the name range is correct, but double check the
+                                * received lines below.
                                 */
                                ISourceRange nameRange = fMember.getNameRange();
                                if (nameRange != null)
@@ -315,9 +332,9 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
                        int lastLine = document.getLineOfOffset(offset + length);
 
                        /*
-                        * see comment above - adjust the caption line to be inside the entire
-                        * folded region, and rely on later element deltas to correct the name
-                        * range.
+                        * see comment above - adjust the caption line to be inside the
+                        * entire folded region, and rely on later element deltas to correct
+                        * the name range.
                         */
                        if (captionLine < firstLine)
                                captionLine = firstLine;
@@ -327,7 +344,8 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
                        IRegion preRegion;
                        if (firstLine < captionLine) {
                                int preOffset = document.getLineOffset(firstLine);
-                               IRegion preEndLineInfo = document.getLineInformation(captionLine);
+                               IRegion preEndLineInfo = document
+                                               .getLineInformation(captionLine);
                                int preEnd = preEndLineInfo.getOffset();
                                preRegion = new Region(preOffset, preEnd - preOffset);
                        } else {
@@ -336,7 +354,8 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
 
                        if (captionLine < lastLine) {
                                int postOffset = document.getLineOffset(captionLine + 1);
-                               IRegion postRegion = new Region(postOffset, offset + length - postOffset);
+                               IRegion postRegion = new Region(postOffset, offset + length
+                                               - postOffset);
 
                                if (preRegion == null)
                                        return new IRegion[] { postRegion };
@@ -353,7 +372,8 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
                /*
                 * @see org.eclipse.jface.text.source.projection.IProjectionPosition#computeCaptionOffset(org.eclipse.jface.text.IDocument)
                 */
-               public int computeCaptionOffset(IDocument document) throws BadLocationException {
+               public int computeCaptionOffset(IDocument document)
+                               throws BadLocationException {
                        int nameStart = offset;
                        try {
                                // need a reconcile here?
@@ -427,7 +447,8 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
         */
        public void projectionEnabled() {
                // http://home.ott.oti.com/teams/wswb/anon/out/vms/index.html
-               // projectionEnabled messages are not always paired with projectionDisabled
+               // projectionEnabled messages are not always paired with
+               // projectionDisabled
                // i.e. multiple enabled messages may be sent out.
                // we have to make sure that we disable first when getting an enable
                // message.
@@ -468,7 +489,8 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
                        fHasHeaderComment = false;
 
                        if (fEditor instanceof PHPUnitEditor) {
-                               IWorkingCopyManager manager = PHPeclipsePlugin.getDefault().getWorkingCopyManager();
+                               IWorkingCopyManager manager = PHPeclipsePlugin.getDefault()
+                                               .getWorkingCopyManager();
                                fInput = manager.getWorkingCopy(fEditor.getEditorInput());
                        }
                        // else if (fEditor instanceof ClassFileEditor) {
@@ -478,14 +500,16 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
                        // }
 
                        if (fInput != null) {
-                               ProjectionAnnotationModel model = (ProjectionAnnotationModel) fEditor.getAdapter(ProjectionAnnotationModel.class);
+                               ProjectionAnnotationModel model = (ProjectionAnnotationModel) fEditor
+                                               .getAdapter(ProjectionAnnotationModel.class);
                                if (model != null) {
                                        fCachedModel = model;
                                        if (fInput instanceof ICompilationUnit) {
                                                ICompilationUnit unit = (ICompilationUnit) fInput;
                                                synchronized (unit) {
                                                        try {
-                                                               // unit.reconcile(ICompilationUnit.NO_AST, false, null, null);
+                                                               // unit.reconcile(ICompilationUnit.NO_AST,
+                                                               // false, null, null);
                                                                unit.reconcile();
                                                        } catch (JavaModelException x) {
                                                        }
@@ -494,14 +518,16 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
 
                                        Map additions = computeAdditions((IParent) fInput);
                                        /*
-                                        * Minimize the events being sent out - as this happens in the UI
-                                        * thread merge everything into one call.
+                                        * Minimize the events being sent out - as this happens in
+                                        * the UI thread merge everything into one call.
                                         */
                                        List removals = new LinkedList();
                                        Iterator existing = model.getAnnotationIterator();
                                        while (existing.hasNext())
                                                removals.add(existing.next());
-                                       model.replaceAnnotations((Annotation[]) removals.toArray(new Annotation[removals.size()]), additions);
+                                       model.replaceAnnotations((Annotation[]) removals
+                                                       .toArray(new Annotation[removals.size()]),
+                                                       additions);
                                }
                        }
 
@@ -516,17 +542,23 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
        }
 
        private void initializePreferences() {
-               IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
-               fCollapseInnerTypes = store.getBoolean(PreferenceConstants.EDITOR_FOLDING_INNERTYPES);
+               IPreferenceStore store = PHPeclipsePlugin.getDefault()
+                               .getPreferenceStore();
+               fCollapseInnerTypes = store
+                               .getBoolean(PreferenceConstants.EDITOR_FOLDING_INNERTYPES);
                // fCollapseImportContainer =
                // store.getBoolean(PreferenceConstants.EDITOR_FOLDING_IMPORTS);
-               fCollapseJavadoc = store.getBoolean(PreferenceConstants.EDITOR_FOLDING_JAVADOC);
-               fCollapseMethods = store.getBoolean(PreferenceConstants.EDITOR_FOLDING_METHODS);
-               fCollapseHeaderComments = store.getBoolean(PreferenceConstants.EDITOR_FOLDING_HEADERS);
+               fCollapseJavadoc = store
+                               .getBoolean(PreferenceConstants.EDITOR_FOLDING_JAVADOC);
+               fCollapseMethods = store
+                               .getBoolean(PreferenceConstants.EDITOR_FOLDING_METHODS);
+               fCollapseHeaderComments = store
+                               .getBoolean(PreferenceConstants.EDITOR_FOLDING_HEADERS);
        }
 
        private Map computeAdditions(IParent parent) {
-               Map map = new LinkedHashMap(); // use a linked map to maintain ordering of
+               Map map = new LinkedHashMap(); // use a linked map to maintain ordering
+                                                                               // of
                // comments
                try {
                        computeAdditions(parent.getChildren(), map);
@@ -535,7 +567,8 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
                return map;
        }
 
-       private void computeAdditions(IJavaElement[] elements, Map map) throws JavaModelException {
+       private void computeAdditions(IJavaElement[] elements, Map map)
+                       throws JavaModelException {
                for (int i = 0; i < elements.length; i++) {
                        IJavaElement element = elements[i];
 
@@ -563,9 +596,9 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
                        collapse = fAllowCollapsing;
                        if (isInnerType((IType) element)) {
                                collapse = collapse && fCollapseInnerTypes;
-                       }
-                       else {
-                               collapse = false; //don't allow the most outer type to be folded, may be changed in future versions
+                       } else {
+                               collapse = false; // don't allow the most outer type to be
+                                                                       // folded, may be changed in future versions
                        }
                        createProjection = true;
                        break;
@@ -580,21 +613,28 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
                        if (regions != null) {
                                // comments
                                for (int i = 0; i < regions.length - 1; i++) {
-                                       Position position = createProjectionPosition(regions[i], null);
+                                       Position position = createProjectionPosition(regions[i],
+                                                       null);
                                        boolean commentCollapse;
                                        if (position != null) {
-                                               if (i == 0 && (regions.length > 2 || fHasHeaderComment) && element == fFirstType) {
-                                                       commentCollapse = fAllowCollapsing && fCollapseHeaderComments;
+                                               if (i == 0 && (regions.length > 2 || fHasHeaderComment)
+                                                               && element == fFirstType) {
+                                                       commentCollapse = fAllowCollapsing
+                                                                       && fCollapseHeaderComments;
                                                } else {
-                                                       commentCollapse = fAllowCollapsing && fCollapseJavadoc;
+                                                       commentCollapse = fAllowCollapsing
+                                                                       && fCollapseJavadoc;
                                                }
-                                               map.put(new JavaProjectionAnnotation(element, commentCollapse, true), position);
+                                               map.put(new JavaProjectionAnnotation(element,
+                                                               commentCollapse, true), position);
                                        }
                                }
                                // code
-                               Position position = createProjectionPosition(regions[regions.length - 1], element);
+                               Position position = createProjectionPosition(
+                                               regions[regions.length - 1], element);
                                if (position != null)
-                                       map.put(new JavaProjectionAnnotation(element, collapse, false), position);
+                                       map.put(new JavaProjectionAnnotation(element, collapse,
+                                                       false), position);
                        }
                }
        }
@@ -619,10 +659,11 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
         * More than one range may be returned if the element has a leading comment
         * which gets folded separately. If there are no foldable regions,
         * <code>null</code> is returned.
-        *
+        * 
         * @param element
-        *          the java element that can be folded
-        * @return the regions to be folded, or <code>null</code> if there are none
+        *            the java element that can be folded
+        * @return the regions to be folded, or <code>null</code> if there are
+        *         none
         */
        private IRegion[] computeProjectionRanges(IJavaElement element) {
 
@@ -649,7 +690,8 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
                                final int shift = range.getOffset();
                                int start = shift;
                                if (element instanceof IType) {
-                                       Scanner scanner = ToolFactory.createScanner(true, false, false, false);
+                                       Scanner scanner = ToolFactory.createScanner(true, false,
+                                                       false, false);
                                        scanner.setSource(contents.toCharArray());
                                        scanner.setPHPMode(true);
 
@@ -662,7 +704,8 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
                                                switch (token) {
                                                case ITerminalSymbols.TokenNameCOMMENT_PHPDOC:
                                                case ITerminalSymbols.TokenNameCOMMENT_BLOCK: {
-                                                       int end = shift + scanner.getCurrentTokenEndPosition() + 1;
+                                                       int end = shift
+                                                                       + scanner.getCurrentTokenEndPosition() + 1;
                                                        regions.add(new Region(start, end - start));
                                                }
                                                case ITerminalSymbols.TokenNameCOMMENT_LINE:
@@ -707,12 +750,14 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
                        }
 
                        /*
-                        * code adapted from CommentFormattingStrategy: scan the header content up
-                        * to the first type. Once a comment is found, accumulate any additional
-                        * comments up to the stop condition. The stop condition is reaching a
-                        * package declaration, import container, or the end of the input.
+                        * code adapted from CommentFormattingStrategy: scan the header
+                        * content up to the first type. Once a comment is found, accumulate
+                        * any additional comments up to the stop condition. The stop
+                        * condition is reaching a package declaration, import container, or
+                        * the end of the input.
                         */
-                       IScanner scanner = ToolFactory.createScanner(true, false, false, false);
+                       IScanner scanner = ToolFactory.createScanner(true, false, false,
+                                       false);
                        scanner.setSource(content.toCharArray());
 
                        int headerStart = -1;
@@ -721,12 +766,15 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
                                boolean foundComment = false;
                                int terminal = scanner.getNextToken();
                                while (terminal != ITerminalSymbols.TokenNameEOF
-                                               && !(terminal == ITerminalSymbols.TokenNameclass || terminal == ITerminalSymbols.TokenNameinterface || foundComment)) {
+                                               && !(terminal == ITerminalSymbols.TokenNameclass
+                                                               || terminal == ITerminalSymbols.TokenNameinterface || foundComment)) {
 
-                                       if (terminal == ITerminalSymbols.TokenNameCOMMENT_PHPDOC || terminal == ITerminalSymbols.TokenNameCOMMENT_BLOCK
+                                       if (terminal == ITerminalSymbols.TokenNameCOMMENT_PHPDOC
+                                                       || terminal == ITerminalSymbols.TokenNameCOMMENT_BLOCK
                                                        || terminal == ITerminalSymbols.TokenNameCOMMENT_LINE) {
                                                if (!foundComment)
-                                                       headerStart = scanner.getCurrentTokenStartPosition();
+                                                       headerStart = scanner
+                                                                       .getCurrentTokenStartPosition();
                                                headerEnd = scanner.getCurrentTokenEndPosition();
                                                foundComment = true;
                                        }
@@ -744,7 +792,8 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
                return null;
        }
 
-       private Position createProjectionPosition(IRegion region, IJavaElement element) {
+       private Position createProjectionPosition(IRegion region,
+                       IJavaElement element) {
 
                if (fCachedDocument == null)
                        return null;
@@ -752,18 +801,21 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
                try {
 
                        int start = fCachedDocument.getLineOfOffset(region.getOffset());
-                       int end = fCachedDocument.getLineOfOffset(region.getOffset() + region.getLength());
+                       int end = fCachedDocument.getLineOfOffset(region.getOffset()
+                                       + region.getLength());
                        if (start != end) {
                                int offset = fCachedDocument.getLineOffset(start);
                                int endOffset;
                                if (fCachedDocument.getNumberOfLines() > end + 1)
                                        endOffset = fCachedDocument.getLineOffset(end + 1);
                                else if (end > start)
-                                       endOffset = fCachedDocument.getLineOffset(end) + fCachedDocument.getLineLength(end);
+                                       endOffset = fCachedDocument.getLineOffset(end)
+                                                       + fCachedDocument.getLineLength(end);
                                else
                                        return null;
                                if (element instanceof IMember)
-                                       return new JavaElementPosition(offset, endOffset - offset, (IMember) element);
+                                       return new JavaElementPosition(offset, endOffset - offset,
+                                                       (IMember) element);
                                else
                                        return new CommentPosition(offset, endOffset - offset);
                        }
@@ -782,7 +834,8 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
                if ((delta.getFlags() & (IJavaElementDelta.F_CONTENT | IJavaElementDelta.F_CHILDREN)) == 0)
                        return;
 
-               ProjectionAnnotationModel model = (ProjectionAnnotationModel) fEditor.getAdapter(ProjectionAnnotationModel.class);
+               ProjectionAnnotationModel model = (ProjectionAnnotationModel) fEditor
+                               .getAdapter(ProjectionAnnotationModel.class);
                if (model == null)
                        return;
 
@@ -805,7 +858,8 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
 
                        Iterator e = updated.keySet().iterator();
                        while (e.hasNext()) {
-                               JavaProjectionAnnotation newAnnotation = (JavaProjectionAnnotation) e.next();
+                               JavaProjectionAnnotation newAnnotation = (JavaProjectionAnnotation) e
+                                               .next();
                                IJavaElement element = newAnnotation.getElement();
                                Position newPosition = (Position) updated.get(newAnnotation);
 
@@ -821,10 +875,14 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
                                                Tuple tuple = (Tuple) x.next();
                                                JavaProjectionAnnotation existingAnnotation = tuple.annotation;
                                                Position existingPosition = tuple.position;
-                                               if (newAnnotation.isComment() == existingAnnotation.isComment()) {
-                                                       if (existingPosition != null && (!newPosition.equals(existingPosition))) {
-                                                               existingPosition.setOffset(newPosition.getOffset());
-                                                               existingPosition.setLength(newPosition.getLength());
+                                               if (newAnnotation.isComment() == existingAnnotation
+                                                               .isComment()) {
+                                                       if (existingPosition != null
+                                                                       && (!newPosition.equals(existingPosition))) {
+                                                               existingPosition.setOffset(newPosition
+                                                                               .getOffset());
+                                                               existingPosition.setLength(newPosition
+                                                                               .getLength());
                                                                updates.add(existingAnnotation);
                                                        }
                                                        matched = true;
@@ -868,10 +926,10 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
 
        /**
         * Matches deleted annotations to changed or added ones. A deleted
-        * annotation/position tuple that has a matching addition / change is updated
-        * and marked as changed. The matching tuple is not added (for additions) or
-        * marked as deletion instead (for changes). The result is that more
-        * annotations are changed and fewer get deleted/re-added.
+        * annotation/position tuple that has a matching addition / change is
+        * updated and marked as changed. The matching tuple is not added (for
+        * additions) or marked as deletion instead (for changes). The result is
+        * that more annotations are changed and fewer get deleted/re-added.
         */
        private void match(List deletions, Map additions, List changes) {
                if (deletions.isEmpty() || (additions.isEmpty() && changes.isEmpty()))
@@ -882,7 +940,8 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
 
                Iterator deletionIterator = deletions.iterator();
                while (deletionIterator.hasNext()) {
-                       JavaProjectionAnnotation deleted = (JavaProjectionAnnotation) deletionIterator.next();
+                       JavaProjectionAnnotation deleted = (JavaProjectionAnnotation) deletionIterator
+                                       .next();
                        Position deletedPosition = fCachedModel.getPosition(deleted);
                        if (deletedPosition == null)
                                continue;
@@ -900,7 +959,8 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
                                IJavaElement element = match.annotation.getElement();
                                deleted.setElement(element);
                                deletedPosition.setLength(match.position.getLength());
-                               if (deletedPosition instanceof JavaElementPosition && element instanceof IMember) {
+                               if (deletedPosition instanceof JavaElementPosition
+                                               && element instanceof IMember) {
                                        JavaElementPosition jep = (JavaElementPosition) deletedPosition;
                                        jep.setMember((IMember) element);
                                }
@@ -918,35 +978,38 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
        }
 
        /**
-        * Finds a match for <code>tuple</code> in a collection of annotations. The
-        * positions for the <code>JavaProjectionAnnotation</code> instances in
-        * <code>annotations</code> can be found in the passed
+        * Finds a match for <code>tuple</code> in a collection of annotations.
+        * The positions for the <code>JavaProjectionAnnotation</code> instances
+        * in <code>annotations</code> can be found in the passed
         * <code>positionMap</code> or <code>fCachedModel</code> if
         * <code>positionMap</code> is <code>null</code>.
         * <p>
-        * A tuple is said to match another if their annotations have the same comment
-        * flag and their position offsets are equal.
+        * A tuple is said to match another if their annotations have the same
+        * comment flag and their position offsets are equal.
         * </p>
         * <p>
         * If a match is found, the annotation gets removed from
         * <code>annotations</code>.
         * </p>
-        *
+        * 
         * @param tuple
-        *          the tuple for which we want to find a match
+        *            the tuple for which we want to find a match
         * @param annotations
-        *          collection of <code>JavaProjectionAnnotation</code>
+        *            collection of <code>JavaProjectionAnnotation</code>
         * @param positionMap
-        *          a <code>Map&lt;Annotation, Position&gt;</code> or
-        *          <code>null</code>
+        *            a <code>Map&lt;Annotation, Position&gt;</code> or
+        *            <code>null</code>
         * @return a matching tuple or <code>null</code> for no match
         */
        private Tuple findMatch(Tuple tuple, Collection annotations, Map positionMap) {
                Iterator it = annotations.iterator();
                while (it.hasNext()) {
-                       JavaProjectionAnnotation annotation = (JavaProjectionAnnotation) it.next();
+                       JavaProjectionAnnotation annotation = (JavaProjectionAnnotation) it
+                                       .next();
                        if (tuple.annotation.isComment() == annotation.isComment()) {
-                               Position position = positionMap == null ? fCachedModel.getPosition(annotation) : (Position) positionMap.get(annotation);
+                               Position position = positionMap == null ? fCachedModel
+                                               .getPosition(annotation) : (Position) positionMap
+                                               .get(annotation);
                                if (position == null)
                                        continue;
 
@@ -980,7 +1043,8 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
 
                Comparator comparator = new Comparator() {
                        public int compare(Object o1, Object o2) {
-                               return ((Tuple) o1).position.getOffset() - ((Tuple) o2).position.getOffset();
+                               return ((Tuple) o1).position.getOffset()
+                                               - ((Tuple) o2).position.getOffset();
                        }
                };
                for (Iterator it = map.values().iterator(); it.hasNext();) {