blob: 227f9d8e1858cef6a2a38681f9b7181201c2d0e0 [file] [log] [blame]
nsandonato95924232009-08-26 20:50:40 +00001/*******************************************************************************
nsandonato20e2c042010-02-26 22:18:54 +00002 * Copyright (c) 2009, 2010 IBM Corporation and others.
nsandonato95924232009-08-26 20:50:40 +00003 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *
11 *******************************************************************************/
12package org.eclipse.wst.xml.ui.internal.projection;
13
14import org.eclipse.jface.text.Position;
15import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
16import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
17import org.eclipse.wst.sse.ui.internal.projection.AbstractStructuredFoldingStrategy;
nsandonato550035b2009-10-21 21:37:11 +000018import org.eclipse.wst.xml.core.internal.document.CommentImpl;
nsandonato95924232009-08-26 20:50:40 +000019import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
20import org.eclipse.wst.xml.core.internal.provisional.document.IDOMText;
21
22
23/**
24 * A folding strategy for XML type structured documents.
25 * See AbstractStructuredFoldingStrategy for more details.
26 */
27public class XMLFoldingStrategy extends AbstractStructuredFoldingStrategy {
28
29 /**
30 * Create an instance of the folding strategy.
31 * Be sure to set the viewer and document after creation.
32 */
33 public XMLFoldingStrategy() {
34 super();
35 }
36
nsandonato550035b2009-10-21 21:37:11 +000037 /**
nsandonato95924232009-08-26 20:50:40 +000038 * @see org.eclipse.wst.sse.ui.internal.projection.AbstractFoldingStrategy#calcNewFoldPosition(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)
39 */
40 protected Position calcNewFoldPosition(IndexedRegion indexedRegion) {
41 Position retPos = null;
42
nsandonato550035b2009-10-21 21:37:11 +000043 //only want to fold regions of the valid type and with a valid range
nsandonato20e2c042010-02-26 22:18:54 +000044 if(indexedRegion.getStartOffset() >= 0 && indexedRegion.getLength() >= 0) {
nsandonato95924232009-08-26 20:50:40 +000045 IDOMNode node = (IDOMNode)indexedRegion;
46 IStructuredDocumentRegion startRegion = node.getStartStructuredDocumentRegion();
47 IStructuredDocumentRegion endRegion = node.getEndStructuredDocumentRegion();
48
nsandonato95924232009-08-26 20:50:40 +000049 //if the node has an endRegion (end tag) then folding region is
50 // between the start and end tag
nsandonato550035b2009-10-21 21:37:11 +000051 //else if the region is a comment
nsandonato95924232009-08-26 20:50:40 +000052 //else if the region is only an open tag or an open/close tag then don't fold it
53 if(startRegion != null && endRegion != null) {
nsandonato550035b2009-10-21 21:37:11 +000054 retPos = new XMLElementFoldingPosition(startRegion, endRegion);
55 } else if(startRegion != null && indexedRegion instanceof CommentImpl) {
56 retPos = new XMLCommentFoldingPosition(startRegion);
57 }
nsandonato95924232009-08-26 20:50:40 +000058 }
59
60 return retPos;
61 }
62
nsandonato550035b2009-10-21 21:37:11 +000063 /**
nsandonato95924232009-08-26 20:50:40 +000064 * @see org.eclipse.wst.sse.ui.internal.projection.AbstractFoldingStrategy#indexedRegionValidType(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)
65 */
66 protected boolean indexedRegionValidType(IndexedRegion indexedRegion) {
67 return (indexedRegion instanceof IDOMNode) && !(indexedRegion instanceof IDOMText);
68 }
69}