JSFunctionImpl.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.*; 
22   import com.intellij.psi.PsiElement; 
23   import com.intellij.psi.PsiElementVisitor; 
24   import com.intellij.psi.PsiSubstitutor; 
25   import com.intellij.psi.scope.PsiScopeProcessor; 
26   import com.intellij.util.Icons; 
27   import com.intellij.util.IncorrectOperationException; 
28    
29   import javax.swing.*; 
30    
31   /** 
32    * Created by IntelliJ IDEA. 
33    * User: max 
34    * Date: Jan 30, 2005 
35    * Time: 8:25:27 PM 
36    * To change this template use File | Settings | File Templates. 
37    */ 
38   public class JSFunctionImpl extends JSElementImpl implements JSFunction { 
39     public JSFunctionImpl(final ASTNode node) { 
40       super(node); 
41     } 
42    
43     public JSParameterList getParameterList() { 
44       return (JSParameterList)getNode().findChildByType(JSElementTypes.PARAMETER_LIST).getPsi(); 
45     } 
46    
47     public JSSourceElement[] getBody() { 
48       final ASTNode[] children = getNode().getChildren(JSElementTypes.SOURCE_ELEMENTS); 
49       if (children.length == 0) return JSSourceElement.EMPTY_ARRAY; 
50       JSSourceElement[] result = new JSSourceElement[children.length]; 
51       for (int i = 0; i < children.length; i++) { 
52         result[i] = (JSSourceElement)children[i].getPsi(); 
53       } 
54       return result; 
55     } 
56    
57     public PsiElement setName(String name) throws IncorrectOperationException { 
58       final ASTNode nameElement = JSChangeUtil.createNameIdentifier(getProject(), name); 
59       getNode().replaceChild(findNameIdentifier(), nameElement); 
60       return this; 
61     } 
62    
63     public String getName() { 
64       final ASTNode name = findNameIdentifier(); 
65       return name != null ? name.getText() : null; 
66     } 
67    
68     public ASTNode findNameIdentifier() { 
69       return getNode().findChildByType(JSTokenTypes.IDENTIFIER); 
70     } 
71    
72     public int getTextOffset() { 
73       final ASTNode name = findNameIdentifier(); 
74       return name != null ? name.getStartOffset() : super.getTextOffset(); 
75     } 
76    
77     public boolean processDeclarations(PsiScopeProcessor processor, 
78                                        PsiSubstitutor substitutor, 
79                                        PsiElement lastParent, 
80                                        PsiElement place) { 
81       if (lastParent != null && lastParent.getParent() == this) { 
82         final JSParameter[] params = getParameterList().getParameters(); 
83         for (JSParameter param : params) { 
84           if (!processor.execute(param, substitutor)) return false; 
85         } 
86       } 
87    
88       return processor.execute(this, substitutor); 
89     } 
90    
91     public void accept(PsiElementVisitor visitor) { 
92       if (visitor instanceof JSElementVisitor) { 
93         ((JSElementVisitor)visitor).visitJSFunctionDeclaration(this); 
94       } 
95       else { 
96         visitor.visitElement(this); 
97       } 
98     } 
99    
100    public String toString() { 
101      return "JSFunction"; 
102    } 
103   
104    public Icon getIcon(int flags) { 
105      return Icons.METHOD_ICON; 
106    } 
107  } 
108