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