aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorzzhongwei2012-08-17 05:59:16 (EDT)
committerzzhongwei2012-08-17 05:59:16 (EDT)
commit20ad5b1ceac66b16e219be0dae76d21f0c781b63 (patch)
tree233a295077e83059a2bd163824aa1677b37ae8cc
parent6b376b5b1060c2f83ead60f4295a04bb015a8bfa (diff)
downloadorg.eclipse.pdt-20ad5b1ceac66b16e219be0dae76d21f0c781b63.zip
org.eclipse.pdt-20ad5b1ceac66b16e219be0dae76d21f0c781b63.tar.gz
org.eclipse.pdt-20ad5b1ceac66b16e219be0dae76d21f0c781b63.tar.bz2
387449: Code completion supports array indexes in PHP code
https://bugs.eclipse.org/bugs/show_bug.cgi?id=387449
-rw-r--r--plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/codeassist/strategies/ArrayStringKeysStrategy.java171
-rw-r--r--plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/codeassist/strategies/CompletionStrategyFactory.java8
2 files changed, 176 insertions, 3 deletions
diff --git a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/codeassist/strategies/ArrayStringKeysStrategy.java b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/codeassist/strategies/ArrayStringKeysStrategy.java
new file mode 100644
index 0000000..3453cd4
--- /dev/null
+++ b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/codeassist/strategies/ArrayStringKeysStrategy.java
@@ -0,0 +1,171 @@
+/*******************************************************************************
+ * Copyright (c) 2009 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ * Zend Technologies
+ *******************************************************************************/
+package org.eclipse.php.internal.core.codeassist.strategies;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.eclipse.dltk.ast.declarations.ModuleDeclaration;
+import org.eclipse.dltk.core.CompletionRequestor;
+import org.eclipse.dltk.core.SourceParserUtil;
+import org.eclipse.dltk.internal.core.ModelElement;
+import org.eclipse.dltk.internal.core.SourceRange;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.php.core.codeassist.ICompletionContext;
+import org.eclipse.php.core.codeassist.IElementFilter;
+import org.eclipse.php.internal.core.codeassist.ICompletionReporter;
+import org.eclipse.php.internal.core.codeassist.contexts.ArrayKeyContext;
+import org.eclipse.php.internal.core.compiler.ast.nodes.*;
+import org.eclipse.php.internal.core.compiler.ast.parser.ASTUtils;
+import org.eclipse.php.internal.core.compiler.ast.visitor.PHPASTVisitor;
+import org.eclipse.php.internal.core.typeinference.FakeField;
+
+/**
+ * This strategy completes builtin array keys, like in _SERVER.
+ *
+ * @author michael
+ */
+public class ArrayStringKeysStrategy extends AbstractCompletionStrategy {
+
+ public ArrayStringKeysStrategy(ICompletionContext context,
+ IElementFilter elementFilter) {
+ super(context, elementFilter);
+ }
+
+ public ArrayStringKeysStrategy(ICompletionContext context) {
+ super(context);
+ }
+
+ public void apply(ICompletionReporter reporter) throws BadLocationException {
+ ICompletionContext context = getContext();
+ if (!(context instanceof ArrayKeyContext)) {
+ return;
+ }
+
+ SourceRange replaceRange = getReplacementRange(context);
+ ArrayKeyContext arrayContext = (ArrayKeyContext) context;
+ CompletionRequestor requestor = arrayContext.getCompletionRequestor();
+
+ String prefix = arrayContext.getPrefix();
+ ModuleDeclaration moduleDeclaration = SourceParserUtil
+ .getModuleDeclaration(arrayContext.getSourceModule());
+ try {
+ ArrayKeyFinder finder = new ArrayKeyFinder(prefix);
+ moduleDeclaration.traverse(finder);
+ Set<String> names = finder.getNames();
+ for (String name : names) {
+
+ if (!requestor.isContextInformationMode()) {
+ if (!arrayContext.hasQuotes()) {
+ name = "\"" + name + "\"";
+ }
+ reporter.reportField(new FakeField(
+ (ModelElement) arrayContext.getSourceModule(),
+ name, 0, 0), "", replaceRange, false); // NON-NLS-1
+ }
+
+ }
+ } catch (Exception e) {
+ }
+ }
+
+ protected void reportVariables(ICompletionReporter reporter,
+ ArrayKeyContext context, String[] variables, String prefix)
+ throws BadLocationException {
+ reportVariables(reporter, context, variables, prefix, false);
+ }
+
+ protected void reportVariables(ICompletionReporter reporter,
+ ArrayKeyContext context, String[] variables, String prefix,
+ boolean removeDollar) throws BadLocationException {
+ CompletionRequestor requestor = context.getCompletionRequestor();
+ SourceRange replaceRange = getReplacementRange(context);
+ for (String variable : variables) {
+ if (removeDollar) {
+ variable = variable.substring(1);
+ }
+ if (variable.startsWith(prefix)) {
+ if (!requestor.isContextInformationMode()
+ || variable.length() == prefix.length()) {
+ reporter.reportField(
+ new FakeField((ModelElement) context
+ .getSourceModule(), variable, 0, 0), "",
+ replaceRange, false); // NON-NLS-1
+ }
+ }
+ }
+ }
+
+ class ArrayKeyFinder extends PHPASTVisitor {
+ Set<String> names = new HashSet<String>();
+ String prefix;
+
+ public ArrayKeyFinder(String prefix) {
+ // TODO Auto-generated constructor stub
+ this.prefix = prefix;
+ }
+
+ public boolean visit(ArrayCreation s) throws Exception {
+ return super.visit(s);
+ }
+
+ public boolean visit(ArrayElement s) throws Exception {
+ if (s.getKey() instanceof Scalar) {
+ Scalar scalar = (Scalar) s.getKey();
+ if (scalar.getScalarType() == Scalar.TYPE_STRING) {
+ String key = ASTUtils.stripQuotes(scalar.getValue());
+ if (key.toLowerCase().startsWith(prefix.toLowerCase())) {
+ names.add(key);
+ }
+ }
+ }
+ return super.visit(s);
+ }
+
+ public boolean visit(ArrayVariableReference s) throws Exception {
+
+ if (s.getIndex() instanceof Scalar) {
+ Scalar scalar = (Scalar) s.getIndex();
+ if (scalar.getScalarType() == Scalar.TYPE_STRING) {
+ String key = ASTUtils.stripQuotes(scalar.getValue());
+ if (key.toLowerCase().startsWith(prefix.toLowerCase())) {
+ names.add(key);
+ }
+ }
+ }
+ return super.visit(s);
+ }
+
+ public boolean visit(ReflectionArrayVariableReference s)
+ throws Exception {
+ if (s.getIndex() instanceof Scalar) {
+ Scalar scalar = (Scalar) s.getIndex();
+ if (scalar.getScalarType() == Scalar.TYPE_STRING) {
+ String key = ASTUtils.stripQuotes(scalar.getValue());
+ if (key.toLowerCase().startsWith(prefix.toLowerCase())) {
+ names.add(key);
+ }
+ }
+ }
+ return super.visit(s);
+ }
+
+ public boolean visit(PHPArrayDereferenceList s) throws Exception {
+ return super.visit(s);
+ }
+
+ public Set<String> getNames() {
+ return names;
+ }
+
+ }
+}
diff --git a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/codeassist/strategies/CompletionStrategyFactory.java b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/codeassist/strategies/CompletionStrategyFactory.java
index 7e5b434..0af4f82 100644
--- a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/codeassist/strategies/CompletionStrategyFactory.java
+++ b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/codeassist/strategies/CompletionStrategyFactory.java
@@ -125,15 +125,17 @@ public class CompletionStrategyFactory implements ICompletionStrategyFactory {
// If array has quotes or double-quotes around the key - show only
// builtin keys:
if (((ArrayKeyContext) context).hasQuotes()) {
- return new ICompletionStrategy[] { new BuiltinArrayKeysStrategy(
- context) };
+ return new ICompletionStrategy[] {
+ new BuiltinArrayKeysStrategy(context),
+ new ArrayStringKeysStrategy(context) };
}
// Otherwise - show all global elements also:
// Example: $array[foo()], $array[$otherVar]
return new ICompletionStrategy[] {
new BuiltinArrayKeysStrategy(context),
new GlobalElementsCompositeStrategy(context, false),
- new LocalMethodVariablesStrategyForArray(context) };
+ new LocalMethodVariablesStrategyForArray(context),
+ new ArrayStringKeysStrategy(context) };
}
if (contextClass == FunctionParameterTypeContext.class) {
return new ICompletionStrategy[] {