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.search.LocalSearchScope;
024    import com.intellij.psi.search.SearchScope;
025    import com.intellij.psi.stubs.IStubElementType;
026    import com.intellij.psi.tree.IElementType;
027    import com.intellij.psi.tree.TokenSet;
028    import com.intellij.psi.util.PsiTreeUtil;
029    import org.jetbrains.annotations.NotNull;
030    import org.jetbrains.annotations.Nullable;
031    import org.jetbrains.jet.JetNodeTypes;
032    import org.jetbrains.jet.lang.psi.stubs.PsiJetPropertyStub;
033    import org.jetbrains.jet.lang.psi.stubs.elements.JetStubElementTypes;
034    import org.jetbrains.jet.lexer.JetTokens;
035    
036    import java.util.List;
037    
038    import static org.jetbrains.jet.JetNodeTypes.PROPERTY_ACCESSOR;
039    import static org.jetbrains.jet.JetNodeTypes.PROPERTY_DELEGATE;
040    import static org.jetbrains.jet.lexer.JetTokens.*;
041    
042    public class JetProperty extends JetTypeParameterListOwnerStub<PsiJetPropertyStub> implements JetVariableDeclaration,
043                                                                                                  JetCallableDeclaration {
044        public JetProperty(@NotNull ASTNode node) {
045            super(node);
046        }
047    
048        public JetProperty(@NotNull PsiJetPropertyStub stub, @NotNull IStubElementType nodeType) {
049            super(stub, nodeType);
050        }
051    
052        @Override
053        public void accept(@NotNull JetVisitorVoid visitor) {
054            visitor.visitProperty(this);
055        }
056    
057        @Override
058        public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
059            return visitor.visitProperty(this, data);
060        }
061    
062        @NotNull
063        @Override
064        public IStubElementType getElementType() {
065            return JetStubElementTypes.PROPERTY;
066        }
067    
068        @Override
069        public boolean isVar() {
070            PsiJetPropertyStub stub = getStub();
071            if (stub != null) {
072                return stub.isVar();
073            }
074    
075            return getNode().findChildByType(JetTokens.VAR_KEYWORD) != null;
076        }
077    
078        public boolean isLocal() {
079            PsiElement parent = getParent();
080            return !(parent instanceof JetFile || parent instanceof JetClassBody);
081        }
082    
083        public boolean isTopLevel() {
084            PsiJetPropertyStub stub = getStub();
085            if (stub != null) {
086                return stub.isTopLevel();
087            }
088    
089            return getParent() instanceof JetFile;
090        }
091    
092        @Nullable
093        @Override
094        public JetParameterList getValueParameterList() {
095            return null;
096        }
097    
098        @Override
099        @Nullable
100        public JetTypeReference getReceiverTypeRef() {
101            ASTNode node = getNode().getFirstChildNode();
102            while (node != null) {
103                IElementType tt = node.getElementType();
104                if (tt == JetTokens.COLON) break;
105    
106                if (tt == JetNodeTypes.TYPE_REFERENCE) {
107                    return (JetTypeReference) node.getPsi();
108                }
109                node = node.getTreeNext();
110            }
111    
112            return null;
113        }
114    
115        @Nullable
116        @Override
117        public JetTypeReference getReturnTypeRef() {
118            return getTypeRef();
119        }
120    
121        @Override
122        @Nullable
123        public JetTypeReference getTypeRef() {
124            ASTNode node = getNode().getFirstChildNode();
125            boolean passedColon = false;
126            while (node != null) {
127                IElementType tt = node.getElementType();
128                if (tt == JetTokens.COLON) {
129                    passedColon = true;
130                }
131                else if (tt == JetNodeTypes.TYPE_REFERENCE && passedColon) {
132                    return (JetTypeReference) node.getPsi();
133                }
134                node = node.getTreeNext();
135            }
136    
137            return null;
138        }
139    
140        @NotNull
141        public List<JetPropertyAccessor> getAccessors() {
142            return findChildrenByType(PROPERTY_ACCESSOR);
143        }
144    
145        @Nullable
146        public JetPropertyAccessor getGetter() {
147            for (JetPropertyAccessor accessor : getAccessors()) {
148                if (accessor.isGetter()) return accessor;
149            }
150    
151            return null;
152        }
153    
154        @Nullable
155        public JetPropertyAccessor getSetter() {
156            for (JetPropertyAccessor accessor : getAccessors()) {
157                if (accessor.isSetter()) return accessor;
158            }
159    
160            return null;
161        }
162    
163        @Nullable
164        public JetPropertyDelegate getDelegate() {
165            return (JetPropertyDelegate) findChildByType(PROPERTY_DELEGATE);
166        }
167    
168        @Nullable
169        public JetExpression getDelegateExpression() {
170            JetPropertyDelegate delegate = getDelegate();
171            if (delegate != null) {
172                return delegate.getExpression();
173            }
174            return null;
175        }
176    
177        @Override
178        @Nullable
179        public JetExpression getInitializer() {
180            return PsiTreeUtil.getNextSiblingOfType(findChildByType(EQ), JetExpression.class);
181        }
182    
183        @Nullable
184        public JetExpression getDelegateExpressionOrInitializer() {
185            JetExpression expression = getDelegateExpression();
186            if (expression == null) {
187                return getInitializer();
188            }
189            return expression;
190        }
191    
192        @Override
193        @NotNull
194        public ASTNode getValOrVarNode() {
195            ASTNode node = getNode().findChildByType(TokenSet.create(VAL_KEYWORD, VAR_KEYWORD));
196            assert node != null : "Val or var should always exist for property";
197            return node;
198        }
199    
200        @Override
201        public ItemPresentation getPresentation() {
202            return ItemPresentationProviders.getItemPresentation(this);
203        }
204    }