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