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.google.common.collect.Lists;
020 import com.intellij.lang.ASTNode;
021 import com.intellij.openapi.util.TextRange;
022 import com.intellij.psi.PsiElement;
023 import com.intellij.psi.util.PsiTreeUtil;
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
029 import java.util.Collections;
030 import java.util.List;
031
032 public class KtArrayAccessExpression extends KtExpressionImpl implements KtReferenceExpression {
033 public KtArrayAccessExpression(@NotNull ASTNode node) {
034 super(node);
035 }
036
037 @Override
038 public <R, D> R accept(@NotNull KtVisitor<R, D> visitor, D data) {
039 return visitor.visitArrayAccessExpression(this, data);
040 }
041
042 @Nullable @IfNotParsed
043 public KtExpression getArrayExpression() {
044 return findChildByClass(KtExpression.class);
045 }
046
047 @NotNull
048 public List<KtExpression> getIndexExpressions() {
049 return PsiTreeUtil.getChildrenOfTypeAsList(getIndicesNode(), KtExpression.class);
050 }
051
052 @NotNull
053 public KtContainerNode getIndicesNode() {
054 KtContainerNode indicesNode = findChildByType(KtNodeTypes.INDICES);
055 assert indicesNode != null : "Can't be null because of parser";
056 return indicesNode;
057 }
058
059 @NotNull
060 public List<TextRange> getBracketRanges() {
061 PsiElement lBracket = getLeftBracket();
062 PsiElement rBracket = getRightBracket();
063 if (lBracket == null || rBracket == null) {
064 return Collections.emptyList();
065 }
066 return Lists.newArrayList(lBracket.getTextRange(), rBracket.getTextRange());
067 }
068
069 @Nullable
070 public PsiElement getLeftBracket() {
071 return getIndicesNode().findChildByType(KtTokens.LBRACKET);
072 }
073
074 @Nullable
075 public PsiElement getRightBracket() {
076 return getIndicesNode().findChildByType(KtTokens.RBRACKET);
077 }
078 }