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.ArrayList;
045: import java.util.List;
046: import org.netbeans.modules.cnd.completion.cplusplus.ext.CsmCompletionQuery;
047: import org.netbeans.modules.cnd.completion.cplusplus.ext.CsmResultItem;
048: import org.netbeans.modules.cnd.api.model.CsmObject;
049: import org.netbeans.modules.cnd.api.model.util.CsmKindUtilities;
050: import org.netbeans.editor.BaseDocument;
051: import javax.swing.text.BadLocationException;
052: import javax.swing.text.JTextComponent;
053: import org.netbeans.editor.SyntaxSupport;
054: import org.netbeans.editor.ext.CompletionQuery;
055: import org.netbeans.modules.cnd.api.model.CsmClass;
056: import org.netbeans.modules.cnd.api.model.CsmFile;
057: import org.netbeans.modules.cnd.api.model.CsmOffsetableDeclaration;
058: import org.netbeans.modules.cnd.api.model.CsmProject;
059: import org.netbeans.modules.cnd.completion.cplusplus.CsmCompletionProvider;
060: import org.netbeans.modules.cnd.modelutil.CsmUtilities;
061: import org.netbeans.modules.editor.NbEditorUtilities;
062:
063: /**
064: *
065: * @author Vladimir Voskresensky
066: */
067: public class CompletionUtilities {
068:
069: public static List/*<CsmDeclaration*/findFunctionLocalVariables(
070: BaseDocument doc, int offset) {
071: CsmFile file = CsmUtilities.getCsmFile(doc, true);
072: CsmContext context = CsmOffsetResolver
073: .findContext(file, offset);
074: return CsmContextUtilities.findFunctionLocalVariables(context);
075: }
076:
077: public static List/*<CsmDeclaration*/findClassFields(
078: BaseDocument doc, int offset) {
079: CsmClass clazz = findClassOnPosition(doc, offset);
080: List res = null;
081: if (clazz != null) {
082: res = new CsmProjectContentResolver().getFields(clazz,
083: false);
084: }
085: return res;
086: }
087:
088: public static List/*<CsmDeclaration*/findFileVariables(
089: BaseDocument doc, int offset) {
090: CsmFile file = CsmUtilities.getCsmFile(doc, true);
091: CsmContext context = CsmOffsetResolver
092: .findContext(file, offset);
093: return CsmContextUtilities.findFileLocalVariables(context);
094: }
095:
096: public static List/*<CsmDeclaration*/findGlobalVariables(
097: BaseDocument doc, int offset) {
098: CsmProject prj = CsmUtilities.getCsmProject(doc);
099: if (prj == null) {
100: return null;
101: }
102: return CsmContextUtilities.findGlobalVariables(prj);
103: }
104:
105: // TODO: think if we need it?
106: public static CsmClass findClassOnPosition(BaseDocument doc,
107: int offset) {
108: CsmFile file = CsmUtilities.getCsmFile(doc, true);
109: CsmContext context = CsmOffsetResolver
110: .findContext(file, offset);
111: CsmClass clazz = CsmContextUtilities.getClass(context, true);
112: return clazz;
113: }
114:
115: public static CsmOffsetableDeclaration findFunDefinitionOrClassOnPosition(
116: BaseDocument doc, int offset) {
117: CsmFile file = CsmUtilities.getCsmFile(doc, true);
118: CsmContext context = CsmOffsetResolver
119: .findContext(file, offset);
120: CsmOffsetableDeclaration out = null;
121: out = CsmContextUtilities.getFunctionDefinition(context);
122: if (out == null
123: || !CsmContextUtilities
124: .isInFunctionBodyOrInitializerList(context,
125: offset)) {
126: out = CsmContextUtilities.getClass(context, false);
127: }
128: return out;
129: }
130:
131: public static CsmObject findItemAtCaretPos(JTextComponent target,
132: int dotPos) {
133: return findItemAtCaretPos(target, null, CsmCompletionProvider
134: .getCompletionQuery(), dotPos);
135: }
136:
137: public static CsmObject findItemAtCaretPos(JTextComponent target,
138: BaseDocument doc, CsmCompletionQuery query, int dotPos) {
139: try {
140: doc = doc != null ? doc : (BaseDocument) target
141: .getDocument();
142: SyntaxSupport sup = doc.getSyntaxSupport();
143: int[] idFunBlk = NbEditorUtilities
144: .getIdentifierAndMethodBlock(doc, dotPos);
145:
146: if (idFunBlk == null) {
147: idFunBlk = new int[] { dotPos, dotPos };
148: }
149:
150: boolean searchFuncsOnly = (idFunBlk.length == 3);
151: for (int ind = idFunBlk.length - 1; ind >= 1; ind--) {
152: CompletionQuery.Result result = query.query(target,
153: doc, idFunBlk[ind], sup, true, false);
154: if (result != null && result.getData().size() > 0) {
155: List<CsmObject> filter = getAssociatedObjects(
156: result.getData(), searchFuncsOnly);
157: CsmObject itm = filter.size() > 0 ? filter.get(0)
158: : getAssociatedObject(result.getData().get(
159: 0));
160: if (filter.size() > 1 && searchFuncsOnly) {
161: // It is overloaded method, lets check for the right one
162: int endOfMethod = findEndOfMethod(doc,
163: idFunBlk[ind] - 1);
164: if (endOfMethod > -1) {
165: CompletionQuery.Result resultx = query
166: .query(target, doc, endOfMethod,
167: sup, true, false);
168: if (resultx != null
169: && resultx.getData().size() > 0) {
170: return getAssociatedObject(resultx
171: .getData().get(0));
172: }
173: }
174: }
175: return itm;
176: }
177: }
178: } catch (BadLocationException e) {
179: }
180: return null;
181: }
182:
183: private static List<CsmObject> getAssociatedObjects(List items,
184: boolean wantFuncsOnly) {
185: List<CsmObject> out = new ArrayList();
186: List<CsmObject> funcs = new ArrayList();
187:
188: for (Object item : items) {
189: if (item instanceof CsmResultItem) {
190: CsmObject ret = getAssociatedObject(item);
191: boolean isFunc = CsmKindUtilities.isFunction(ret);
192: if (isFunc) {
193: funcs.add(ret);
194: } else {
195: out.add(ret);
196: }
197: }
198: }
199: if (!wantFuncsOnly) {
200: out.addAll(funcs);
201: } else {
202: out = funcs;
203: }
204: return out;
205: }
206:
207: private static CsmObject getAssociatedObject(Object item) {
208: if (item instanceof CsmResultItem) {
209: CsmObject ret = (CsmObject) ((CsmResultItem) item)
210: .getAssociatedObject();
211: if (ret != null) {
212: return ret;
213: }
214: }
215: return null;
216: }
217:
218: static int findEndOfMethod(BaseDocument doc, int startPos) {
219: try {
220: int level = 0;
221: for (int i = startPos; i < doc.getLength(); i++) {
222: char ch = doc.getChars(i, 1)[0];
223: if (ch == ';')
224: return -1;
225: if (ch == '(')
226: level++;
227: if (ch == ')') {
228: level--;
229: if (level == 0) {
230: return i + 1;
231: }
232: }
233: }
234: return -1;
235: } catch (BadLocationException e) {
236: return -1;
237: }
238: }
239:
240: }
|