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.JetPsiUtil;
031    import org.jetbrains.jet.lang.psi.JetTypeReference;
032    import org.jetbrains.jet.lang.psi.stubs.PsiJetPropertyStub;
033    import org.jetbrains.jet.lang.psi.stubs.impl.PsiJetPropertyStubImpl;
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, JetStubElementTypes.PROPERTY);
051        }
052    
053        @Override
054        public boolean shouldCreateStub(ASTNode node) {
055            if (super.shouldCreateStub(node)) {
056                PsiElement psi = node.getPsi();
057                if (psi instanceof JetProperty) {
058                    JetProperty property = (JetProperty) psi;
059                    return property.getName() != null;
060                }
061            }
062    
063            return false;
064        }
065    
066        @Override
067        public PsiJetPropertyStub createStub(@NotNull JetProperty psi, StubElement parentStub) {
068            JetTypeReference typeRef = psi.getTypeRef();
069            JetExpression expression = psi.getInitializer();
070    
071            assert !psi.isLocal() :
072                    String.format("Should not store local property: %s, parent %s",
073                                  psi.getText(), psi.getParent() != null ? psi.getParent().getText() : "<no parent>");
074    
075            return new PsiJetPropertyStubImpl(JetStubElementTypes.PROPERTY, parentStub,
076                psi.getName(), psi.isVar(), psi.isTopLevel(), JetPsiUtil.getFQName(psi),
077                typeRef != null ? typeRef.getText() : null,
078                expression != null ? expression.getText() : null);
079        }
080    
081        @Override
082        public void serialize(PsiJetPropertyStub stub, StubOutputStream dataStream) throws IOException {
083            dataStream.writeName(stub.getName());
084            dataStream.writeBoolean(stub.isVar());
085            dataStream.writeBoolean(stub.isTopLevel());
086    
087            FqName topFQName = stub.getTopFQName();
088            dataStream.writeName(topFQName != null ? topFQName.toString() : null);
089    
090            dataStream.writeName(stub.getTypeText());
091            dataStream.writeName(stub.getInferenceBodyText());
092        }
093    
094        @Override
095        public PsiJetPropertyStub deserialize(StubInputStream dataStream, StubElement parentStub) throws IOException {
096            StringRef name = dataStream.readName();
097            boolean isVar = dataStream.readBoolean();
098            boolean isTopLevel = dataStream.readBoolean();
099    
100            StringRef topFQNameStr = dataStream.readName();
101            FqName fqName = topFQNameStr != null ? new FqName(topFQNameStr.toString()) : null;
102    
103            StringRef typeText = dataStream.readName();
104            StringRef inferenceBodyText = dataStream.readName();
105    
106            return new PsiJetPropertyStubImpl(JetStubElementTypes.PROPERTY, parentStub,
107                                              name, isVar, isTopLevel, fqName, typeText, inferenceBodyText);
108        }
109    
110        @Override
111        public void indexStub(PsiJetPropertyStub stub, IndexSink sink) {
112            StubIndexServiceFactory.getInstance().indexProperty(stub, sink);
113        }
114    }