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.JetFile;
029    import org.jetbrains.jet.lang.psi.JetNamedFunction;
030    import org.jetbrains.jet.lang.psi.stubs.PsiJetFunctionStub;
031    import org.jetbrains.jet.lang.psi.stubs.impl.PsiJetFunctionStubImpl;
032    import org.jetbrains.jet.lang.resolve.lazy.ResolveSessionUtils;
033    import org.jetbrains.jet.lang.resolve.name.FqName;
034    
035    import java.io.IOException;
036    
037    public class JetFunctionElementType extends JetStubElementType<PsiJetFunctionStub, JetNamedFunction> {
038    
039        public JetFunctionElementType(@NotNull @NonNls String debugName) {
040            super(debugName);
041        }
042    
043        @Override
044        public JetNamedFunction createPsiFromAst(@NotNull ASTNode node) {
045            return new JetNamedFunction(node);
046        }
047    
048        @Override
049        public JetNamedFunction createPsi(@NotNull PsiJetFunctionStub stub) {
050            return new JetNamedFunction(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 JetNamedFunction;
058            }
059    
060            return false;
061        }
062    
063        @Override
064        public PsiJetFunctionStub createStub(@NotNull JetNamedFunction psi, @NotNull StubElement parentStub) {
065            boolean isTopLevel = psi.getParent() instanceof JetFile;
066            boolean isExtension = psi.getReceiverTypeRef() != null;
067            FqName fqName = ResolveSessionUtils.safeFqNameForLazyResolve(psi);
068            return new PsiJetFunctionStubImpl(parentStub, psi.getName(), isTopLevel, fqName, isExtension);
069        }
070    
071        @Override
072        public void serialize(@NotNull PsiJetFunctionStub stub, @NotNull StubOutputStream dataStream) throws IOException {
073            dataStream.writeName(stub.getName());
074            dataStream.writeBoolean(stub.isTopLevel());
075    
076            FqName fqName = stub.getFqName();
077            dataStream.writeName(fqName != null ? fqName.asString() : null);
078    
079            dataStream.writeBoolean(stub.isExtension());
080        }
081    
082        @NotNull
083        @Override
084        public PsiJetFunctionStub deserialize(@NotNull StubInputStream dataStream, StubElement parentStub) throws IOException {
085            StringRef name = dataStream.readName();
086            boolean isTopLevel = dataStream.readBoolean();
087    
088            StringRef fqNameAsString = dataStream.readName();
089            FqName fqName = fqNameAsString != null ? new FqName(fqNameAsString.toString()) : null;
090    
091            boolean isExtension = dataStream.readBoolean();
092    
093            return new PsiJetFunctionStubImpl(parentStub, name, isTopLevel, fqName, isExtension);
094        }
095    
096        @Override
097        public void indexStub(@NotNull PsiJetFunctionStub stub, @NotNull IndexSink sink) {
098            StubIndexServiceFactory.getInstance().indexFunction(stub, sink);
099        }
100    }