JSVariableImpl.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.JSType; 
24   import com.intellij.lang.javascript.psi.JSVariable; 
25   import com.intellij.psi.PsiElement; 
26   import com.intellij.psi.PsiElementVisitor; 
27   import com.intellij.util.Icons; 
28   import com.intellij.util.IncorrectOperationException; 
29    
30   import javax.swing.*; 
31    
32   /** 
33    * Created by IntelliJ IDEA. 
34    * User: max 
35    * Date: Jan 30, 2005 
36    * Time: 8:47:58 PM 
37    * To change this template use File | Settings | File Templates. 
38    */ 
39   public class JSVariableImpl extends JSElementImpl implements JSVariable { 
40     public JSVariableImpl(final ASTNode node) { 
41       super(node); 
42     } 
43    
44     public boolean hasInitializer() { 
45       return getInitializer() != null; 
46     } 
47    
48     public JSExpression getInitializer() { 
49       final ASTNode[] initializer = getNode().getChildren(JSElementTypes.EXPRESSIONS); 
50       return (JSExpression)(initializer.length == 1 ? initializer[0].getPsi() : null); 
51     } 
52    
53     public String getName() { 
54       final ASTNode nameIdentifier = findNameIdentifier(); 
55       return nameIdentifier.getText(); 
56     } 
57    
58     public ASTNode findNameIdentifier() { 
59       return getNode().findChildByType(JSTokenTypes.IDENTIFIER); 
60     } 
61    
62     public void setInitializer(JSExpression expr) throws IncorrectOperationException { 
63       throw new UnsupportedOperationException("TODO: implement"); 
64     } 
65    
66     public JSType getType() { 
67       return null; //TODO 
68     } 
69    
70     public PsiElement setName(String name) throws IncorrectOperationException { 
71       final ASTNode nameElement = JSChangeUtil.createNameIdentifier(getProject(), name); 
72       getNode().replaceChild(getNode().getFirstChildNode(), nameElement); 
73       return this; 
74     } 
75    
76     public String toString() { 
77       return "JSVariable"; 
78     } 
79    
80     public void accept(PsiElementVisitor visitor) { 
81       if (visitor instanceof JSElementVisitor) { 
82         ((JSElementVisitor)visitor).visitJSVariable(this); 
83       } 
84       else { 
85         visitor.visitElement(this); 
86       } 
87     } 
88    
89     public int getTextOffset() { 
90       final ASTNode name = findNameIdentifier(); 
91       return name != null ? name.getStartOffset() : super.getTextOffset(); 
92     } 
93    
94     public boolean isConst() { 
95       final ASTNode parent = getNode().getTreeParent(); 
96       return parent.getElementType() == JSElementTypes.VAR_STATEMENT && parent.getFirstChildNode().getElementType() == JSTokenTypes.CONST_KEYWORD; 
97     } 
98    
99     public Icon getIcon(int flags) { 
100      return Icons.VARIABLE_ICON; 
101    } 
102  } 
103