[289842] [projection] Upgrades for new comment folding to act more like JDT
diff --git a/bundles/org.eclipse.wst.css.ui/src/org/eclipse/wst/css/ui/internal/projection/CSSFoldingStrategy.java b/bundles/org.eclipse.wst.css.ui/src/org/eclipse/wst/css/ui/internal/projection/CSSFoldingStrategy.java
index 8ddfb8e..2811691 100644
--- a/bundles/org.eclipse.wst.css.ui/src/org/eclipse/wst/css/ui/internal/projection/CSSFoldingStrategy.java
+++ b/bundles/org.eclipse.wst.css.ui/src/org/eclipse/wst/css/ui/internal/projection/CSSFoldingStrategy.java
@@ -15,12 +15,9 @@
 import java.util.List;
 
 import org.eclipse.jface.text.Position;
-import org.eclipse.wst.css.core.internal.document.CSSStructuredDocumentRegionContainer;
-import org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem;
+import org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule;
 import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
 import org.eclipse.wst.sse.ui.internal.projection.AbstractStructuredFoldingStrategy;
-import org.w3c.dom.css.CSSStyleRule;
-import org.w3c.dom.css.CSSStyleSheet;
 
 /**
  * A folding strategy for CSS structured documents.
@@ -36,29 +33,15 @@
 		super();
 	}
 	
-	/*
-	 * (non-Javadoc)
+	/**
 	 * @see org.eclipse.wst.sse.ui.internal.projection.AbstractFoldingStrategy#calcNewFoldPosition(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)
 	 */
 	protected Position calcNewFoldPosition(IndexedRegion indexedRegion) {
 		Position newPos = null;
-		//don't want a fold region for the entire sheet
-		if(indexedRegionValidType(indexedRegion)) {
-			CSSStructuredDocumentRegionContainer node = (CSSStructuredDocumentRegionContainer)indexedRegion;
-			
-			int start = node.getStartOffset();
-			//so that multi-line CSS selector text does not get folded
-			if(node instanceof CSSStyleRule) {
-				CSSStyleRule rule = (CSSStyleRule)node;
-				start += rule.getSelectorText().length();
-			}
-			
-			//-1 for the end brace
-			int length = node.getEndOffset()-start-1;
-
-			if(length >= 0) {
-				newPos = new Position(start,length);
-			}
+		
+		//only want to fold regions with a valid range
+		if(indexedRegionValidType(indexedRegion) && indexedRegion.getStartOffset() >= 0 && indexedRegion.getLength() >= 0) {
+			newPos = new CSSRuleFoldingPosition(indexedRegion);
 		}
 		return newPos;
 	}
@@ -96,11 +79,10 @@
 		}
 	}
 	
-	/*
-	 * (non-Javadoc)
+	/**
 	 * @see org.eclipse.wst.sse.ui.internal.projection.AbstractFoldingStrategy#indexedRegionValidType(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)
 	 */
 	protected boolean indexedRegionValidType(IndexedRegion indexedRegion) {
-		return (!(indexedRegion instanceof CSSStyleSheet || indexedRegion instanceof ICSSStyleDeclItem));
+		return (indexedRegion instanceof ICSSStyleRule);
 	}
 }
