001 /*
002 * Copyright 2010-2015 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.kotlin.psi.stubs.elements;
018
019 import com.intellij.psi.stubs.IndexSink;
020 import com.intellij.psi.stubs.StubElement;
021 import com.intellij.psi.stubs.StubInputStream;
022 import com.intellij.psi.stubs.StubOutputStream;
023 import com.intellij.util.io.StringRef;
024 import org.jetbrains.annotations.NonNls;
025 import org.jetbrains.annotations.NotNull;
026 import org.jetbrains.kotlin.name.FqName;
027 import org.jetbrains.kotlin.psi.KtFile;
028 import org.jetbrains.kotlin.psi.KtNamedFunction;
029 import org.jetbrains.kotlin.psi.stubs.KotlinFunctionStub;
030 import org.jetbrains.kotlin.psi.stubs.impl.KotlinFunctionStubImpl;
031 import org.jetbrains.kotlin.resolve.lazy.ResolveSessionUtils;
032
033 import java.io.IOException;
034
035 public class KtFunctionElementType extends KtStubElementType<KotlinFunctionStub, KtNamedFunction> {
036
037 public KtFunctionElementType(@NotNull @NonNls String debugName) {
038 super(debugName, KtNamedFunction.class, KotlinFunctionStub.class);
039 }
040
041 @Override
042 public KotlinFunctionStub createStub(@NotNull KtNamedFunction psi, @NotNull StubElement parentStub) {
043 boolean isTopLevel = psi.getParent() instanceof KtFile;
044 boolean isExtension = psi.getReceiverTypeReference() != null;
045 FqName fqName = ResolveSessionUtils.safeFqNameForLazyResolve(psi);
046 boolean hasBlockBody = psi.hasBlockBody();
047 boolean hasBody = psi.hasBody();
048 return new KotlinFunctionStubImpl(parentStub, StringRef.fromString(psi.getName()), isTopLevel, fqName,
049 isExtension, hasBlockBody, hasBody, psi.hasTypeParameterListBeforeFunctionName());
050 }
051
052 @Override
053 public void serialize(@NotNull KotlinFunctionStub stub, @NotNull StubOutputStream dataStream) throws IOException {
054 dataStream.writeName(stub.getName());
055 dataStream.writeBoolean(stub.isTopLevel());
056
057 FqName fqName = stub.getFqName();
058 dataStream.writeName(fqName != null ? fqName.asString() : null);
059
060 dataStream.writeBoolean(stub.isExtension());
061 dataStream.writeBoolean(stub.hasBlockBody());
062 dataStream.writeBoolean(stub.hasBody());
063 dataStream.writeBoolean(stub.hasTypeParameterListBeforeFunctionName());
064 }
065
066 @NotNull
067 @Override
068 public KotlinFunctionStub deserialize(@NotNull StubInputStream dataStream, StubElement parentStub) throws IOException {
069 StringRef name = dataStream.readName();
070 boolean isTopLevel = dataStream.readBoolean();
071
072 StringRef fqNameAsString = dataStream.readName();
073 FqName fqName = fqNameAsString != null ? new FqName(fqNameAsString.toString()) : null;
074
075 boolean isExtension = dataStream.readBoolean();
076 boolean hasBlockBody = dataStream.readBoolean();
077 boolean hasBody = dataStream.readBoolean();
078 boolean hasTypeParameterListBeforeFunctionName = dataStream.readBoolean();
079
080 return new KotlinFunctionStubImpl(parentStub, name, isTopLevel, fqName, isExtension, hasBlockBody, hasBody,
081 hasTypeParameterListBeforeFunctionName);
082 }
083
084 @Override
085 public void indexStub(@NotNull KotlinFunctionStub stub, @NotNull IndexSink sink) {
086 StubIndexService.getInstance().indexFunction(stub, sink);
087 }
088 }