blob: ea61d17188e962900681a95d5c53c317254813b4 [file] [log] [blame]
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jetbrains.python.psi.impl.stubs;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.stubs.*;
import com.intellij.util.io.StringRef;
import com.jetbrains.python.PyElementTypes;
import com.jetbrains.python.psi.PyFunction;
import com.jetbrains.python.psi.PyStringLiteralExpression;
import com.jetbrains.python.psi.PyStubElementType;
import com.jetbrains.python.psi.impl.PyFunctionImpl;
import com.jetbrains.python.psi.impl.PyPsiUtils;
import com.jetbrains.python.psi.stubs.PyFunctionNameIndex;
import com.jetbrains.python.psi.stubs.PyFunctionStub;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
/**
* @author max
*/
public class PyFunctionElementType extends PyStubElementType<PyFunctionStub, PyFunction> {
public PyFunctionElementType() {
this("FUNCTION_DECLARATION");
}
public PyFunctionElementType(String debugName) {
super(debugName);
}
public PsiElement createElement(@NotNull final ASTNode node) {
return new PyFunctionImpl(node);
}
public PyFunction createPsi(@NotNull final PyFunctionStub stub) {
return new PyFunctionImpl(stub);
}
public PyFunctionStub createStub(@NotNull final PyFunction psi, final StubElement parentStub) {
PyFunctionImpl function = (PyFunctionImpl)psi;
String message = function.extractDeprecationMessage();
final PyStringLiteralExpression docStringExpression = function.getDocStringExpression();
return new PyFunctionStubImpl(psi.getName(), PyPsiUtils.strValue(docStringExpression),
message == null ? null : StringRef.fromString(message), parentStub, getStubElementType());
}
public void serialize(@NotNull final PyFunctionStub stub, @NotNull final StubOutputStream dataStream)
throws IOException {
dataStream.writeName(stub.getName());
dataStream.writeUTFFast(stub.getDocString() != null ? stub.getDocString() : "");
dataStream.writeName(stub.getDeprecationMessage());
}
@NotNull
public PyFunctionStub deserialize(@NotNull final StubInputStream dataStream, final StubElement parentStub) throws IOException {
String name = StringRef.toString(dataStream.readName());
String docString = dataStream.readUTFFast();
StringRef deprecationMessage = dataStream.readName();
return new PyFunctionStubImpl(name, docString.length() > 0 ? docString : null, deprecationMessage, parentStub, getStubElementType());
}
public void indexStub(@NotNull final PyFunctionStub stub, @NotNull final IndexSink sink) {
final String name = stub.getName();
if (name != null) {
sink.occurrence(PyFunctionNameIndex.KEY, name);
}
}
protected IStubElementType getStubElementType() {
return PyElementTypes.FUNCTION_DECLARATION;
}
}