diff --git a/bundles/org.eclipse.wst.css.ui/src/org/eclipse/wst/css/ui/internal/projection/CSSRuleFoldingPosition.java b/bundles/org.eclipse.wst.css.ui/src/org/eclipse/wst/css/ui/internal/projection/CSSRuleFoldingPosition.java
new file mode 100644
index 0000000..dfc0984
--- /dev/null
+++ b/bundles/org.eclipse.wst.css.ui/src/org/eclipse/wst/css/ui/internal/projection/CSSRuleFoldingPosition.java
@@ -0,0 +1,61 @@
+/*******************************************************************************
+ * Copyright (c) 2009 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ * 
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ *     
+ *******************************************************************************/
+package org.eclipse.wst.css.ui.internal.projection;
+
+import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
+import org.eclipse.wst.sse.ui.internal.projection.AbstractStructuredFoldingPosition;
+import org.w3c.dom.css.CSSStyleRule;
+
+/**
+ * An {@link AbstractStructuredFoldingPosition} used to cover CSS regions
+ */
+public class CSSRuleFoldingPosition extends AbstractStructuredFoldingPosition {
+
+	/**
+	 * the region that will be folded
+	 */
+	private IndexedRegion fRegion;
+	
+	/**
+	 * Creates a folding position that covers {@link IndexedRegion}s
+	 * in a CSS document
+	 * 
+	 * @param region the {@link IndexedRegion} that this folding position covers
+	 */
+	public CSSRuleFoldingPosition(IndexedRegion region) {
+		super(region.getStartOffset(), region.getLength());
+		this.fRegion = region;
+	}
+	
+	/**
+	 * @see org.eclipse.wst.sse.ui.internal.projection.AbstractStructuredFoldingPosition#getStartOffset()
+	 */
+	protected int getStartOffset() {
+		int startOffset = fRegion.getStartOffset();
+		
+		//so that multi-line CSS selector text does not get folded
+		if(this.fRegion instanceof CSSStyleRule) {
+			CSSStyleRule rule = (CSSStyleRule)this.fRegion;
+			startOffset += rule.getSelectorText().length();
+		}
+		
+		return startOffset;
+	}
+	
+	/**
+	 * @see org.eclipse.wst.sse.ui.internal.projection.AbstractStructuredFoldingPosition#getEndOffset()
+	 */
+	protected int getEndOffset() {
+		return fRegion.getEndOffset();
+	}
+
+}
\ No newline at end of file
diff --git a/bundles/org.eclipse.wst.dtd.ui/src/org/eclipse/wst/dtd/ui/internal/projection/DTDFoldingStrategy.java b/bundles/org.eclipse.wst.dtd.ui/src/org/eclipse/wst/dtd/ui/internal/projection/DTDFoldingStrategy.java
index c4864c3..27bd38b 100644
--- a/bundles/org.eclipse.wst.dtd.ui/src/org/eclipse/wst/dtd/ui/internal/projection/DTDFoldingStrategy.java
+++ b/bundles/org.eclipse.wst.dtd.ui/src/org/eclipse/wst/dtd/ui/internal/projection/DTDFoldingStrategy.java
@@ -12,10 +12,14 @@
 package org.eclipse.wst.dtd.ui.internal.projection;
 
 import org.eclipse.jface.text.Position;
+import org.eclipse.wst.dtd.core.internal.Comment;
 import org.eclipse.wst.dtd.core.internal.DTDNode;
 import org.eclipse.wst.dtd.core.internal.Unrecognized;
 import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
+import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
 import org.eclipse.wst.sse.ui.internal.projection.AbstractStructuredFoldingStrategy;
