001 /*
002 * Copyright 2010-2015 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.kotlin.psi;
018
019 import com.intellij.lang.ASTNode;
020 import com.intellij.psi.PsiElement;
021 import com.intellij.psi.search.LocalSearchScope;
022 import com.intellij.psi.search.SearchScope;
023 import com.intellij.psi.tree.TokenSet;
024 import org.jetbrains.annotations.NotNull;
025 import org.jetbrains.annotations.Nullable;
026 import org.jetbrains.kotlin.KtNodeTypes;
027 import org.jetbrains.kotlin.lexer.KtTokens;
028 import org.jetbrains.kotlin.name.FqName;
029 import org.jetbrains.kotlin.psi.typeRefHelpers.TypeRefHelpersKt;
030
031 import java.util.Collections;
032 import java.util.List;
033
034 import static org.jetbrains.kotlin.lexer.KtTokens.VAL_KEYWORD;
035 import static org.jetbrains.kotlin.lexer.KtTokens.VAR_KEYWORD;
036
037 public class KtDestructuringDeclarationEntry extends KtNamedDeclarationNotStubbed implements KtVariableDeclaration {
038 public KtDestructuringDeclarationEntry(@NotNull ASTNode node) {
039 super(node);
040 }
041
042 @Override
043 public KtTypeReference getTypeReference() {
044 return TypeRefHelpersKt.getTypeReference(this);
045 }
046
047 @Override
048 @Nullable
049 public KtTypeReference setTypeReference(@Nullable KtTypeReference typeRef) {
050 return TypeRefHelpersKt.setTypeReference(this, getNameIdentifier(), typeRef);
051 }
052
053 @Nullable
054 @Override
055 public PsiElement getColon() {
056 return findChildByType(KtTokens.COLON);
057 }
058
059 @Nullable
060 @Override
061 public KtParameterList getValueParameterList() {
062 return null;
063 }
064
065 @NotNull
066 @Override
067 public List<KtParameter> getValueParameters() {
068 return Collections.emptyList();
069 }
070
071 @Nullable
072 @Override
073 public KtTypeReference getReceiverTypeReference() {
074 return null;
075 }
076
077 @Nullable
078 @Override
079 public KtTypeParameterList getTypeParameterList() {
080 return null;
081 }
082
083 @Nullable
084 @Override
085 public KtTypeConstraintList getTypeConstraintList() {
086 return null;
087 }
088
089 @NotNull
090 @Override
091 public List<KtTypeConstraint> getTypeConstraints() {
092 return Collections.emptyList();
093 }
094
095 @NotNull
096 @Override
097 public List<KtTypeParameter> getTypeParameters() {
098 return Collections.emptyList();
099 }
100
101 @Override
102 public <R, D> R accept(@NotNull KtVisitor<R, D> visitor, D data) {
103 return visitor.visitDestructuringDeclarationEntry(this, data);
104 }
105
106 @Override
107 public boolean isVar() {
108 return getParentNode().findChildByType(KtTokens.VAR_KEYWORD) != null;
109 }
110
111 @Nullable
112 @Override
113 public KtExpression getInitializer() {
114 return null;
115 }
116
117 @Override
118 public boolean hasInitializer() {
119 return false;
120 }
121
122 @NotNull
123 private ASTNode getParentNode() {
124 ASTNode parent = getNode().getTreeParent();
125 assert parent.getElementType() == KtNodeTypes.DESTRUCTURING_DECLARATION;
126 return parent;
127 }
128
129 @Override
130 public PsiElement getValOrVarKeyword() {
131 ASTNode node = getParentNode().findChildByType(TokenSet.create(VAL_KEYWORD, VAR_KEYWORD));
132 if (node == null) return null;
133 return node.getPsi();
134 }
135
136 @Nullable
137 @Override
138 public FqName getFqName() {
139 return null;
140 }
141
142 @NotNull
143 @Override
144 public SearchScope getUseScope() {
145 KtElement enclosingBlock = KtPsiUtil.getEnclosingElementForLocalDeclaration(this, false);
146 if (enclosingBlock instanceof KtParameter) {
147 enclosingBlock = KtPsiUtil.getEnclosingElementForLocalDeclaration((KtParameter) enclosingBlock, false);
148 }
149 if (enclosingBlock != null) return new LocalSearchScope(enclosingBlock);
150
151 return super.getUseScope();
152 }
153 }