]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
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 | |
7 | * | |
8 | * Contributors: | |
9 | * IBM Corporation - initial API and implementation | |
10 | *******************************************************************************/ | |
11 | package org.eclipse.jdt.internal.corext.dom; | |
12 | ||
13 | import org.eclipse.core.runtime.Assert; | |
14 | ||
15 | import org.eclipse.jface.text.IRegion; | |
16 | ||
17 | import org.eclipse.jdt.core.dom.ASTNode; | |
18 | ||
19 | ||
20 | public class Selection { | |
21 | ||
22 | /** Flag indicating that the AST node somehow intersects with the selection. */ | |
23 | public static final int INTERSECTS= 0; | |
24 | ||
25 | /** Flag that indicates that an AST node appears before the selected nodes. */ | |
26 | public static final int BEFORE= 1; | |
27 | ||
28 | /** Flag indicating that an AST node is covered by the selection. */ | |
29 | public static final int SELECTED= 2; | |
30 | ||
31 | /** Flag indicating that an AST nodes appears after the selected nodes. */ | |
32 | public static final int AFTER= 3; | |
33 | ||
34 | private int fStart; | |
35 | private int fLength; | |
36 | private int fExclusiveEnd; | |
37 | ||
38 | protected Selection() { | |
39 | } | |
40 | ||
41 | /** | |
42 | * Creates a new selection from the given start and length. | |
43 | * | |
44 | * @param s the start offset of the selection (inclusive) | |
45 | * @param l the length of the selection | |
46 | * @return the created selection object | |
47 | */ | |
48 | public static Selection createFromStartLength(int s, int l) { | |
49 | Assert.isTrue(s >= 0 && l >= 0); | |
50 | Selection result= new Selection(); | |
51 | result.fStart= s; | |
52 | result.fLength= l; | |
53 | result.fExclusiveEnd= s + l; | |
54 | return result; | |
55 | } | |
56 | ||
57 | /** | |
58 | * Creates a new selection from the given start and end. | |
59 | * | |
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 | |
63 | */ | |
64 | public static Selection createFromStartEnd(int s, int e) { | |
65 | Assert.isTrue(s >= 0 && e >= s); | |
66 | Selection result= new Selection(); | |
67 | result.fStart= s; | |
68 | result.fLength= e - s + 1; | |
69 | result.fExclusiveEnd= result.fStart + result.fLength; | |
70 | return result; | |
71 | } | |
72 | ||
73 | public int getOffset() { | |
74 | return fStart; | |
75 | } | |
76 | ||
77 | public int getLength() { | |
78 | return fLength; | |
79 | } | |
80 | ||
81 | public int getInclusiveEnd() { | |
82 | return fExclusiveEnd - 1; | |
83 | } | |
84 | ||
85 | public int getExclusiveEnd() { | |
86 | return fExclusiveEnd; | |
87 | } | |
88 | ||
89 | /** | |
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 | |
92 | * <code>AFTER</code>. | |
93 | * | |
94 | * @param node the node to return the visit mode for | |
95 | * | |
96 | * @return the selection mode of the given AST node regarding this selection | |
97 | * @see #INTERSECTS | |
98 | * @see #BEFORE | |
99 | * @see #SELECTED | |
100 | * @see #AFTER | |
101 | */ | |
102 | public int getVisitSelectionMode(ASTNode node) { | |
103 | int nodeStart= node.getStartPosition(); | |
104 | int nodeEnd= nodeStart + node.getLength(); | |
105 | if (nodeEnd <= fStart) | |
106 | return BEFORE; | |
107 | else if (covers(node)) | |
108 | return SELECTED; | |
109 | else if (fExclusiveEnd <= nodeStart) | |
110 | return AFTER; | |
111 | return INTERSECTS; | |
112 | } | |
113 | ||
114 | public int getEndVisitSelectionMode(ASTNode node) { | |
115 | int nodeStart= node.getStartPosition(); | |
116 | int nodeEnd= nodeStart + node.getLength(); | |
117 | if (nodeEnd <= fStart) | |
118 | return BEFORE; | |
119 | else if (covers(node)) | |
120 | return SELECTED; | |
121 | else if (nodeEnd >= fExclusiveEnd) | |
122 | return AFTER; | |
123 | return INTERSECTS; | |
124 | } | |
125 | ||
126 | // cover* methods do a closed interval check. | |
127 | ||
128 | public boolean covers(int position) { | |
129 | return fStart <= position && position < fStart + fLength; | |
130 | } | |
131 | ||
132 | public boolean covers(ASTNode node) { | |
133 | int nodeStart= node.getStartPosition(); | |
134 | return fStart <= nodeStart && nodeStart + node.getLength() <= fExclusiveEnd; | |
135 | } | |
136 | ||
137 | public boolean coveredBy(ASTNode node) { | |
138 | int nodeStart= node.getStartPosition(); | |
139 | return nodeStart <= fStart && fExclusiveEnd <= nodeStart + node.getLength(); | |
140 | } | |
141 | ||
142 | public boolean coveredBy(IRegion region) { | |
143 | int rangeStart= region.getOffset(); | |
144 | return rangeStart <= fStart && fExclusiveEnd <= rangeStart + region.getLength(); | |
145 | } | |
146 | ||
147 | public boolean endsIn(ASTNode node) { | |
148 | int nodeStart= node.getStartPosition(); | |
149 | return nodeStart < fExclusiveEnd && fExclusiveEnd < nodeStart + node.getLength(); | |
150 | } | |
151 | ||
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; | |
158 | } | |
159 | ||
160 | @Override | |
161 | public String toString() { | |
162 | return "<start == " + fStart + ", length == " + fLength + "/>"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | |
163 | } | |
164 | } |