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.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.PsiReference;
024    import com.intellij.psi.PsiReferenceService;
025    import com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry;
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.lexer.JetTokens;
031    
032    import java.util.Collections;
033    import java.util.List;
034    
035    public class JetArrayAccessExpression extends JetReferenceExpression {
036        public JetArrayAccessExpression(@NotNull ASTNode node) {
037            super(node);
038        }
039    
040        @Nullable
041        @Override
042        public PsiReference getReference() {
043            PsiReference[] references = getReferences();
044            if (references.length == 1) return references[0];
045            else return null;
046        }
047    
048        @NotNull
049        @Override
050        public PsiReference[] getReferences() {
051            return ReferenceProvidersRegistry.getReferencesFromProviders(this, PsiReferenceService.Hints.NO_HINTS);
052        }
053    
054        @Override
055        public void accept(@NotNull JetVisitorVoid visitor) {
056            visitor.visitArrayAccessExpression(this);
057        }
058    
059        @Override
060        public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
061            return visitor.visitArrayAccessExpression(this, data);
062        }
063    
064        @Nullable @IfNotParsed
065        public JetExpression getArrayExpression() {
066            return findChildByClass(JetExpression.class);
067        }
068    
069        @NotNull
070        public List<JetExpression> getIndexExpressions() {
071            return PsiTreeUtil.getChildrenOfTypeAsList(getIndicesNode(), JetExpression.class);
072        }
073    
074        @NotNull
075        public JetContainerNode getIndicesNode() {
076            JetContainerNode indicesNode = (JetContainerNode) findChildByType(JetNodeTypes.INDICES);
077            assert indicesNode != null : "Can't be null because of parser";
078            return indicesNode;
079        }
080    
081        @NotNull
082        public List<TextRange> getBracketRanges() {
083            PsiElement lBracket = getIndicesNode().findChildByType(JetTokens.LBRACKET);
084            PsiElement rBracket = getIndicesNode().findChildByType(JetTokens.RBRACKET);
085            if (lBracket == null || rBracket == null) {
086                return Collections.emptyList();
087            }
088            return Lists.newArrayList(lBracket.getTextRange(), rBracket.getTextRange());
089        }
090    }