001    /*
002     * Copyright 2010-2013 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.jet.lang.psi;
018    
019    import com.intellij.lang.ASTNode;
020    import com.intellij.navigation.ItemPresentation;
021    import com.intellij.navigation.ItemPresentationProviders;
022    import com.intellij.psi.PsiElement;
023    import com.intellij.psi.tree.IElementType;
024    import com.intellij.psi.util.PsiTreeUtil;
025    import org.jetbrains.annotations.NotNull;
026    import org.jetbrains.annotations.Nullable;
027    import org.jetbrains.jet.JetNodeTypes;
028    import org.jetbrains.jet.lang.psi.stubs.PsiJetFunctionStub;
029    import org.jetbrains.jet.lang.psi.stubs.elements.JetStubElementTypes;
030    import org.jetbrains.jet.lexer.JetTokens;
031    
032    import java.util.Collections;
033    import java.util.List;
034    
035    public class JetNamedFunction extends JetTypeParameterListOwnerStub<PsiJetFunctionStub> implements JetFunction, JetWithExpressionInitializer {
036        public JetNamedFunction(@NotNull ASTNode node) {
037            super(node);
038        }
039    
040        public JetNamedFunction(@NotNull PsiJetFunctionStub stub) {
041            super(stub, JetStubElementTypes.FUNCTION);
042        }
043    
044        @Override
045        public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
046            return visitor.visitNamedFunction(this, data);
047        }
048    
049        public boolean hasTypeParameterListBeforeFunctionName() {
050            JetTypeParameterList typeParameterList = getTypeParameterList();
051            if (typeParameterList == null) {
052                return false;
053            }
054            PsiElement nameIdentifier = getNameIdentifier();
055            if (nameIdentifier == null) {
056                return false;
057            }
058            return nameIdentifier.getTextOffset() > typeParameterList.getTextOffset();
059        }
060    
061        @Override
062        public boolean hasBlockBody() {
063            return getEqualsToken() == null;
064        }
065    
066        @Nullable
067        public PsiElement getEqualsToken() {
068            return findChildByType(JetTokens.EQ);
069        }
070    
071        @Override
072        @Nullable
073        public JetExpression getInitializer() {
074            return PsiTreeUtil.getNextSiblingOfType(getEqualsToken(), JetExpression.class);
075        }
076    
077        @Override
078        public ItemPresentation getPresentation() {
079            return ItemPresentationProviders.getItemPresentation(this);
080        }
081    
082        @Override
083        @Nullable
084        public JetParameterList getValueParameterList() {
085            return (JetParameterList) findChildByType(JetNodeTypes.VALUE_PARAMETER_LIST);
086        }
087    
088        @Override
089        @NotNull
090        public List<JetParameter> getValueParameters() {
091            JetParameterList list = getValueParameterList();
092            return list != null ? list.getParameters() : Collections.<JetParameter>emptyList();
093        }
094    
095        @Override
096        @Nullable
097        public JetExpression getBodyExpression() {
098            return findChildByClass(JetExpression.class);
099        }
100    
101        @Override
102        public boolean hasDeclaredReturnType() {
103            return getReturnTypeRef() != null;
104        }
105    
106        @Override
107        @Nullable
108        public JetTypeReference getReceiverTypeRef() {
109            PsiElement child = getFirstChild();
110            while (child != null) {
111                IElementType tt = child.getNode().getElementType();
112                if (tt == JetTokens.LPAR || tt == JetTokens.COLON) break;
113                if (child instanceof JetTypeReference) {
114                    return (JetTypeReference) child;
115                }
116                child = child.getNextSibling();
117            }
118    
119            return null;
120        }
121    
122        @Override
123        @Nullable
124        public JetTypeReference getReturnTypeRef() {
125            boolean colonPassed = false;
126            PsiElement child = getFirstChild();
127            while (child != null) {
128                IElementType tt = child.getNode().getElementType();
129                if (tt == JetTokens.COLON) {
130                    colonPassed = true;
131                }
132                if (colonPassed && child instanceof JetTypeReference) {
133                    return (JetTypeReference) child;
134                }
135                child = child.getNextSibling();
136            }
137    
138            return null;
139        }
140    
141        @NotNull
142        @Override
143        public JetElement asElement() {
144            return this;
145        }
146    
147        @Override
148        public boolean isLocal() {
149            PsiElement parent = getParent();
150            return !(parent instanceof JetFile || parent instanceof JetClassBody);
151        }
152    }