JSReferenceExpressionImpl.java

1    /* 
2     * Copyright 2000-2005 JetBrains s.r.o. 
3     * 
4     * Licensed under the Apache License, Version 2.0 (the "License"); 
5     * you may not use this file except in compliance with the License. 
6     * You may obtain a copy of the License at 
7     * 
8     * http://www.apache.org/licenses/LICENSE-2.0 
9     * 
10    * Unless required by applicable law or agreed to in writing, software 
11    * distributed under the License is distributed on an "AS IS" BASIS, 
12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13    * See the License for the specific language governing permissions and 
14    * limitations under the License. 
15    */ 
16   package com.intellij.lang.javascript.psi.impl; 
17    
18   import com.intellij.lang.ASTNode; 
19   import com.intellij.lang.javascript.JSElementTypes; 
20   import com.intellij.lang.javascript.JSTokenTypes; 
21   import com.intellij.lang.javascript.psi.JSElementVisitor; 
22   import com.intellij.lang.javascript.psi.JSExpression; 
23   import com.intellij.lang.javascript.psi.JSReferenceExpression; 
24   import com.intellij.lang.javascript.psi.JSResolveUtil; 
25   import com.intellij.openapi.util.Comparing; 
26   import com.intellij.openapi.util.TextRange; 
27   import com.intellij.psi.PsiElement; 
28   import com.intellij.psi.PsiElementVisitor; 
29   import com.intellij.psi.PsiNamedElement; 
30   import com.intellij.psi.PsiReference; 
31   import com.intellij.util.IncorrectOperationException; 
32   import org.jetbrains.annotations.Nullable; 
33    
34   /** 
35    * TODO: implement all reference stuff... 
36    */ 
37   public class JSReferenceExpressionImpl extends JSElementImpl implements JSReferenceExpression{ 
38     public JSReferenceExpressionImpl(final ASTNode node) { 
39       super(node); 
40     } 
41    
42     @Nullable public JSExpression getQualifier() { 
43       final ASTNode[] nodes = getNode().getChildren(JSElementTypes.EXPRESSIONS); 
44       return (JSExpression)(nodes.length == 1 ? nodes[0].getPsi() : null); 
45     } 
46    
47     @Nullable public String getReferencedName() { 
48       final ASTNode nameElement = getNameElement(); 
49       return nameElement != null ? nameElement.getText() : null; 
50     } 
51    
52     public PsiElement getElement() { 
53       return this; 
54     } 
55    
56     public PsiReference getReference() { 
57       return this; 
58     } 
59    
60     public TextRange getRangeInElement() { 
61       final ASTNode nameElement = getNameElement(); 
62       final int startOffset = nameElement != null ? nameElement.getStartOffset() : getNode().getTextRange().getEndOffset(); 
63       return new TextRange(startOffset - getNode().getStartOffset(), getTextLength()); 
64     } 
65    
66     private ASTNode getNameElement() { 
67       return getNode().findChildByType(JSTokenTypes.IDENTIFIER); 
68     } 
69    
70     public PsiElement resolve() { 
71       final String referencedName = getReferencedName(); 
72       if (referencedName == null) return null; 
73    
74       if (getQualifier() != null) { 
75         return null; // TODO? 
76       } 
77    
78       return JSResolveUtil.treeWalkUp(new JSResolveUtil.ResolveProcessor(referencedName), this, this, this); 
79     } 
80    
81     public String getCanonicalText() { 
82       return null; 
83     } 
84    
85     public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException { 
86       final ASTNode nameElement = JSChangeUtil.createNameIdentifier(getProject(), newElementName); 
87       getNode().replaceChild(getNameElement(), nameElement); 
88       return this; 
89     } 
90    
91     public PsiElement bindToElement(PsiElement element) throws IncorrectOperationException { 
92       final ASTNode nameElement = JSChangeUtil.createNameIdentifier(getProject(), ((PsiNamedElement)element).getName()); 
93       getNode().replaceChild(getNameElement(), nameElement); 
94       return this; 
95     } 
96    
97     public boolean isReferenceTo(PsiElement element) { 
98       if (element instanceof PsiNamedElement) { 
99         if (Comparing.equal(getReferencedName(), ((PsiNamedElement)element).getName())) return resolve() == element; 
100      } 
101      return false; 
102    } 
103   
104    public Object[] getVariants() { 
105      if (getQualifier() != null) { 
106        return new Object[0]; // TODO? 
107      } 
108   
109      final JSResolveUtil.VariantsProcessor processor = new JSResolveUtil.VariantsProcessor(); 
110      JSResolveUtil.treeWalkUp(processor, this, this, this); 
111      return processor.getResult(); 
112    } 
113   
114    public boolean isSoft() { 
115      return false; 
116    } 
117   
118    public void accept(PsiElementVisitor visitor) { 
119      if (visitor instanceof JSElementVisitor) { 
120        ((JSElementVisitor)visitor).visitJSReferenceExpression(this); 
121      } 
122      else { 
123        visitor.visitElement(this); 
124      } 
125    } 
126   
127    public String toString() { 
128      return "JSReferenceExpression"; 
129    } 
130  } 
131