1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 IBM Corporation and others.
3 * 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.corext.dom;
13 import org.eclipse.core.runtime.Assert;
15 import org.eclipse.jface.text.IRegion;
17 import org.eclipse.jdt.core.dom.ASTNode;
20 public class Selection {
22 /** Flag indicating that the AST node somehow intersects with the selection. */
23 public static final int INTERSECTS= 0;
25 /** Flag that indicates that an AST node appears before the selected nodes. */
26 public static final int BEFORE= 1;
28 /** Flag indicating that an AST node is covered by the selection. */
29 public static final int SELECTED= 2;
31 /** Flag indicating that an AST nodes appears after the selected nodes. */
32 public static final int AFTER= 3;
36 private int fExclusiveEnd;
38 protected Selection() {
42 * Creates a new selection from the given start and length.
44 * @param s the start offset of the selection (inclusive)
45 * @param l the length of the selection
46 * @return the created selection object
48 public static Selection createFromStartLength(int s, int l) {
49 Assert.isTrue(s >= 0 && l >= 0);
50 Selection result= new Selection();
53 result.fExclusiveEnd= s + l;
58 * Creates a new selection from the given start and end.
60 * @param s the start offset of the selection (inclusive)
61 * @param e the end offset of the selection (inclusive)
62 * @return the created selection object
64 public static Selection createFromStartEnd(int s, int e) {
65 Assert.isTrue(s >= 0 && e >= s);
66 Selection result= new Selection();
68 result.fLength= e - s + 1;
69 result.fExclusiveEnd= result.fStart + result.fLength;
73 public int getOffset() {
77 public int getLength() {
81 public int getInclusiveEnd() {
82 return fExclusiveEnd - 1;
85 public int getExclusiveEnd() {
90 * Returns the selection mode of the given AST node regarding this selection. Possible
91 * values are <code>INTERSECTS</code>, <code>BEFORE</code>, <code>SELECTED</code>, and
94 * @param node the node to return the visit mode for
96 * @return the selection mode of the given AST node regarding this selection
102 public int getVisitSelectionMode(ASTNode node) {
103 int nodeStart= node.getStartPosition();
104 int nodeEnd= nodeStart + node.getLength();
105 if (nodeEnd <= fStart)
107 else if (covers(node))
109 else if (fExclusiveEnd <= nodeStart)
114 public int getEndVisitSelectionMode(ASTNode node) {
115 int nodeStart= node.getStartPosition();
116 int nodeEnd= nodeStart + node.getLength();
117 if (nodeEnd <= fStart)
119 else if (covers(node))
121 else if (nodeEnd >= fExclusiveEnd)
126 // cover* methods do a closed interval check.
128 public boolean covers(int position) {
129 return fStart <= position && position < fStart + fLength;
132 public boolean covers(ASTNode node) {
133 int nodeStart= node.getStartPosition();
134 return fStart <= nodeStart && nodeStart + node.getLength() <= fExclusiveEnd;
137 public boolean coveredBy(ASTNode node) {
138 int nodeStart= node.getStartPosition();
139 return nodeStart <= fStart && fExclusiveEnd <= nodeStart + node.getLength();
142 public boolean coveredBy(IRegion region) {
143 int rangeStart= region.getOffset();
144 return rangeStart <= fStart && fExclusiveEnd <= rangeStart + region.getLength();
147 public boolean endsIn(ASTNode node) {
148 int nodeStart= node.getStartPosition();
149 return nodeStart < fExclusiveEnd && fExclusiveEnd < nodeStart + node.getLength();
152 public boolean liesOutside(ASTNode node) {
153 int nodeStart= node.getStartPosition();
154 int nodeEnd= nodeStart + node.getLength();
155 boolean nodeBeforeSelection= nodeEnd < fStart;
156 boolean selectionBeforeNode= fExclusiveEnd < nodeStart;
157 return nodeBeforeSelection || selectionBeforeNode;
161 public String toString() {
162 return "<start == " + fStart + ", length == " + fLength + "/>"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$