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.extapi.psi.ASTWrapperPsiElement;
020    import com.intellij.lang.ASTNode;
021    import com.intellij.lang.Language;
022    import com.intellij.psi.PsiElementVisitor;
023    import com.intellij.psi.PsiFile;
024    import com.intellij.psi.PsiReference;
025    import com.intellij.psi.PsiReferenceService;
026    import com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry;
027    import com.intellij.util.IncorrectOperationException;
028    import org.jetbrains.annotations.NotNull;
029    import org.jetbrains.kotlin.idea.KotlinLanguage;
030    
031    public class KtElementImpl extends ASTWrapperPsiElement implements KtElement {
032        public KtElementImpl(@NotNull ASTNode node) {
033            super(node);
034        }
035    
036        @NotNull
037        @Override
038        public Language getLanguage() {
039            return KotlinLanguage.INSTANCE;
040        }
041    
042        @Override
043        public String toString() {
044            return getNode().getElementType().toString();
045        }
046    
047        @Override
048        public final void accept(@NotNull PsiElementVisitor visitor) {
049            if (visitor instanceof KtVisitor) {
050                accept((KtVisitor) visitor, null);
051            }
052            else {
053                visitor.visitElement(this);
054            }
055        }
056    
057        @NotNull
058        @Override
059        public KtFile getContainingKtFile() {
060            PsiFile file = getContainingFile();
061            assert file instanceof KtFile : "KtElement not inside KtFile: " + file + " " + file.getText();
062            return (KtFile) file;
063        }
064    
065        @Override
066        public <D> void acceptChildren(@NotNull KtVisitor<Void, D> visitor, D data) {
067            KtPsiUtil.visitChildren(this, visitor, data);
068        }
069    
070        @Override
071        public <R, D> R accept(@NotNull KtVisitor<R, D> visitor, D data) {
072            return visitor.visitKtElement(this, data);
073        }
074    
075        @Override
076        public void delete() throws IncorrectOperationException {
077            KtElementUtilsKt.deleteSemicolon(this);
078            super.delete();
079        }
080    
081        @Override
082        public PsiReference getReference() {
083            PsiReference[] references = getReferences();
084            if (references.length == 1) return references[0];
085            else return null;
086        }
087    
088        @NotNull
089        @Override
090        public PsiReference[] getReferences() {
091            return ReferenceProvidersRegistry.getReferencesFromProviders(this, PsiReferenceService.Hints.NO_HINTS);
092        }
093    }