Search in sources :

Example 1 with PerlSubArgument

use of com.perl5.lang.perl.psi.utils.PerlSubArgument in project Perl5-IDEA by Camelcade.

the class PerlSubUtil method getArgumentsListAsString.

/**
 * Builds arguments string for presentation
 *
 * @param subArguments list of arguments
 * @return stringified prototype
 */
public static String getArgumentsListAsString(List<PerlSubArgument> subArguments) {
    int argumentsNumber = subArguments.size();
    List<String> argumentsList = new ArrayList<>();
    List<String> optionalAargumentsList = new ArrayList<>();
    for (PerlSubArgument argument : subArguments) {
        if (!optionalAargumentsList.isEmpty() || argument.isOptional()) {
            optionalAargumentsList.add(argument.toStringShort());
        } else {
            argumentsList.add(argument.toStringShort());
        }
        int compiledListSize = argumentsList.size() + optionalAargumentsList.size();
        if (compiledListSize > 5 && argumentsNumber > compiledListSize) {
            if (!optionalAargumentsList.isEmpty()) {
                optionalAargumentsList.add("...");
            } else {
                argumentsList.add("...");
            }
            break;
        }
    }
    if (argumentsList.isEmpty() && optionalAargumentsList.isEmpty()) {
        return "";
    }
    String argumentListString = StringUtils.join(argumentsList, ", ");
    String optionalArgumentsString = StringUtils.join(optionalAargumentsList, ", ");
    if (argumentListString.isEmpty()) {
        return "([" + optionalArgumentsString + "])";
    }
    if (optionalAargumentsList.isEmpty()) {
        return "(" + argumentListString + ")";
    } else {
        return "(" + argumentListString + " [, " + optionalArgumentsString + "])";
    }
}
Also used : PerlSubArgument(com.perl5.lang.perl.psi.utils.PerlSubArgument)

Example 2 with PerlSubArgument

use of com.perl5.lang.perl.psi.utils.PerlSubArgument in project Perl5-IDEA by Camelcade.

the class PerlSubDefinitionBase method getPerlSubArgumentsFromSignature.

/**
 * Returns list of arguments defined in signature
 *
 * @return list of arguments or null if there is no signature
 */
@Nullable
private List<PerlSubArgument> getPerlSubArgumentsFromSignature() {
    List<PerlSubArgument> arguments = null;
    PsiElement signatureContainer = getSignatureContainer();
    if (signatureContainer != null) {
        arguments = new ArrayList<>();
        // noinspection unchecked
        PsiElement signatureElement = signatureContainer.getFirstChild();
        while (signatureElement != null) {
            processSignatureElement(signatureElement, arguments);
            signatureElement = signatureElement.getNextSibling();
        }
    }
    return arguments;
}
Also used : PerlSubArgument(com.perl5.lang.perl.psi.utils.PerlSubArgument) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with PerlSubArgument

use of com.perl5.lang.perl.psi.utils.PerlSubArgument in project Perl5-IDEA by Camelcade.

the class PerlSubDefinitionBase method getSubArgumentsList.

@NotNull
@Override
public List<PerlSubArgument> getSubArgumentsList() {
    PerlSubDefinitionStub stub = getStub();
    if (stub != null) {
        return new ArrayList<>(stub.getSubArgumentsList());
    }
    List<PerlSubArgument> arguments = getPerlSubArgumentsFromSignature();
    if (arguments == null) {
        arguments = getPerlSubArgumentsFromBody();
    }
    return arguments;
}
Also used : PerlSubDefinitionStub(com.perl5.lang.perl.psi.stubs.subsdefinitions.PerlSubDefinitionStub) PerlSubArgument(com.perl5.lang.perl.psi.utils.PerlSubArgument) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with PerlSubArgument

use of com.perl5.lang.perl.psi.utils.PerlSubArgument in project Perl5-IDEA by Camelcade.

the class PerlBuiltInSubsService method readArgument.

@Nullable
private PerlSubArgument readArgument(@NotNull Element element, boolean isOptional, String subName) {
    String variableName = element.getAttribute("name").getValue();
    if (StringUtil.isEmpty(variableName)) {
        LOG.warn("Missing argument name for " + subName);
        return null;
    }
    PerlVariableType variableType;
    String type = element.getAttribute("type").getValue();
    if (type == null || type.length() != 1 || (variableType = PerlVariableType.bySigil(type.charAt(0))) == null) {
        LOG.warn("Unknown type modifier for argument: " + variableName + " in " + subName);
        return null;
    }
    return PerlSubArgument.create(variableType, variableName, isOptional);
}
Also used : PerlVariableType(com.perl5.lang.perl.psi.utils.PerlVariableType) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with PerlSubArgument

use of com.perl5.lang.perl.psi.utils.PerlSubArgument in project Perl5-IDEA by Camelcade.

the class PerlCodeGeneratorImpl method getOverrideCodeText.

