001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.completion.csm;
043:
044: import java.util.Collection;
045: import org.netbeans.modules.cnd.api.model.CsmObject;
046: import org.netbeans.modules.cnd.api.model.CsmOffsetable;
047: import org.netbeans.modules.cnd.api.model.util.CsmKindUtilities;
048: import java.util.Iterator;
049: import org.netbeans.modules.cnd.api.model.CsmFunction;
050: import org.netbeans.modules.cnd.api.model.CsmFunctionDefinition;
051: import org.netbeans.modules.cnd.api.model.CsmInitializerListContainer;
052: import org.netbeans.modules.cnd.api.model.CsmParameter;
053: import org.netbeans.modules.cnd.api.model.CsmType;
054: import org.netbeans.modules.cnd.api.model.deep.CsmExpression;
055:
056: /**
057: * utilities method for working with offsets of Csm objects
058: * and CsmOffsetable objects
059: * @author vv159170
060: */
061: public class CsmOffsetUtilities {
062:
063: /** Creates a new instance of CsmOffsetUtils */
064: private CsmOffsetUtilities() {
065: }
066:
067: ////////////////////////////////////////////////////////////////////////////
068:
069: public static boolean isInObject(CsmObject obj, int offset) {
070: if (!CsmKindUtilities.isOffsetable(obj)) {
071: return false;
072: }
073: CsmOffsetable offs = (CsmOffsetable) obj;
074: if ((offs.getStartOffset() <= offset)
075: && (offset <= offs.getEndOffset())) {
076: return true;
077: } else {
078: return false;
079: }
080: }
081:
082: public static boolean isBeforeObject(CsmObject obj, int offset) {
083: if (!CsmKindUtilities.isOffsetable(obj)) {
084: return false;
085: }
086: CsmOffsetable offs = (CsmOffsetable) obj;
087: if (offset < offs.getStartOffset()) {
088: return true;
089: } else {
090: return false;
091: }
092: }
093:
094: public static boolean isAfterObject(CsmObject obj, int offset) {
095: if (!CsmKindUtilities.isOffsetable(obj)) {
096: return false;
097: }
098: CsmOffsetable offs = (CsmOffsetable) obj;
099: if (offset > offs.getEndOffset()) {
100: return true;
101: } else {
102: return false;
103: }
104: }
105:
106: // list is ordered by offsettable elements
107: public static <T extends CsmObject> T findObject(
108: Collection<T> list, CsmContext context, int offset) {
109: assert (list != null) : "expect not null list";
110: for (Iterator<T> it = list.iterator(); it.hasNext();) {
111: T obj = it.next();
112: assert (obj != null) : "can't be null declaration";
113: if (CsmOffsetUtilities.isInObject((CsmObject) obj, offset)) {
114: // we are inside csm element
115: CsmContextUtilities.updateContextObject(obj, offset,
116: context);
117: return obj;
118: }
119: }
120: return null;
121: }
122:
123: public static boolean isInFunctionScope(final CsmFunction fun,
124: final int offset) {
125: boolean inScope = false;
126: if (fun != null) {
127: inScope = true;
128: // in function, but check that not in return type
129: // check if offset in return value
130: CsmType retType = fun.getReturnType();
131: if (CsmOffsetUtilities.isInObject(retType, offset)) {
132: return false;
133: }
134: // check if offset is before parameters
135: Collection<CsmParameter> params = fun.getParameters();
136: if (!params.isEmpty()) {
137: CsmParameter firstParam = params.iterator().next();
138: if (CsmOffsetUtilities.isBeforeObject(firstParam,
139: offset)) {
140: return false;
141: }
142: return true;
143: }
144: // check initializer list for constructors
145: if (CsmKindUtilities.isConstructor(fun)) {
146: Collection<CsmExpression> izers = ((CsmInitializerListContainer) fun)
147: .getInitializerList();
148: if (!izers.isEmpty()) {
149: CsmExpression firstIzer = izers.iterator().next();
150: if (CsmOffsetUtilities.isBeforeObject(firstIzer,
151: offset)) {
152: return false;
153: }
154: return true;
155: }
156: }
157: // for function definitions check body
158: if (CsmKindUtilities.isFunctionDefinition(fun)) {
159: CsmFunctionDefinition funDef = (CsmFunctionDefinition) fun;
160: if (CsmOffsetUtilities.isBeforeObject(funDef.getBody(),
161: offset)) {
162: return false;
163: }
164: }
165: }
166: return inScope;
167: }
168: }
|