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:
042: package org.netbeans.modules.php.editor.completion;
043:
044: import java.io.IOException;
045: import java.util.ArrayList;
046: import java.util.HashSet;
047: import java.util.List;
048: import java.util.Set;
049: import org.netbeans.modules.gsf.api.CompletionProposal;
050: import org.netbeans.api.lexer.Token;
051: import org.netbeans.api.lexer.TokenSequence;
052: import org.netbeans.modules.php.editor.TokenUtils;
053:
054: /**
055: * Implementation of the <code>CompletionResultProvider</code> for the string
056: * scope.
057: * @see http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing
058: * @author Victor G. Vasilyev
059: */
060: public class StringScope extends ASTBasedProvider implements
061: CompletionResultProvider {
062:
063: private static final String SINGLE_QUOTE = "'";
064: private static final String VARIABLE_PREFIX = "$";
065:
066: protected final List<CompletionProposal> proposalList = new ArrayList<CompletionProposal>();
067:
068: public boolean isApplicable(CodeCompletionContext context) {
069: init(context);
070: if (!isMatchedCL1(CURR_TOKENS, ANY_TOKEN)) {
071: return false;
072: }
073: String tokenText;
074: try {
075: TokenSequence ts = getTokenSequencePHP();
076: Token t = TokenUtils.getEnteredToken(ts, getCaretOffset());
077: tokenText = t.text().toString();
078: } catch (IOException ex) {
079: return false;
080: } catch (Exception ex) {
081: return false;
082: }
083: if (tokenText == null || tokenText.length() < 2) {
084: return false;
085: }
086: if (isSingleQuotedString(tokenText)) {
087: // Note: Unlike the two other syntaxes, variables and
088: // escape sequences for special characters will not be expanded
089: // when they occur in single quoted strings.
090: return false;
091: }
092: // i.e. here we have either a Double quoted string or Heredoc string
093: return true;
094: }
095:
096: public List<CompletionProposal> getProposals(
097: CodeCompletionContext context) {
098: addVariableProposals(context);
099: return proposalList;
100: }
101:
102: private void addVariableProposals(CodeCompletionContext context) {
103: String prefix = context.getPrefix();
104: try {
105: int indexOfVarPrefix = prefix.lastIndexOf(VARIABLE_PREFIX); // NPE !
106: prefix = prefix.substring(indexOfVarPrefix); // IOBE !
107: CodeCompletionContext newContext = CodeCompletionContext
108: .changePrefix(context, prefix);
109: newContext.setCurrentSourceElement(newContext
110: .getSourceElement());
111: VariableProvider vp = new VariableProvider();
112: List<CompletionProposal> pl = vp.getProposals(newContext);
113: proposalList.addAll(pl);
114: } catch (IndexOutOfBoundsException iobe) {
115: // do nothing.
116: } catch (NullPointerException npe) {
117: // do nothing.
118: }
119: }
120:
121: protected void init(CodeCompletionContext context) {
122: assert context != null;
123: myContext = context;
124: proposalList.clear();
125: }
126:
127: private boolean isSingleQuotedString(String tokenText) {
128: return tokenText != null && tokenText.startsWith(SINGLE_QUOTE)
129: && tokenText.endsWith(SINGLE_QUOTE);
130: }
131:
132: private static final Set<ExpectedToken> CURR_TOKENS = new HashSet<ExpectedToken>();
133: static {
134: CURR_TOKENS.add(new ExpectedToken(TokenUtils.STRING, null));
135: CURR_TOKENS.add(new ExpectedToken(TokenUtils.EOD_STRING, null));
136: }
137:
138: }
|