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.PsiElement;
021 import com.intellij.psi.stubs.IndexSink;
022 import com.intellij.psi.stubs.StubElement;
023 import com.intellij.psi.stubs.StubInputStream;
024 import com.intellij.psi.stubs.StubOutputStream;
025 import com.intellij.util.io.StringRef;
026 import org.jetbrains.annotations.NonNls;
027 import org.jetbrains.annotations.NotNull;
028 import org.jetbrains.jet.lang.psi.JetExpression;
029 import org.jetbrains.jet.lang.psi.JetProperty;
030 import org.jetbrains.jet.lang.psi.JetTypeReference;
031 import org.jetbrains.jet.lang.psi.stubs.PsiJetPropertyStub;
032 import org.jetbrains.jet.lang.psi.stubs.impl.PsiJetPropertyStubImpl;
033 import org.jetbrains.jet.lang.resolve.lazy.ResolveSessionUtils;
034 import org.jetbrains.jet.lang.resolve.name.FqName;
035
036 import java.io.IOException;
037
038 public class JetPropertyElementType extends JetStubElementType<PsiJetPropertyStub, JetProperty> {
039 public JetPropertyElementType(@NotNull @NonNls String debugName) {
040 super(debugName);
041 }
042
043 @Override
044 public JetProperty createPsiFromAst(@NotNull ASTNode node) {
045 return new JetProperty(node);
046 }
047
048 @Override
049 public JetProperty createPsi(@NotNull PsiJetPropertyStub stub) {
050 return new JetProperty(stub);
051 }
052
053 @Override
054 public boolean shouldCreateStub(ASTNode node) {
055 if (super.shouldCreateStub(node)) {
056 PsiElement psi = node.getPsi();
057 return psi instanceof JetProperty;
058 }
059
060 return false;
061 }
062
063 @Override
064 public PsiJetPropertyStub createStub(@NotNull JetProperty psi, StubElement parentStub) {
065 JetTypeReference typeRef = psi.getTypeRef();
066 JetExpression expression = psi.getInitializer();
067
068 assert !psi.isLocal() :
069 String.format("Should not store local property: %s, parent %s",
070 psi.getText(), psi.getParent() != null ? psi.getParent().getText() : "<no parent>");
071
072 return new PsiJetPropertyStubImpl(parentStub,
073 psi.getName(), psi.isVar(), psi.isTopLevel(), ResolveSessionUtils.safeFqNameForLazyResolve(psi),
074 typeRef != null ? typeRef.getText() : null,
075 expression != null ? expression.getText() : null);
076 }
077
078 @Override
079 public void serialize(@NotNull PsiJetPropertyStub stub, @NotNull StubOutputStream dataStream) throws IOException {
080 dataStream.writeName(stub.getName());
081 dataStream.writeBoolean(stub.isVar());
082 dataStream.writeBoolean(stub.isTopLevel());
083
084 FqName fqName = stub.getFqName();
085 dataStream.writeName(fqName != null ? fqName.asString() : null);
086
087 dataStream.writeName(stub.getTypeText());
088 dataStream.writeName(stub.getInferenceBodyText());
089 }
090
091 @NotNull
092 @Override
093 public PsiJetPropertyStub deserialize(@NotNull StubInputStream dataStream, StubElement parentStub) throws IOException {
094 StringRef name = dataStream.readName();
095 boolean isVar = dataStream.readBoolean();
096 boolean isTopLevel = dataStream.readBoolean();
097
098 StringRef fqNameAsString = dataStream.readName();
099 FqName fqName = fqNameAsString != null ? new FqName(fqNameAsString.toString()) : null;
100
101 StringRef typeText = dataStream.readName();
102 StringRef inferenceBodyText = dataStream.readName();
103
104 return new PsiJetPropertyStubImpl(parentStub,
105 name, isVar, isTopLevel, fqName, typeText, inferenceBodyText);
106 }
107
108 @Override
109 public void indexStub(@NotNull PsiJetPropertyStub stub, @NotNull IndexSink sink) {
110 StubIndexServiceFactory.getInstance().indexProperty(stub, sink);
111 }
112 }