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
017package org.jetbrains.jet.lang.psi.stubs.elements;
018
019import com.intellij.lang.ASTNode;
020import com.intellij.psi.stubs.IndexSink;
021import com.intellij.psi.stubs.StubElement;
022import com.intellij.psi.stubs.StubInputStream;
023import com.intellij.psi.stubs.StubOutputStream;
024import com.intellij.util.io.StringRef;
025import org.jetbrains.annotations.NonNls;
026import org.jetbrains.annotations.NotNull;
027import org.jetbrains.jet.lang.psi.*;
028import org.jetbrains.jet.lang.psi.stubs.PsiJetAnnotationStub;
029import org.jetbrains.jet.lang.psi.stubs.impl.PsiJetAnnotationStubImpl;
030import org.jetbrains.jet.lang.resolve.name.Name;
031
032import java.io.IOException;
033
034public class JetAnnotationElementType extends JetStubElementType<PsiJetAnnotationStub, JetAnnotationEntry> {
035
036    public JetAnnotationElementType(@NotNull @NonNls String debugName) {
037        super(debugName);
038    }
039
040    @Override
041    public JetAnnotationEntry createPsiFromAst(@NotNull ASTNode node) {
042        return new JetAnnotationEntry(node);
043    }
044
045    @Override
046    public JetAnnotationEntry createPsi(@NotNull PsiJetAnnotationStub stub) {
047        return new JetAnnotationEntry(stub);
048    }
049
050    @Override
051    public PsiJetAnnotationStub createStub(@NotNull JetAnnotationEntry psi, StubElement parentStub) {
052        Name shortName = JetPsiUtil.getShortName(psi);
053        String resultName = shortName != null ? shortName.asString() : psi.getText();
054        return new PsiJetAnnotationStubImpl(parentStub, JetStubElementTypes.ANNOTATION_ENTRY, resultName);
055    }
056
057    @Override
058    public void serialize(PsiJetAnnotationStub stub, StubOutputStream dataStream) throws IOException {
059        dataStream.writeName(stub.getShortName());
060    }
061
062    @Override
063    public PsiJetAnnotationStub deserialize(StubInputStream dataStream, StubElement parentStub) throws IOException {
064        StringRef text = dataStream.readName();
065        return new PsiJetAnnotationStubImpl(parentStub, JetStubElementTypes.ANNOTATION_ENTRY, text);
066    }
067
068    @Override
069    public void indexStub(PsiJetAnnotationStub stub, IndexSink sink) {
070        StubIndexServiceFactory.getInstance().indexAnnotation(stub, sink);
071    }
072}