+import org.eclipse.wst.xml.ui.internal.projection.XMLCommentFoldingPosition;
+import org.eclipse.wst.xml.ui.internal.projection.XMLElementFoldingPosition;
 
 
 /**
@@ -36,29 +40,31 @@
 		super();
 	}
 	
-	/*
-	 * (non-Javadoc)
+	/**
 	 * @see org.eclipse.wst.sse.ui.internal.projection.AbstractFoldingStrategy#calcNewFoldPosition(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)
 	 */
 	protected Position calcNewFoldPosition(IndexedRegion indexedRegion) {
 		Position newPos = null;
-		if(indexedRegionValidType(indexedRegion)) {
-			DTDNode node = (DTDNode)indexedRegion;
-			int start = node.getStartOffset();
-			int length = node.getEndOffset() - start;
-			
-			if(length > 0) {
-				newPos = new Position(start,length);
+		
+		//only want to fold regions with a valid range
+		if(indexedRegionValidType(indexedRegion) && indexedRegion.getStartOffset() >= 0 && indexedRegion.getLength() >= 0) {
+			IStructuredDocumentRegion structRegion = ((DTDNode) indexedRegion).getStructuredDTDDocumentRegion();
+			//if Comment then use comment folding position
+			//else use element folding position
+			if(indexedRegion instanceof Comment) {
+				newPos = new XMLCommentFoldingPosition(structRegion);
+			} else {
+				newPos = new XMLElementFoldingPosition(structRegion);
 			}
 		}
 		return newPos;
 	}
 
-	/*
-	 * (non-Javadoc)
+	/**
 	 * @see org.eclipse.wst.sse.ui.internal.projection.AbstractFoldingStrategy#indexedRegionValidType(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)
 	 */
 	protected boolean indexedRegionValidType(IndexedRegion indexedRegion) {
-		return (!(indexedRegion instanceof Unrecognized));
+		//can only fold DTDNodes
+		return (indexedRegion instanceof DTDNode && !(indexedRegion instanceof Unrecognized));
 	}
 }
diff --git a/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/internal/projection/AbstractStructuredCommentFoldingPosition.java b/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/internal/projection/AbstractStructuredCommentFoldingPosition.java
new file mode 100644
index 0000000..f346653
--- /dev/null
+++ b/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/internal/projection/AbstractStructuredCommentFoldingPosition.java
@@ -0,0 +1,130 @@
+/*******************************************************************************
+ * Copyright (c) 2009 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ * 
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ *     
+ *******************************************************************************/
+package org.eclipse.wst.sse.ui.internal.projection;
+
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.IRegion;
+import org.eclipse.jface.text.Position;
+import org.eclipse.jface.text.Region;
+import org.eclipse.jface.text.source.projection.IProjectionPosition;
+
+/**
+ * Represents a folding position for an XML comment
+ */
+public abstract class AbstractStructuredCommentFoldingPosition extends Position implements IProjectionPosition {
+
+	/**
+	 * Default constructor
+	 * 
+	 * @param offset the offset of the folding position
+	 * @param length the length of the folidng position
+	 */
+	public AbstractStructuredCommentFoldingPosition(int offset, int length) {
+		super(offset, length);
+	}
+	
+	/**
+	 * @see org.eclipse.jface.text.source.projection.IProjectionPosition#computeCaptionOffset(org.eclipse.jface.text.IDocument)
+	 */
+	public int computeCaptionOffset(IDocument document) throws BadLocationException {
+		return findFirstContent(document.get(offset, length));
+	}
+
+	/**
+	 * @see org.eclipse.jface.text.source.projection.IProjectionPosition#computeProjectionRegions(org.eclipse.jface.text.IDocument)
+	 */
+	public IRegion[] computeProjectionRegions(IDocument document) throws BadLocationException {
+		//get the content of the comment
+		String content = document.get(offset, length);
+		int contentStart = findFirstContent(content);
+		
+		//find the start line of the comment
+		//find the end line of the comment
+		//find the first line of text in the comment
+		int startLineNum = document.getLineOfOffset(getStartOffset());
+		IRegion startLine = document.getLineInformation(startLineNum);
+		int endLineNum = document.getLineOfOffset(getEndOffset()) +1;
+		IRegion endLine = document.getLineInformation(endLineNum);
+		int captionLineNum = document.getLineOfOffset(getStartOffset() + contentStart);
+		
+		int foldOffset;			
+		int foldEndOffset;
+		
+		synchronized (this) {
+			foldOffset = startLine.getOffset();
+			if(foldOffset < offset) {
+				offset = foldOffset;
+			}
+		
+			foldEndOffset = endLine.getOffset();
+		
+			if((foldEndOffset-offset) > length) {
+				length = foldEndOffset-offset;
+			}
+		}
+		
+		//fold before the first line of text in the comment
+		IRegion preRegion = null;
+		IRegion postRegion = null;
+		if(startLineNum < captionLineNum) {
+			IRegion captionLine = document.getLineInformation(captionLineNum);
+			preRegion = new Region(foldOffset, captionLine.getOffset()-foldOffset);
+		}
+		
+		//fold after the first line of text in the comment
+		if(captionLineNum < endLineNum) {
+			int postRegionOffset = document.getLineOffset(captionLineNum+1);
+			postRegion = new Region(postRegionOffset, foldEndOffset-postRegionOffset);
+		}
+		
+		IRegion[] regions = null;
+		if(preRegion != null && postRegion != null) {
+			regions = new IRegion[] {preRegion, postRegion};
+		} else if(preRegion != null) {
+			regions = new IRegion[] {preRegion};
+		} else if(postRegion != null) {
+			regions = new IRegion[] {postRegion};
+		}
+		
+		return regions;
+	}
+	
+	/**
+	 * Finds the offset of the first identifier part within <code>content</code>.
+	 * Returns 0 if none is found.
+	 *
+	 * @param content the content to search
+	 * @param prefixEnd the end of the prefix
+	 * @return the first index of a unicode identifier part, or zero if none can
+	 *         be found
+	 */
+	private int findFirstContent(final CharSequence content) {
+		int lenght= content.length();
+		for (int i= 0; i < lenght; i++) {
+			if (Character.isUnicodeIdentifierPart(content.charAt(i)))
+				return i;
+		}
+		return 0;
+	}
+	
+	/**
+	 * @return the start offset of the folding position
+	 */
+	protected abstract int getStartOffset();
+	
+	/**
+	 * @return the end offset of the folding position
+	 */
+	protected abstract int getEndOffset();
+
+}
diff --git a/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/internal/projection/AbstractStructuredFoldingPosition.java b/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/internal/projection/AbstractStructuredFoldingPosition.java
new file mode 100644
index 0000000..3ddb47a
--- /dev/null
+++ b/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/internal/projection/AbstractStructuredFoldingPosition.java
@@ -0,0 +1,82 @@
+/*******************************************************************************
+ * Copyright (c) 2009 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ * 
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ *     
+ *******************************************************************************/
+package org.eclipse.wst.sse.ui.internal.projection;
+
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.IRegion;
+import org.eclipse.jface.text.Position;
+import org.eclipse.jface.text.Region;
+import org.eclipse.jface.text.source.projection.IProjectionPosition;
+
+/**
+ * Represents a single folding position in an <code>IStructuredDocument</code>
+ */
+public abstract class AbstractStructuredFoldingPosition extends Position implements IProjectionPosition {
+	
+	/**
+	 * Default constructor
+	 * 
+	 * @param offset the offset of the folding position
+	 * @param length the length of the folidng position
+	 */
+	public AbstractStructuredFoldingPosition(int offset, int length) {
+		super(offset, length);
+	}
+	
+	/**
+	 * @see org.eclipse.jface.text.source.projection.IProjectionPosition#computeCaptionOffset(org.eclipse.jface.text.IDocument)
+	 */
+	public int computeCaptionOffset(IDocument document) throws BadLocationException {
+		
+		return 0;
+	}
+	
+	/**
+	 * @see org.eclipse.jface.text.source.projection.IProjectionPosition#computeProjectionRegions(org.eclipse.jface.text.IDocument)
+	 */
+	public IRegion[] computeProjectionRegions(IDocument document) throws BadLocationException {
+		
+		int startLineNum = document.getLineOfOffset(getStartOffset()) + 1;
+		IRegion startLine = document.getLineInformation(startLineNum);
+		int endLineNum = document.getLineOfOffset(getEndOffset()) + 1;
+		IRegion endLine = document.getLineInformation(endLineNum);
+		
+		int foldOffset;			
+		int foldEndOffset;
+		
+		synchronized (this) {
+			foldOffset = startLine.getOffset();
+			if(foldOffset < offset) {
+				offset = foldOffset;
+			}
+		
+			foldEndOffset = endLine.getOffset();
+		
+			if((foldEndOffset-offset) > length) {
+				length = foldEndOffset-offset;
+			}
+		}
+		
+		return new IRegion[] {new Region(foldOffset, foldEndOffset-foldOffset)};
+	}
+	
+	/**
+	 * @return the start offset of the folding position
+	 */
+	protected abstract int getStartOffset();
+	
+	/**
+	 * @return the end offset of the folding position
+	 */
+	protected abstract int getEndOffset();
+}
diff --git a/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/projection/XMLCommentFoldingPosition.java b/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/projection/XMLCommentFoldingPosition.java
new file mode 100644
index 0000000..d9740e9
--- /dev/null
+++ b/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/projection/XMLCommentFoldingPosition.java
@@ -0,0 +1,52 @@
+/*******************************************************************************
+ * Copyright (c) 2009 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ * 
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ *     
+ *******************************************************************************/
+package org.eclipse.wst.xml.ui.internal.projection;
+
+import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
+import org.eclipse.wst.sse.ui.internal.projection.AbstractStructuredCommentFoldingPosition;
+
+/**
+ *
+ */
+public class XMLCommentFoldingPosition extends AbstractStructuredCommentFoldingPosition {
+
+	/**
+	 * The region covering an XML comment
+	 */
+	private IStructuredDocumentRegion fRegion;
+	
+	/**
+	 * Create a folding position to cover a XML comment region
+	 * 
+	 * @param region
+	 */
+	public XMLCommentFoldingPosition(IStructuredDocumentRegion region) {
+		//can't use region.getLength here because doesn't work in DTD docs for some reason
+		super(region.getStartOffset(), region.getEndOffset() - region.getStartOffset());
+		this.fRegion = region;
+	}
+	
+	/**
+	 * @see org.eclipse.wst.sse.ui.internal.projection.AbstractStructuredCommentFoldingPosition#getEndOffset()
+	 */
+	protected int getEndOffset() {
+		return fRegion.getEndOffset();
+	}
+
+	/**
+	 * @see org.eclipse.wst.sse.ui.internal.projection.AbstractStructuredCommentFoldingPosition#getStartOffset()
+	 */
+	protected int getStartOffset() {
+		return fRegion.getStartOffset();
+	}
+
+}
diff --git a/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/projection/XMLElementFoldingPosition.java b/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/projection/XMLElementFoldingPosition.java
new file mode 100644
index 0000000..233e301
--- /dev/null
+++ b/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/projection/XMLElementFoldingPosition.java
@@ -0,0 +1,77 @@
+/*******************************************************************************
+ * Copyright (c) 2009 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ * 
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ *     
+ *******************************************************************************/
+package org.eclipse.wst.xml.ui.internal.projection;
+
+import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
+import org.eclipse.wst.sse.ui.internal.projection.AbstractStructuredFoldingPosition;
+
+/**
+ * Folds an XML element
+ */
+public class XMLElementFoldingPosition extends AbstractStructuredFoldingPosition {
+
+	/**
+	 * The region representing the start of the folding region
+	 */
+	private IStructuredDocumentRegion fStartRegion;
+	
+	/**
+	 * The region representing the end of the folding region, or <code>null</code>
+	 * if the entire folding region is represented by <code>fStartRegion</code>
+	 */
+	private IStructuredDocumentRegion fEndRegion;
+	
+	/**
+	 * <p>Used to represent a folding position that covers a single
+	 * {@link IStructuredDocumentRegion}.</p>
+	 * 
+	 * @param region the region that covers the entire position of the folding region
+	 */
+	public XMLElementFoldingPosition(IStructuredDocumentRegion region) {
+		super(region.getStartOffset(), region.getEndOffset()-region.getStartOffset());
+		this.fStartRegion = region;
+		this.fEndRegion = null;
+	}
+	
+	/**
+	 * <p>Used to represent a folding position that covers more then one
+	 * {@link IStructuredDocumentRegion}.</p>
+	 * 
+	 * @param startRegion the first region covered by this position
+	 * @param endRegion the last region covered by this position
+	 */
+	public XMLElementFoldingPosition(IStructuredDocumentRegion startRegion, IStructuredDocumentRegion endRegion) {
+		super(startRegion.getStartOffset(), endRegion.getEndOffset()-startRegion.getStartOffset());
+		this.fStartRegion = startRegion;
+		this.fEndRegion = endRegion;
+	}
+
+	/**
+	 * @see org.eclipse.wst.sse.ui.internal.projection.AbstractStructuredFoldingPosition#getStartOffset()
+	 */
+	protected int getStartOffset() {
+		return fStartRegion.getStartOffset();
+	}
+	
+	/**
+	 * @see org.eclipse.wst.sse.ui.internal.projection.AbstractStructuredFoldingPosition#getEndOffset()
+	 */
+	protected int getEndOffset() {
+		int endOffset;
+		if(fEndRegion != null) {
+			endOffset = fEndRegion.getEndOffset();
+		} else {
+			endOffset = fStartRegion.getEndOffset();
+		}
+		return endOffset;
+	}
+}
\ No newline at end of file
diff --git a/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/projection/XMLFoldingStrategy.java b/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/projection/XMLFoldingStrategy.java
index 00e66cb..8665182 100644
--- a/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/projection/XMLFoldingStrategy.java
+++ b/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/projection/XMLFoldingStrategy.java
@@ -15,6 +15,7 @@
 import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
 import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
 import org.eclipse.wst.sse.ui.internal.projection.AbstractStructuredFoldingStrategy;
+import org.eclipse.wst.xml.core.internal.document.CommentImpl;
 import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
 import org.eclipse.wst.xml.core.internal.provisional.document.IDOMText;
 
@@ -33,36 +34,33 @@
 		super();
 	}
 