@Nullable
@Override
public String getOverrideCodeText(PsiElement subBase) {
    if (subBase instanceof PerlSubElement) {
        PerlSubElement perlSubBase = (PerlSubElement) subBase;
        StringBuilder code = new StringBuilder();
        code.append("#@override\n");
        PerlSubAnnotations annotations = perlSubBase.getAnnotations();
        if (annotations != null) {
            if (annotations.isDeprecated()) {
                code.append("#@deprecated\n");
            }
            if (annotations.isAbstract()) {
                code.append("#@abstract\n");
            }
            if (annotations.isMethod() || subBase instanceof PerlMethodDefinition) {
                code.append("#@method\n");
            }
            if (StringUtil.isNotEmpty(annotations.getReturns())) {
                code.append("#@returns ");
                code.append(annotations.getReturns());
                code.append("\n");
            }
        }
        code.append("sub ");
        code.append(perlSubBase.getSubName());
        code.append("{\n");
        List<String> superArgs = new ArrayList<>();
        List<PerlSubArgument> arguments = Collections.emptyList();
        if (perlSubBase instanceof PerlSubDefinitionElement) {
            // noinspection unchecked
            arguments = ((PerlSubDefinitionElement) perlSubBase).getSubArgumentsList();
            if (!arguments.isEmpty()) {
                boolean useShift = false;
                for (PerlSubArgument argument : arguments) {
                    if (StringUtil.isNotEmpty(argument.getVariableClass())) {
                        useShift = true;
                        break;
                    }
                }
                if (useShift) {
                    for (PerlSubArgument argument : arguments) {
                        if (!argument.isEmpty()) {
                            code.append("my ");
                            code.append(argument.getVariableClass());
                            code.append(" ");
                            String superArg = argument.toStringShort();
                            superArgs.add(superArg);
                            code.append(superArg);
                            code.append(" = ");
                        }
                        code.append("shift;\n");
                    }
                } else {
                    code.append("my ");
                    code.append('(');
                    boolean insertComma = false;
                    for (PerlSubArgument argument : arguments) {
                        if (insertComma) {
                            code.append(", ");
                        } else {
                            insertComma = true;
                        }
                        String superArg = argument.toStringShort();
                        superArgs.add(superArg);
                        code.append(superArg);
                    }
                    code.append(") = @_;\n");
                }
            } else {
                code.append("my ($self) = @_;\n");
            }
        }
        if (!superArgs.isEmpty()) {
            superArgs.remove(0);
        }
        if (!arguments.isEmpty() && !arguments.get(0).isEmpty()) {
            // noinspection StringConcatenationInsideStringBufferAppend
            code.append(arguments.get(0).toStringShort() + "->SUPER::" + perlSubBase.getSubName() + "(" + StringUtil.join(superArgs, ", ") + ");\n");
        }
        code.append("}");
        return code.toString();
    }
    return null;
}
Also used : PerlSubAnnotations(com.perl5.lang.perl.psi.utils.PerlSubAnnotations) PerlSubArgument(com.perl5.lang.perl.psi.utils.PerlSubArgument) PerlSubElement(com.perl5.lang.perl.psi.PerlSubElement) ArrayList(java.util.ArrayList) PerlMethodDefinition(com.perl5.lang.perl.psi.PerlMethodDefinition) PerlSubDefinitionElement(com.perl5.lang.perl.psi.PerlSubDefinitionElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PerlSubArgument (com.perl5.lang.perl.psi.utils.PerlSubArgument)9 NotNull (org.jetbrains.annotations.NotNull)4 PsiElement (com.intellij.psi.PsiElement)3 ArrayList (java.util.ArrayList)3 Nullable (org.jetbrains.annotations.Nullable)3 PerlSubDefinitionElement (com.perl5.lang.perl.psi.PerlSubDefinitionElement)2 PerlSubAnnotations (com.perl5.lang.perl.psi.utils.PerlSubAnnotations)2 PerlLightExceptionClassDefinition (com.perl5.lang.perl.parser.Exception.Class.psi.light.PerlLightExceptionClassDefinition)1 PerlMethodDefinition (com.perl5.lang.perl.psi.PerlMethodDefinition)1 PerlSubElement (com.perl5.lang.perl.psi.PerlSubElement)1 PerlVariable (com.perl5.lang.perl.psi.PerlVariable)1 PerlVariableDeclarationElement (com.perl5.lang.perl.psi.PerlVariableDeclarationElement)1 PsiPerlAnonArray (com.perl5.lang.perl.psi.PsiPerlAnonArray)1 PsiPerlSubSignatureElementIgnore (com.perl5.lang.perl.psi.PsiPerlSubSignatureElementIgnore)1 PerlSubDefinitionStub (com.perl5.lang.perl.psi.stubs.subsdefinitions.PerlSubDefinitionStub)1 PerlVariableType (com.perl5.lang.perl.psi.utils.PerlVariableType)1 Element (org.jdom.Element)1