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-2006 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: package org.netbeans.modules.php.editor.completion;
042:
043: import java.util.ArrayList;
044: import java.util.HashSet;
045: import java.util.List;
046: import java.util.Set;
047: import org.netbeans.modules.gsf.api.CompletionProposal;
048: import org.netbeans.modules.gsf.api.HtmlFormatter;
049: import org.netbeans.modules.languages.php.lang.Keywords;
050: import org.netbeans.modules.languages.php.lang.Keywords;
051: import org.netbeans.modules.languages.php.lang.MagicConstants;
052: import org.netbeans.modules.php.editor.TokenUtils;
053: import org.netbeans.modules.php.model.SourceElement;
054:
055: /**
056: * Implementation of the <code>CompletionResultProvider</code> for the new
057: * instruction context.
058: * <p><b>Note that this implementation is not synchronized.</b></p>
059: *
060: * @author Victor G. Vasilyev
061: */
062: public class NewInstructionContext extends ASTBasedProvider implements
063: CompletionResultProvider {
064:
065: protected final List<CompletionProposal> proposalList = new ArrayList<CompletionProposal>();
066:
067: /**
068: * Returns <code>true</code> iif the specified <code>context</code>
069: * is applicable for inserting a new instruction.
070: * E.g
071: * <p>
072: * <b><?php</b>
073: * <span style="color: rgb(255, 0, 0);"><blink>|</blink></span>
074: * ... <b>?></b>
075: * </p>
076: * or
077: * <p>
078: * <b><?php</b> <i>Instruction</i><b>;</b>
079: * <span style="color: rgb(255, 0, 0);"><blink>|</blink></span>
080: * ... <b>?></b>
081: * </p>
082: *
083: * @param context
084: * @return <code>true</code> iif the specified <code>context</code>
085: * is applicable.
086: */
087: public boolean isApplicable(CodeCompletionContext context) {
088: init(context);
089: if (isMatchedCL1(CURR_TOKENS, PREV_TOKENS)) {
090: return true;
091: }
092: SourceElement e = context.getSourceElement();
093: if (e != null && ExpressionContext.isNewIncompletedStatement(e)) {
094: return true;
095: }
096: return false;
097: }
098:
099: public List<CompletionProposal> getProposals(
100: final CodeCompletionContext context) {
101: checkContext(context);
102: addStaticProposals();
103: ExpressionContext.addBuiltinFunctionProposals(proposalList,
104: context);
105: ExpressionContext.addUserDefinedFunctionProposals(proposalList,
106: context);
107: return proposalList;
108: }
109:
110: protected void init(CodeCompletionContext context) {
111: assert context != null;
112: myContext = context;
113: proposalList.clear();
114: }
115:
116: /** Adds the proposals that are applicable to any context. */
117: protected void addStaticProposals() {
118: String prefix = myContext.getPrefix();
119: int insertOffset = myContext.getInsertOffset();
120: HtmlFormatter formater = myContext.getFormatter();
121: for (Keywords k : KEYWORDS) {
122: if (myContext.isEmptyPrefix() || k.isMatched(prefix)) {
123: proposalList.add(new KeywordItem(k, insertOffset,
124: formater));
125: }
126: }
127: for (MagicConstants mc : MagicConstants.values()) {
128: if (myContext.isEmptyPrefix() || mc.isMatched(prefix)) {
129: proposalList.add(new MagicConstantItem(mc,
130: insertOffset, formater));
131: }
132: }
133: }
134:
135: private static final Set<Keywords> KEYWORDS = new HashSet<Keywords>();
136: static {
137: KEYWORDS.add(Keywords.CLASS);
138: KEYWORDS.add(Keywords.FUNCTION);
139: KEYWORDS.add(Keywords.DECLARE);
140: KEYWORDS.add(Keywords.ARRAY);
141: KEYWORDS.add(Keywords.CLASS);
142: KEYWORDS.add(Keywords.DIE);
143: KEYWORDS.add(Keywords.DECLARE);
144: KEYWORDS.add(Keywords.DO);
145: KEYWORDS.add(Keywords.ECHO);
146: KEYWORDS.add(Keywords.EMPTY);
147: KEYWORDS.add(Keywords.EVAL);
148: KEYWORDS.add(Keywords.EXIT);
149: KEYWORDS.add(Keywords.FOR);
150: KEYWORDS.add(Keywords.FOREACH);
151: KEYWORDS.add(Keywords.FUNCTION);
152: KEYWORDS.add(Keywords.IF);
153: KEYWORDS.add(Keywords.INCLUDE);
154: KEYWORDS.add(Keywords.INCLUDE_ONCE);
155: KEYWORDS.add(Keywords.ISSET);
156: KEYWORDS.add(Keywords.LIST);
157: KEYWORDS.add(Keywords.NEW);
158: KEYWORDS.add(Keywords.PRINT);
159: KEYWORDS.add(Keywords.REQUIRE);
160: KEYWORDS.add(Keywords.REQUIRE_ONCE);
161: KEYWORDS.add(Keywords.RETURN);
162: KEYWORDS.add(Keywords.SWITCH);
163: KEYWORDS.add(Keywords.UNSET);
164: KEYWORDS.add(Keywords.WHILE);
165: KEYWORDS.add(Keywords.INTERFACE);
166: KEYWORDS.add(Keywords.ABSTRACT);
167: KEYWORDS.add(Keywords.TRY);
168: KEYWORDS.add(Keywords.TROW);
169: }
170:
171: private static final Set<ExpectedToken> PREV_TOKENS = new HashSet<ExpectedToken>();
172: static {
173: PREV_TOKENS.add(new ExpectedToken(
174: TokenUtils.PHPTokenName.SEPARATOR.value(), null));
175: PREV_TOKENS.add(new ExpectedToken(
176: TokenUtils.PHPTokenName.BLOCK_COMMENT.value(), null));
177: PREV_TOKENS.add(new ExpectedToken(
178: TokenUtils.PHPTokenName.LINE_COMMENT.value(), null));
179: PREV_TOKENS.add(new ExpectedToken(
180: TokenUtils.PHPTokenName.WHITESPACE.value(), null));
181: }
182:
183: private static final Set<ExpectedToken> CURR_TOKENS = new HashSet<ExpectedToken>();
184: static {
185: CURR_TOKENS.add(new ExpectedToken(
186: TokenUtils.PHPTokenName.WHITESPACE.value(), null));
187: }
188:
189: }
|