Switched to Eclipse 2.1 development; eliminated JTidy package => now standalone plugin
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / codeassist / complete / CompletionOnQualifiedNameReference.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v0.5 
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v05.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  ******************************************************************************/
11 package net.sourceforge.phpdt.internal.codeassist.complete;
12
13 /*
14  * Completion node build by the parser in any case it was intending to
15  * reduce a qualified name reference containing the completion identifier.
16  * e.g.
17  *
18  *      class X {
19  *    Y y;
20  *    void foo() {
21  *      y.fred.ba[cursor]
22  *    }
23  *  }
24  *
25  *      ---> class X {
26  *         Y y;
27  *         void foo() {
28  *           <CompleteOnName:y.fred.ba>
29  *         }
30  *       }
31  *
32  * The source range of the completion node denotes the source range
33  * which should be replaced by the completion.
34  */
35
36 import net.sourceforge.phpdt.internal.compiler.ast.QualifiedNameReference;
37 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
38 import net.sourceforge.phpdt.internal.compiler.lookup.FieldBinding;
39 import net.sourceforge.phpdt.internal.compiler.lookup.ProblemFieldBinding;
40 import net.sourceforge.phpdt.internal.compiler.lookup.ProblemReferenceBinding;
41 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
42
43 public class CompletionOnQualifiedNameReference extends QualifiedNameReference {
44         public char[] completionIdentifier;
45         public long[] sourcePositions; // positions of each token, the last one being the positions of the completion identifier
46 public CompletionOnQualifiedNameReference(char[][] previousIdentifiers, char[] completionIdentifier, long[] positions) {
47         super(previousIdentifiers, (int) (positions[0] >>> 32), (int) positions[positions.length - 1]);
48         this.completionIdentifier = completionIdentifier;
49         this.sourcePositions = positions;
50 }
51 public CompletionOnQualifiedNameReference(char[][] previousIdentifiers, char[] completionIdentifier, int sourceStart, int sourceEnd) {
52         super(previousIdentifiers, sourceStart, sourceEnd);
53         this.completionIdentifier = completionIdentifier;
54         this.sourcePositions = new long[] {((long)sourceStart << 32) + sourceEnd};
55 }
56 public TypeBinding resolveType(BlockScope scope) {
57         // it can be a package, type, member type, local variable or field
58         binding = scope.getBinding(tokens, this);
59         if (!binding.isValidBinding()) {
60                 if (binding instanceof ProblemFieldBinding) {
61                         scope.problemReporter().invalidField(this, (FieldBinding) binding);
62                 } else if (binding instanceof ProblemReferenceBinding) {
63                         scope.problemReporter().invalidType(this, (TypeBinding) binding);
64                 } else {
65                         scope.problemReporter().unresolvableReference(this, binding);
66                 }
67                 throw new CompletionNodeFound();
68         }
69
70         throw new CompletionNodeFound(this, binding, scope);
71 }
72 public String toStringExpression() {
73
74         StringBuffer buffer = new StringBuffer("<CompleteOnName:"); //$NON-NLS-1$
75         for (int i = 0; i < tokens.length; i++) {
76                 buffer.append(tokens[i]);
77                 buffer.append("."); //$NON-NLS-1$
78         }
79         buffer.append(completionIdentifier).append(">"); //$NON-NLS-1$
80         return buffer.toString();
81 }
82 }