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.cplusplus.utils;
043:
044: import java.util.ArrayList;
045: import java.util.Collections;
046: import java.util.List;
047: import javax.swing.text.BadLocationException;
048: import javax.swing.text.Document;
049: import org.netbeans.cnd.api.lexer.CndLexerUtilities;
050: import org.netbeans.editor.BaseDocument;
051: import org.netbeans.editor.SyntaxSupport;
052: import org.netbeans.editor.TokenContextPath;
053: import org.netbeans.editor.TokenID;
054: import org.netbeans.editor.TokenProcessor;
055: import org.netbeans.modules.cnd.completion.cplusplus.NbCsmSyntaxSupport;
056: import org.netbeans.modules.cnd.editor.cplusplus.CCTokenContext;
057:
058: /**
059: *
060: * @author Vladimir Voskresensky
061: */
062: public final class TokenUtilities {
063:
064: /** Creates a new instance of TokenUtilities */
065: private TokenUtilities() {
066: }
067:
068: public static List<Token> getTokens(Document doc, int start, int end) {
069: if (!(doc instanceof BaseDocument))
070: return Collections.<Token> emptyList();
071:
072: try {
073: SyntaxSupport sup = ((BaseDocument) doc).getSyntaxSupport();
074: NbCsmSyntaxSupport nbSyntaxSup = (NbCsmSyntaxSupport) sup
075: .get(NbCsmSyntaxSupport.class);
076: if (nbSyntaxSup == null) {
077: String name = (String) doc
078: .getProperty(Document.TitleProperty);
079: System.err
080: .println("document "
081: + name
082: + " doesn't have NbCsmSyntaxSupport syntax support "
083: + doc);
084: System.err.println("document "
085: + (CndLexerUtilities.getCppTokenSequence(doc,
086: start) == null ? "DOESN'T" : "")
087: + " have " + "lexer info");
088: return Collections.<Token> emptyList();
089: }
090:
091: AllTokensTP tp = new AllTokensTP();
092: nbSyntaxSup.tokenizeText(tp, start, end, true);
093: return tp.getTokens();
094: } catch (BadLocationException e) {
095: // there could be some modifications in document
096: return Collections.<Token> emptyList();
097: }
098: }
099:
100: public static Token getToken(Document doc, int offset) {
101: if (!(doc instanceof BaseDocument))
102: return null;
103:
104: try {
105: BaseDocument bdoc = (BaseDocument) doc;
106:
107: SyntaxSupport sup = bdoc.getSyntaxSupport();
108: NbCsmSyntaxSupport nbSyntaxSup = (NbCsmSyntaxSupport) sup
109: .get(NbCsmSyntaxSupport.class);
110: if (nbSyntaxSup == null) {
111: return null;
112: }
113: FirstTokenTP fttp = new FirstTokenTP();
114: nbSyntaxSup.tokenizeText(fttp, offset, doc.getLength(),
115: true);
116: Token token = fttp.getToken();
117:
118: return token;
119: } catch (BadLocationException e) {
120: // there could be some modifications in document
121: return null;
122: }
123: }
124:
125: private static int TOKEN_LIMIT = 100;
126:
127: public static int correctOffsetToID(BaseDocument doc, int offset) {
128: if (doc == null) {
129: return offset;
130: }
131: SyntaxSupport sup = doc.getSyntaxSupport();
132: if (sup == null) {
133: return offset;
134: }
135: NbCsmSyntaxSupport nbSyntaxSup = (NbCsmSyntaxSupport) sup
136: .get(NbCsmSyntaxSupport.class);
137: if (nbSyntaxSup == null) {
138: return offset;
139: }
140: int tokenLimitCounter = 0;
141: int origOffset = offset;
142: boolean wasNonWS = false;
143: try {
144: while (tokenLimitCounter < TOKEN_LIMIT && offset >= 0) {
145: TokenID token = nbSyntaxSup.getTokenID(offset);
146: tokenLimitCounter++;
147: if (token == null) {
148: return origOffset;
149: } else if (token == CCTokenContext.IDENTIFIER
150: || token == CCTokenContext.SYS_INCLUDE
151: || token == CCTokenContext.USR_INCLUDE) {
152: return offset;
153: } else if (token == CCTokenContext.WHITESPACE) {
154: offset--;
155: if (wasNonWS) {
156: return origOffset;
157: }
158: } else if (token.getCategory() == CCTokenContext.OPERATORS) {
159: offset--;
160: if (wasNonWS) {
161: return origOffset;
162: }
163: wasNonWS = true;
164: } else {
165: return origOffset;
166: }
167: }
168: } catch (BadLocationException e) {
169: // there could be some modifications in document
170: }
171: return origOffset;
172: }
173:
174: private static class FirstTokenTP implements TokenProcessor {
175:
176: private Token token;
177: private int bufferStartPos;
178: private char[] buffer;
179:
180: public Token getToken() {
181: return token;
182: }
183:
184: public boolean token(TokenID tokenID,
185: TokenContextPath tokenContextPath, int offset,
186: int tokenLen) {
187: String text = new String(buffer, offset, tokenLen);
188: token = new Token(bufferStartPos + offset, tokenLen,
189: tokenID, text);
190: return false; // no more tokens
191: }
192:
193: public int eot(int offset) {
194: return 0;
195: }
196:
197: public void nextBuffer(char[] buffer, int offset, int len,
198: int startPos, int preScan, boolean lastBuffer) {
199: this .buffer = buffer;
200: bufferStartPos = startPos - offset;
201: }
202:
203: }
204:
205: private static class AllTokensTP implements TokenProcessor {
206:
207: private List<Token> tokens = new ArrayList<Token>();
208: private int bufferStartPos;
209: private char[] buffer;
210:
211: public List<Token> getTokens() {
212: return tokens;
213: }
214:
215: public boolean token(TokenID tokenID,
216: TokenContextPath tokenContextPath, int offset,
217: int tokenLen) {
218: String text = new String(buffer, offset, tokenLen);
219: tokens.add(new Token(bufferStartPos + offset, tokenLen,
220: tokenID, text));
221: return true;
222: }
223:
224: public int eot(int offset) {
225: return 0;
226: }
227:
228: public void nextBuffer(char[] buffer, int offset, int len,
229: int startPos, int preScan, boolean lastBuffer) {
230: this.buffer = buffer;
231: bufferStartPos = startPos - offset;
232: }
233:
234: }
235: }
|