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.JetExpression;
028import org.jetbrains.jet.lang.psi.JetParameter;
029import org.jetbrains.jet.lang.psi.JetTypeReference;
030import org.jetbrains.jet.lang.psi.stubs.PsiJetParameterStub;
031import org.jetbrains.jet.lang.psi.stubs.impl.PsiJetParameterStubImpl;
032
033import java.io.IOException;
034
035public class JetParameterElementType extends JetStubElementType<PsiJetParameterStub, JetParameter> {
036    public JetParameterElementType(@NotNull @NonNls String debugName) {
037        super(debugName);
038    }
039
040    @Override
041    public JetParameter createPsiFromAst(@NotNull ASTNode node) {
042        return new JetParameter(node);
043    }
044
045    @Override
046    public JetParameter createPsi(@NotNull PsiJetParameterStub stub) {
047        return new JetParameter(stub, JetStubElementTypes.VALUE_PARAMETER);
048    }
049
050    @Override
051    public PsiJetParameterStub createStub(@NotNull JetParameter psi, StubElement parentStub) {
052        JetTypeReference typeReference = psi.getTypeReference();
053        JetExpression defaultValue = psi.getDefaultValue();
054
055        return new PsiJetParameterStubImpl(JetStubElementTypes.VALUE_PARAMETER, parentStub,
056                psi.getName(), psi.isMutable(), psi.isVarArg(),
057                typeReference != null ? typeReference.getText() : null,
058                defaultValue != null ? defaultValue.getText() : null);
059    }
060
061    @Override
062    public boolean shouldCreateStub(ASTNode node) {
063        return node.getElementType() == JetStubElementTypes.VALUE_PARAMETER;
064    }
065
066    @Override
067    public void serialize(PsiJetParameterStub stub, StubOutputStream dataStream) throws IOException {
068        dataStream.writeName(stub.getName());
069        dataStream.writeBoolean(stub.isMutable());
070        dataStream.writeBoolean(stub.isVarArg());
071        dataStream.writeName(stub.getTypeText());
072        dataStream.writeName(stub.getDefaultValueText());
073    }
074
075    @Override
076    public PsiJetParameterStub deserialize(StubInputStream dataStream, StubElement parentStub) throws IOException {
077        StringRef name = dataStream.readName();
078        boolean isMutable = dataStream.readBoolean();
079        boolean isVarArg = dataStream.readBoolean();
080        StringRef typeText = dataStream.readName();
081        StringRef defaultValueText = dataStream.readName();
082
083        return new PsiJetParameterStubImpl(JetStubElementTypes.VALUE_PARAMETER, parentStub, name, isMutable, isVarArg,
084                                           typeText, defaultValueText);
085    }
086
087    @Override
088    public void indexStub(PsiJetParameterStub stub, IndexSink sink) {
089        // No index
090    }
091}