-	/*
-	 * (non-Javadoc)
+	/**
 	 * @see org.eclipse.wst.sse.ui.internal.projection.AbstractFoldingStrategy#calcNewFoldPosition(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)
 	 */
 	protected Position calcNewFoldPosition(IndexedRegion indexedRegion) {
 		Position retPos = null;
 		
-		if(indexedRegionValidType(indexedRegion)) {
+		//only want to fold regions of the valid type and with a valid range
+		if(indexedRegionValidType(indexedRegion) && indexedRegion.getStartOffset() >= 0 && indexedRegion.getLength() >= 0) {
 			IDOMNode node = (IDOMNode)indexedRegion;
 			IStructuredDocumentRegion startRegion = node.getStartStructuredDocumentRegion();
 			IStructuredDocumentRegion endRegion = node.getEndStructuredDocumentRegion();
 			
-			int start;
-			int length;
 			//if the node has an endRegion (end tag) then folding region is
 			//	between the start and end tag
+			//else if the region is a comment
 			//else if the region is only an open tag or an open/close tag then don't fold it
 			if(startRegion != null && endRegion != null) {
-				start = startRegion.getStartOffset();
-				length = endRegion.getStartOffset() - start;
-				
-				retPos = new Position(start, length);
-			} 
+				retPos = new XMLElementFoldingPosition(startRegion, endRegion);
+			} else if(startRegion != null && indexedRegion instanceof CommentImpl) {
+				retPos = new XMLCommentFoldingPosition(startRegion);
+			}
 		}
 		
 		return retPos;
 	}
 
-	/*
-	 * (non-Javadoc)
+	/**
 	 * @see org.eclipse.wst.sse.ui.internal.projection.AbstractFoldingStrategy#indexedRegionValidType(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)
 	 */
 	protected boolean indexedRegionValidType(IndexedRegion indexedRegion) {